var W=null;
H=null;
H_side=null;
W_side=null;
 logopos=0;
 beginbutton=0;
 opc=1;
voting=null;
blinking=1;
winner=null;
procent1=new Array
procent2=new Array
procent1[1]=1;
procent1[2]=2;
procent1[3]=3;
procent2[1]=1;
procent2[2]=3;
procent2[3]=9;
rotator=0;
noprint1=new Array
noprint2=new Array
pr1=new Array
pr2=new Array
procent_to1=20;
procent_to2=30;


$(window).ready(function(){
	initial()
	
		$('.submt').click(function(){
		var emll=$('#inp').val();
		if(emll!=''){
			$('#inp').val('Thank You '+emll);
setTimeout("$('#inp').val('')",2000);
			sendEmail(emll);
		}
		
		
		})
	
	
	
	
	
	$('#btl').click(function(){
		
		$('#pic1,.battle_begin').fadeOut('slow',function(){
			
			player.playVideo();
			
			
			});
		
		
		})
		$('.vote1').click(function(){
			blinking=0;
			winner='tv1'
				calculate();
			procent_to1=parseInt(procent_to1);
			//alert(procent_to1);
			//procent_to1=addnull(procent_to1);
			//procent_to2=addnull(procent_to2);
			 procent(procent_to1,procent_to2)
		$('.vote1,.vote2').css('display','none');
		
			
			
			
		
		
		})
		$('.vote2').click(function(){
			blinking=0;
			winner='tv2'
				calculate();
			procent_to2=parseInt(procent_to2);
			//alert(procent_to1);
			//procent_to1=addnull(procent_to1);
			//procent_to2=addnull(procent_to2);
			 procent(procent_to1,procent_to2)
			 $('.vote1,.vote2').css('display','none');
		
		
		})
	
	
	
	});
	
	$(window).resize(function(){
		initial()
	
	});
	function logo(){
		if(logopos==0){
			$('#logo_holder').html('<img src="img/logo_on.png" />');
			logopos=1;
			
			} else {
				$('#logo_holder').html('<img src="img/logo_off.png" />');
				logopos=0;
				}
		setTimeout('logo()',1000);
	}
	function initial(){
		
		
		H=$(window).height(); 
		W=$(window).width();
		W_side=parseInt((W-1024)/2);
		if(W_side<0){
			W_side=0;
			
			}
		H_side=parseInt((H-767)/2);

		if(H_side<0){
			H_side=0;
			
			}
			$('#top_side').css('height',H_side+'px');
			$('#bottom_side').css('height',H_side+'px');
			$('#left_side').css('width',W_side+'px');
			$('#right_side').css('width',W_side+'px');
				$('#holder').css('height',H+'px');
				$('#holder').css('width',W+'px');
			

			
			
	}

	function begindisplay(){
		$('.battle_begin').fadeIn('slow',function(){
			blinkf('.battle_begin')
		})
	}
	function beginvote(){
		
		$('.vote1,.vote2').fadeIn('slow',function(){
			blinkf('.vote1,.vote2')
		})
	}
	
