/**
 * @author fred
 */

$(document).ready(function() {
	
	var toolbarCss 		= $("ul[class=toolbar]").attr("style");

	// full screen mode
    $("#shadow").css("height", $(document).height()).hide();
	$(".lightSwitcher").click(function(){

		if (false === enableFull) {
			openDialog("Register to the site to use this feature! It's free! <a href='/index.php?action=register'>Click to register</a>");
			return false;
		}	
				
		if (false === gameLoaded) {
			openDialog("The game is not loaded yet. About "+timer.toString()+" seconds left !");
			return false;
		}

		$("#shadow").toggle();
        if ($("#shadow").is(":hidden")) { 
        	$("#dgameplay").attr("style","background:#000000;");
			
			$("ul[class=toolbar]").removeAttr("style");			
			$("ul[class=toolbar]").attr("style",toolbarCss);
			
        	$(this).html("Full Screen").removeClass("turnedOff");
        	$("[id^=dgameplay]").css({ width: gameNorWidth+'px', height : gameNorHeight+'px'});
			$("[id^=dgameplayswf]").attr({ width: gameNorWidth+'px', height : gameNorHeight+'px'});
			
        }
        else {
        	
			$("[id^=dgameplay]").attr("style","background:#000000;");
			$("[id^=dgameplay]").css({ width: gameFullWidth+'px', height : gameFullHeight+'px'});
			$("[id^=dgameplayswf]").attr({ width: gameFullWidth+'px', height : gameFullHeight+'px'});
			
			$("#dgameplay").centerGame();			
			gameOffset =  $("#dgameplay").offset();			
			var diff = $("#dgameplay").width() - $("ul[class=toolbar]").width();
			$("ul[class=toolbar]").css("top",gameOffset.top-30);
			$("ul[class=toolbar]").css("left",gameOffset.left+diff);
			$("ul[class=toolbar]").css("position","absolute");
			$("ul[class=toolbar]").css("z-index","101");
			
        	$(this).html("Reset").addClass("turnedOff");	        	
        }
        return false;
    });	
	
});

function timerCount() {
	timer = timer - 1;
	if (timer < 0) {
		clearInterval(timeOutId);
		preadmod();
	}
	else {
		$("#adtime").html(timer.toString());
	}
}

function preadmod() {
	var x=document.getElementById('ad').style;
	var y=document.getElementById('dgame').style;
	if(x.display=='block') {

		gameLoaded = 1;
		
		x.display='none'; 
		y.visibility='visible';
		$("#dgame").css({width:"auto", height:"auto"});
		
		$('#dgameplay').css({ width: gameNorWidth+'px', height : gameNorHeight+'px'});
		$("#adtop").css({'margin-bottom':'16px','width':'100%','height':'90px'});
		$("#adbottom").css({'margin-top':'16px','width':'100%','height':'60px'});
		$("#dgameplayswf").css({ width: gameNorWidth+'px', height : gameNorHeight+'px'});
		
		if (!FlashDetect.installed) {			
			$('#dgameplay').html("<h4><a href='http://get.adobe.com/shockwave/' target='_blank'>Download The ShockWave Player</a> to play games!</h4> AND <h4><a href='http://www.macromedia.com/go/getflashplayer' target='_blank'>Download the Flash Player</a> to play games!</h4>");
		}
		if (shockGame == 1) {
			$("#dgameplayiframe").removeAttr("style");
		}
	}
	else { 
		x.display='block'; y.visibility='none'; 
	}
}

function loadSWFgame(gamefile) {
	var s1 = new SWFObject(gamefile,'dgameplayswf',gameNorWidth, gameNorHeight,'8');						
	s1.addParam('allowscriptaccess','always');
	s1.addParam('allowfullscreen','true');
	s1.write('dgameplay');		
	$("#dgameplayswf").attr("style","width:1px;height:1px;overflow:hidden;");
}

/*** Jquery extensions ***/
jQuery.fn.centerGame = function () {
	var position = $("ul[class=toolbar]").offset();
	
	this.css("position","absolute");
    this.css("top",  position.top + 30  + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

	
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
