Current File : /data/web/virtuals/215191/virtual/www/domains/gryna2.com.pl/js/game.js
/**
 * @author Jan Řasa, honza.rasa@seznam.cz
 * @copyright 2012
 * @description game functions
**/

var gameLoaded = false;
var hovered = false;
$( document ).ready(function() {
    
    $(".overlayer").click(function(){
    	//$(this).css("display", "none");
    });
     $(".send_game").click(function(){
    	//
    });
    $(".game_image img").mouseover(function(e){
    	$(".absolute").html("");
    	$(".absolute").html("<img class='game_image_popup' src='"+$(this).attr("src")+"' />");
    	$(".absolute").css("display", "");
    	$(".absolute").css("top", (e.pageY+10)+"px");
    	$(".absolute").css("left", (e.pageX+10)+"px");
    });	
    
    $(".game_image img").mousemove(function(e){
    	$(".absolute").css("top", (e.pageY+10)+"px");
    	$(".absolute").css("left", (e.pageX+10)+"px");
    });	
    
    $(".game_image").mouseout(function(e){
    	$(".absolute").css("display", "none");
    });		
    
    
    		
});


// load game
    var load_interval = setInterval(function(){loading_game()}, 100);
    var load_counter = 0;
    function loading_game(){
    	if(load_counter >= 60){
    		clearInterval(load_interval);
    		load_game();
    		return;
    	}
    	width = 12 * load_counter;
    	width = Math.ceil(width);
    	$(".innerloader").css("width", width);
    	load_counter = load_counter + 1;
    }
    
    function load_game(){
    	$(".header").css("display", "none");
    	
    	$(".before_game").html("");
    	$(".before_game").html("<span class=\"asd\"><object id=\"game_object\" style=\"margin-left: 7px;margin-top: 15px; z-index: 100;\" id=\"swf_object\" type=\"application/x-shockwave-flash\" data=\""+$(".game_url").html()+"\" width=\"690\" height=\"426\"><param name=\"wmode\" value=\"opaque\"><param name=\"movie\" value=\""+$(".game_url").html()+"\"></object></span>");
   		$(".asd").mouseover(function() {
	    	hovered = true;
		});
		$(".asd").mouseout(function(){
	    	hovered = false;
		});
    	gameLoaded = true;
    }

similar_loading = false;
var page_similar = 1;
function similar_games_next(categories,url_base){
	
	
	if(similar_loading)
		return false;
	if(max_page_similar <= page_similar)
		page_similar = 1;
	else
		page_similar += 1;
	similar_loading = true;
	hideSimilarGames(categories, url_base);	
}

function similar_games_prev(categories,url_base){
	if( page_similar<=1)
		return false;
		
	if(similar_loading)
		return false;
	page_similar -= 1;
	similar_loading = true;
	hideSimilarGames(categories, url_base);	
}


function hideSimilarGames(categories,url_base){
	$(".similar_game").each(function(){
		$(this).slideToggle(300, function(){
			
			updateSimilar(categories,url_base);
  		})	
	});
}

updating = false;
var similar_games;
var similar_games_ = new Array();
function updateSimilar(categories,url_base){
	similar_games_ = new Array();
	if(updating)
		return false;
	updating = true;
	$.ajax({
      type: "POST",
      url: url_base+"ajax/similar_games.php?similar=1",
      data: "categories="+categories+"&page="+page_similar,
      dataType: "html", 
      success: function(data){
      	similar_games = data.split("[]");
      	i = 0;
      	for (var key in similar_games) {  
		    game_data = similar_games[key].split("|");
		    
		    similar_games_[i] = new Array();
		    similar_games_[i] = game_data;
		    i = i + 1;
		} 
		  updateSimilarGames(similar_games_, url_base);     	
      }   
	});
}

function updateSimilarGames(data, url_base){
	$("#similar_games").html("");
	for (var key in data) {
		data[key][0] = data[key][0].replace(" ","");
		mark = Math.ceil(data[key][3]/data[key][4]);
		
		colorclass = "h25";
		if(mark >= 80)
				colorclass =  "h100";
				else if(mark >= 55)
					colorclass =  "h75";
				else if(mark >= 35)
					colorclass =  "h50";
					//alert($("#similar_games").html());
		name_ = data[key][2];
		if(name_.length > 16){
			data[key][2] = name_.substr(0,14) + " ...";
		}
		html_data="<a class=\"similar_game\" href=\""+url_base+data[key][0]+"-"+data[key][1]+"\"><div class=\"category2_game\" style=\"width: 215px;height: 143px;\"><div class=\"hlavicka\"><div class=\"text\" style=\"width: 200px;\">"+data[key][2]+"</div>	</div><div class=\"image\" style=\"width: 200px;\"><img width=\"199\" src=\""+url_base+"game_image/"+data[key][0]+".png\" /><div style=\"font-weight: bold;\" class=\"procenta2 "+colorclass+"\">"+mark+"%</div></div></div></a>";
		$("#similar_games").append(html_data);		
	}
	
	similar_loading = false;
	updating = false;
}

