/**
 * toggle game size, to max and normal
 * @deprecated 2011-12-15
 */
function toggleGame(){
	toggleVisButton = $("#game_toggle_inactive");
	toggleInvisButton = $("#game_toggle_active");

	if (toggleVisButton.is(':visible')){
		toggleVisButton.hide();
		toggleInvisButton.show();
		showZoomStatus = true;
	}else{
		toggleVisButton.show();
		toggleInvisButton.hide();
		showZoomStatus = false;
	}
	/* get original game Dims */
	gDim = getGameDimensions(true);

	if (showZoomStatus){
		maxDimX = $("#game_dimension_container").width();
		(maxDimX<=0) ? maxDimX=733 : null ;
		relRatio = gDim[1]/gDim[0];
		newDim = [maxDimX,(maxDimX*relRatio)];
	}else{
		newDim = gDim;
	}

	/* set new Sizes */
	setGameDimensions(newDim);

	/* resize gameContainer */
	levelContainerControl("game");
}

function toggleGameZoom(showZoom){
	if (showZoom){
		$("#game_toggle_inactive_container").hide();
		$("#game_toggle_container").fadeIn(2500);
	}else{
		$("#game_toggle_inactive_container").show();
		$("#game_toggle_container").hide();
	}
}

function zoomGame(zoomMode,zoomValue){
	minZoomSize = 250;
	maxZoomSize = 955;
	zoomStep = 50;
	toggleSize = 733;
	setGameDims = true;

	cZV = $('#zoomslider').slider('value');

	switch(zoomMode){
		case "setup":
			$("#zoomslider").slider({ value: zoomValue, animate: true, min: minZoomSize, max: maxZoomSize, step: 10, change: function(event,ui) {zoomGame('set',ui.value); }});
			cZV = zoomValue;
		break;
		case "zoomin":
			cZV = cZV+zoomStep;
			setGameDims = false;
		break;
		case "zoomout":
			cZV = cZV-zoomStep;
			setGameDims = false;
		break;
		default:
			cZV = zoomValue;
		break;
	}

	/* check max sizes */
	if (cZV < minZoomSize){
		cZV = minZoomSize;
	}else if(cZV > maxZoomSize){
		cZV = maxZoomSize;
	}

	/* do NOT change - WARNING: recursiv error */
	if (!setGameDims){
		$('#zoomslider').slider('value',cZV);
		return false;
	}

	/* get original game Dims */
	gDim = getGameDimensions(true);
	relRatio = gDim[1]/gDim[0];
	newDim = [cZV,(cZV*relRatio)];
	/* set new Sizes */
	setGameDimensions(newDim);

	if (cZV > maximumGameSize){
		if (!$("#game_dimension_container").hasClass("fullSize")){
			/* special fade for adv-sqare */
			if (app_isAdvertisingAvailable("app_advertising_square")){
				$("#app_advertising_square").hide();
			}
			
			$("#side_game_container").fadeOut('slow', function() {
				$("#game_dimension_container").addClass("fullSize");
				levelContainerControl("game");
				app_reposAllAdvertisingContainers();
			});
		}else{
			levelContainerControl("game");
			app_reposAllAdvertisingContainers();
		}
	}else if($("#side_game_container").html() != ""){
		if ($("#game_dimension_container").hasClass("fullSize")){
			$("#game_dimension_container").removeClass("fullSize");

			$("#side_game_container").fadeIn('slow', function() {
				levelContainerControl("game");
				app_reposAllAdvertisingContainers();
			});
			
			if (app_isAdvertisingAvailable("app_advertising_square")){
				$("#app_advertising_square").show();
			}
		}else{
			levelContainerControl("game");
			app_reposAllAdvertisingContainers();
		}
	}else{
		levelContainerControl("game");
		app_reposAllAdvertisingContainers();
	}
}

/**
  * zoom game size, to max and normal
  */
function zoomsGame(zoomSize){
	/* get original game Dims */
	gDim = getGameDimensions(true);
	relRatio = gDim[1]/gDim[0];
	newDim = [zoomSize,(zoomSize*relRatio)];
	/* set new Sizes */
	setGameDimensions(newDim);

	maxDimX = $("#game_dimension_container").width();
	if (zoomSize > maxDimX){
		/* hide right */
	}else{
		/* show right */
	}

	/* resize gameContainer */
	levelContainerControl("game");
}

/**
  * get current or original gameDimensions
  */
function getGameDimensions(getOriginal){
	(!getOriginal) ? getOriginal=false : null;

	refreshGameDimensions(false);

	if (getOriginal){
		return originalGameDimensions;
	}else{
		return currentGameDimensions;
	}
}

