/* Commonly used function among pages */

/* prevent conflict with scriptaculous, prototype */
jQuery.noConflict();





/* run this config at start */
jQuery(document).ready(function()
{
	repaint();
	eventSetting();
});





function eventSetting()
{
	jQuery(".menucat").bind("click", function()
	{
		var src	= jQuery(this).find("img:eq(1)").attr("src");
		
		src = (src.lastIndexOf("-.png") > -1) ? src.replace("-.png", ".png") : src.replace(".png", "-.png");
		
		jQuery(this).find("img:eq(1)").attr("src", src);
		jQuery(this).next().stop(false, true).slideToggle(500);
	});
	
	jQuery(".menu").find("a").bind("click", function()
	{
		var action = jQuery(this).attr("href");
		
		jQuery(".menu").each(function()
		{
			var alt = jQuery(this).prev(".menucat").find("img:eq(1)").attr("alt");
			
			if (jQuery(this).is(":visible"))
				jQuery("input[name="+ alt +"]").val("true");
			else
				jQuery("input[name="+ alt +"]").val("false");
		});
		
		jQuery("form[name=menu-management]").attr("action", action).submit();
	});
	
	jQuery("td.catagory").find("a").bind("click", loadfaq);
	
	jQuery(".expand").bind("click", function()
	{
		jQuery(this).nextAll("span").find("img").each(thumbnails);
		jQuery(this).nextAll("span").stop(false, true).slideToggle(500, repaint);
	});
}





function repaint()
{
	var w = 0;
	var h = 0;
	
	jQuery(".main-content").each(function()
	{
		w = jQuery(this).width();
		h = jQuery(this).height();
		
		jQuery(this).prev(".panelbg").css({ "width":w +"px", "height":h +"px" });
	});
	
	jQuery(".content").each(function()
	{
		w = jQuery(this).width();
		h = jQuery(this).height();
		
		jQuery(this).prev(".panelbg").css({ "width":w +"px", "height":h +"px" });
	});
}



function loadfaq()
{
	var page = jQuery(this).attr("href");
		
	jQuery.ajax(
	{
		async		: false,
		beforeSend	: function()
		{
			jQuery("#loaderarea").html("<img src='graphics/loader/ajax-loader.gif' alt='loading' /> Loading...");
		},
		cache		: true,
		complete	: function(xmlhttp)
		{
			jQuery("#loaderarea").html(xmlhttp.responseText);
			repaint();
			
			jQuery(".expand").bind("click", function()
			{
				jQuery(this).nextAll("span").find("img").each(thumbnails);
				jQuery(this).nextAll("span").toggle();
				repaint();
			});
		},
		dataType	: "html",
		type		: "GET",
		url			: page
	});
}



function thumbnails()
{
	var w = parseInt( jQuery(this).attr("width") );
	var h = parseInt( jQuery(this).attr("height") );
	
	if (w > 300)
	{
		var scale = 300 / w;
		jQuery(this).attr({ "width":(w * scale), "height":(h * scale) });
	}
	
	jQuery(this).css("cursor", "pointer").bind("click", function()
	{
		window.open(jQuery(this).attr("src"), jQuery(this).attr("alt"));
	});
}