//NOTE: REQUIRES jQUERY 1.3 OR HIGHER

//parameters
var speed = 100; //in ms

$(document).ready(function(){
	$('.flyout-open').live("click",function(){
		var id = $(this).parent().find('.flyout-panel').attr("id");
		openFlyout(this);
	});
	
	$('.flyout-close').live("click",function(){
		closeFlyout(this);
	});
	
	$('#wrapper').click(function(){
		$('.flyout-close').each(function(){
			closeFlyout(this);
			closeMemberFlyout();
		});
	});
	
	$('.show_user_info').live("click", function(){
		closeMemberFlyout(); //close other instances to avoid multiple ids on page (bug 1427)
		var obj = $(this);
		var classes = $(this).attr("class");
		var id  = classes.substr(classes.indexOf("id"), classes.length).replace("id",'')
		var dat = {"id" : id } ;
		$.post("/ajax/load_plugin/member_info_flyout/", dat, function(response){
			var json = eval('(' + response + ')');
			if (json.status == "error")
			{
				$(obj).parent().append(json.message);
			}else{
				$(obj).parent().append(json.payload);
			}
		});
	});
	
});

function openFlyout(target)
{
	var h 	= $(target).parent().children('.flyout-panel').height();
	var w 	= $(target).parent().children('.flyout-panel').width();
	var pos = $(target).position();
	
	$(target).parent().children('.flyout-panel').children().show();
	$(target).parent().find('.flyout-panel').removeAttr('style');
	$(target).parent().children('.flyout-panel').width(0).height(0).css("left",pos.left + 5);
	
	$(target).parent().find('.flyout-panel').animate({
			height:h,
			width:w
		}, speed, function(){
			ReplaceCssClass($(target),'flyout-open', 'flyout-close');
			$(target).parent().children('.flyout-panel').width(w).height(h);
		});
		
}
function closeFlyout(target)
{
	var h = $(target).parent().children('.flyout-panel').height();
	var w = $(target).parent().children('.flyout-panel').width();
	
	$(target).parent().children('.flyout-panel').children().hide();
	$(target).parent().children('.flyout-panel').animate({
			height:"0px",
			width:"0px"
		}, speed, function(){
			$(this).removeAttr('style').hide(); //use this to hide after animation is completed
			ReplaceCssClass($(target),'flyout-close', 'flyout-open'); 
			$(target).parent().children('.flyout-panel').width(w).height(h);

			
		});
		
}
function closeMemberFlyout()
{
	$('.member-popup').hide().remove();
}
function ReplaceCssClass(target, oldCssClass, newCssClass) {
	$(target).addClass(newCssClass);
	$(target).removeClass(oldCssClass);
}