/**
  * refresh current or original gameDimensions
  */
var currentGameDimensions = new Array();
var originalGameDimensions = new Array();
function refreshGameDimensions(setOriginal){
	(!setOriginal) ? setOriginal=false : null;
	gameObject = $("#flash_game_1");

	tmpData = gameObject.height();
	if (tmpData > 0 && tmpData != undefined){

	}else{
		gameObject = $("#flash_game_0");
	}
	currentGameDimensions[0] = gameObject.width();
	currentGameDimensions[1] = gameObject.height();

	if (setOriginal || originalGameDimensions[0] == undefined || originalGameDimensions[0] == ""){
		originalGameDimensions[0] = gameObject.width();
		originalGameDimensions[1] = gameObject.height();
	}
	return true;
}

/**
  * set current or original gameDimensions to the game
  */
function setGameDimensions(gDim){
	gameObject = $("#flash_game_0");
	gameObject1 = $("#flash_game_1");

	gameObject.width(gDim[0]);
	gameObject.height(gDim[1]);
	gameObject1.width(gDim[0]);
	gameObject1.height(gDim[1]);

	refreshGameDimensions();
	return true;
}

/**
  * show the warning (report) container
  * @deprecated 2011-12-14
  */
function makeWarn(){
	$("#game__share_container").fadeOut(2500);
	$("#game__report_container").fadeIn(2500);
	app_reposAllAdvertisingContainers();
}

/**
  * show the mail (report) container
  * @deprecated 2011-12-14
  */
function makeMail(){
	$("#game__report_container").fadeOut(2500);
	$("#game__share_container").fadeIn(2500);
	app_reposAllAdvertisingContainers();
}

/**
  * send share params
  * @deprecated 2011-12-14
  */
function send_sharing_mail(){
	tData = {};
	tData["name"] = $("#input_share_name").val();
	tData["gameid"] = $("#input_share_gameid").val();
	tData["email"] = $("#input_share_email").val();
	tData["message"] = $("#input_share_message").val();
	$("#game__sharing_outer_div").load("/game/remote_send_sharing_mail",tData);
}

/**
  * send report params
  * @deprecated 2011-12-14
  */
function send_report_mail(){
	tData = {};

	if ($("#check_report_reason_1").is(':checked')){
		tData["reason"] = "not_working";
	}else{
		tData["reason"] = "other_reason";
	}
	tData["gameid"] = $("#input_report_gameid").val();
	tData["message"] = $("#input_report_message").val();
	$("#game__report_outer_div").load("/game/remote_send_report_mail",tData);
}

/*
 * show jackpot info
 */
function makeJackpotInfo(){
	jackpotInfoWindow = window.open("/game/remote_jackpot_info", "secondAppWindow", "width=640,height=515,dependent=yes,scrollbars=no,menubar=no,resizable=no,titlebar=no,status=no,toolbar=no");
}

/*
 * rate the current game
 */
function rateGame(gameId,points){
	$("#gamefooter_rate").hide();
	$("#gamefooter_load").show();
	$("#game_footer_container").load("/game/remote_rate_game/"+gameId+"/"+points);
}

function rateGameReport(gameId,points){
	remoteWindowRequest('/static/remote_rate_game_report',{points: points, game_id: gameId});
	handleHideArea(false);
}
function handleRateGameReport(windowId){
	handleHideArea(true);
	$("#gamefooter_rate").hide();
	$("#gamefooter_load").show();
	submitAjaxUpdateFormData('game_footer_container','/game/remote_rate_game/');
	toggleWindowContainer(windowId,false);
	return false;
}

/*
 * load HS-result for the current game / user
 */
function loadHighscoreResults(gameId){
	$("#highscore_load_results").hide();
	$("#highscore_loading_results").show();
	$("#highscore_results_container").load("/game/remote_highscore_results/"+gameId);
}

/*
 * send request - upg after x seconds
 */
function loadUserPlayedGame(gameId){
	$.ajax({
		url: "/game/remote_user_played_game/"+gameId,
		async: false
	});
}

/**
 * Refreshes all required functions on gamepage
 *
 * @type: JS:jQuery
 */
function updateGamePage(gameId){
	updateFunctionsContainer(gameId);
	updateCommentContainer(gameId);
	updateSideContainer(gameId);
	$(".game__info_box").hide();
}

/**
 * Refresh game function area
 *
 * @type: JS:jQuery
 */
function updateFunctionsContainer(gameId){
	$("#zoomslider").slider("destroy");
	$("#zoomslider").remove();
	$("#game_functions_inner_container").hide();
	$("#game_functions_wait_container").show();
	$('#game_functions_container').load("/game/remote_update_functions/"+gameId);
}

