var shareField = 0;
var gameRef = null;
var featId = 0;
var featuredOffs = 0;
var featLastChange = 0;
var featRefList = null;
var featTitleList = null;
var eventTracks = new Array();

function getTime()
{
	var d = new Date();
	return parseInt(d.getTime() / 1000);
}

function starRateClick(rate,ref)
{
	if (cookie.get("rated"))
	{
		$("#ratingvote").html("Thanks for voting!");
		return;
	}
	cookie.set("rated",1,"/"+ref);
	cookie.set("rated",1,"/play/"+ref);

	$.getJSON("/ajax/star_rate", {"rate": rate, "ref": ref}, finishedRate);
}

function finishedRate(data)
{
	var rate = parseFloat(data.rate);
	var width = Math.round(rate * 16);

	$("#rateid").html(rate);
	$("#current-rating").attr("style","width: "+width+"px");
	$("#ratingvote").html("Thanks for voting!");
}

function onClickSearchField()
{
	if ($("#searchGame").hasClass("initSearchText"))
	{
		$("#searchGame").removeClass("initSearchText");
		$("#searchGame").attr("value","");
	}
}

function onKeyDownSearchButton(e)
{
	e = e ? e : window.event;
	if ($.browser.msie)
		var key = e.keyCode;
	else
		var key = e.which;
	
	if (key == 13)
		onClickSearchButton();
}

function onClickSearchButton()
{
	if (!$("#searchGame").hasClass("initSearchText") && $("#searchGame").attr("value").length)
	{
		var href = $("#searchGame").attr("value");
		window.location.href = "/search?game=" + encodeURIComponent(href);
	}
}

function onClickVoteButton(ref)
{
	var vote = $("input:radio[name='vote']:checked").attr("id");
	if (vote == undefined)
		return;

	$("#votebutton").html("Thanks for voting!");

	if (cookie.get(vote))
		return;
	cookie.set(vote,1,"/"+ref);
	cookie.set(vote,1,"/play/"+ref);

	$.getJSON("/ajax/vote", {"vote": vote, "ref": ref}, function() {} );
}

/*
function extendThumbs()
{
	$("a.cthumb").hover(
		function () { thumbChange(this,1); },
		function () { thumbChange(this,0); }
	);
}

function thumbChange(node,change)
{
	var imgnode = $(node).find("img");

	var src = $(imgnode).attr("src");
	var img = src.substr(0,src.length-3);
	$(imgnode).attr("src",change ? (img+"gif") : (img+"jpg"));
}
*/

function setShareFieldVisibility(field)
{
	if ($("#share_fields").hasClass("hidden"))
	{
		$("#share_fields").removeClass("hidden");
		shareField = field;
	}
	else
	{
		if (shareField != field)
		{
			shareField = field;
		}
		else
		{
			$("#share_fields").addClass("hidden");
			shareField = 0;
		}
	}
}

function share_AddToMySite(ref,title,width,height)
{
	var content =
	"<span style='font-size: 12px; font-weight: normal;'>Embed "+title+" on Your Site: </span>"+
	"<input type='text' size='100' readonly='1' value=\""+getFlashObject(width,height,ref)+"\" onclick='this.focus(); this.select(); pageTracker._trackEvent(\"MyButtons\",\"AddToMySite\",location.href);'/><img style='cursor: pointer; position: relative; top: 4px; left: 8px;' src='/images/close_icon.png' onclick='share_hideField();'/>";
	$("#share_fields").html(content);

	setShareFieldVisibility("add_to_my_site");
}

function getFlashObject(width,height,ref)
{
	var host = "http://www.mylostgames.com";
	
	return "<object width='"+width+"' height='"+height+"'>" +
	"<param name='movie' value='"+host+"/swf/"+ref+".swf'></param>" +
	"<param name='allowScriptAccess' value='always'></param>" +
	"<param name='allowNetworking' value='all'></param>" +
	"<embed src='"+host+"/swf/"+ref+".swf' allowNetworking='all' allowScriptAccess='always' type='application/x-shockwave-flash' width='"+width+"' height='"+height+"'></embed>" +
	"</object>";
}

function share_Download(ref)
{
	pageTracker._trackEvent("MyButtons","Download",location.href);
}

