// Home Rotator ---------------
jjj=jQuery.noConflict();
var isPlay=false;
//var players =new Array();
jjj(document).ready(function(){
		var counter=1;
		var timerCounter=false;
		var speed=10000;
		var temp_i=0;
		var titles =new Array();
		var titlesPos =new Array();
		var slidePos =new Array();
		titles=["Content Production","Interactive Content","3D Films","Sales"];
		titlesPos=["290px","367px","465px","540px"];
		slidePos=["0px","-950px","-1900px","-2850px"];
		jjj("#currentTitle").html(titles[0]);
		    jjj("*").keyup(function (e) {  
				var key = e.charCode ? e.charCode : (e.keyCode ? e.keyCode : 0);
				//alert(key)
     			 if (key == 39) {
					 
					if (jjj("#rotatorContentHolder").is(":animated"))
					return false;
		  			counter++;
					if(counter<5){						   
						setDiv(jjj("#c"+counter).get(0));
					}else{
						counter=1;	
						setDiv(jjj("#c"+counter).get(0));
					}
					stopVideos(counter);
					return false;
	 			}else if (key == 37) {
					
					if (jjj("#rotatorContentHolder").is(":animated"))
					return false;
					counter--;
					if(counter>0){						   
						setDiv(jjj("#c"+counter).get(0));
					}else{
						counter=4;	
						setDiv(jjj("#c"+counter).get(0));
					}
					stopVideos(counter);
					return false;
				}else if (key == 32 || key == 13 || key == 80) {
					if(timerCounter){
					timerCounter=false;
					jjj("a[href=#pp]").css("background-position","0px 0px");
					jjj("#pp").attr("title", "Play");
				}else{
					timerCounter=true;
					jjj("a[href=#pp]").css("background-position","0px -14px");
					jjj("#pp").attr("title", "Pause");
				}
				return false;
				}
  			});

		jjj("#c1").click(function(){ setDiv(this); return false;});
		jjj("#c2").click(function(){ setDiv(this); return false;});
		jjj("#c3").click(function(){ setDiv(this); return false;});
		jjj("#c4").click(function(){ setDiv(this); return false;});
		jjj("#pp").ready(function(){ if(!timerCounter){jjj("#pp").attr("title", "Play"); jjj("a[href=#pp]").css("background-position","0px 0px");}});
		jjj("#pp").click(function(){
				if(isPlay){			
				//player.sendEvent('PLAY');				
				isPlay=false;
				}
				if(timerCounter){
					timerCounter=false;
					jjj("a[href=#pp]").css("background-position","0px 0px");
					this.title="Play";
				}else{
					timerCounter=true;
					jjj("a[href=#pp]").css("background-position","0px -14px");
					this.title="Pause";
				}
				return false;
								});
		jjj("a[href=#rightArrow]").click(function(){
												  
			if (jjj("#rotatorContentHolder").is(":animated"))
			return false;									
			counter++;
			if(counter<5){						   
				setDiv(jjj("#c"+counter).get(0));
			}else{
				counter=1;	
				setDiv(jjj("#c"+counter).get(0));
			}
			stopVideos(counter);
			return false;
			});
		jjj("a[href=#leftArrow]").click(function(){
												 
			if (jjj("#rotatorContentHolder").is(":animated"))
			return false;								   
			counter--;
			if(counter>0){						   
				setDiv(jjj("#c"+counter).get(0));
			}else{
				counter=4;	
				setDiv(jjj("#c"+counter).get(0));
			}
			stopVideos(counter);
			return false;
			});
		
		jjj(".rotator").ready(function(){
			intval=window.setInterval(rotate,speed);
			intval2=window.setInterval(checkPlay,100);
			function rotate() {
				if (jjj("#rotatorContentHolder").is(":animated"))
				return false;
				if(timerCounter){
					counter++;
					if(counter<5){						   
						setDiv(jjj("#c"+counter).get(0));
					}else{
						counter=1;	
						setDiv(jjj("#c"+counter).get(0));
					}
					stopVideos(counter);
				}
		}
		function checkPlay(){
			if(isPlay){
			timerCounter=false;
			jjj("a[href=#pp]").css("background-position","0px 0px");
			jjj("#pp").attr("title", "Play");
			}
		}
		});
		function setDiv(id){
			//alert(id.id);
			if (jjj("#rotatorContentHolder").is(":animated"))
			return false;
			for(var i=1;i<5;++i){
				var e=jjj("#c"+i).get(0);
				//alert(id.className);
				if(e==id){
					jjj(id).addClass("selected");
					temp_i=i;
					jjj("#rotatorContentHolder").animate({ "left" : slidePos[i-1]}, 1000,"swing");
					jjj("#currentTitle").animate({ "left" : titlesPos[i-1]}, 1000,"swing",function() {jjj("#currentTitle").html(titles[temp_i-1]);});
					if(isPlay){
						//player.sendEvent('PLAY');
						isPlay=false;
					}
					counter=i;
				}else{
					if(e.className.indexOf("selected")!=0){
						jjj(e).removeClass("selected");
					}
				}
			}
		}
		function stopVideos(id){
			for(var i=0;i<players.length;++i){
				
				var m=players[i];
				//alert(i+"--"+id)
				if(i==(parseInt(id)-1)){
				m.sendEvent('PLAY');	
				}else{
				m.sendEvent('STOP');
				}
			}
		}
		
 });

