/*-------------------------------------------------
The slideshow works using a javascript timer and some CityScript. 
The CityScript builds several arrays of data from the slideshow articles.
The timer then increments the index of these arrays and resets fade effects.
The timer is started thanks to the bodyloader function, referenced in all templates.

The text fade effect is currently disabled, as it was surprisingly CPU Intensive. 
To re-enable it, comment out replace_comment and comment in anim_comment (ditto for news).
-------------------------------------------------*/
		var timerCore;
		var timerIncrement_slide;
		var timerIncrement_article;
		
		var slideList_Num = 0;
		var slideList_Current = 0;
		var slideList_Last = 0;
		var slideList_Title = new Array();
		var slideList_Caption = new Array();
		var slideList_Image = new Array();
		var slideList_Link = new Array();
		
		var imageOpacity = 0;
		
		var letterNum_Title = 0;
		var letterNum_Caption = 0;
		var letterCurrent_Title = 0;
		var letterCurrent_Caption = 0;
		var slideCurrentTitle = "";
		var slideCurrentCaption = "";
		var slideFullTitle = "";
		var slideFullCaption = ""; 
		
		var refLink, refTitle, refCaption, refImage, refImageBG;

		
		
			slideList_Title[ slideList_Num ] = "Tiger Woods PGA Tour 09.";
			slideList_Caption[ slideList_Num ] = "IGN talk about our latest DS title.";
			slideList_Image[ slideList_Num ] = "http://www.exient.com/slideshow/slide_images/slide_Tiger09.jpg";
			slideList_Link[ slideList_Num ] = "http://uk.ps2.ign.com/articles/920/920841p1.html";
			slideList_Num = slideList_Num+1;
		
			slideList_Title[ slideList_Num ] = "Need For Speed Undercover";
			slideList_Caption[ slideList_Num ] = "IGN talk about our latest DS title.";
			slideList_Image[ slideList_Num ] = "http://www.exient.com/slideshow/slide_images/slide_NFSUndercover.jpg";
			slideList_Link[ slideList_Num ] = "http://uk.wii.ign.com/articles/931/931609p1.html";
			slideList_Num = slideList_Num+1;
		
			slideList_Title[ slideList_Num ] = "Skate It";
			slideList_Caption[ slideList_Num ] = "IGN talk about our latest DS title.";
			slideList_Image[ slideList_Num ] = "http://www.exient.com/slideshow/slide_images/slide_SkateIt.jpg";
			slideList_Link[ slideList_Num ] = "http://uk.ds.ign.com/articles/873/873067p1.html";
			slideList_Num = slideList_Num+1;
		

		function bodyloader()
		{
			timerIncrement_slide = 0;
			timerIncrement_article = 0;

			replace_image();
			timer_start();
		}
		
		function timer_start()
		{
			timerCore = setTimeout( "timer_start()", 110 );
			timer_step();
		}
		
		function timer_step()
		{
			timerIncrement_slide = timerIncrement_slide + 1;
			if( timerIncrement_slide > 90 )
			{
				timerIncrement_slide = 0;
				switch_slide();
				
				replace_comment();
			}				

			timerIncrement_article = timerIncrement_article + 1;
			if( timerIncrement_article > 60 )
			{
				timerIncrement_article = 0;
				switch_article();

				replace_news();
			}			
			
			anim_image();
			//anim_comment();
			//anim_news();
		}		
		

		function switch_slide()
		{
			refImage = document.getElementById("slideshow_image");
			refImageBG = document.getElementById("slideshow");
			refLink	= document.getElementById("slideshow_link");
			refTitle = document.getElementById("slideshow_title");
			refCaption = document.getElementById("slideshow_content");

			slideList_Current = slideList_Current + 1;
			slideList_Current = ( slideList_Current % slideList_Num );

			refLink.href = slideList_Link[ slideList_Current ];
			refImage.alt = slideList_Title[ slideList_Current ];
			refImage.title = slideList_Caption[ slideList_Current ];

			slideFullTitle = slideList_Title[ slideList_Current ];
			slideFullCaption = slideList_Caption[ slideList_Current ];
			
			reset_comment();
			reset_image();
		}

		function reset_comment()
		{
			letterCurrent_Title = 0;
			letterCurrent_Caption = 0;
		
			letterNum_Title = slideFullTitle.length;
			letterNum_Caption = slideFullCaption.length;
		}

		function reset_image()
		{
			refImage = document.getElementById("slideshow_image");
			refImageBG = document.getElementById("slideshow");

			slideList_Last = slideList_Current + slideList_Num - 1;
			slideList_Last = ( slideList_Last % slideList_Num );
			
			refImage.src = slideList_Image[ slideList_Current ];
			refImageBG.style.backgroundImage = "url(" + slideList_Image[ slideList_Last ] + ")";

			set_opacity( 0 );
		}
	
		function replace_image()
		{
			reset_image();
			
	    	imageOpacity = 100;
	    	set_opacity( imageOpacity );
		}		

		function anim_image()
		{
		    if (imageOpacity < 100) 
		    {
		    	imageOpacity += 12;
		    	set_opacity( imageOpacity );
		    	if( imageOpacity > 100 )
		    		imageOpacity = 100;
		    }
		}
		
		// Effect coertesy of http://clagnut.com/sandbox/imagefades/
		function set_opacity( opacity )
		{
			imageOpacity = opacity;
			if( imageOpacity >= 100 )
				imageOpacity = 99.999;
			if( imageOpacity <= 0 )
				imageOpacity = 0.001;

			// IE/Win
			refImage.style.filter = "alpha(opacity:"+imageOpacity+")";
			  
			// Safari pre-1.2, Konqueror
			refImage.style.KHTMLOpacity = imageOpacity/100;
			  
			// Older Mozilla and Firefox
			refImage.style.MozOpacity = imageOpacity/100;
			  
			// Safari 1.2, newer Firefox and Mozilla, CSS3
			refImage.style.opacity = imageOpacity/100;
		}
	
		function replace_comment()
		{
			letterCurrent_Title = letterNum_Title;
			slideCurrentTitle = slideFullTitle;
			refTitle = replaceHtml( refTitle, slideCurrentTitle );

			letterCurrent_Caption = letterNum_Caption;
			slideCurrentCaption = slideFullCaption;
			refCaption = replaceHtml( refCaption, slideCurrentCaption );
		}

		function anim_comment()
		{
			if( letterCurrent_Title < letterNum_Title )
			{
				letterCurrent_Title = letterCurrent_Title + 1;
				slideCurrentTitle = slideFullTitle.substring( 0, letterCurrent_Title );
				refTitle = replaceHtml( refTitle, slideCurrentTitle );

				//refTitle.innerHTML = slideCurrentTitle;
			}
				
			if( letterCurrent_Caption < letterNum_Caption )
			{
				letterCurrent_Caption = letterCurrent_Caption + 1;
				slideCurrentCaption = slideFullCaption.substring( 0, letterCurrent_Caption );
				refCaption = replaceHtml( refCaption, slideCurrentCaption );
				
				//refCaption.innerHTML = slideCurrentCaption;			
			}
		}
		
// This is much faster than using (el.innerHTML = str) when there are many
// existing descendants, because in some browsers, innerHTML spends much longer
// removing existing elements than it does creating new ones. 
// code coertesy of http://blog.stevenlevithan.com/
		function replaceHtml(el, html) 
		{
	        var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
	        var newEl = oldEl.cloneNode(false);

	        // Preserve the element's id and class (other properties are lost)
	        newEl.id = oldEl.id;
	        newEl.className = oldEl.className;

			// Replace the old with the new
	        newEl.innerHTML = html;
    	    oldEl.parentNode.replaceChild(newEl, oldEl);
			
			// Since we just removed the old element from the DOM, return a reference
			// to the new element, which can be used to restore variable references. 
	        return newEl;
		}
