// Zuup Js Utils

//open close game bgd
$(function(){
	//comments
	$("#captcha").bind("click", function() {
		  $("#captcha").attr("src","/library/captcha.php?c="+Math.random());
	});
	//zebra table
	$(".ldbTbl tr:odd").addClass("tr_odd");
						   
	$("#page-count").val(1);
	$("#load_more_content").click(function(){
		$.ajax({
			type: "GET",
			url: "/library/ajax_commentResult.php",
			data: "p="+$("#page-count").val()+"&game_id="+$("#c_game_id").val(),
			success: function(data) {
				$("#page-count").val(parseInt($("#page-count").val())+1);
				$("#comments").append(data);
				if (data=="") $("#load_more_content").toggle();
			}
		});  							   
	});
	
	//
	var messageArea = $('textarea#c_message');
	
	//$('div#submit-comment').click(function(){		
	$("div#submit-comment").live("click", function(){										 
		// Store vars
		var message = $("#c_message").val();
		var dataString = 'c_message='+ message +'&c_email='+$("#c_email").val()+'&c_name='+$("#c_name").val()+'&c_game_id='+$("#c_game_id").val()+'&c_captcha='+$("#c_captcha").val();
		
		
		
		$.ajax({
			type: "POST",
			url: "/library/ajax_submitComment.php",
			data: dataString,
			success: function(data) {
				//$('div#loader').find('img.load-gif').remove();
				$('div#loader').hide().fadeIn('slow');
				$('div#resultsComment').html(data);
					
				if ($('#status').val() == "ok"){
					$('div#submit-comment').unbind('click').click(function(){
						return false;
					});
					$('div#resultsComment div.comment-unapproved:nth-child(1)').hide().slideDown('slow');
					
					//increment comments number
					var comments = parseFloat($('.comments_no').html());  
					$('.comments_no').html(parseFloat(comments) + 1);

				}
				else{
					$('div#resultsComment div.errorSub:nth-child(1)').hide().slideDown('slow');
				}
			}
		});  
		return false;
	});
	
	messageArea.keyup(function(){
		var limit = 400;
		var currentLength = $(this).val().length;
		var charsLeft = limit - currentLength;
		$('span.limit').html(charsLeft);
		if(currentLength >= limit){
			$(this).val($(this).val().substring(0, limit));
			$('span.limit').html('0');
			var textarea = document.getElementById('c_message');
			textarea.scrollTop = textarea.scrollHeight + 9999;
		}
	});
	
	$('.comment_input, .comment_textarea').focus(function(){
		$(this).css({ 
			border: "2px solid #f9b532",
			color: "#4c4c4c",
			margin: "0"
		});
	});
	
	//input + textarea blur
	$('.comment_input, .comment_textarea').blur(function(){
		$(this).css({ 
			border: "1px solid #c6c7c7",
			color: "#7b7b7b",
			margin: "1px"
		});
	});
	
	
	//textarea embed
	$('.embed_input').focus(function(){
		$(this).css({ 
			border: "2px solid #f9b532",
			margin: "0"
		});
		$(this).select();
	});
	
	$('.embed_input').blur(function(){
		$(this).css({ 
			border: "1px solid #c6c7c7",
			margin: "1px"
		});
	});
	
	$('a[rel="selectCode"]').click(function(){
		$('.embed_input').select();
		$('.embed_input').css({ 
			border: "2px solid #f9b532",
			margin: "0"
		});
	});
	
	
	
	//contest inputs
	$('.contestInputTxt').focus(function(){
		$(this).css({ 
			border: "2px solid #fca12e",
			color: "#4c4c4c",
			margin: "0"
		});
	});
	
	//input + textarea blur
	$('.contestInputTxt').blur(function(){
		$(this).css({ 
			border: "1px solid #dbdbdb",
			color: "#7b7b7b",
			margin: "1px"
		});
	});
	
	//game box hover / out
	$('.game-box, .gameBox, .gameBox2, .mpgames-container a').hover(function() {
		$(this).addClass('gameBox_hover');
		
	}, function() {
		$(this).removeClass('gameBox_hover');
	});
	
	$('.cat-new-games-box').hover(function() {
		$(this).addClass('cat-new-games-box_hover');
		
	}, function() {
		$(this).removeClass('cat-new-games-box_hover');
	});
	
	$('.sliderwrapper').hover(function() {
		$('.title_desc').animate({ marginTop : "-61" }, 150);  
		
	}, function() {
		$('.title_desc').animate({ marginTop : "-31" }, 150);  
	});
	
	$('.carousel ul li a').hover(function() {
		$(this).addClass('liItem_hover');
		
	}, function() {
		$(this).removeClass('liItem_hover');
	});
	
	/*var Toggled=true;
	$('.getRelated').click(function(){
		if(Toggled==false){
			$('.getRelated').html('Ascunde jocurile similare'); 
			Toggled=true;
		}
		else{
			$('.getRelated').html('Arata jocurile similare');
			Toggled=false;
		}
		
		$(".relatedGames").animate({ 
			height: "toggle",
			opacity: "toggle"
		  }, 200 );
		
		return false;
	});*/
	
	//realtime search input
	
	$('#inputString').focus(function(){
		//if (this.val() =='') this.val() = '';
		 //= "Jocuri Gratis";
		if($(this).val("Cauta jocuri"))
		{
			$(this).val("");
			//alert("true");
		}
	});
	
	$('#inputString').blur(function(){
		if($(this).val(""))
		{
			$(this).val("Cauta jocuri");
			//alert("true");
		}
		$(this).removeClass('loader');
		$('#suggestions').fadeOut(); // Hide the suggestions box
	});
	
	$('#inputString').keyup(function(){
		lookup(this.value); //search script
		$(this).removeClass('loader');
	});
	
	$('#inputString').keydown(function(){
		$(this).addClass('loader');
	});
	
	
	//tabs
	$('#tabNav').tabs({ fxFade: true, fxSpeed: 'fast' });
	/*$(".tabC > li > a").click(function(){										 
		var docHeight = $(document).height();
	});*/
	
	//game focus
	var docHeight = $(document).height();
	var gameFocus = false;

	$('.gameFocus').click(function(){
		if(gameFocus==false){
			$('.gameFocus').addClass('gameFocus_active'); 
			$.scrollTo({top:'178px', left:'0px'}, 800);
			gameFocus=true;
		}
		else{
			//$.scrollTo({top:'0px', left:'0px'}, 800);
			$('.gameFocus').removeClass('gameFocus_active'); 
			gameFocus=false;
		}
		
		$("#overlay").animate({ 
			opacity: "toggle"
		}, 300 );
		
		$(".gifText").animate({ 
			opacity: "toggle"
		}, 100 );
		
		return false;
	});
   
   $("body").append("<div id='overlay'></div>");
   $("#overlay")
	  .height(docHeight)
	  .css({
		 'opacity' : 0.85,
		 'position': 'absolute',
		 'display': 'none',
		 'top': 0,
		 'left': 0,
		 'background-color': '#000',
		 'width': '100%',
		 'z-index': 400
	  });
	///
	
	$.getScript("http://s7.addthis.com/js/250/addthis_widget.js?domready=1",function(d,s){});
});

