/*

*/
var client_now = new Date().getTime();
var end = server_end - server_now + client_now; // this is the real end time

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour *24
var timer;


//mouseover mouseout            mouseenter mouseleave   touchstart
var myEnter = "mouseenter ";
var myLeave = "mouseleave";



/* Android's horrificly bad browser does not support window.Touch so we'll resort to UA sniffing */
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
	
if (window.Touch || isAndroid) {
	myEnter = " click ";
	myLeave = " mouseleave";
}


	
var dimensionMaxSize = 392;

var html5_audiotypes={"mp3": "audio/mpeg", "mp4": "audio/mp4", "ogg": "audio/ogg", "wav": "audio/wav"}
var playSound = true;


function pad(number, length) {

    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}


function showRemaining()
{
    var now = new Date();
    var distance = end - now;
    
    var days;
    var hours;
    var minutes;
    var seconds;
    
    if (distance < 0 ) {
		window.clearInterval( timer );
		days = 0;
		hours = 0;
		minutes = 0;
		seconds = 0;
    } else {
		days = Math.floor(distance / _day);
		hours = Math.floor( (distance % _day ) / _hour );
		minutes = Math.floor( (distance % _hour) / _minute );
		seconds = Math.floor( (distance % _minute) / _second );
	}

	$("#countdown").text(pad(days,2) +":"+pad(hours,2) +":"+pad(minutes,2) +":"+pad(seconds,2));
}


// setup the timer for the counter
timer = window.setInterval(showRemaining, 1000);





$('document').ready(function() {



	var $targetsBTN=$('.buttonDIV');


	$('#soundonoff').click(function() {
		if (playSound) {
			playSound = false;
			$("#soundonoff").text("sound on");
		} else {
			playSound = true;
			$("#soundonoff").text("sound off");
		}
	});







		// do the big timer buttons for the countdown		

		// do the big timer buttons for the countdown		

		$("#buttonDIV0").bind(myEnter, function(){

			if ($(this).hasClass("smallSize")) {
 		 			$("#countdown").stop(true);
 		 			$("#countdown").css('z-index', 3);
 		 			$("#countdown").animate({
						height: 60,
						width: dimensionMaxSize,
						paddingTop: 205,
						fontSize: 42 
 		 			}, 'fast');
			} else {
			
 		 			$("#countdown").stop(true);
 		 			$("#countdown").css('z-index', 2);
					$("#countdown").animate({
						height: 30,
						width: 222,
						paddingTop: 112,
						fontSize: 23
					}, 'fast');
			}

		}
		);
		
		
		
		
		$("#buttonDIV0").bind(myLeave, function(){

			if ($(this).hasClass("bigSize")) {

 		 			$("#countdown").stop(true);
 		 			$("#countdown").css('z-index', 2);
					$("#countdown").animate({
						height: 30,
						width: 222,
						paddingTop: 112,
						fontSize: 23
					}, 'fast');
			}

		}
		);
		
		
		
		
		
		/*
		// do the big timer buttons for the countdown		
        $("#buttonDIV0").hover(
                function() {

 		 			$("#countdown").stop(true);
 		 			$("#countdown").css('z-index', 3);
 		 			$("#countdown").animate({
						height: 60,
						width: dimensionMaxSize,
						paddingTop: 205,
						fontSize: 42 
 		 			}, 'fast');
                }
        ,
                function(event) {

 		 			$("#countdown").stop(true);
 		 			$("#countdown").css('z-index', 2);
					$("#countdown").animate({
						height: 30,
						width: 222,
						paddingTop: 112,
						fontSize: 23
					}, 'fast');
                }
        );
        
        */
        
        
        




	$targetsBTN.each(function(){

		var $this=$(this);
		
		var wNow = $this.width();
		
		var lNow = $this.position().left;
		var lBig = lNow-((dimensionMaxSize/2)-(wNow/2));
		var tNow = $this.position().top;
		var tBig = tNow-((dimensionMaxSize/2)-(wNow/2));

		if (tNow < 0 ) {
			tBig += 25; 
		}

		doRolloverSound($this);


		$this.bind(myEnter, function(){


			if ($this.hasClass("smallSize")) {

				zoomIn($this,wNow,lNow,lBig,tBig,lNow,tNow);
			} else {
			
				zoomOut($this,wNow,lNow,lBig,tBig,lNow,tNow);
			
			}


		}		
			
		);

		
		$this.bind(myLeave, function(){

				zoomOut($this,wNow,lNow,lBig,tBig,lNow,tNow);

		}		
			
		);


	});
});


function zoomIn($this,wNow,lNow,lBig,tBig,lNow,tNow) {
//

 		 			$this.stop(true);
 		 			$this.css('z-index', 1);
 		 			$this.animate({
						height: dimensionMaxSize,
						width: dimensionMaxSize,
						left: lBig,
						top:  tBig
 		 			}, 'fast');
 		 		$this.removeClass("smallSize").addClass("bigSize");

}

function zoomOut($this,wNow,lNow,lBig,tBig,lNow,tNow) {

 		 			$this.stop(true);
 		 			$this.css('z-index', 0);
					$this.animate({
						height: wNow,
						width: wNow,
						left: lNow,
						top:  tNow
					}, 'fast');
				$this.removeClass("bigSize").addClass("smallSize");

}



// attach the sound-play to the rollover
function doRolloverSound($this){
		
			var files = $this.data('soundover').split(',')
			var wrapper=document.createElement('audio')
			
  			if (wrapper.canPlayType){
  				for (var x=0; x<files.length; x++){
  					var sourcetag=document.createElement('source')
  					sourcetag.setAttribute('src', files[x].trim())
  					if (files[x].match(/\.(\w+)$/i))
  						sourcetag.setAttribute('type', html5_audiotypes[RegExp.$1])
  					wrapper.appendChild(sourcetag)
  				}
				$this.bind(myEnter, function(){
					if (playSound && $this.hasClass("smallSize")) {

      					try{
      						//wrapper.load();
      						wrapper.pause();
      						wrapper.currentTime=0;
      						wrapper.play();
      					}catch(e){
      						wrapper.load();
      						wrapper.play();
      					}
      					
      				}
				})			
  			}	
}



