$(document).ready(function(){

	function registerCSFlash()
	{
		if($('#barndoors_flash').length == 1)
		{
			swfobject.embedSWF("/media/images/done_barndoors_nongeneric_942.swf?xmlRef=barndoors_2.xml", "barndoors_flash", "525", "225", "9.0.0");
		}
		if($('#onsert_flash').length == 1)
		{
			swfobject.embedSWF("/media/images/done_onsert_nongeneric_956.swf?xmlRef=onserts.xml", "onsert_flash", "533", "255", "9.0.0");
		}
		if($('#polybag_flash').length == 1)
		{
			swfobject.embedSWF("/media/images/done_polybag_nongeneric_1024.swf?xmlRef=polybags_2.xml", "polybag_flash", "533", "255", "9.0.0");
		}
		if($('#coverwrap_flash').length == 1)
		{
			swfobject.embedSWF("/media/images/done_coverwraps_nongeneric_941.swf?xmlRef=coverwraps_2.xml", "coverwrap_flash", "533", "255", "9.0.0");
		}
		if($('#gatefolds_flash').length == 1)
		{
			swfobject.embedSWF("/media/images/done_gatefolds2_nongeneric_916.swf?xmlRef=gatefolds_2.xml", "gatefolds_flash", "533", "255", "9.0.0");
		}
		if($('#insert_flash').length == 1)
		{
			swfobject.embedSWF("/media/images/done_insert2_nongeneric_917.swf?xmlRef=inserts.xml", "insert_flash", "533", "255", "9.0.0");
		}
		if($('#inverted_flash').length == 1)
		{
			swfobject.embedSWF("/media/images/done_inverted_nongeneric_939.swf?xmlRef=invertedmagazinesadverts_2.xml", "inverted_flash", "533", "255", "9.0.0");
		}
	}

	$("ul.CreativeSolutions li ul").hide();
	$("ul.CreativeSolutions li#Title ul").show();
	$("ul.CreativeSolutions li ul li div.Slider").hide();
	
	$("ul.CreativeSolutions h3").each(function(t){
		if($(this).siblings("ul").children("li").length == 1)
		{
			$(this).siblings("ul").children("li").children('h4').remove();
			var a = $(this).siblings().find(".Slider");
			$(this).siblings("ul").remove();
			$(this).after(a);
		}
	});
	
	$("ul.CreativeSolutions h3").click(function(){
		// If this is the <li> containing the titles
		if($(this).parent().parent().parent().is('#Title')){
			// do nothing
		}
		// If the panel is open already (we click on it to close it)
		else if($(this).is('.Active')) {
			// Remove the .gray class to display the colored dots again
			$("h3 span").removeClass("gray"); 
			// Remove the active class
			$(this).removeClass("Active");
			// Close the ul, the slider and remove from the slider the Open class
			$(this).siblings("ul").slideUp("slow");
			$("div.Slider").slideUp("slow");
			$("div.Slider").removeClass("Open");
			
			// Make the containing li 29px high again
			$(this).parent("li").animate({ height: "29px" });
			return false;
		}
		
		// If the panel is closed
		else {
			// The li becomes as big as the content it has to show
			$(this).parent("li").css({ height: "auto" });
			
			// Make all the dots go gray
			$("h3 span").each(function(t){
				if($(this).attr('class') != '') {
					$(this).addClass("gray");
				}
			})
			
			// Close the other opened lists and open the current one
			$("h3.Active").siblings("ul").slideUp("slow");
			$(this).siblings("ul").slideDown("slow");

			// Remove the .Active class from the other h3 and add it to the current one
			$("ul.CreativeSolutions h3").removeClass("Active");
			$(this).addClass("Active");
				
			// Close all the open divs with a .Slider class and removes their .Open class
			$("div.Slider").slideUp("slow");
			$("div.Slider").removeClass("Open");
			
			// Then open the current one and give it a .Open class
			$(this).siblings("div.Slider").slideDown("fast");
			$(this).siblings("div.Slider").addClass("Open");
			
			return false;
 		}
	});
	
	$("ul.CreativeSolutions h4").click(function(){
		if($(this).siblings("div.Slider").is('.Open')) 
		{
			$(this).siblings("div.Slider").children("div.ajaxExtender").slideUp("slow");
			$(this).siblings("div.Slider").slideUp("slow").removeClass("Open");
			$(this).siblings('div.Slider').children('ajaxExtender').children().remove();
			return false;
		} 
		else 
		{
			getUrl = $(this).siblings('div.Slider').children('div.ajaxExtender').attr('rel');
			$("ul.CreativeSolutions li ul li div.Slider div.ajaxExtender").slideUp("slow");
			$("ul.CreativeSolutions li ul li div.Slider").slideUp("slow");
			$(this).siblings("div.Slider").slideDown("slow",function(){
				$(this).children('div.ajaxExtender').load(getUrl,{},function(){
					$(this).slideDown();
					registerCSFlash();
				});
			}).addClass("Open");
			return false;
		}
	});
});