// Copyright (c) www.farfarfar.com. All rights reserved.
// You cannot use this script for your website.

// You cannot call drawButtons() after loading because it is equal to refreshing the page with
// buttons. You must call drawButtons() when it is loading so the browser won't load the file
// again after it draw the buttons.

var gameId="resizableGame";
var gameWrapper="gameWrapper";
var resizeButtonsId="resizeGameButtons";
var codeBase = "http://www.farfarfar.com/js/";

var resizeButtonMinusId = 'resizeButtonMinus';
var resizeButtonPlusId = 'resizeButtonPlus';

var autoResized = false;
var isAutoResize = true;

var adHeight = -90;

var width;
var height;
var fractions;

var curRatio;

var ratioPlus;

var isGame;

var origTitle = document.title;

var movie;

var firstResize = false;

if (document.getElementById(gameId))
{
	movie = document.getElementById(gameId);
	flashPercent();
}

function flashPercent()
{
  document.title = "Loading " + movie.PercentLoaded() + "%" + " - " + origTitle;
  
  if (movie.PercentLoaded() == 100)
  {
    document.title = origTitle;
    return;
  }
  
  setTimeout("flashPercent()", 100);
}

function initResizeGame()
{
	if (!/^http:\/\/([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9]\.)*)*farfarfar\.com\//.test(window.location.href))return;
	isGame=(document.getElementById(gameId))?true:false;
	if (isGame==false)return;
	//width=document.getElementById(gameId).style.width.substr(0,document.getElementById(gameId).style.width.length-2);
	//height=document.getElementById(gameId).style.height.substr(0,document.getElementById(gameId).style.width.length-2);
	width=document.getElementById(gameId).width;
	height=document.getElementById(gameId).height;
	fractions=simplifyFraction(width,height);

	curRatio=width/fractions[0];

	ratioPlus=Math.ceil((screen.width/8)/fractions[0]); // *MUST* be ceil() instead of floor(), spent hours debugging floor()
	var finalWidth;
	var finalHeight;
	var screenWidth=screen.width;

	if (screenWidth<800)
	{
		isAutoResize = false;
	}

	if (autoResized==true)return;
	if (isAutoResize==true)
	{
		var frameWidth;
		var frameHeight;
		if (self.innerWidth)
		{
			frameWidth = self.innerWidth;
			frameHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			frameWidth = document.documentElement.clientWidth;
			frameHeight = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			frameWidth = document.body.clientWidth;
			frameHeight = document.body.clientHeight;
		}

		frameHeight += adHeight + adHeight;

		if (frameWidth>=screenWidth/16 && frameHeight>=screenWidth/16)
		{
			curRatio=Math.floor(Math.min(frameWidth/fractions[0],frameHeight/fractions[1]));
			if (curRatio == 0) return;
			//document.getElementById(gameId).style.width=(curRatio*fractions[0])+'px';
			//document.getElementById(gameId).style.height=(curRatio*fractions[1])+'px';
			document.getElementById(gameId).width=(curRatio*fractions[0]);
			document.getElementById(gameId).height=(curRatio*fractions[1]);
			//document.getElementById(gameWrapper).style.position="relative";
			//window.scrollTo(0,document.getElementById(gameWrapper).offsetTop + adHeight);
		}
		autoResized = true;
	}
	
	//document.getElementById(resizeButtonMinusId).style.display="block";
	//document.getElementById(resizeButtonPlusId).style.display="block";
	//document.getElementById(resizeButtonMinusId).style.visibility="visible";
	//document.getElementById(resizeButtonPlusId).style.visibility="visible";

	drawButtons();
}

window.onload=initResizeGame;

function resizeGame(size)
{
	if (size==1)
	{
	    if (firstResize) {
	        firstResize = false;
	        return;
	    }
		curRatio+=ratioPlus;
		var tmp=curRatio;
		var finalWidth=((fractions[0]*tmp));
		var finalHeight=((fractions[1]*tmp));
		if (finalWidth >= (screen.width * 2))
		{
			curRatio-=ratioPlus;
			return;
		}
	}
	else
	{
		curRatio-=ratioPlus;
		var tmp=curRatio;
		var finalWidth=((fractions[0]*tmp));
		var finalHeight=((fractions[1]*tmp));
		if (finalWidth<(screen.width/8))
		{
			curRatio+=ratioPlus;
			return;
		}
	}

	document.getElementById(gameId).width=finalWidth;
	document.getElementById(gameId).height=finalHeight;
}

function gcd(a, b)
{
  while (b != 0)
  {
    var t = b;
    b = a % b;
    a = t;
  }
return a;
}

function simplifyFraction(numerator,denominator)
{
	return new Array(numerator / gcd(numerator, denominator), denominator / gcd(numerator, denominator));
}

//
function drawButton(text,name,callFunction)
{//text-align:center;padding:1px;color:#aa5500;font-size:88%;font-family:arial;width:8em;background-color:#ffdd00;border-style:solid;border-color:#ffdd99 #dd9900 #dd9900 #ffdd99;border-width:2px;
var result='<span id="' + name + '" style="cursor:hand;cursor:pointer" '+callFunction+'>';
result+=text;
result+='<\/span>';
return result;
}

document.write('<div id="' + resizeButtonsId + '"><\/div>');

function drawButtons()
{
//elements=document.getElementsByTagName("body");
//elements[0].style.backgroundColor="#ff00ff";

var minusImg='<img src="' + codeBase + 'minus.png" alt="Resize Smaller" \/>';
var plusImg='<img src="' + codeBase + 'plus.png" alt="Resize Larger" \/>';

//var minusImg='Resize Smaller';
//var plusImg='Resize Larger';

if (navigator.appName.indexOf("Microsoft")!=-1)
{
var result=drawButton(minusImg,resizeButtonMinusId,'onclick="resizeGame(-1)" ondblclick="resizeGame(-1)"')+' '+drawButton(plusImg,resizeButtonPlusId,'onclick="resizeGame(1)" ondblclick="resizeGame(1)"');
}
else
{
var result=drawButton(minusImg,resizeButtonMinusId,'onmousedown="resizeGame(-1)"')+drawButton(plusImg,resizeButtonPlusId,'onmousedown="resizeGame(1)"');
}
document.getElementById(resizeButtonsId).innerHTML=result;
}

