/* Javascript rotating/fading div carousel 
 *
 * Modification of code from original source here
 * Fade In Content Viewer: By JavaScript Kit: http://www.javascriptkit.com
 *
 * Additions include properties, slideshow facility cross fading between slides,change to init parameters
 * 
 *
 * Sachin Patel
 */

var Rotator={
	csszindex: 100,
	fade:function($allcontents, selected){
		var selected=parseInt(selected)
		var $controlbar=$("#"+Rotator.props.CONTROLBARID)
		
		var $newtext=$("."+Rotator.props.FADETEXTID+"_"+selected)
		var $oldtext=$("."+Rotator.props.FADETEXTID+"_"+parseInt($controlbar.attr('lastselected')));
		var $newfade=$("."+Rotator.props.FADEPANELID+"_"+selected)
		var $oldfade=$("."+Rotator.props.FADEPANELID+"_"+parseInt($controlbar.attr('lastselected')));
		
		var $target=$allcontents.eq(selected)
		var $oldone = $allcontents.eq(parseInt($controlbar.attr('lastselected')))
		if ($target.length==0){ 
			return 
		}
		if ($controlbar.attr('lastselected')==null || parseInt($controlbar.attr('lastselected'))!=selected){
			var $toc=$("#"+Rotator.props.CONTROLBARID+" .pageItem")
			var $selectedlink=$toc.eq(selected)
			$("#"+Rotator.props.CONTROLBARID+" .next").attr('nextSlide', (selected<$allcontents.length-1)? selected+1+'pg' : 0+'pg')
			$("#"+Rotator.props.CONTROLBARID+" .play").attr('nextSlide', (selected<$allcontents.length-1)? selected+1+'pg' : 0+'pg')
			$("#"+Rotator.props.CONTROLBARID+" .prev").attr('previousSlide', (selected==0)? $allcontents.length-1+'pg' : selected-1+'pg')
			$newfade.show()			
			$newtext.show()
			$oldfade.hide()
			$oldtext.hide()
			
			$target.css({zIndex: this.csszindex, visibility: 'visible'})			
			$oldone.fadeOut(Rotator.props.SLIDEFADEOUTSPEED)
			$target.hide()
			$target.fadeIn(Rotator.props.SLIDEFADEINSPEED)
			$toc.removeClass('selected')
			$selectedlink.addClass('selected')
			$controlbar.attr('lastselected', selected+'pg')
			$oldone.css({zIndex: 0})
		}
	},

	setupcontrolbar:function($allcontents){
		var $toc=$("#"+Rotator.props.CONTROLBARID+" .pageItem");
		$toc.each(function(index){
				$(this).attr('slideNumber', index+'pg')
		});
		
		var $next=$("#"+Rotator.props.CONTROLBARID+" .next")
		var $prev=$("#"+Rotator.props.CONTROLBARID+" .prev")
		var $play=$("#"+Rotator.props.CONTROLBARID+" .play")
		$play.attr('nextSlide', 0+'pg')

		$next.click(function(){
			Rotator.fade($allcontents,$(this).attr('nextSlide'))
			return false
		})
		$prev.click(function(){
			Rotator.fade($allcontents, $(this).attr('previousSlide'))
			return false
		})
		$toc.click(function(){
			Rotator.fade($allcontents, $(this).attr('slideNumber'))
			return false
		})
		$play.click(function(){
			if (Rotator.props.RUNNING==0) {
			Rotator.props.RUNNING=1
			$play.addClass("pause")
			$play.timer=setInterval(function(){Rotator.fade($allcontents, $("#"+Rotator.props.CONTROLBARID+" .play").attr('nextSlide'))},Rotator.props.TRANSITION)
				return false
			} else {
				$play.removeClass("pause")
				clearInterval($play.timer)
				Rotator.props.RUNNING=0
				return false	
			}
		})

	},
	props : {
		RUNNING : 0,
		TRANSITION : 9500,
		SLIDEFADEINSPEED : 950,
		SLIDEFADEOUTSPEED : 750,
		WRAPPERID : "rotator",
		CONTROLBARID : "controls",
		PANELCLASS : "fadecontent",
		FADETEXTID : "largePromoModuleCopy",
		FADEPANELID : "fade",
		DEFAULTSELECTED : 0
	},		
	init:function(){
		$(document).ready(function(){
			var faderheight=$("#"+Rotator.props.WRAPPERID).height()
			var $fadecontents=$("#"+Rotator.props.WRAPPERID+" ."+Rotator.props.PANELCLASS)
			$fadecontents.css({top: 0, left: 0, height: faderheight, visibility: 'hidden'})
			Rotator.setupcontrolbar($fadecontents)
			/* Initialise fade and start playing */
			setTimeout(function(){Rotator.fade($fadecontents, Rotator.props.DEFAULTSELECTED);$("#"+Rotator.props.CONTROLBARID+" .play").click();}, 100)	
			$(window).bind('unload', function(){ //clean up
				$("#"+Rotator.props.CONTROLBARID+" .pageItem").unbind('click')
				$("#"+Rotator.props.CONTROLBARID+" .next", "#"+Rotator.props.CONTROLBARID+" .prev").unbind('click')
				$("#"+Rotator.props.CONTROLBARID+" .play").unbind('click')
			})
		})
	}
}

