﻿ForteCarousel = new ForteClass
(
	function(elm, options)
	{
		this.data=
		{
			 numberOfSlides				: 0
			,transitionSpeed			: 400
			,transitionType				: 'slide'
			,displayTime				: 3250
			,textholderHeight			: .2
			,autoRotate					: 1
			,autoPlayVideo				: 0
			,imageHeight				: 0
			,imageWidth					: 0
			,displayControllerBar		: 1
			,displaySelectors			: 1
			,isDebugMode				: 0
			,isOptimizePerformanceMode	: 1//currently, just performs a blind one-time thru with every slide as that seems to help browsers (noticed browsers always did better the second time around)
			,createControlsJsMethod		: null//(ForteCarousel, MainContainerElement) a method can be called to create custom control buttons
		}

		jQuery(elm);//ensure the base element is jQuery extended
		jQuery.extend(this.data, options);//same as structAppend() in ColdFusion

		if(!elm.id)
			throw 'Element received by ForteCarousel, does not have an element ID attribute'

		this.data.baseElementId		= elm.id
		this.data.enableControls	= true;
		
		this.registerElementIds();
	
		this.initSetup();

		return this;
	},'ForteCarousel'
)

ForteCarousel.prototype.getBaseElement = function()
{return document.getElementById(this.data.baseElementId)}

ForteCarousel.prototype.registerElementIds = function()
{
	var local = {elmArray:jQuery(".mediaObjectCt ul li", this.getBaseElement())}

	this.data.numberOfSlides = local.elmArray.length;

	this.SiblingElementIdCollection = new SiblingElementIdCollection({isInfinitLoopMode:1, isDebugMode:this.data.isDebugMode})

	for(local.elmIndex=0; local.elmIndex < local.elmArray.length; ++local.elmIndex)
	{
		local.element = jQuery(local.elmArray[local.elmIndex])
		local.id = local.element.attr('id');

		if(typeof(local.id)=='undefined' || !local.id.length)
		{
			local.id = this.data.baseElementId+'_slide'+local.elmIndex;
			local.element.attr('id',local.id);
		}

		this.SiblingElementIdCollection.setElementId(local.id);
	}

	this.log('total slide found: '+this.SiblingElementIdCollection.getLength())
}

