// JavaScript Document

var numGames=0; // set by php to number of extra games in 'more games' area
var curGame=0;
var gameTitles=[];

var demoLinks = [];
var buyLinks = [];

var goatMsgOK=null;
var goatMsgCancel=null;

var strMail2="min";

var strMail1="ad";

var support = [];


$.fn.isChildOf = function(exp){
return $(this).parents().filter(exp).length>0;
}; 

$(document).ready(function(){
	// document setup
	
	// non JS removal
	$("div#nonJS").remove();
	
	// logo click
	$("div#headerFakeLogo").click(function(){
		window.location.href='http://www.oddgoat.com/index.php';
		});
	
	// place email addy
	$("a#nospama").attr("href", "mailto:"+strMail1+strMail2+"@oddgoat.com");
	$("span#nospam").text(strMail1+strMail2+"@oddgoat.com");
	

	if (($.browser.msie && /MSIE 6\./.test(navigator.userAgent)))
	{
		// any IE6 specific stuff goes here
		
		// swap any pngs for gifs
		$("img[src$='.png']").each(function(){
			var str = $(this).attr("src");
			str = str.replace(/\.png/, ".gif");
			$(this).attr("src", str);

			});
		// swap goat says logo bg
		$("#theGoatSaysLogo").css("backgroundImage", "url(images/layout/goatSaysLogo.gif)");
	}
	
	// more games button clicks
	$("div#sliderButtonR").click(SlideRight).data("slidePos", 0);
	$("div#sliderButtonL").click(SlideLeft).hide().data("slidePos", 0);
	
	// more info hover
	$("img.extraGamesmoreInfoButton").mouseenter(function(){
			$(this).css("backgroundPosition", "0px 52px");
		}).mouseleave(function(){
			$(this).css("backgroundPosition", "0px 0px");
		});
	// sama again for game page
	$("img.extraGamesmoreInfoButton2").mouseenter(function(){
			$(this).css("backgroundPosition", "0px 52px");
		}).mouseleave(function(){
			$(this).css("backgroundPosition", "0px 0px");
		});
	
	//demo/buy buttons hovering
	$("img.mainButton").mouseenter(function(){
			$(this).css("backgroundPosition", "0px 77px");
		}).mouseleave(function(){
			$(this).css("backgroundPosition", "0px 0px");
		});
	
	// screenshot hover effecy
	$("img#showImage").mouseenter(function(){
			$(this).css("opacity", "0.5");
		}).mouseleave(function(){
			$(this).css("opacity", "1.0");
		});
	
	$("div#sliderTitle").text(gameTitles[0]);
	
	// setup screenshot links
	$("img#showImage").click(function(){
			if (window.location.href.indexOf("games.php")!=-1 || $(this).parents().filter("#sliderInterior").length>0)
			{					  
				ShowScreenshot($(this), false);
			}
			else
			{
				ShowScreenshot($(this), true);
			}
		}).css("cursor","pointer");
	
	// external links - use _blank via script to keep validators happy
	$("a[rel='blank']").attr("target", "_blank");
	$("a[rel='external']").attr("target", "_blank");
	
	// setup buttons
	$("div[id^='nav']").each(function(){
		var id = $(this).attr("id");
		if (id=='navDivider' || id=='navLeftEnd' || id=='navRightEnd')
			return true;
		$(this).data("hover", GetButtonState($(this).attr("id")));
		if ($(this).data("hover")==2)
		{
			$(this).css("background-position", "0px 39px").css("cursor","default");
		}
		$(this).mouseenter(function(){
			if ($(this).data("hover")==0)
			{
				$(this).css("background-position", "0px 78px");
				$(this).data("hover", 1);
			}
			});
		$(this).mouseleave(function(){
			if ($(this).data("hover")==1)
			{
				$(this).css("background-position", "0px 0px");
				$(this).data("hover", 0);
			}
			});
		});
	
	// setup form elements
	$("form textArea, form input").click(function(){
			$(this).val("").unbind("click");
		}).blur(function(){
			$(this).css({border:"border:#C90 1px solid"});
		}).focus(function(){
			$(this).css({border:"border:#D50 1px solid"});
		});
	
	// end of ready func
	});

