/**
 * @author fred
 */


var fullScreenState = false;
var tempGameFullWidth;
var tempGameFullHeight;
var gameNorWidth;
var gameNorHeight;
var tempGameNorWidth;
var tempGameNorHeight;
var gameFullWidth;
var gameFullHeight;
var toolbarCss;
var widget_comment_on = false;

$(document).ready(function() {
	
	toolbarCss 		= $("ul[class=toolbar]").attr("style");

	// full screen mode
    $("#shadow").css("height", $(document).height()).hide();
	$(".lightSwitcher").click(function(){
		if ($("#shadow").is(":hidden")) { 
			fullScreen(true);
		}
		else {
			fullScreen(false);
		}
    });	
	
	$("[class~=comment_write]").click(function() {
		// reset full screen mode
		if (fullScreenState === true)
			fullScreen(false);
			
		$("#comments-bubble-add").show();
		
		
    });	
});

function fullScreen(full)
{
	if (false === gameLoaded) {
		openDialog("The game is not loaded yet. About "+timer.toString()+" seconds left !");
		return false;
	}

	$("#shadow").toggle();
	
	// hide fullscreen
    if (full === false) {

    	//
    	fullScreenState = false;
    	
    	// show ads
    	$("div[id^=ad]").css("display","block");
    	if ($(".slider").length > 0 ) 
    		$(".slider").show();
    	
    	$("#dgameplay").attr("style","background:#000000;");
		
		$("ul[class=toolbar]").removeAttr("style");			
		$("ul[class=toolbar]").attr("style",toolbarCss);
		
		tempGameNorWidth 	= gameNorWidth * 2;
		tempGameNorHeight 	= gameNorHeight * 2;
		
    	$(this).removeClass("turnedOff");
    	$("[id^=dgameplay]").css({ width: gameNorWidth+'px', height : gameNorHeight+'px'});
		$("[id^=dgameplayswf]").attr({ width: gameNorWidth+'px', height : gameNorHeight+'px'});
	
		$("[class^=toolbar_txt]").removeAttr("style");
    }
    // show fullscreen
    else {

    	//
    	fullScreenState = true;
    	
    	// hide every ads
    	$("div[id^=ad]").css("display","none");
    	if ($(".slider").length > 0 )
    		$(".slider").hide();
    	
    	//tempGameFullWidth 	= parseInt(gameFullWidth * 1.2);
		//tempGameFullHeight	= parseInt(gameFullHeight * 1.2);
    	tempGameFullWidth 	= gameFullWidth;
    	tempGameFullHeight	= gameFullHeight;
    	
    	// show our game in the middle
		$("[id^=dgameplay]").attr("style","background:#000000;");
		$("[id^=dgameplay]").css({ width: tempGameFullWidth+'px', height : tempGameFullHeight+'px'});
		$("[id^=dgameplayswf]").attr({ width: tempGameFullWidth+'px', height : tempGameFullHeight+'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).addClass("turnedOff");
    	//$("[class=toolbar_txt_fullscreen]").html("reset screen");
    	$("[class=toolbar_txt_fullscreen]").css("color","#ffffff");
    	$("[class=toolbar_txt_favorite]").css("color","#ffffff");
    	$("[class=toolbar_txt_comment]").css("color","#ffffff");
    }
	
    return false;
  
}

function timerCount() {
	timer = timer - 1;
	if (timer < 0) {
		clearInterval(timeOutId);
		preadmod();
	}
	else {
		if (timer == preloadGame)
			preloadGameBack();
		$("#adtime").html(timer.toString());
	}
}

function timerCountSkip() {
	timerAdsCount = timerAdsCount + 1;
	if (timerAdsCount == skipAdsCount) {		
		$("#adskipcounter").show();
		clearInterval(skipAdTimeOutId);
	}
}

function preadmod() {
	
	$('#dgameplay').css({ width: gameNorWidth+'px', height : gameNorHeight+'px'});
	$("#adtop").css({'margin-bottom':'16px','width':'100%','height':'25px'});
	$("#adbottom").css({'margin-top':'16px','width':'100%','height':'60px'});
	$("#dgameplayswf").css({ width: gameNorWidth+'px', height : gameNorHeight+'px', 'display' : 'block'});
	var x=document.getElementById('preloader_ad').style;
	var y=document.getElementById('dgame').style;
	if(x.display=='block') {
		
		gameLoaded = 1;
		
		x.display='none'; 
		//y.visibility='visible';
		//$("#dgame").removeAttr("style");
		$('#dgameplay').css({ width: gameNorWidth+'px', height : gameNorHeight+'px',display:'block'});
		$("#adtop").css({'margin-bottom':'12px','width':'100%','height':'24px','display':'block'});
		$("#adbottom").css({'margin-top':'16px','width':'100%','height':'60px','display':'block'});
		//$("#dgameplayswf").removeAttr("style");
		$("#dgame").css({ width: gameNorWidth+'px', height : parseInt($("#adtop").height()+$("#adbottom").height()+gameNorHeight+32)+'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'; 
	}
	
	// close any dialogs opened
	$("#dialog").dialog("close");
}

function loadSWFgame(gamefile) {
	var s1 = new SWFObject(gamefile,'dgameplayswf',gameNorWidth, gameNorHeight,'8');						
	s1.addParam('allowscriptaccess','always');
	s1.addParam('allowfullscreen','true');
	s1.addParam('wmode','window');
	s1.write('dgameplay');	
	$("#dgameplayswf").attr("style","display:none;");	
}

function preloadGameBack() {
	//console.log('load background');
	$("#dgameplay").attr("style","width:1px;height:1px;overflow:hidden;");
	$("#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",  $(window).scrollTop()+ 40 + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