ForteCarousel.prototype.initSetup = function()
{
	var local = {BaseElement:jQuery(this.getBaseElement())}

	/* init processing */
		// setup main container
		local.mainUl = jQuery(".mediaObjectCt ul", local.BaseElement);
		local.mainUl.width(this.data.imageWidth);
		local.mainUl.height(this.data.imageHeight);

		local.itemArray = jQuery(".mediaObjectCt ul li", local.BaseElement);
		local.itemArray.width(this.data.imageWidth);
		local.itemArray.height(this.data.imageHeight);
		local.itemArray.css('float', 'left');
		local.itemArray.css('position', 'relative');//break the css that is saying absolute

		//jQuery(".mediaObjectCt .objectCaptionCt", local.BaseElement).width(this.data.imageWidth);//caption that describes a slide
	
		// add video support
		local.videoHref = jQuery(".mediaObjectCt ul li a.video", local.BaseElement);
		local.videoHref.append('<div class="objectPlayButtonCt"><img height="83" width="83" border="0" src="/global/HomepageMedia/4_0/images/play_large.png"></div>');
		local.videoHref.click(this.getMethodCallback('onPlayVideo'));

		local.youTube = jQuery('.mediaObjectCt ul li a.youtube',local.BaseElement);
		local.youTube.click(this.getMethodCallback('onYoutubeClicked'));
		local.youTube.append('<div class="objectPlayButtonCt"><img height="83" width="83" border="0" src="/global/HomepageMedia/4_0/images/play_large.png"></div>');
/*
		local.swf = jQuery('.mediaObjectCt ul li a.swf',local.BaseElement);
		local.swf.click(this.getMethodCallback('onPlaySwf'));
		local.swf.append('<div class="objectPlayButtonCt"><img height="83" width="83" border="0" src="/global/HomepageMedia/4_0/images/play_large.png"></div>');
*/
		/* swf player container */
/*
			this.data.swfPlayerId = local.BaseElement.attr("id") + "_SwfPlayerCt";
			
			local.jDiv = jQuery(document.createElement('div')).attr('id',this.data.swfPlayerId).addClass('swfPlayerCt');
			
			jQuery(".mediaObjectCt", local.BaseElement).prepend(local.jDiv);
			local.jDiv.height(this.data.imageHeight);
			local.jDiv.width(this.data.imageWidth);
			local.jDiv.css('z-index','200');
			local.jDiv.css('position','absolute');
			local.jDiv.css('display','none');
*/
		/* end */
		
		/* video player container */
			this.data.videoPlayerId = local.BaseElement.attr("id") + "_VideoPlayerCt";
			
			local.jDiv = jQuery(document.createElement('div')).attr('id',this.data.videoPlayerId).addClass('videoPlayerCt');
			
			jQuery(".mediaObjectCt", local.BaseElement).prepend(local.jDiv);
			local.jDiv.height(this.data.imageHeight);
			local.jDiv.width(this.data.imageWidth);
			local.jDiv.css('z-index','200');
			local.jDiv.css('position','absolute');
			local.jDiv.css('display','none');
		/* end */
	
		//Build Carousel Controller Elements
		if(this.data.displayControllerBar)
			this.createDisplayControllerBarInElement(local.BaseElement)

		if(this.data.transitionType=='fade' || this.data.transitionType=='fadeSlide')
			this.Animator = new SiblingElementFadeAnimator(this.SiblingElementIdCollection,this.data)
		else if(this.data.transitionType=='slide')
			this.Animator = new SiblingElementSlideAnimator(this.SiblingElementIdCollection,this.data)
		else
			throw 'ForteCarousel invalid tranistionType of '+this.data.transitionType+'. Only fade, fadeSlide and slide are only supported';


		/* perform a once thru which seems to help browsers */
			if(this.data.isOptimizePerformanceMode && this.data.transitionType=='slide')
			{
				local.BaseElement.css('visibility','hidden')

				for(local.aIndex=0; local.aIndex < this.data.numberOfSlides; ++local.aIndex)
				{
					this.SiblingElementIdCollection.getNext();
					this.Animator.animate(0);
				}
				this.log('Performed a one time thru all slides. Making base container element visible.');
				local.BaseElement.css('visibility','visible');
			}
		/* end */

		setTimeout( this.getMethodCallback('autoMove'), this.data.displayTime + this.data.transitionSpeed);
	/* end */
		
	return this
}