function GetButtonState(id)
{
	if (id=='navGames' && window.location.href.indexOf('games.php')!=-1)
		return 2;
	if (id=='navContact' && window.location.href.indexOf('contact.php')!=-1)
		return 2;
	if (id=='navAbout' && window.location.href.indexOf('about.php')!=-1)
		return 2;
	if (id=='navNews' && window.location.href.indexOf('news.php')!=-1)
		return 2;
	if (id=='navHelp' && window.location.href.indexOf('help.php')!=-1)
		return 2;
		
	return 0;
}

function SlideRight()
{
	if (curGame < numGames)
	{
		var pos = $("div#sliderButtonR").data("slidePos");
		pos -= 764 + 16;
		$("div#sliderInterior").stop().animate( {left: pos+'px'}, 700);
		$("div#sliderButtonR").data("slidePos", pos);
		$("div#sliderButtonL").data("slidePos", pos);
		curGame++;
		$("div#sliderButtonL").show();
		if (curGame==numGames)
			$("div#sliderButtonR").hide();
		$("div#sliderTitle").text(gameTitles[curGame]);
	}
}

function SlideLeft()
{
	if (curGame > 0)
	{
		var pos = $("div#sliderButtonL").data("slidePos");
		pos += 764 + 16;
		$("div#sliderInterior").stop().animate( {left: pos+'px'}, 700);
		$("div#sliderButtonR").data("slidePos", pos);
		$("div#sliderButtonL").data("slidePos", pos);
		curGame--;
		$("div#sliderButtonR").show();
		if (curGame==0)
			$("div#sliderButtonL").hide();
		$("div#sliderTitle").text(gameTitles[curGame]);
	}
}

