<!--
/* ------------------------------------------------------------------------ */
/* Swoosher Promo Panel by Abrar Khan 2009

   This is version 4 of the Promo Panel (Swoosher). The only change between this 
   and version 3 is that this version can be used sitewide whereas the previous 
   version had the url of the site hardcoded this one does not have this and instead
   has the site url passed in by the calling function as below:
      
		PromoPanel.Initialise(1, 4000, SiteURL, "default");   
      
   Enhancements rolled forward from version 3:
   This version has has more efficient code as clicking a link 
   is calling the same function as looping through all promos once so reducing 
   duplicate functions. 
   
   Enahncements rolled forward from version 2 in this version is that you are now 
   able to specify time intervals for how long you want the displayed panel to be 
   displayed before fading out and also what speed you want the fade out/in transition 
   to take.

   This version also allows you to add as many <li> link tags for the swoosher, 
   the previous version was restricted to 4 and only 4. This version calculates 
   how many <li> tags are in the promo_number_links element and then automatically 
   loops through all of them and finishing by returning to the first one
   
   You are also able to specify a directory that you want to use for the 
   promo panel content. 
   
   You would add the following code to the header of the page displaying the
   swoosher to specify the content directory:

                 <script type="text/javascript">
                    var swoosher_directory = "monday_highlights";
                 </script>

		or if calling from a javascript file you would use the following call:
	
		PromoPanel.Initialise(starting_image, user_defined_interval, "directory_name");

		with the "directory_name" being the specified directory name.   
   */
/* ------------------------------------------------------------------------ */
var glow;
var dom;
var $;
var events;
var anim;

var myPanel = null;

gloader.load(["glow", "1", "glow.dom", "glow.anim", "glow.net", "glow.events"], {
	async: true,
	onLoad: function(glowLocal)
	{
		glowLocal.ready(function()
		{
			glow = glowLocal;
			dom = glowLocal.dom;
			$ = glowLocal.dom.get;
			events = glowLocal.events;
			anim = glowLocal.anim;
			
//			PromoPanel.Initialise();
		});
	}
});

var updating = false;
var completed_first_loop = false;
var firstInterval = 0;
var slideShowInterval = 0;
var promo_interval_time = 4000;
var myInterval = Array();
var total_promo_items =0;
var promo_directory = "";
var site_path = "";
var patches_dir_path = "";
var number_map = new Array();
number_map["one"]="1";
number_map["two"]="2";
number_map["three"]="3";
number_map["four"]="4";