ForteCarousel.prototype.createDisplayControllerBarInElement = function(Element)
{
	var local = {BaseElement:jQuery(Element)}

	local.MediaControlElement = jQuery('.mediaControllerCt', local.BaseElement);
	//local.MediaControlElement = document.createElement('div');
	//local.MediaControlElement.className = 'mediaControllerCt';
	
	jQuery(local.BaseElement).append(local.MediaControlElement);

	// player controls
	local.MediaControlElement.append('<div class="controllerCt"><div class="controllerInnerCt"></div></div>');

	if(typeof(this.data.createControlsJsMethod) == typeof(function(){}))//is custom build buttons method defined
	{
		this.data.createControlsJsMethod(this, this.getBaseElement());
		this.log('controls created by outside method');
	}else
	{
		local.LiButtonContainer = document.createElement('ul');
		local.LiButtonContainer.className='controllerItemCt';
		local.LiButtonContainer= jQuery(local.LiButtonContainer);
		jQuery('div.controllerCt div.controllerInnerCt', local.MediaControlElement).append(local.LiButtonContainer);
		

		local.LiButtonContainer.append('<li class="previous_button" title="Previous"><a href="javascript:void(0);"><span>Previous</span></a></li>');
		local.LiButtonContainer.append('<li class="play_button" title="Play"><a href="javascript:void(0);"><span>Play</span></a></li>');
		local.LiButtonContainer.append('<li class="pause_button" title="Pause"><a href="javascript:void(0);"><span>Pause</span></a></li>');
		local.LiButtonContainer.append('<li class="next_button" title="Next"><a href="javascript:void(0);"><span>Next</span></a></li>');
	
		local.MediaControlElement.append('<div class="selectorCt"><div class="selectorInnerCt"><ul class="selectorItemCt"></ul></div></div>')	

		/* put selectors in */
			local.selectorItemCt = jQuery('.selectorItemCt', local.BaseElement);
			for (local.i=0; local.i < this.data.numberOfSlides; local.i++)
			{
				local.LiElement = document.createElement('li');
				local.LiElement.className = 'selectorItem';

				local.AElement = document.createElement('a');
				local.AElement.href='javascript:void(0);';
				local.SpanElement = document.createElement('span');
				local.SpanElement.innerHTML = local.i+1;
		
				local.jAElement = jQuery(local.AElement);
	
				local.AElement.appendChild(local.SpanElement);
				local.LiElement.appendChild(local.AElement);
				local.selectorItemCt.append(local.LiElement);

			}
		/* end */
	}

	/* set events on the buttons */
		this.setPreviousButtonElement(jQuery(".previous_button", local.LiButtonContainer));
		this.setPlayButtonElement(jQuery(".play_button", local.LiButtonContainer))
		this.setPauseButtonElement(jQuery(".pause_button", local.LiButtonContainer))
		this.setNextButtonElement(jQuery(".next_button", local.LiButtonContainer))
	
		/* set click events */
			local.selectorArray = jQuery('li.selectorItem', local.BaseElement);
			for (local.i=0; local.i < this.data.numberOfSlides; local.i++)
			{
				local.selector = jQuery(local.selectorArray[local.i]);
				local.selector.attr('slideIndex', local.i);
				jQuery('*',local.selector).attr('slideIndex', local.i);
				local.selector.click( this.getMethodCallback('onSelectorClicked') );
			}
			
			this.log('Total Slide Selector Button Events Applied: '+local.selectorArray.length);
		/* end */
	/* end */


	this.calculateActiveSelectorClass();
	this.calculateRotateButtonDisplay();

	this.log('Created Display Controller Bar')
}

/* event subscribers */
	ForteCarousel.prototype.setNextButtonElement = function(Elm)//enables any button to be specified
	{jQuery(Elm).click(this.getMethodCallback('onNextButtonClicked'))}
	
	ForteCarousel.prototype.setPauseButtonElement = function(Elm)//enables any button to be specified
	{jQuery(Elm).click(this.getMethodCallback('onPauseButtonClicked'))}
	
	ForteCarousel.prototype.setPlayButtonElement = function(Elm)//enables any button to be specified
	{jQuery(Elm).click(this.getMethodCallback('onPlayButtonClicked'))}
	
	ForteCarousel.prototype.setPreviousButtonElement = function(Elm)//enables any button to be specified
	{jQuery(Elm).click(this.getMethodCallback('onPreviousButtonClicked'))}
/* end */

ForteCarousel.prototype.play = function()
{
	this.data.autoRotate = true;
	this.autoMove();
}

ForteCarousel.prototype.pause = function()
{
	this.data.autoRotate = false
	this.calculateRotateButtonDisplay();
	this.log('paused');
}

ForteCarousel.prototype.moveToPrevious = function()
{
	this.SiblingElementIdCollection.getPrevious();
	this.animate()
}

ForteCarousel.prototype.moveToNext = function()
{
	this.SiblingElementIdCollection.getNext()
	this.animate()
}

ForteCarousel.prototype.moveTo = function(targetIndex)
{
	if(this.SiblingElementIdCollection.isCurrentIndex(targetIndex))return;
	this.SiblingElementIdCollection.getByIndex(targetIndex)
	this.animate();
}

ForteCarousel.prototype.autoMove = function()
{
	if (!this.data.autoRotate)return;
	this.moveToNext();
	setTimeout(this.getMethodCallback('autoMove'), this.data.displayTime + this.data.transitionSpeed)
}

ForteCarousel.prototype.animate = function()
{

	this.disableControllerElements();
	this.removeVideoPlayer(this.data.videoPlayerId);

	this.Animator.animate();
	
	this.calculateActiveSelectorClass();
	this.calculateRotateButtonDisplay();

	setTimeout(this.getMethodCallback('enableControllerElements'), this.data.transitionSpeed);

	this.log('Now @ '+(this.SiblingElementIdCollection.data.currentIndex+1)+' of '+this.data.numberOfSlides+' slides @ speed '+this.data.transitionSpeed);
}

