/* 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("input[name=credit]").bind("change", retrievePricing);
	jQuery("select[name=country]").bind("change", retrievePricing);
	jQuery("select[name=currency]").bind("change", retrievePricing).bind("change", currencyShow);
}





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 retrievePricing()
{
	var cdt		= jQuery("input[name=credit]:checked").val();
	var cry		= jQuery("select[name=country] :selected").val();
	var cur		= jQuery("select[name=currency] :selected").val();
	var cryn	= jQuery("select[name=country] :selected").text();
	
	if ( ((cdt == "") || (cdt == null)) || ((cry == "") || (cry == null)) || ((cur == "") || (cur == null)) ) return;
	
	jQuery.ajax(
	{
		type		:	"POST",						// post method
		async		:	false,						//
		url			:	"ajax/marketing.php",		// server url fo retrieval
		data		:	{
							"type"		:	"pricing",
							"rate"		:	cdt,
							"country"	:	cry,
							"currency"	:	cur
						},
		datatype	:	"json",						// return type
		success		:	function(xhr)				// successful receive data
		{
			var json = (typeof xhr == "object") ? xhr :  eval("(" + xhr + ")");

			jQuery("#countryinput").text(cryn);
			jQuery("#mtcreditinput").text( json.mtcredit );
			jQuery("#mtcurrencyinput").text( json.mtprice );
			jQuery("#mocreditinput").text( json.mocredit );
			jQuery("#mocurrencyinput").text( json.moprice );
		}
	});
}





function currencyShow()
{
	jQuery(".currdisplay").text( jQuery(this).find(":selected").val() );
}