function game_add_favourite(id){
	$.ajax({
      type: "POST",
      url: base_url+"ajax/favouriteGame.php?type=2&action=setFavouriteGame&id="+id,
      dataType: "html", 
      success: function(data){
      	//alert(data); 
      	$(".v10").toggle();	
      }   
	});
}

var resized = false;
function resize_game_window(){
	if(!gameLoaded){
		return false;
	}
	if(!resized){
		/*$(".overlayer").css("display", "block");
		$(".overlayer_content").css("display", "block");
		$(".overlayer_content").html($("#game_handler").html());*/
		meter = 1.2;
		object = $("#game_object");
		object.css("width", (object.width()*meter)+"px");
		object.css("height", (object.height()*meter)+"px");
		$(".before_game").css("background", "rgba(0,0,0,0.7)");
		$(".before_game").css("width", "100%");
		$(".before_game").css("height", "100%");
		$(".before_game").css("position", "fixed");
		$(".before_game").css("z-index", "20");
		$(".before_game").css("top", "0");
		$(".before_game").css("left", "0");
		w = ($(window).width() / 2 )-(object.width()/2);
		h = ($(window).height() / 2 )-(object.height()/2);
		$("#game_object").css("margin-top", h+"px");
		$("#game_object").css("margin-left", w+"px");
		
		$(".before_game").click(function(){
			if(hovered)
				return false;
			$("#game_object").css("margin-top", "15px");
			$("#game_object").css("margin-left", "7px");
			$("#game_object").css("width", "690px");
			$("#game_object").css("height", "426px");
			
			$(".before_game").css("align-content", "stretch");
			$(".before_game").css("align-items", "stretch");
			$(".before_game").css("align-self", "stretch");
			$(".before_game").css("color", "#1A5278");
			$(".before_game").css("display", "block");
			$(".before_game").css("flex", "0 1 auto");
			$(".before_game").css("flex-basis", "auto");
			$(".before_game").css("flex-direction", "row");
			$(".before_game").css("flex-flow", "row");
			$(".before_game").css("flex-grow", "0");
			$(".before_game").css("flex-shrink", "1");
			$(".before_game").css("flex-wrap", "nowrap");
			$(".before_game").css("font", "normal normal 400 14px/20.02px Arial");
			$(".before_game").css("height", "444px");
			$(".before_game").css("justify-content", "flex-start");
			$(".before_game").css("margin", "0px");
			$(".before_game").css("order", "0");
			$(".before_game").css("padding", "0px");
			$(".before_game").css("position", "relative");
			$(".before_game").css("resize", "none");
			$(".before_game").css("text-align", "left");
			$(".before_game").css("vertical-align", "baseline");
			$(".before_game").css("width", "720px");
			$(".before_game").css("background", "none");
		});
		
		
	}
}

function hideSendPrompt(){
	$(".sendPropmt").css("display", "none");
}

function displaySendPrompt(){
	$(".sendPropmt").css("display", "block");
}

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 

function send_game_invitation(url_base, game_url, game_name, game_id){
	var email = $(".friend_email").val();
	var text = $(".email_text").val();
	var captcha = $(".captcha").val();
	
	if(!validateEmail(email)){
		alert(text_check_failed);
		return false;
	}

	$.ajax({
      type: "POST",
      url: url_base+"ajax/send_game.php?action=send_game",
      dataType: "html", 
      data: "email="+$(".friend_email").val()+"&text="+$(".email_text").val()+"&captcha_text="+$(".captcha").val()+"&captcha_id="+$(".captcha_id").val()+"&g_u="+game_url+"&g_n="+game_name+"&g_id="+game_id,
      success: function(data){
      		
      	if(data == 0){
      		alert(text_check_failed);
      		$(".sendPropmt").css("display", "none");
			return false;
      	}
      	if(data == 1){
      		alert(text_invitation_sent);
      		$(".sendPropmt").css("display", "none");
			return false;
      	}	
      
      }   
	});
	
	
}

function likeGame(id, url_base){
	$.ajax({
      type: "POST",
      url: url_base+"ajax/likeGame.php?type=1&action=likegame&id="+id,
      dataType: "html", 
      success: function(data){
      	//alert(data); 	
      	$(".v11").toggle();
      }   
	});
}

function dislikeGame(id, url_base){
	$.ajax({
      type: "POST",
      url: url_base+"ajax/likeGame.php?type=2&action=likegame&id="+id,
      dataType: "html", 
      success: function(data){
      	//alert(data); 	
      	$(".v11").toggle();
      }   
	});
}


// idle check
idleTime = 0;
playing = true;
$(document).ready(function () {
    var idleInterval = setInterval(timerIncrement, 45000); 
    var playingInterval = setInterval(playingSet, 60000); 

    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e) {
        idleTime = 0;
    });
});

function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime >= 2) { 
        playing = false;
    }
}

function playingSet(){
	if(!playing)
		return;
		
	$.ajax({
      type: "POST",
      url: base_url+"ajax/setPlaying.php?action=played&id="+game_id_handler,
      dataType: "html", 
      success: function(data){
      }   
	});
}