/* ------------------------------------------------------------------------ */
/* --------->>>   G L O B A L  H O M E P A G E   O B J E C T   <<<--------- */
/* ------------------------------------------------------------------------ */
var PromoPanel =
{
/* ------------------------------------------------------------------------ */
/* ------------->>> I N I T I A L I S E  H O M E P A G E   <<<------------- */
/* ------------------------------------------------------------------------ */
	Initialise: function(starting_image, user_defined_interval, site_path, directory_name)
	{
		site_path = site_path;

		if(typeof(swoosher_directory) != "undefined")
		{
			directory_name = swoosher_directory;
		}
		promo_interval_time = user_defined_interval;
		promo_directory = directory_name;
		patches_dir_path = site_path+"patches/swoosher/"+promo_directory;

		//Lets start by getting a total of all promo links.
		total_promo_items = glow.dom.get(".promo_number_links li").length;

		var current_image = starting_image;
		var next_promo = starting_image + 1;
		if(next_promo > total_promo_items)
		{
			next_promo = 1;
		}

		PromoPanel.MainPromoImage.InitPromoImages();
		firstInterval = setInterval("PromoPanel.MainPromoImage.PlayAllPromos("+next_promo+")", promo_interval_time);
	},

/* ------------------------------------------------------------------------ */
/* ------------------>>>  M A I N   P R O M O   I M A G E   <<<------------ */
/* ------------------------------------------------------------------------ */
	MainPromoImage: 
	{
/* ------------------------------------------------------------------------ */
/* Function:PromoPanel.MainPromoImage.InitPromoImages(); */
/* ------------------------------------------------------------------------ */
		InitPromoImages: function()
		{
			glow.events.addListener('.promo_number_links ul li a', 'click',  
				function(e) 
				{ 
					e.preventDefault();

					if(updating == false)
					{
						url = this.href;
						var qs = url.split("?")[1];
						var vars = qs.split("&");

						for (var i = 0; i < vars.length; i++)
						{
							url = vars[i].split("=");
							var name = url[0];
							var value = url[1];

							if(name == "selected_promo")
							{
								_selected_promo = value;
							}

							//lets set the completed playing all promo flag to true so that it doesn't continue changing after 
							//being clicked if the first loop is not yet completed.
							completed_first_loop = true;
							PromoPanel.MainPromoImage.FadeOutImagePanel(number_map[_selected_promo]);
						}		
					}
				}
			);
		},
/* ------------------------------------------------------------------------ */
/* --------------->>>    P L A Y   A L L   P R O M O S       <<<----------- */
/* ------------------------------------------------------------------------ */
		PlayAllPromos: function(next_promo)
		{
/* ------------------------------------------------------------------------ */
/* Function:PromoPanel.MainPromoImage.PlayAllPromos(); */
/* ------------------------------------------------------------------------ */
			//first thing we should do is clear the "firstInterval" interval flag.
			clearInterval(firstInterval);
			PromoPanel.MainPromoImage.FadeOutImagePanel(next_promo);
		},
/* ------------------------------------------------------------------------ */
/* ----------->>>    F A D E O U T   I M A G E   P A N E L      <<<-------- */
/* ------------------------------------------------------------------------ */
		FadeOutImagePanel: function(next_promo)
		{
/* ------------------------------------------------------------------------ */
/* Function:PromoPanel.MainPromoImage.FadeOutImagePanel(); */
/* ------------------------------------------------------------------------ */
			function fadeOutImage()
			{
				glow.net.get(patches_dir_path+"/promo"+next_promo+".sssi", 
				{	onLoad: 
					function(response)  
					{  
						if(updating == false)
						{					
							next_promo++;
							if(next_promo > total_promo_items)
							{
								next_promo = 1;
							}
							
							var animate_out = glow.anim.css(".main_image_holder", 1, {"opacity" : {from:1,to:0}});
							glow.events.addListener(animate_out, "complete", 
								function()
								{
									$(".main_image_holder").empty();
									glow.dom.get(".main_image_holder").append(response.text());
									PromoPanel.MainPromoImage.FadeInImagePanel(next_promo);								
								} 
							);

							animate_out.start();
						}
					},  
					onError: 
					function() 
					{  
						alert("failed");
					}
				});
			}

			thisInterval = myInterval.shift();
			clearInterval(thisInterval);
			fadeOutImage();		
			
		},
/* ------------------------------------------------------------------------ */
/* ----------->>>    F A D E  I N    I M A G E   P A N E L      <<<-------- */
/* ------------------------------------------------------------------------ */
		FadeInImagePanel: function(next_promo)
		{
/* ------------------------------------------------------------------------ */
/* Function:PromoPanel.MainPromoImage.FadeInImagePanel(); */
/* ------------------------------------------------------------------------ */
			function fadeInImage()
			{
				ClearSelectedLink();
				HighlightCurrentLink();
				var animate_in = glow.anim.css(".main_image_holder", 1, {"opacity" : {from:0,to:1}}).start();
				glow.events.addListener(animate_in, "complete", 
					function()
					{
						if(updating == false)
						{
							//if not completed first loop call fade out again.
							if(completed_first_loop == false)
							{
								slideShowInterval = setInterval("PromoPanel.MainPromoImage.FadeOutImagePanel("+next_promo+")", promo_interval_time);
								myInterval.push(slideShowInterval);
								if(next_promo == 1)
								{
									//completed first loop so set boolean to true.
									completed_first_loop = true;
								}
							}
						}
					} 
				);
			}

			function ClearSelectedLink()
			{
				//Remove the active class from all promo links then add to the clicked link
				glow.dom.get(".promo_number_links li a").removeClass("one");
				glow.dom.get(".promo_number_links li a").removeClass("two");
				glow.dom.get(".promo_number_links li a").removeClass("three");
				glow.dom.get(".promo_number_links li a").removeClass("four");
				glow.dom.get(".promo_number_links li a").removeClass("selected");
			}

			function HighlightCurrentLink()
			{
				var current_promo = next_promo-1;
				if(current_promo == 0)
				{
					current_promo = total_promo_items;
				}
				var linkid = ".promo_number_links li.promo"+current_promo+ " a";
				glow.dom.get(linkid).addClass("selected");
			}

			fadeInImage();
		}
	}
}

function debug(myMessage)
{
	console.log(myMessage);
}
//-->