/*
function share_ShareByIM(ref)
{
	var content =
	"<span style='font-size: 12px; font-weight: normal;'>Copy and paste this link into Your Instant Messenger: </span>"+
	"<input type='text' size='45' value='http://www.mylostgames.com/play/"+ref+"' onclick='this.focus(); this.select(); updateCounter(\"share_by_im_selected\");'/><img style='cursor: pointer; position: relative; top: 4px; left: 8px;' src='/images/close_icon.png' onclick='share_hideField();'/>";
	$("#share_fields").html(content);

	setShareFieldVisibility("share_by_im");
}
*/

function share_AddToAny_top(ref)
{
	pageTracker._trackEvent("MyButtons","AddToAny",location.href);
}

function share_AddToAny_bottom(ref)
{
	pageTracker._trackEvent("MyButtons","AddToAny",location.href);
}

function share_hideField()
{
	$("#share_fields").addClass("hidden");
	shareField = 0;
}

function pushEvent(category,action,label,value)
{
	eventTracks.push( {category:category, action:action, label:label, value:value} );
}

function setEventTracks()
{
	for (var key in eventTracks)
		pageTracker._trackEvent(eventTracks[key].category,eventTracks[key].action,eventTracks[key].label,eventTracks[key].value);
}

function initFeatured(fRefs,fTitles)
{
	featRefList = fRefs;
	featTitleList = fTitles;
	featId = getTime() % fRefs.length;
	featuredOffs = -198*featId;

	refreshFeatured();
}

function showFeatured()
{
	$("#featured_title").html(featTitleList[featId]);
	if (featRefList[featId].length)
		$("#featured_games").attr("href","/play/"+featRefList[featId]);
	else
		$("#featured_games").removeAttr("href");
	$("#featured_games").html("<div style='width: 396px; height: 196px; border: 2px solid black; background: url(/images/featured.jpg) no-repeat 0 "+featuredOffs+"px;)'></div>");
}

function refreshFeatured()
{
	setTimeout("refreshFeatured()",40);

	if (!featLastChange)
	{
		featLastChange = getTime();
		showFeatured();
	}

	var currTime = getTime();
	if ((currTime - featLastChange) < 6)
		return;

	featuredOffs -= 10;
	if (featuredOffs < -198*(featId+1))
	{
		featuredOffs = -198*(featId+1);
		if (featId == (featRefList.length-1))
		{
			featId = 0;
			featuredOffs = 0;
		}
		else
		{
			featId++;
		}
		featLastChange = currTime;
	}

	showFeatured();
}

function showFB_Likebox()
{
	var width = 976;
	var height = 88;
	var content = '<iframe src="http://www.facebook.com/plugins/likebox.php?id=116385728371916&amp;width='+width+'&amp;height='+height+'&amp;connections=99&amp;stream=false&amp;header=false" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:'+width+'px; height:'+height+'px; background-color: #ffddb4; margin-bottom: 10px;"></iframe>';
	$("#fb_likebox").html(content);
}

var highscores = {

	content : null,

	refresh : function(ref)
	{
		gameRef = ref;
		setTimeout(this.refreshNow,1000);
 	},

	refreshNow : function()
	{
		$.getJSON("/ajax/get_highscores", {"game": gameRef}, highscores.update);
	},
	
	update : function(data)
	{
		this.content = "";

		this.content += "<div class='rightbox'><div class='tl'></div><div class='tr'></div>";
		this.content += "<p style='font-weight: bold; text-align: center; margin: 0;'>" + data.title + " High Scores</p>";

		this.content +=
		"<p/>" +
		"<table border='0' cellpadding='2' cellspacing='0'>" +
		"<tr>" +
		"<td></td>" +
		"<td width='70'></td>" +
		"<td></td>" +
		"<td></td>" +
		"</tr>";

		for (var key in data.scores)
		{
			var row = data.scores[key];

			var counter = parseInt(key) + 1;
			bcolor = (counter & 1) ? "#fecc90" : "#ffddb4";
			color = (counter == 1) ? "#f00" : "#000";
			this.content +=
			"<tr style='background-color: "+bcolor+"; color:"+color+";'>" +
			"<td align='right'>"+counter+"</td>" +
			"<td style='padding-left: 4px;'>"+row["name"]+"</td>" +
			"<td style='text-align: right;'>"+row["score"]+"</td>" +
			"<td style='padding-left: 5px; font-style: italic;'>"+row["mode"]+"</td>" +
 			"</tr>";
		}
		
		this.content += "<div class='bl'></div><div class='br'></div></div>";

		$("#highscores_table").html(this.content);
	}
}