function blinkf(sel){
	if(blinking==1){
	if(beginbutton==0){
		opc=.5
		beginbutton=1
	} else {
		
		opc=1
		beginbutton=0
	}
	
		  $(sel).animate({
		    opacity: opc
		   
		  }, 300, function() {
		    setTimeout('blinkf("'+sel+'")',1)
		  });
		
}
}
function procent(id1,id2){
	
	$('#fvote1').html('000');
	$('#fvote2').html('000');
	
	id1=id1.toString();
	pr1[1]=id1.substr(2,1);
	pr1[3]=id1.substr(1,1);
	pr1[2]=id1.substr(0,1);
	
	if(pr1[1]==''){
		pr1[1]='0';	
	}
	if(pr1[2]==''){
		pr1[2]='0';	
	}
	if(pr1[3]==''){
		pr1[3]='0';	
	}
	if(id1=='100'){
		pr1[1]='1';
		pr1[2]='0';
		pr1[3]='0';
	}
	//val2
	id2=id2.toString();
	pr2[1]=id2.substr(2,1);
	pr2[3]=id2.substr(1,1);
	pr2[2]=id2.substr(0,1);
	
	if(pr2[1]==''){
		pr2[1]='0';	
	}
	if(pr2[2]==''){
		pr2[2]='0';	
	}
	if(pr2[3]==''){
		pr2[3]='0';	
	}
	
	if(id2=='100'){
		pr2[1]='1';
		pr2[2]='0';
		pr2[3]='0';
	}
	
	setTimeout('drawProcent('+id1+','+id2+')',10);
}
function drawProcent(id1,id2){
	
	if(noprint1[1]!=1){
		procent1[1]++;
		if(procent1[1]==9){
			
			procent1[1]=0;
		}
		}
	if(noprint1[2]!=1){
	procent1[2]++;
	if(procent1[2]==9){
		
		procent1[2]=0;
	}
	}
	if(noprint1[3]!=1){
	procent1[3]++;
	if(procent1[3]==9){
		
		procent1[3]=0;
	}
	}
	procent1[1]=procent1[1].toString()
		procent1[2]=procent1[2].toString()
			procent1[3]=procent1[3].toString()
			if(rotator==90){
				noprint1[1]=1;
				procent1[1]=pr1[1]
				}
	if(rotator==180){
		noprint1[2]=1;
		procent1[2]=pr1[2]
		}
	if(rotator==220){
		noprint1[3]=1;
		procent1[3]=pr1[3]
		}
	
	//value2
	if(noprint2[1]!=1){
		procent2[1]++;
		if(procent2[1]==9){
			
			procent2[1]=0;
		}
		}
	if(noprint1[2]!=1){
	procent2[2]++;
	if(procent2[2]==9){
		
		procent2[2]=0;
	}
	}
	if(noprint2[3]!=1){
	procent2[3]++;
	if(procent2[3]==9){
		
		procent2[3]=0;
	}
	}
	procent2[1]=procent2[1].toString()
		procent2[2]=procent2[2].toString()
			procent2[3]=procent2[3].toString()
			if(rotator==90){
				noprint2[1]=1;
				procent2[1]=pr2[1]
				}
	if(rotator==180){
		noprint2[2]=1;
		procent2[2]=pr2[2]
		}
	if(rotator==220){
		noprint2[3]=1;
		procent2[3]=pr2[3]
		}
	procent2[1]=procent2[1].substr(0,1);
	procent2[2]=procent2[2].substr(0,1);
	procent2[3]=procent2[3].substr(0,1);
	
	
	
	
	rotator++;
	$('#fvote1').html(procent1[1]+procent1[2]+procent1[3]);
	
	$('#fvote2').html(procent2[1]+procent2[2]+procent2[3]);
	setTimeout('drawProcent(0,0)',30)
	if(rotator==220){
		setTimeout('dowinner("'+winner+'")',2000);	
	}
}
function dowinner(win){
	if(win=='tv1'){
	looserGoHome('2')
	
	} else {
	looserGoHome('1') 
	
	}



	}
	function looserGoHome(id){
		if(id!=1){
			$('#fireworks').css('left','100px');	
			$('#fireworks1').css('left','150px');	
			
		} else {
			
			$('#fireworks').css('left','650px');	
			$('#fireworks1').css('left','700px');	
			
		}
		$(document).ready(function(){

			$("#jquery_jplayer_2").jPlayer({
				ready: function () {
					$(this).jPlayer("setMedia", {
						mp3: "sounds/glass.mp3",
						
						
					}).jPlayer("play");
				},
				ended: function (event) {
					//$("#jquery_jplayer_2").jPlayer("play", 0);
				},
				swfPath: "js",
				supplied: "mp3"
			})
			.bind($.jPlayer.event.play, function() {

				$('#player1,#player2').html(''); 
				$('#broken_holder'+id).html("<img src='img/broken.png' id='broken' />");
				 $('#pic1,#pic2').css('top','30px');
				 $('#fvote1,#fvote2').css('top','40px');
				 fireworks();
			 });
		 });
		
		 /*$('#pic'+id).hide("clip", 1000,function(){
				
				
				
			});
		 fireworks();
		 
		 
		
		
		/* player1 = new YT.Player('player2', {
	          height: '215',
	          width: '332',
			 playerVars: { 'autoplay': 0, 'controls': 0,'rel':0,'version':3,'showinfo':0,'wmode':"opaque"  },
	          videoId: 'h_a0WFwS6HM',
	          events: {
	            'onReady': onPlayerReady1,
	            'onStateChange': onPlayerStateChange1
				
	          }
	        });*/
		 //player1.loadVideoById('h_a0WFwS6HM');
		 //player1.pauseVideo()
		
		/*$(document).ready(function(){

			$("#jquery_jplayer_1").jPlayer({
				ready: function () {
					$(this).jPlayer("setMedia", {
						mp3: "sounds/bomb.mp3",
						
						
					}).jPlayer("play");
				},
				ended: function (event) {
					$("#jquery_jplayer_2").jPlayer("play", 0);
				},
				swfPath: "js",
				supplied: "mp3"
			})
			.bind($.jPlayer.event.play, function() {

				$('#'+winner).animate({
					  
				    left: '290',
					top: '50'
				   
				  }, 2000, function() {
				    // Animation complete.
					  fireworks();
				  });
				$('#'+id).hide("explode",{ pieces: 50 }, 1000);
				$('#vs').hide('slow');
				$('#play').hide('slow');
				$('#battle').hide('slow');
				$('#soon').hide('slow');
				//$('#v1').hide('slow');
				//$('#v2').hide('slow');
				$('#vb2').hide('slow'); 
				$('#vb1').hide('slow');
				
				
			});

			
		});
		
		
		
		
		
		
		
		/*$("#jquery_jplayer_1").jPlayer({

			ready: function () {
			
				$(this).jPlayer("setMedia", {

					wav: "http://kachoom.com/nv/sounds/bomb.wav"

				

				}).jPlayer("play");

			},
			playing:function (event) {
				
				$('#'+winner).animate({
					  
				    left: '270',
					top: '-150'
				   
				  }, 2000, function() {
				    // Animation complete.
				  });
				$('#'+id).hide("explode",{ pieces: 50 }, 1000);
				$('#vs').hide('slow');
				$('#play').hide('slow');
				$('#battle').hide('slow');
			},

			ended: function (event) {

				//$(this).jPlayer("play");

			},

			swfPath: "http://kachoom.com/nv/js/Jplayer.swf",

			supplied: "wav"

		}); */



	}
	function fireworks(){
		$(document).ready(function(){
		$("#jquery_jplayer_1").jPlayer({
			ready: function () {
				$(this).jPlayer("setMedia", {
					mp3: "sounds/firework.mp3",
					
					
				}).jPlayer("play");
			},
			ended: function (event) {
				//$("#jquery_jplayer_2").jPlayer("play", 0);
			},
			swfPath: "js",
			supplied: "mp3"
		})
		.bind($.jPlayer.event.play, function() {

			setTimeout('fireball()',3000);	
			
		});
		
		});
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/*$("#jquery_jplayer_1").jPlayer("setMedia", {
			mp3: "sounds/firework.mp3",
			
		}).jPlayer("play")
		
		.bind($.jPlayer.event.play, function() {

			$('#fireworks').show('fast');
			$('#fireworks').animate({
				  
				
				top: '-200'
			   
			  }, 1500, function() {
			    // Animation complete.
				  $('#fireworks').show('fast');
					$('#fireworks').animate({
						  
						
						width: '341',
						height: '374',
						left: '-=120',
						top: '-=135'
					   
					  }, 1000, function() {
					    // Animation complete.
						  $('#fireworks').fadeOut(2000) 
					  });
			  });
			
		})
		.bind($.jPlayer.event.ended, function() {

		
			
		});*/
		
		
		
		
		
		
		
		
		

	}

	
	function fireball(){
		
		$('#fireworks').show('fast');
		$('#fireworks').animate({
			  
			
			top: '-1260'
		   
		  }, 1500, function() {
		    // Animation complete.
			  $('#fireworks').show('fast');
				$('#fireworks').animate({
					  
					
					width: '341',
					height: '374',
					left: '-=120',
					top: '-=135'
				   
				  }, 1000, function() {
				    // Animation complete.
					  $('#fireworks').fadeOut(2000) 
				  });
		  });
		$('#fireworks1').show('fast');
		$('#fireworks1').animate({
			  
			
			top: '-1200'
		   
		  }, 1500, function() {
		    // Animation complete.
			  $('#fireworks1').show('fast');
				$('#fireworks1').animate({
					  
					
					width: '341',
					height: '374',
					left: '-=320',
					top: '-=135'
				   
				  }, 1000, function() {
				    // Animation complete.
					  $('#fireworks1').fadeOut(2000,function(){setTimeout('location.reload(true)',2000)}) 
				  });
		  });
	}
	
	function addnull(digits_c){
		var p1=new Array;
		digits_c=digits_c.toString();
		 p1[1]=digits_c.substr(2,1);
		p1[3]=digits_c.substr(1,1);
		p1[2]=digits_c.substr(0,1);
		
		if(p1[1]==''){
			p1[1]='0';	
		}
		if(p1[2]==''){
			p1[2]='0';	
		}
		if(p1[3]==''){
			p1[3]='0';	
		}
		return (p1[1]+p1[2]+p1[3])
	}
	
	function drawPosters(){
		
		$('#br1').html('<img src="http://i4.ytimg.com/vi/'+video1+'/hqdefault.jpg" id="pic1"/> ')
		$('#br2').html('<img src="http://i3.ytimg.com/vi/'+video2+'/hqdefault.jpg" id="pic2"/> ')
	}
	function drawVideos(){
		
	}
	function drawinitPr(){
		
		
	}
	function resetall(){
		location.reload(true);
	}
	function sendEmail(emll){
		$.ajax({
			  url: 'ajax/save.php',
			 data:'email='+emll
			});
	}
	function calculate() {
		/*video1_win=
			video1_views=
			video1_id=
				procent_to1=
					*/
		if(winner=='tv1'){
			video1_win++;
			video1_views++;
			video2_views++;
			
		} else {
			video2_win++;
			video1_views++;
			video2_views++;
		}
		procent_to1=parseInt((video1_win/video1_views)*100);
		procent_to2=parseInt((video2_win/video2_views)*100);
	
		save_result()
	}
	function save_result(){
		$.ajax({
			  url: 'ajax/save_param.php',
			 data:'id1='+video1_id+'&id2='+video2_id+'&id1_win='+video1_win+'&id2_win='+video2_win+'&id1_view='+video1_views+'&id2_view='+video2_views
			});
	}