ForteCarousel.prototype.calculateRotateButtonDisplay = function()
{
	var local = {BaseElement:this.getBaseElement()};

	if(this.data.autoRotate)
	{
		if(jQuery('.play_button',local.BaseElement)[0]){
			jQuery('.play_button',local.BaseElement)[0].style.display='none';
		}
		if(jQuery('.pause_button',local.BaseElement)[0]){
			jQuery('.pause_button',local.BaseElement)[0].style.display='';
		}
	}else
	{
		if(jQuery('.play_button',local.BaseElement)[0]){
			jQuery('.play_button',local.BaseElement)[0].style.display='';
		}
		if(jQuery('.pause_button',local.BaseElement)[0]){
			jQuery('.pause_button',local.BaseElement)[0].style.display='none';
		}
	}
}

ForteCarousel.prototype.calculateActiveSelectorClass = function()
{
	var local = {};
	local.index = this.SiblingElementIdCollection.getCurrentIndex();
	local.slideSelector = jQuery('.selectorItemCt li.selectorItem',this.getBaseElement());

	for(local.selIndex=local.slideSelector.length-1; local.selIndex > -1; --local.selIndex)
	{
		local.jSlide = jQuery(local.slideSelector[local.selIndex]);

		if(local.selIndex == local.index)
			local.jSlide.addClass("active");
			
		else
			local.jSlide.removeClass("active");
	}
}

ForteCarousel.prototype.enableControllerElements = function()
{this.data.enableControls = true}

ForteCarousel.prototype.disableControllerElements = function()
{this.data.enableControls = false}

/* interaction event handlers */
	ForteCarousel.prototype.onPlayButtonClicked = function()
	{if(this.data.enableControls)this.play()}
	
	ForteCarousel.prototype.onPauseButtonClicked = function()
	{if(this.data.enableControls)this.pause()}
	
	ForteCarousel.prototype.onNextButtonClicked = function()
	{
		if(!this.data.enableControls)return;
		this.pause()
		this.moveToNext()
	}
	
	ForteCarousel.prototype.onSelectorClicked = function(evt)
	{
		if (!this.data.enableControls)return;

		this.pause();
		var index = parseInt(jQuery(evt.target).attr('slideIndex'));
		this.log('Index Button Clicked For '+index)
		this.moveTo(index, 10);
	}

	ForteCarousel.prototype.onPreviousButtonClicked = function()
	{
		if (!this.data.enableControls)return;
	
		this.pause();
		this.moveToPrevious();
	}
/* end */