function updateVoteCount(gid){
	$.ajax({
		   type: "GET",
		   url: "../../library/vote.php",
		   data: "a=getVote&gid="+gid,
		   dataType : "json",
		   success: function(votes){
			   $("#votesNrUp").html(votes.good);
			   $("#votesNrDown").html(votes.bad);
			   if (votes.good=="0" && votes.bad=="0"){
				   $(".votingButtons span").html("Fii primul care voteaza!");
					}
			   }
	});
}

function disableVoting(){
	$("#voteUp").removeClass("up").addClass("up_h");
	$("#voteDown").removeClass("down").addClass("down_h");
}

function catchVoteStatus(status,gid){		
	switch(status){
		case "duplicatevote":
			$(".votingButtons span").html("Deja ai votat acest joc!");
		break;
	
		case "":
		default:
		case "errvote":
		case "errparams":
			$(".votingButtons span").html("Votul tau nu a fost inregistrat!<br/>Te rugam sa incerci mai tarziu!");
		break;
	
		case "ok":
			$(".votingButtons span").html("Votul tau a fost inregistrat!");
			disableVoting();
		break;				
	}
	updateVoteCount(gid);
}	

function setupVote(gid){

	$("#voteUp").click(function(){
		   if (!$(this).hasClass("up_h") ){
			$.ajax({
				   type: "GET",
				   url: "../../library/vote.php",
				   data: "a=postVote&gid="+gid+"&vote=good",
				   dataType : "json",	   
				   success: function(response){
						catchVoteStatus(response.status,gid);
				   }
			});
		}
	});	
	
	$("#voteDown").click(function(){
		  if (!$(this).hasClass("down_h") ){
			$.ajax({
				   type: "GET",
				   url: "../../library/vote.php",
				   data: "a=postVote&gid="+gid+"&vote=bad",
				   dataType : "json",	   
				   success: function(response){
					   catchVoteStatus(response.status,gid);
				   }
			});
		}
	});			
			
	
	$.ajax({
		   type: "GET",
		   url: "../../library/vote.php",
		   data: "a=isVoted&gid="+gid,
		   dataType : "json",	   
		   success: function(msg){
				if(msg.vote == false){
					disableVoting();
					$(".votingButtons span").html("Deja ai votat acest joc!");
					$(".votingButtons").toggle();
				} else {
					$(".votingButtons").toggle();
					}
					
			}
		});
	
}


//show / hide footer games
function showList(id)
{
	document.getElementById(id).style.display = "block";
	document.getElementById('showListTrigger_on').style.display = "none";
	document.getElementById('showListTrigger_off').style.display = "block";
}

function hideList(id)
{
	document.getElementById(id).style.display = "none";
	document.getElementById('showListTrigger_on').style.display = "block";
	document.getElementById('showListTrigger_off').style.display = "none";
}

function lookup(inputString) {
	if(inputString.length == 0) {
		$('#suggestions').fadeOut(); // Hide the suggestions box
	} else {
		//alert("test!")
		$.post("/library/ajax_search.php", {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
			$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').html(data); // Fill the suggestions box
		});
	}
}

//embed preview
function previewEmbed(id, width, height, game_name)
{
	window.open ("http://embed.zuup.ro/embed_preview.php?gid=" + id, "mywindow","location=no, status=no, resizable=no, scrollbars=no, width="+width+", height="+height+"");
}



