var videoPlayer = new function()
{
	this.clipIndex = 0;
	this.clips = [];
	this.clip = null;
	this.developmentMode = false;
	this.showPopupVideo = false;
	this.popUpUrl = "http://content.screencast.com/users/i3/folders/homepage-en/media/c92df4cb-8837-4c4d-868c-3e7157c86d9d/25th-anniversary.flv";
	this.popUpPlayed = false;

	this.init = function()
	{
		this.clips[0] = flowHelper.findVideo("corporate")[0];
		this.clips[1] = flowHelper.findVideo("technology")[0];
		this.clips[2] = flowHelper.findVideo("testimonials")[0];
			
		$(document).ready(function(){			

			$('#panel-sliders .bar').mosaic({
				opacity:0.8,
				animation:'slide'
			});		

			//Listen to user video click and interup automatic playback
			$(".home-page-slider-text #home-page-video-nav a").click(function(){
				
				//Get Index and pass index to videoPlayer.playNext();				
				index = $(".home-page-slider-text #home-page-video-nav a").index(this);
				videoPlayer.playNext(index);				
				return false;
			});
			
			sliderWidget.init();

			videoPlayer.start();
		});
	}

	this.start = function()
	{
		if(this.showPopupVideo && flowHelper.language == 'en')
		{
			this.popUpPlayed = true;
			this.loadVideoPopup();
		}
		else
		{
			this.startHomePageVideos();
		}
	}

	this.loadVideoPopup = function()
	{
		/*document.write('<a href="#" id="fancyPopup" title="FancyBox Demo" style="display:none"></a>');
		document.write('<link href="/js/fancybox/jquery.fancybox-1.3.4.css" rel="stylesheet"  type="text/css"/>');
		document.write('<script src="/js/fancybox/jquery.fancybox-1.3.4.pack.js"><\/script>');*/
		
		$(document).ready(function(){
			//$("#fancyPopup").fancybox().trigger('click');
			$("#fancyPopup").fancybox({
				'autoScale': true,
				'transitionIn': 'fade',
				'transitionOut': 'fade',
				'onClosed': function(){
					videoPlayer.startHomePageVideos();
				}
			}).trigger('click');
			
			videoPlayer.startPlayer(videoPlayer.popUpUrl, 
									"fancyPopupTarget", 
									videoPlayer.startHomePageVideos);
		});
	}

	this.startHomePageVideos = function()
	{
		if(!videoPlayer.developmentMode)
		{
			if(this.popUpPlayed)
			{
				//$("#fancyPopup").fancybox().remove();
				parent.$.fancybox.close();
			}
			videoPlayer.playNext(0);
		}
	}
	
	this.autoPlay = function()
	{
		this.clip = this.clips[this.clipIndex];
		this.clipIndex++;
	}

	this.startPlayer = function(_url, _container, _callback)
	{
		if(_url==null || typeof(_url) == "undefined")
			return

		if(_container==null || typeof(_container) == "undefined")
		{
			_container = "video-container";
		}

		if(_callback==null || typeof(_callback) == "undefined")
		{
			_callback = videoPlayer.playNextHomeVideo;
		}


		flowplayer(_container, {src:"../media/swf/flowplayer-3.2.7.swf",wmode: 'opaque'}, {
			clip:{
					url:_url,
					onFinish:_callback
				}
			});
	}

	this.playNextHomeVideo = function(clip)
	{
		videoPlayer.playNext();
	}

	this.playNext = function(index)
	{
		if(index!=null && (index>=0 || index<=2) )
		{
			this.clipIndex = index;
		}

		if(this.clipIndex < this.clips.length)
		{
			this.clip = this.clips[this.clipIndex];
			this.startPlayer(this.clip);
			this.highlightTopic(this.clipIndex);
			sliderWidget.step(this.clipIndex);		
			sliderWidget.slide();
			this.clipIndex++;
		}
	}

	this.highlightTopic = function(index)
	{
		selector =  ".home-page-slider-text .home-page-video-nav a";
		$(selector).removeClass("selected");
		$(selector).eq(index).addClass("selected");
	}
		
	this.init();
}