function ShowScreenshot(obj, bShowNextPrev)
{
	var str = obj.css("backgroundImage");
	if (str.length>7)
	{
		str = str.substr(str.indexOf("(")+1);
		str = str.substr(0, str.indexOf(")"));
	}
	else
		str = obj.attr("src");
	// get realtive large screen url
	var path = str.substr(0, str.lastIndexOf('/'));
	path += '/large/';
	path = path.substr(path.indexOf("images/"));
	path += str.substr(str.lastIndexOf('/')+1);
	// remove quotes
	path = path.replace(/\"/, "");
	
	OpenLightBox(path, bShowNextPrev);
}

function GoatMessage(msg)
{
	if (msg.length>2)
	{
		$("#goatText").html(msg);
		var lpos = $(window).width()/2;
		lpos -= $("#theGoatSays").width()/2;
		$("#theGoatSays").stop();
		if (!$("#theGostSays").is(":visible"))
			$("#theGoatSays").show().animate({left: lpos+'px'}, 500);
		$("#goatMessageButtons").hide();
		$("#theGoatSaysClose").show();
	}
}

function CloseGoatMessage()
{
	$("#theGoatSays").animate({left: '-1500px'}, 750, function(){ $(this).hide(); });
}

function GoatDecisionMessage(msg, func_ok, func_cancel)
{
	if (msg.length>2)
	{
		$("#goatText").html(msg);
		var lpos = $(window).width()/2;
		lpos -= $("#theGoatSays").width()/2;
		$("#theGoatSays").stop();
		if (!$("#theGostSays").is(":visible"))
			$("#theGoatSays").show().animate({left: lpos+'px'}, 500);
		$("#goatMessageButtons").show();
		$("#theGoatSaysClose").hide();
		goatMsgOK = func_ok;
		goatMsgCancel = func_cancel;
	}
}

function GoatOK()
{
	CloseGoatMessage();
	if (goatMsgOK!=null)
		goatMsgOK();
}

function GoatCancel()
{
	CloseGoatMessage();
	if (goatMsgCancel!=null)
		goatMsgCancel();
}

function BigButtonPopup(obj_clicked)
{
	// pops out a box that shows platform variations, or directl links for single platform games
	var obj = $(obj_clicked);
	
	// single platform test
	if (support['windows'] + support['mac'] + support['linux'] == 1)
	{
		if (obj.attr("title")=="Buy Now")
		{
			if (support['windows']==1)
				window.location.href=buyLinks[0];
			else if (support['mac']==1)
				window.location.href=buyLinks[1];
			else if (support['linux']==1)
				window.location.href=buyLinks[2];
		}
		else
		{
			if (support['windows']==1)
				window.location.href=demoLinks[0];
			else if (support['mac']==1)
				window.location.href=demoLinks[1];
			else if (support['linux']==1)
				window.location.href=demoLinks[2];
		}
		return true;
	}
	
	
	$("div#popupButtons a#buttonWin").hide();
	$("div#popupButtons a#buttonMac").hide();
	$("div#popupButtons a#buttonLinux").hide();
	$("span#platformText").show();
	
	var lstart = obj.offset().left + (obj.width()/2);
	var tstart = obj.offset().top + (obj.height()/2);
	var lend = lstart - (obj.width()/2);
	//var tend = tstart - (obj.height()/2);
	var tend = tstart - obj.height();
	var w=40; // becomes the width of the box
	
	if (obj.attr("title")=="Buy Now")
	{		
		// show buy now links
		if (buyLinks[0]!='undefined' && buyLinks[0]!='' && support['windows']==1)
		{
			$("div#popupButtons a#buttonWin").show().attr("href", buyLinks[0]).click(function(){ClosePopup(); return true;});
			w += 115;
		}
		if (buyLinks[1]!='undefined' && buyLinks[1]!='' && support['mac']==1)
		{
			$("div#popupButtons a#buttonMac").show().attr("href", buyLinks[1]).click(function(){ClosePopup(); return true;});
			w += 115;
		}
		if (buyLinks[2]!='undefined' && buyLinks[2]!='' && support['linux']==1)
		{
			$("div#popupButtons a#buttonLinux").show().attr("href", buyLinks[2]).click(function(){ClosePopup(); return true;});
			w += 115;
		}
		
		lend = lstart - (w/2);		
		$("div#popupButtons").css({opacity: 0.0, left: lend+'px', top:tend+'px', width:w+'px'}).show()
						.animate({opacity: 1.0}, 300);
	}
	else
	{
		// show demo links
		if (demoLinks[0]!='undefined' && demoLinks[0]!='' && support['windows']==1)
		{
			$("div#popupButtons a#buttonWin").show().attr("href", "download.php?id="+curGame+"&platform=windows");
			w += 100;
		}
		if (demoLinks[1]!='undefined' && demoLinks[1]!='' && support['mac']==1)
		{
			$("div#popupButtons a#buttonMac").show().attr("href", demoLinks[1]);
			w += 100;
		}
		if (demoLinks[2]!='undefined' && demoLinks[2]!='' && support['linux']==1)
		{
			$("div#popupButtons a#buttonLinux").show().attr("href", demoLinks[2]);
			w += 100;
		}
		
		lend = lstart - (w/2);		
		$("div#popupButtons").css({opacity: 0.0, left: lend+'px', top:tend+'px', width:w+'px'}).show()
						.animate({opacity: 1.0}, 300);
	}
	
}

function ClosePopup()
{
	$("div#popupButtons").stop().show().animate({opacity: 0.0}, 300, function(){ $(this).hide(); });
}

function OpenHelpItem(t)
{
	var obj = $(t);
	
	var i = obj.children("h2").children("img");
	if (i.attr("src").indexOf("arrowUp")!=-1)
	{
		i.attr("src", "images/layout/arrowDown.gif");
		obj.parent("div").children("div#helpItemBody").slideUp(200);
	}
	else
	{
		i.attr("src", "images/layout/arrowUp.gif");
		obj.parent("div").children("div#helpItemBody").slideDown(200);
	}
	
}
