﻿function Rotator () 
{
	var zone = new Array(), container, buttons, playbutton, current = 0, next = 1, transitionTime = 50, playing = true, laststate = true, timer, replayText, skipText;
	
	function setTimer(method, time)
	{
		if (timer != null)
		{
			clearTimeout(timer);
		}
		
		timer = setTimeout(method, time);
	}
	
	function showimage(button)
	{
		if (container.style.display == 'none')
		{
			showRotator();
		}
		
		contentIndex = button.contentIndex;
		playerstate = playing;
		SetButtonClass(next, 'rotatorindexlink');
		SetButtonClass(current, 'rotatorindexlink');
		SetButtonClass(contentIndex, 'rotatorindexcurrentlink');
		
		if (playing)
		{
			playing = false;
			SetPlayButtonClass('rotatorplay');
		}
		
		if (contentIndex == next)
		{
			if (zone[contentIndex].zoneOpacity <= 0)
			{
				zone[contentIndex].zoneOpacity = .05;
			}
			
			setTimer(rotateFade, container.transition);
		}
		else if (contentIndex == current)
		{
			if (zone[next].zoneOpacity > 0)
			{
				current = next;
				next = contentIndex;
				
				if (zone[contentIndex].zoneOpacity <= 0)
				{
					zone[contentIndex].zoneOpacity = .05;
				}
				
				setTimer(rotateFade, container.transition);
			}
		}
		else
		{
			zone[contentIndex].zoneOpacity = zone[next].zoneOpacity > 0 ? zone[next].zoneOpacity : .05;
			
			if (zone[next].zoneOpacity != 0)
			{
				zone[next].zoneOpacity = 0;
				setOpacity(zone[next]);
			}
			
			zone[next].style.display = 'none';
			next = contentIndex;
			setOpacity(zone[next]);
			zone[next].style.display = 'block';
			setTimer(rotateFade, container.transition);
		}
		
	}

	function pauseplay()
	{
		if (container.style.display == 'none')
		{
			laststate = true;
			
			showRotator();
		}
		else
		{
			playing = !playing;
			
			if (playing)
			{
				setTimer(rotateFade, container.timeout);
				SetPlayButtonClass('rotatorpause');
			}
			else
			{
				SetPlayButtonClass('rotatorplay');
			}
		}
	}	

	function setOpacity(item) 
	{
		if(item.zoneOpacity > 1) 
		{
			item.zoneOpacity = 1;
			item.style.filter = "alpha(opacity=100, enabled=false)";
		}
		else
		{
			item.style.opacity = item.zoneOpacity;
			item.style.MozOpacity = item.zoneOpacity;
			item.style.filter = "alpha(opacity=" + (item.zoneOpacity * 100) + ", enabled=true)";
		}
	}	
	
	function rotateFade() 
	{
		if (!playing && zone[next].zoneOpacity == 0)
		{
			return;
		}
		
		zone[current].zoneOpacity -=.05; 
		zone[next].zoneOpacity += .05;
		zone[next].style.display = 'block';
		setOpacity(zone[current]);
		setOpacity(zone[next]);
		
		if(zone[current].zoneOpacity <= 0) 
		{
			zone[current].zoneOpacity = 0;
			zone[current].style.display = 'none';
			zone[current].style.position ='absolute';
			zone[next].style.position = '';
			SetButtonClass(next, 'rotatorindexcurrentlink');
			SetButtonClass(current, 'rotatorindexlink');
			current = next;
			next = zone[current + 1] ? current + 1 : 0;
			
			if (playing)
			{
				setTimer(rotateFade, container.timeout);
			}
		} 
		else 
		{
			setTimer(rotateFade, container.transition);
		}
	}
	
	function SetPlayButtonClass(className)
	{
		if (playbutton)
		{
			playbutton.className = className;
		}
	}
	
	function SetButtonClass(index, className)
	{
		if (buttons)
		{
			buttons[index].className = className;
		}
	}

	function hideRotator()
	{
		laststate = playing;
		playing = !playing;
		
		if (zone[next].zoneOpacity > 0)
		{
			zone[current].zoneOpacity = 0;
			zone[next].zoneOpacity = 1;
			setOpacity(zone[current]);
			setOpacity(zone[next]);
			zone[current].style.display = 'none';
			SetButtonClass(next, 'rotatorindexcurrentlink');
			SetButtonClass(current, 'rotatorindexlink');
			current = next;
			next = zone[current + 1] ? current + 1 : 0;
		}
		else
		{
			zone[current].zoneOpacity = 1;
			zone[next].zoneOpacity = 0;
			setOpacity(zone[current]);
			setOpacity(zone[next]);
		}
		
		SetPlayButtonClass('rotatorplay');
		
		container.style.display = 'none';
	}
	
	function hideVideo()
	{
		if(container.hasflash)
		{
			container.videoouterdiv.style.display = 'none';
			container.videoinnerdiv.innerHTML = '';
			container.link.innerHTML = replayText;
			container.link.onclick = showVideo;
		}
	}
	
	function showVideo()
	{
		if(container.hasflash)
		{
			hideRotator();
			container.videoouterdiv.style.display = '';
			container.link.innerHTML = skipText;
			container.link.onclick = showRotator;
			var so = new SWFObject('/_layouts/swf/Multimedia/player.swf', container.video,'100%','100%','9.0.115.0');
			so.addParam('allowscriptaccess','always');
			so.addParam('allowfullscreen','false');
			so.addParam('wmode', 'transparent');
			so.addParam('flashvars', 'file=' + container.videourl + '&autostart=true&displayclick=none&controlbar=none&rotatorcontainer=' + container.id)
			so.write(container.videoinnerdiv.id);



		}
		
		return false;
	}
	
	function showRotator()
	{
		hideVideo();
		container.style.display = '';
		playing = laststate;
		
		if (playing)
		{
			setTimer(rotateFade, container.timeout);
			SetPlayButtonClass('rotatorpause');
		}
		
		return false;
	}
	
	function hasCookie(name)
	{
		if (document.cookie.length > 0)
		{
			return document.cookie.indexOf(name) > -1;
		}
		
		return false;
	}

	function loadContainer(containerId, tagname, classname, timeout, transition, controllerId, videoId, videoInnerDivId, videoOuterDivId, videoUrl, linkId)
	{
		container = document.getElementById(containerId);
		container.Show = function(){showRotator();};
		
		if (document.getElementById(controllerId))
		{
			container.controller = document.getElementById(controllerId);
			container.controller.style.display = '';
		}
		
		container.tagname = tagname;
		container.classname = classname;
		container.timeout = timeout ? timeout : 3000;
		container.transition = transition ? transition : 50;
		
		if (videoId)
		{
			container.video = document.getElementById(videoId);
			container.videoinnerdiv = document.getElementById(videoInnerDivId);
			container.videoouterdiv = document.getElementById(videoOuterDivId);
			container.videourl = videoUrl;
			container.link = document.getElementById(linkId);
			var so = new SWFObject('/_layouts/swf/Multimedia/player.swf', videoId,'0','0','9.0.115.0');
			so.addParam('allowscriptaccess','always');
			so.addParam('allowfullscreen','false');
			
			container.hasflash = so.write(container.videoinnerdiv.id);
		}
		
		return container;
	}
	
	
	function loadControllerButtons()
	{
		if (container.controller)
		{
			controllerButtons = container.controller.getElementsByTagName('a');
			contentIndex = 0;
			buttons = new Array();
			
			for (index = controllerButtons.length - 1; index > -1; index--)
			{
				if (controllerButtons[index].className == 'rotatorpause')
				{
					playbutton = controllerButtons[index];
					playbutton.onclick = function() { pauseplay(); return false; }; 
				}
				
				if (controllerButtons[index].className == 'rotatorindexcurrentlink' || controllerButtons[index].className == 'rotatorindexlink')
				{
					controllerButtons[index].contentIndex = contentIndex;
					controllerButtons[index].onclick = function(){showimage(this);return false;};
					buttons.push(controllerButtons[index]);
					contentIndex++;
				}
			}
		}
	}

	function loadZones()
	{
		zones = container.getElementsByTagName(container.tagname);
		
		for(index = 0; index < zones.length; index++)
		{	
			if (zones[index].className == container.classname)
			{
				zone.length > 0 ? zones[index].zoneOpacity = 0 : zones[index].zoneOpacity = 1;
				zone.push(zones[index]);
			}
		}
	}
	
	function startRotator(cookiename)
	{
		if (cookiename && !hasCookie(cookiename) && container.hasflash)
		{
			showVideo();
			var date = new Date();
			date.setDate(date.getDate() + 365);
			document.cookie = cookiename + '=true;expires=' + date.toGMTString() + '; path=/';
		}
		else
		{
			showRotator();
		}
	}
	

	
	this.Init = function(containerId, tagname, classname, timeout, transition, controllerId, videoId, videoInnerDivId, videoOuterDivId, videoUrl, cookiename, linkId, replay, skip)
	{
	    replayText = replay;
	    skipText = skip;
		container = loadContainer(containerId, tagname, classname, timeout, transition, controllerId, videoId, videoInnerDivId, videoOuterDivId, videoUrl, linkId);
		loadControllerButtons();
		loadZones();
		startRotator(cookiename);
	}
}

function playerReady(thePlayer) 
{
	player = document.getElementById(thePlayer.id);
	
	if (player.getConfig().rotatorcontainer)
	{
	    player.sendEvent("STOP", "true");
	    player.sendEvent("PLAY", "true");
	}
	
	addListeners(player);
}

function stateListener(obj) 
{ 
	if (obj.newstate == "COMPLETED")
	{
		player = document.getElementById(obj.id);
		
		if (player.getConfig().rotatorcontainer)
		{
			document.getElementById(player.getConfig().rotatorcontainer).Show();
		}
	}
}

function addListeners(player) 
{
	player.addModelListener("STATE", "stateListener");
}