/* video based methods */	
	ForteCarousel.prototype.onPlayVideo = function()
	{
		this.pause();
		var videoUrl = jQuery("a:first", jQuery(".mediaObjectCt ul li", this.getBaseElement()).eq(this.SiblingElementIdCollection.data.currentIndex)).attr("href");
		this.loadVideoPlayer(this.data.videoPlayerId, videoUrl);
		return false;
	}
	
	ForteCarousel.prototype.removeVideoPlayer = function(playerId)
	{
		jQuery('#' + playerId,this.getBaseElement()).empty().hide()

		/* remove all and every youtube */
			jQuery('.inlineYoutubePlayer',this.getBaseElement()).each(function(){jQuery(this).remove()});
		/* end */
	}
	
	ForteCarousel.prototype.loadVideoPlayer = function(playerId, videoUrl)
	{
		var local = {};
		
		local.jPlayerElement = jQuery('#' + playerId,this.getBaseElement()).empty().show();
	
		flowplayer(playerId, {
			src: 'http://cs.trustedpartner.com/flowplayer/swf/flowplayer-3.2.6.swf',
			wmode: 'transparent',
			autoBuffering: false,
			bufferLength: 30
		}, {
			clip: { url: videoUrl },
			cachebusting: 'true',
			plugins: {
				controls: {
					backgroundColor: '#cfcfcf',
					backgroundGradient: 'high',
					timeColor: '#000000',
					durationColor: '#000000',
					progressColor: '#E72727',
					bufferColor: '#C78F8E',
					buttonColor: '#999999',
					buttonOverColor: '#666666',
					volumeSliderColor: '#C78F8E',
					timeBgColor: '#B1B1B1'
				}
			}
		}).ipad();
	}
	
	ForteCarousel.prototype.onYoutubeClicked = function(event)
	{
		var local = {};

		this.pause();

		local.randomVideoID = Math.round(Math.random()*100000000);
		local.playerId = 'InlineYoutubePlayerCt_' + local.randomVideoID;

		local.elmTest = [jQuery(event.target), jQuery(event.target).parent(), jQuery(event.target).parent().parent(), jQuery(event.target).parent().parent().parent()];

		local.videoUrl = '';
		local.anchorElem = '';
		local.loadPlayer = false;

		for(local.elmIndex=0; local.elmIndex < local.elmTest.length; ++local.elmIndex)
		{
			local.elm = local.elmTest[local.elmIndex];

			if (local.elm.is('a'))
			{
				local.anchorElem = local.elm;
				local.videoUrl = local.elm.attr('href');
				local.img = jQuery('img',local.elm);
				break;
			}
		}
		
		if(local.anchorElem.length && local.videoUrl != '')
			local.loadPlayer = true;

		if (local.loadPlayer)
		{
			jQuery(local.anchorElem).after('<div id="'+local.playerId+'" class="inlineYoutubePlayer"></div>');
			jQuery('.inlineYoutubePlayer').width(local.img.attr('width')).height(this.data.imageHeight);
			this.loadYoutubePlayer(local.playerId,local.videoUrl);
		}

		return false;
	}

	ForteCarousel.prototype.loadYoutubePlayer = function(playerId,videoUrl)
	{
		flashembed(playerId, { 
			src: videoUrl,
			wmode: 'transparent'
		},{
			autoplay: 1,
			playerMode: 'embedded',
			rel: 0,
			iv_load_policy: 3,
			showinfo: 0,
			wmode: "transparent"
		});
	}
	
	ForteCarousel.prototype.onSwfClicked = function(event)
	{
		var local = {};

		this.pause();

		local.randomVideoID = Math.round(Math.random()*100000000);
		local.playerId = 'InlineSwfPlayerCt_' + local.randomVideoID;

		local.elmTest = [jQuery(event.target), jQuery(event.target).parent(), jQuery(event.target).parent().parent(), jQuery(event.target).parent().parent().parent()];

		local.videoUrl = '';
		local.anchorElem = '';
		local.loadPlayer = false;

		for(local.elmIndex=0; local.elmIndex < local.elmTest.length; ++local.elmIndex)
		{
			local.elm = local.elmTest[local.elmIndex];

			if (local.elm.is('a'))
			{
				local.anchorElem = local.elm;
				local.videoUrl = local.elm.attr('href');
				local.img = jQuery('img',local.elm);
				break;
			}
		}
		
		if(local.anchorElem.length && local.videoUrl != '')
			local.loadPlayer = true;

		if (local.loadPlayer)
		{
			jQuery(local.anchorElem).after('<div id="'+local.playerId+'" class="inlineSwfPlayer"></div>');
			jQuery('.inlineSwfPlayer').width(local.img.attr('width')).height(this.data.imageHeight);
			local.videoUrl = local.videoUrl + this.SiblingElementIdCollection.data.currentIndex;
			this.loadSwfPlayer(local.playerId,local.videoUrl);
		}

		return false;
	}
	
	ForteCarousel.prototype.loadSwfPlayer = function(playerId,videoUrl)
	{
		swfobject.embedSWF(videoUrl, playerId, "300", "300", "9.0.0");
	}
	/*
		ForteCarousel.prototype.onPlayVideo = function()
		{
			this.pause();
			var swfUrl = jQuery("a:first", jQuery(".mediaObjectCt ul li", this.getBaseElement()).eq(this.SiblingElementIdCollection.data.currentIndex)).attr("href");
			this.loadVideoPlayer(this.data.swfPlayerId, swfUrl);
			return false;
		}
	*/
	
/* end */
