//Click Big Button Function

//variable for preventing too many clicks
var prevent_click = false;

redButton = function() {

	//counter vars function
	function counter_vars_init(v){
		val_num = v;
		oldCount = val_num;	
		oldCountStr = oldCount.toString();
		oldCountArr = oldCountStr.split("");
		oldCountRev = oldCountArr.reverse();
		oldCountLength = (oldCountRev.length)-1;
		newCount = val_num+1;	
		newCountStr = newCount.toString();
		newCountArr = newCountStr.split("");
		newCountRev = newCountArr.reverse();
		newCountLength = (newCountRev.length)-1;
		if( val_num > 999){
			$('#comma1 img').css("display","block");
		}
		if( val_num > 999999){
			$('#comma2 img').css("display","block");	
		}	
	}
	
	//intitialize count
	counter_vars_init(parseInt(val));

	//add to count (when red button is clicked)
	add_to_count = function(){
			
		if(prevent_click==false){
			
			//loop through counter digits and increase by one
			var i=0;		
			for (i=0;i<=window.newCountLength;i=i+1)
			{
				var divID = '#digit'+i;
				var oldnum = window.oldCountRev[i];
				var num = window.newCountRev[i];  
				var newPosNum = (0-(num*90)-90);
				var newPos = newPosNum+"px";
				if(num==0&&oldnum!=num){$(divID).find('img').css("top","0px");}
				$(divID).find('img').animate({"top":newPos},650, function(){
					//no callback
					});					
			}
			//add commas
			if( (window.val_num + 1) > 999){
				$('#comma1 img').css("display","block");
			}
			if( (window.val_num + 1) > 999999){
				$('#comma2 img').css("display","block");	
			}	
		
			// send post request to counter.php script
			$.post( pathToCounter , {
				count: newCount
				}
				//function(data){alert("Data Loaded: " + data);}
				);
		
			//open name generator			
			$('#nameGen').slideDown(1000);
			$('html, body').animate({scrollTop:300}, 500);	
			$('.sphdzNum').html(newCount);
			

			//set cookie var to false
			sphdzCookie = false;
	
			//set cookie to 'true' for 24 hours		
			var cookieOptions = {
			  hoursToLive: 24
			}			
			$.cookies.set('sphdz','true',cookieOptions);
			$.cookies.set('sphdz_number',newCount,cookieOptions);

			//update oldCount
			counter_vars_init(newCount);
			
			//prevent another click
			prevent_click = true;
			
		}else{
		
			broken_counter();
		
		}
			

	}//end add_to_count

	
	//red button play sound and down/up animate
	$('#redButton a').mousedown(function(){
		window.click.play();		
		$(this).find("img").attr("src","images/btn-red_on.gif");
		});
	$('#redButton a').mouseup(function(){
		$(this).find("img").attr("src","images/btn-red_off.gif");				
				
		});


	// Broken counter function
	function broken_counter(){
	
		window.powerdown.play();

		//loop through counter digits and generate random number
		var i=0;		
		for (i=0;i<=8;i=i+1)
		{
			var randomInt = Math.round(10*(Math.random()))
				if(randomInt==10){ randomInt = 9 }
			var divID = '#digit'+i;
			var num = randomInt; 
			var newPosNum = (0-(num*23)-90);
			var newPos = newPosNum+"px";
											
			if(num==0){$(divID).find('img').css("top","0px");}
			$(divID).find('img').animate({"top":newPos}, {queue:true},1000, function(){
			});
		}
		//add commas
		$('#comma1 img').css("display","block");
		$('#comma2 img').css("display","block");	
		
		//change click variable
		clickTwo = 1;	

	}

	
	//Big alert function
	function big_alert(){
	
		window.horn.play();

		var alertMsgs = new Array(
			"ALERT:<br/><br/>You can only click SPHDZ once a day.<br/><br/>",
			"ALERT:<br/><br/>Please go find more SPHDZ.<br/><br/>",
			"ALERT:<br/><br/>Be SPHDZ.  Save the World.<br/><br/>"
		)
		
		var randNum=Math.floor(Math.random()*3)
		
		var topOffset = $(window).scrollTop() + 50;
		$('#alertWin').css('top',topOffset);	
		$('.screen').fadeIn('fast');
		$('#alertWin h1').html(alertMsgs[randNum]);
		$('#alertWin').fadeIn('fast');
		
		
	
		$('#closeAlert').click(function(){
			$('.screen').fadeOut('fast');
			$('#alertWin').fadeOut('fast');
			
		});
	
	}



}