/**
 * Refreshes the comment container on gamePage
 *
 * @type: JS:jQuery
 */
function updateCommentContainer(gameId){
	$("#game_comments_inner_container").hide();
	$("#game_comments_wait_container").show();
	$('#game_comment_content_container').load("/game/remote_update_comments/"+gameId);
}

function updateSideContainer(gameId){
	$("#side_game_inner_container").hide();
	$("#side_game_wait_container").show();
	$('#side_game_container').load("/game/remote_update_side/"+gameId,function() {
		levelContainerControl("game");
	});
}

/**
 * make / unmake favorite to the current game / user
 *
 * @type: JS:jQuery
 */
function toggleFavorite(gameId){
	$("#zoomslider").slider("destroy");
	$("#zoomslider").remove();

	$("#game_functions_inner_container").hide();
	$("#game_functions_wait_container").show();
	$.ajax({
		url: "/game/remote_toggle_favorite/"+gameId,
		async: false
	});
	$('#game_functions_container').load("/game/remote_update_functions/"+gameId);
}

/**
  * first call this function after game-page load
  */
var showGameToggle = false;
function initGameParams(showGameToggleInl){
	(!showGameToggleInl) ? showGameToggleInl=false : null;
	showGameToggle = showGameToggleInl;

	/* set/save game dimensions */
	refreshGameDimensions(true);

	/* check container height for game-backgrounds */
	cCV0 = $("#game_overlay_container");
	cCV1 = $("#game_background_content");
	if (cCV0.length > 0 && cCV1.length > 0){
		if (cCV1.height() > cCV0.height()){
			cCV0.height(cCV1.height());
		}
	}
	checkPregameStateDiff();

	/* check / set gameZoom */
	if (showGameToggle){
		toggleGameZoom(true);
		gDim = getGameDimensions(true);
		afterDOMLoad("zoomGame('setup','"+gDim[0]+"');");
	}
}

/**
  * use this function, to handle portal-differences
  */
function checkPregameStateDiff(){
	afterDOMLoad('levelContainerControl("game")',true);
	afterDOMLoad('levelContainerControl("gamebottom")',true);
}


/**
 * used to 'refresh' or 'load' the real game (flash, iframe...)
 * @type: JS:jQuery
 */
function afterloadGame(linkName){
	toggleWaitContainer(true);

	$("#flash_game_0").remove();
	$("#flash_game_1").remove();
	$("#game_overlay_container").load("/game/remote_afterload/"+linkName);
	updateFunctionsContainer(linkName);
}

/**
 * used, to check the window size for affiliate-sites - resize current window
 * @type: JS:jQuery
 */
function checkAffiliateState(setTimeoutPre,timerDelay){
	if (!timerDelay){
		timerDelay = 0;
	}
	if (setTimeoutPre){
		$("html").css("overflow", "hidden");
		$("body").css("overflow", "hidden");

		window.resizeTo(($("#content_container").width()+10),($("#fullcontent_container").height()+60));
		$(window).animate(
			{
				innerHeight : ($("#fullcontent_container").height()+10),
				innerWidth : ($("#content_container").width()+2)
			},
			timerDelay,
			function(){
				app_reposAdvertisingPregameContainer();
			}
		);
	}else{
		window.setTimeout("checkAffiliateState(true)",120);
	}
}

function toggleLightness(status){
	if(status){
		if (($.browser.msie && $.browser.version > 8) || (!$.browser.msie)){
			$('#game_highlight').fadeOut();
		}else{
			$('#game_highlight').hide();
		}
		$('#lightness_switch_off').hide();
		$('#lightness_switch_on').show();
	}else{
		if (($.browser.msie && $.browser.version > 8) || (!$.browser.msie)){
			$('#game_highlight').fadeIn();
		}else{
			$('#game_highlight').show();
		}
		$('#lightness_switch_on').hide();
		$('#lightness_switch_off').show();
	}
}

function addEmailfield(){
	fieldIdPrefix = "share_email_container";

	$("div[id^="+fieldIdPrefix+"]").each(function(){
		if (!$(this).is(":visible")){
			$(this).show();
			return false;
		}
	});
	return true;
}

function removeEmailfield(fieldPos){
	fieldIdPrefix = "share_email_container";
	$("#"+fieldIdPrefix+"_"+fieldPos).hide();
	$("#share_emails_"+fieldPos).val("");
}

function submitCommentData(targetUrl){
	$("#game_comments_inner_container").hide();
	$("#game_comments_wait_container").show();
	submitButton = eId("game_comments_submit_button");
	submitButton.disabled = true;

	submitAjaxUpdateFormData('game_comment_content_container',targetUrl,'comment_form');
}


