/**
 * (C) Copyright 2009 Evan de la Cruz
 * evan.delacruz@gmail.com
 * This code is beta and may not be used by anyone anywhere for any reason
 */

var balloon = null;
var regFormHtml = null;
var loginFormHtml = null;
var tosHtml = null;
var loggedin = 0;

function GetControlPanelHtml()
{
    ajaxConnection = new AjaxHttpConnection("http://www.blitzgamer.com/templates/bg/boxes/boxControlPanel2.php");
    vars = "";
    cpHtml = ajaxConnection.SendDataSync(vars);
	var cpDiv = document.getElementById("userControlPanelBox");
	if (cpDiv)
	{
		cpDiv.innerHTML = cpHtml;
	}
}
function RegisterNewUser()
{
    var username = document.getElementById("newusername").value;
    var email = document.getElementById("newemailaddress").value;
    var pass1 = document.getElementById("newpasswordone").value;
    var pass2 = document.getElementById("newpasswordtwo").value;
    var gender = document.getElementById("newgender").value;
    var accept = document.getElementById("newaccept").value;
    var about = document.getElementById("newabout").value;
    var country = document.getElementById("newcountry").value;
    var fullname = document.getElementById("newfullname").value;
    var bdmonth = document.getElementById("newbdmonth").value;
    var bdday = document.getElementById("newbdday").value;
    var bdyear = document.getElementById("newbdyear").value;
    var website = document.getElementById("newwebsite").value;
    var messenger = document.getElementById("newmessenger").value;
    var messengertype = document.getElementById("newmessengertype").value;


    ajaxConnection = new AjaxHttpConnection("http://www.blitzgamer.com/index.php");
    vars = "newusername="+escape(username)+"&newemailaddress="+escape(email)+"&newhideemail="+escape(email)+"&newpasswordone="+escape(pass1)+"&newpasswordtwo="+escape(pass2)+"&newfullname="+escape(fullname)+"&newbdmonth="+escape(bdmonth)+"&newbdday="+escape(bdday)+"&newbdyear="+escape(bdyear)+"&newcountry="+escape(country)+"&newgender="+escape(gender)+"&newwebsite="+escape(website)+"&newmessenger="+escape(messenger)+"&newmessengertype="+escape(messengertype)+"&newabout="+escape(about)+"&newaccept="+escape(accept)+"&ajaxReply=1&action=newmember";
    regReturn = ajaxConnection.SendDataSync(vars);
	if (regReturn == "1")
	{
	    Popups.ClosePopup();
	    Popups.ShowPopup(new Point(400,400), new Point(50,50), "<div style='background-color: #142A3A; padding: 10px; border: solid 2px #1155af;'><h2>You are almost done with your registration...</h2><p> You need to confirm your account. Please check your email (spam box) and click the confirmation link before trying to login.</p><br/><p><a href='javascript:void(0)' onclick='Popups.ClosePopup()'>Close</a></div>", 1, 2,true);
	} else {
        document.getElementById("ErrorInfo").innerHTML = regReturn;
        document.getElementById("ErrorInfo").style.display = "block";
	}
}


function HideGameDiv()
{
    var gameDiv = document.getElementById('GamePlayCenterDiv');
    if (gameDiv)
    {
	gameDiv.style.visibility = 'hidden';
    }
}
function ShowGameDiv()
{
    var gameDiv = document.getElementById('GamePlayCenterDiv');
    if (gameDiv)
    {
	gameDiv.style.visibility = 'visible';
    }
}

function ShowRegistrationPopup()
{
    Popups.ClosePopup();
    if (regFormHtml== null)
    {
        regFormHtml= AjaxClient.SendRequest("ShowRegistrationPopup", "");
    }
    HideGameDiv();
    Popups.ShowPopup(new Point(660,650), new Point(50,50), regFormHtml, 1, 2,true,true)
    //AjaxClient.SendRequestAsyncPopup("ShowRegistrationPopup","",new Point(660,650), new Point(50,50), 1, 2,true,true);
}
function ShowLoginPopup()
{
    Popups.ClosePopup();
    if (loginFormHtml == null)
    {
        loginFormHtml = AjaxClient.SendRequest("ShowLoginPopup", "");
    }
    HideGameDiv();
    Popups.ShowPopup(new Point(200,200), new Point(50,50), loginFormHtml, 1, 2,true);
}
function ShowTosPopup()
{
    if (tosHtml == null)
    {
        tosHtml = AjaxClient.SendRequest("GetTerms", "");
    }
    HideGameDiv();
    Popups.ShowPopup(new Point(400,400), new Point(50,50), tosHtml, 1, 2,true);
}
function ShowHelpBalloon(nextToElemId,text)
{
    var containerTop;
    var elemTop;
    var balloonNewTop;
    if (balloon == null)
    {
        balloon = document.createElement("div");
        balloon.style.top = '100px';
        balloon.style.left = "520px";
    }
    if (text != "" & text != null)
    {
        var elem = document.getElementById(nextToElemId);
	 var balloonInnerDiv = document.createElement("div");
	 balloonInnerDiv.innerHTML = text;
        balloon.id = "infoBalloon";
        balloon.style.display = "block";
        balloon.style.backgroundColor = "#4466df";
        balloon.style.position = "absolute";
        balloon.style.width = "120px";
        balloon.style.border = "solid 1px #1145df";
	 
	 while (balloon.lastChild != null) 
	 {
	 	balloon.removeChild(balloon.lastChild );
	 }
        balloon.appendChild(balloonInnerDiv);
        elem.parentNode.appendChild(balloon);
        balloon.style.zIndex = 1999999;
        //Dhtml.FadeElement('infoBalloon',0,100,15);
        Dhtml.SetElemOpacity(balloon, 100);

        elemLoc = Core.GetElementAbsolutePosition(elem);
        containerLoc = Core.GetElementAbsolutePosition(document.getElementById('RegDiv'));
        elemTop = elemLoc.Y;
        containerTop = containerLoc.Y;
        balloonNewTop = elemTop - containerTop;
        Dhtml.TweenElement('infoBalloon', 520, balloonNewTop, 17);
    } else {
        Dhtml.SetElemOpacity(balloon, 0);
    }


}
function CheckUserName()
{
	var infoString   = null;
	var returnVal = true;
	var elem = document.getElementById('newusername');
            if (elem.value && elem.value != "")
            {
                $userNameAvail = AjaxClient.SendRequest("IsUserNameAvailable", "username="+elem.value);

                if ($userNameAvail == "1")
                {
                    infoString= "<strong>Username Accepted</strong>  - The entered user name is available";
			document.getElementById('SubmitRegistrationButton').disabled = false;
			returnVal = true;
                } else {
                    infoString= "The entered user name is <strong>not available</strong>";
			document.getElementById('SubmitRegistrationButton').disabled = true;
			returnVal = false;
			setTimeout("document.getElementById('newusername').focus()",10);
                }
            }
	if (infoString)
	{
        document.getElementById("ErrorInfo").innerHTML = infoString   ;
        document.getElementById("ErrorInfo").style.display = "block";
	}
	return returnVal;
}
function CheckLogin()
{
       document.getElementById("LoginInfo").innerHTML = "Checking login credentials...";
      	document.getElementById("LoginInfo").style.display = "block";
	var infoString   = null;
	var returnVal = true;
	var userelem = document.getElementById('loginuser');
	var passelem = document.getElementById('loginpassword');
            if (userelem && passelem)
            {
                $userNameAvail = AjaxClient.SendRequest("IsLoginCorrect", "username="+userelem.value+"&password="+passelem.value);

                if ($userNameAvail != "1")
                {
                    infoString= "Invalid Login.  If you've forgotten your password, <a href='javascript: void(0)' onclick='ShowResetPassword()'>click here to reset it</a>";
			returnVal = false;
                } else {
                    infoString= "Login verified.  Accessing website...";
			returnVal = true;
		  }

            }
	if (infoString)
	{
	       document.getElementById("LoginInfo").innerHTML = infoString;
       	document.getElementById("LoginInfo").style.display = "block";
	} else {
       	document.getElementById("LoginInfo").style.display = "none";
	}
	return returnVal;
}
function AjaxLogin()
{
    document.getElementById("LoginInfo").innerHTML = "Checking login credentials...";
   	document.getElementById("LoginInfo").style.display = "block";
	var infoString   = null;
	var returnVal = true;
	var userelem = document.getElementById('loginuser');
	var passelem = document.getElementById('loginpassword');
	var saveme = document.getElementById('saveme').value;
            if (userelem && passelem)
            {
                $loginok = AjaxClient.SendRequest("AjaxLogin", "username="+userelem.value+"&password="+passelem.value+"&cookieme="+saveme);

                if ($loginok == "1")
                {
                    infoString= "Login verified.  Accessing website...";
                    document.getElementById("LoginInfo").innerHTML = infoString;
                    document.getElementById("LoginInfo").style.display = "block";
                    //do other stuff to show we're logged in, like reraw the control panel'
			GetControlPanelHtml();
                   	document.getElementById("LoginInfo").style.display = "none";

			//rating box
			var mustLoginDiv = document.getElementById('MustBeLoggedInToRateGameDiv');
			var ratingDiv = document.getElementById('RateGameDiv');
			if (mustLoginDiv)
			{
				mustLoginDiv.style.display = 'none';
			}
			if (ratingDiv)
			{
				ratingDiv.style.color = '#000000';
				ratingDiv.style.display = 'block';
			}

                    Popups.ClosePopup();
                	returnVal = true;
			loggedin = 1;
		  } else if ($loginok == "2") {
                    infoString= "<strong>ACCOUNT NOT CONFIRMED. PLEASE CHECK YOUR EMAIL AND CLICK THE CONFIRM ACCOUNT LINK</strong>";
                    document.getElementById("LoginInfo").innerHTML = infoString;
                    document.getElementById("LoginInfo").style.display = "block";
        			returnVal = false;
			loggedin = 0;
                } else {
                    infoString= "Invalid Login.  If you've forgotten your password, <a href='javascript: void(0)' onclick='ShowResetPassword()'>click here to reset it</a>";
                    document.getElementById("LoginInfo").innerHTML = infoString;
                    document.getElementById("LoginInfo").style.display = "block";
        			returnVal = false;
			loggedin = 0;
                }
            }
	return returnVal;
}
function ResetPassword()
{
       $emailIsAvail = AjaxClient.SendRequest("IsEmailAvailable", "email="+document.getElementById("emailaddress").value);

	if ($emailIsAvail != "1")
	{
		AjaxClient.SendRequest("ResetPassword", "email="+document.getElementById("emailaddress").value);
       	document.getElementById("LoginInfo").innerHTML = "Your new password has been sent to you at: " + document.getElementById("emailaddress").value;
		document.getElementById('resetPasswordDiv').style.display = 'none';
	} else {
       	document.getElementById("LoginInfo").innerHTML = "That email address (" + document.getElementById("emailaddress").value + ") cannot be found in our system";
	}
      	document.getElementById("LoginInfo").style.display = "block";
}

function ShowResetPassword()
{
	document.getElementById('resetPasswordDiv').style.display = 'block';
}
function CheckEmail()
{
	var infoString   = null;
	var returnVal = true;
	var elem = document.getElementById('newemailaddress');
            if (elem.value && elem.value != "")
            {
                $userNameAvail = AjaxClient.SendRequest("IsEmailAvailable", "email="+elem.value);

                if ($userNameAvail == "1")
                {
                    infoString   = "<strong>Email address Accepted</strong>  - The entered email address has not been used before";
			document.getElementById('SubmitRegistrationButton').disabled = false;
			returnVal = true;
                } else {
                    infoString   = "<strong>A user with this email address already exists</strong>";
			document.getElementById('SubmitRegistrationButton').disabled = true;
			setTimeout("document.getElementById('newemailaddress').focus()",10);
			returnVal = false;
                }
            }
	if (infoString   )
	{
        document.getElementById("ErrorInfo").innerHTML = infoString   ;
        document.getElementById("ErrorInfo").style.display = "block";
	}
	return returnVal;
}

function validate (elemId)
{
    var elem;
    var returnVal = "";
    elem = document.getElementById(elemId);
    switch (elemId)
    {

        case "newusername":
            returnVal = "Username should be at least 3 characters long";
            break;
        case "newemailaddress":
//            if (!Dhtml.ValidateEmailAddress(elem.value))
//            {
                returnVal = "A <em>Valid</em> email address is required.  You will be sent a confirmation email at this address which is needed to activate your account.";
//            }
            break;
        case "newpasswordone":
            returnVal = "Your password should be at least 5 characters long and contain a number.  <em>case-sensitive</em>"
            break;
        case "newpasswordtwo":
            returnVal = "Please enter the password again"
            break;
        case "newgender":
            returnVal = "Please select your gender"
            break;
        case "newabout":
            returnVal = "Tell us a little bit about yourself.  <br /><br />Once you agree to our terms of service you can click the button below to complete your registration."
            break;
        case "newaccept":
            returnVal = "Please read our <a href='javascript: void(0)' onclick='ShowTosPopup()'>Terms and Privacy Policy</a> and then check this box";
            break;
    }
    return returnVal;
}
function submitRegistration()
{
    if (balloon)
    {
        Dhtml.SetElemOpacity(balloon, 0);
    }
    var errorMessage = "";
    var form = document.getElementById("formRegister");
    var username = document.getElementById("newusername");
    var email = document.getElementById("newemailaddress");
    var pass1 = document.getElementById("newpasswordone");
    var pass2 = document.getElementById("newpasswordtwo");
    var gender = document.getElementById("newgender");
    var accept = document.getElementById("newaccept");

    if(username.value == "") errorMessage += "Please input a username<br />";
    else
    {

        var specialChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_";
        for (var i = 0; i < username.value.length; i++)
        {
            if (specialChars.indexOf(username.value.charAt(i)) != -1)
            {
                errorMessage += "The username cannot have any special characters<br />";
            }
        }
    }

    if(email.value.indexOf("@") == -1) errorMessage += "Please input a valid email address<br />";
    if(pass1.value == "") errorMessage += "Please input a password<br />";
    else if(pass1.value != pass2.value) errorMessage += "Your passwords did not match<br />";
    if(gender.options[gender.selectedIndex].value == 0)  errorMessage += "Please select your gender<br />";

    if(!accept.checked) errorMessage += "Please accept our Terms and Privacy Policy<br />";


    if(errorMessage == "")
    {
	RegisterNewUser();
    }
    else
    {
        errorMessage = "Please correct the following errors:<br /><br />" + errorMessage;
        document.getElementById("ErrorInfo").innerHTML = errorMessage;
        document.getElementById("ErrorInfo").style.display = "block";
    }
}

function AjaxClient()
{
}

AjaxClient.SendRequest = function(call,vars)
{
    ajaxConnection = new AjaxHttpConnection(Core.AjaxHandlerUrl);
    vars = "call="+call+"&"+vars;
    return ajaxConnection.SendDataSync(vars);
}

AjaxClient.SendRequestAsync = function(call,vars,sendToken,callbackMethodName,callbackOwner)
{
    ajaxConnection = new AjaxHttpConnection(Core.AjaxHandlerUrl);
    vars = "call="+call+"&"+vars;
    ajaxConnection.SendDataAsync(sendToken,callbackMethodName,callbackOwner,vars);
}

AjaxClient.SendRequestAsyncPopup = function(call,vars,size,location,sizeMode,locationMode,modal,absFromTop)
{
    var ajaxConnection = new AjaxHttpConnection(Core.AjaxHandlerUrl);
    vars = "call="+call+"&"+vars;

    var token = new Array();
    token[0] = size;
    token[1] = location;
    token[2] = sizeMode;
    token[3] = locationMode;
    token[4] = modal;
    token[5] = absFromTop;
    ajaxConnection.SendDataAsync(token,"AjaxPopupResponse_handler",AjaxClient,vars);
}

AjaxClient.AjaxPopupResponse_handler = function(responseText,token)
{
    Popups.ShowPopup(token[0], token[1], responseText, token[2], token[3], token[4], token[5])
}


function AjaxConnection(serverUrl)
{
    this.ServerUrl = serverUrl;
    this.HttpClientAsync = null;

    function GetHttpObj()
    {
        var returnHttp;
        if (Core.Browser == Browsers.IE5)
        {
            returnHttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
        else if (Browsers.IsIeVariant(Core.Browser))
        {
            returnHttp = new ActiveXObject('Msxml2.XMLHTTP');
        }
        else
        {
            returnHttp = new XMLHttpRequest();
        }
        return returnHttp;
    }

    this.SendDataSync = function(getVariables)
    {
        if (typeof(getVariables) == "undefined" || getVariables == null)
        {
            getVariables = "";
        }
        var http;
        http = GetHttpObj();
		http.open('GET', this.ServerUrl + "?" + getVariables, false);
		http.send(null);
		return http.responseText;
    }

    this.SendDataAsync = function (sendToken,callbackMethodName,callbackOwner,getVariables)
    {
        if (typeof(callbackOwner) == "undefined" || callbackOwner == null)
        {
            callbackOwner = window;
        }
        if (typeof(getVariables) == "undefined" || getVariables == null)
        {
            getVariables = "";
        }
        var thisCopy = this;
        this.HttpClientAsync = GetHttpObj();
   		this.HttpClientAsync.onreadystatechange = function(){if (thisCopy.HttpClientAsync.readyState == 4) {callbackOwner[callbackMethodName](thisCopy.HttpClientAsync.responseText,sendToken,getVariables)}};
		this.HttpClientAsync.open('GET', this.ServerUrl + "?token=" + sendToken + "&" + getVariables, true);
		this.HttpClientAsync.send(null);
		return true;
    }

    return true;
}

function AjaxHttpConnection(serverUrl)
{
    AjaxConnection.call(this,serverUrl);
    return true;
}
AjaxHttpConnection.prototype = new AjaxConnection();
AjaxHttpConnection.prototype.constructor = AjaxHttpConnection;

function Browsers()
{
}
Browsers.IELT5 = 1;
Browsers.IE5 = 2;
Browsers.IEOTHER = 3;
Browsers.IE6 = 3;
Browsers.IE7 = 4;
Browsers.IEGT7 = 5;
Browsers.FF1 = 6;
Browsers.FF2 = 7;
Browsers.FF3 = 8;
Browsers.FFOTHER = 9;
Browsers.MOZILLA = 10;
Browsers.NETSCAPE = 11;
Browsers.OTHER = 20;

Browsers.IsIeVariant = function(browser)
{
    var returnVal = false;
    if (browser == Browsers.IE5 || Core.Browser == Browsers.IE6)
    {
        returnVal = true;
    }
    return returnVal;
}

function Core()
{
}
Core.Browser = Browsers.OTHER;
Core.Bind = function(toObject, methodName){
    return function(){toObject[methodName]()}
}
Core.SetCssLocation = function(element, x, y)
{
	element.style.left = x+'px';
	element.style.top = y+'px';

}
Core.AddHandler = function(obj, eventName,listenerFunction,duringCapture)
{
    if (Browsers.IsIeVariant(Core.Browser))
    {
        obj.attachEvent('on'+eventName,listenerFunction);
    } else {
        obj.addEventListener(eventName,listenerFunction,duringCapture);
    }
}
Core.RemoveEventListener = function(obj, eventName,listenerFunction,duringCapture)
{
    if (Browsers.IsIeVariant(Core.Browser))
    {
        obj.detachEvent(eventName,listenerFunction);
    } else {
        obj.removeEventListener(eventName,listenerFunction,duringCapture);
    }
}
Core.AddChildToElement = function(parentElement, newElement) {
  parentElement.appendChild(newElement);
}

Core.RemoveChildFromElement = function(parentElement, element) {
  parentElement.removeChild(element);
}

Core.GetElementAbsolutePosition = function(obj) {
	var curleft = 0;
	var curtop = 0;

	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while ((obj = obj.offsetParent));
	}
	return new Point(curleft,curtop);
}

Core.PreventDefaultEvent = function(e)
{
	if (!IE)
	{
		e.preventDefault();
	}
}

var userAgentString = navigator.userAgent.toLowerCase();
if (!window.ActiveXObject) {
    Core.Browser = Browsers.FFOTHER;
} else if (userAgentString.indexOf('msie 5') > -1) {
    Core.Browser = Browsers.IE5;
} else if (userAgentString.indexOf('msie') > -1) {
    Core.Browser = Browsers.IEOTHER;
} else if (userAgentString.indexOf('mozilla') > -1) {
    Core.Browser = Browsers.MOZILLA;
} else {
    Core.Browser = Browsers.IEOTHER;
}
Core.AjaxHandlerUrl = "http://www.blitzgamer.com/AjaxHandler.php";

function Dhtml()
{
    this._tweenTimer = null;
    this._fadeTimer = null;
}


Dhtml.FindElementPosition = function(element) {
	var curleft = 0;
	var curtop = 0;

	if (element.offsetParent) {
		do {
				curleft += element.offsetLeft;
				curtop += element.offsetTop;
			} while ((element = element.offsetParent));
	}
	return [curleft,curtop];
}

Dhtml.FadeElement = function(elem,currentOpacity,endOpacity,increment)
{
    clearTimeout(this._fadeTimer);
	var obj = document.getElementById(elem);
	if (Browsers.IsIeVariant(Core.Browser)) {
		//fade doesnt work in IE for now
		Dhtml.SetElemOpacity(obj,endOpacity);
	} else {
		Dhtml.SetElemOpacity(obj,currentOpacity);
		if (currentOpacity < endOpacity) {
			this._fadeTimer = setTimeout("Dhtml.FadeElement('"+elem+"',"+(currentOpacity+increment)+","+endOpacity+","+increment+")",20);
		}
	}
}

Dhtml.SetElemOpacity = function(obj,opacity)
{
        //throw("DhtmlEffects.SetElemOpacity doesnt work");
		obj.style.filter 		='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity='+opacity+')';
		obj.style.MozOpacity 	=.01*opacity;
}

Dhtml.TweenElement = function (elementid,x,y, increment)
{
    clearTimeout(this._tweenTimer);
    var element = document.getElementById(elementid);
    var curX = element.style.left.replace("px","");
    var curY = element.style.top.replace("px","");
    var newX = curX;
    var newY = curY;
    var moved = false;

    curX = parseInt(curX);
    curY = parseInt(curY);
    newX = parseInt(newX);
    newY = parseInt(newY);

    if (curX < x) {
        newX = curX + increment;
        if (newX > x)
        {
            newX = x;
        }
        moved = true;
    } else if (curX > x) {
        newX = curX - increment;
        if (newX < x)
        {
            newX = x;
        }
        moved = true;
    }
    if (curY < y) {
        newY = curY + increment;
        if (newY > y)
        {
            newY = y;
        }
        moved = true;
    } else if (curY > y) {
        newY = curY - increment;
        if (newY < y)
        {
            newY = y;
        }
        moved = true;
    }

    if (moved == true)
    {
        Dhtml.MoveElement(element,newX,newY);
        this._tweenTimer = setTimeout("Dhtml.TweenElement('"+elementid+"',"+x+","+y+","+increment+")",60);
    }
}
Dhtml.MoveElement = function(element, x, y)
{
	element.style.left = x+'px';
	element.style.top = y+'px';

}

Dhtml.GetCurrentYOffset = function()
{
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

Dhtml.GetPageSize = function()
{
	var pageWidth =  window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
	var pageHeight = window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
    return new Point(pageWidth,pageHeight);
}

Dhtml.ValidateEmailAddress = function (emailAddress)
{
  apos=emailAddress.indexOf("@");
  dotpos=emailAddress.lastIndexOf(".");
  return (apos>1&&((dotpos-apos)<2));
}

function Popups()
{
}
Popups.PopupDiv = null;
Popups.PopupDiv2 = null;
Popups.BalloonDiv = null;

//sizeMode & locationMode
//1 = Absolute in pixels
//2 = Relative percent, 0-100
//3 = Relative ratio, 0-1
Popups.ShowPopup = function(size,location,html,sizeMode,locationMode,modal,absTop)
{
    var ldiv;
    if (modal == null || typeof(modal) === 'undefined')
    {
        modal = true; //modal by default
    }

    if (Popups.PopupDiv == null)
    {
        Popups.PopupDiv = document.createElement("div");
        Popups.PopupDiv.style.position = "absolute";
        Core.AddChildToElement(document.body, Popups.PopupDiv);
        ldiv = Popups.PopupDiv;
    } else {
        if (Popups.PopupDiv.style.display == 'block')
        {
            if (Popups.PopupDiv2 == null)
            {
                Popups.PopupDiv2 = document.createElement("div");
                Popups.PopupDiv2.style.position = "absolute";
                Core.AddChildToElement(document.body, Popups.PopupDiv2);
            }
            ldiv = Popups.PopupDiv2;
        } else {
            ldiv = Popups.PopupDiv;
        }
    }

    var currentYOffset = Dhtml.GetCurrentYOffset();
    var pageSize = Dhtml.GetPageSize();
	var pageWidth =  pageSize.X;
	var pageHeight = pageSize.Y;
    var x = location.X;
    var y = location.Y;

    switch (locationMode)
    {
        case 1:
            if (Browsers.IsIeVariant(Core.Browser))
            {
                location.Y = location.Y + currentYOffset;
            }

            //if its gonna go off the right then pop left
            var popRight 	= location.X+Size.X;
            var popLeft 	= location.X;
            var popBottom 	= location.Y+Size.Y;
            x = x - 20;
            y = y - 20;
            if (popRight > pageWidth) {
                x = x - Size.X+40;
            }

            //if its gonna go off the left then pop right
            if (popLeft < 50) {
                x = x + Size.X -20;
            }

            //if its gonna go off the bottom then pop up
            if (popBottom > (pageHeight+currentYOffset)) {
                y = y - Size.Y+40;
            }

            //if its gonna go off the top then pop down
            if (y <= currentYOffset) {
                y = y + Size.Y-20;
            }

            //there are some cases where there just is not enough room to pop up/down/right/or left
            //in that case center it on the new x and/or y and hope for best
            popRight 	= x+Size.X;
            popLeft 	= x;
            popBottom 	= y+Size.Y;

            if (popRight > pageWidth) {
                x = x - (Size.X/2);
            }
            //if its gonna go off the left then pop right
            if (popLeft < 50) {
                x = x + (Size.X/2);
            }
            //if its gonna go off the bottom then pop up
            if (popBottom > (pageHeight+currentYOffset)) {
                y = y - (Size.Y/2);
            }
            //if its gonna go off the top then pop down
            if (y <= currentYOffset) {
                y = y + (Size.Y/2);
            }

            ldiv.style.left	= x+"px";
            ldiv.style.top 	= y+"px";
            break;
        case 2:
            //recalc the top based on screen height and passed percentage
            x = x * .01;
            y = y *.01;
        case 3:
            //need to add y offset for scroll position
            y = pageHeight * y;
            x = pageWidth * x;
            y = y + Dhtml.GetCurrentYOffset();
            break;
    }

	ldiv.innerHTML = html;
	ldiv.style.zIndex = 1001;
	ldiv.style.zIndex = 9999;
	ldiv.style.zIndex = 99999;
    var relativeWidth = size.X;
    var relativeHeight = size.Y;

    switch (sizeMode)
    {
        case 1:
            ldiv.style.width 		= size.X+"px";
            ldiv.style.height 		= size.Y+"px";

            if (locationMode > 1)
            {
                ldiv.style.left = (x-(size.X/2))+"px";
                ldiv.style.top = (y-(size.Y/2))+"px";
            }
            break;
        case 2:
            relativeWidth = size.X * .01;
            relativeHeight = size.Y * .01;
        case 3:
            ldiv.style.width 		= (relativeWidth * 100) + "%";
            ldiv.style.height 		= (relativeHeight * 100) + "%";
            if (locationMode > 1)
            {
                ldiv.style.left 		= (x-((pageWidth * relativeWidth)/2))+"px";
                ldiv.style.top = (y-((pageHeight * relativeHeight)/2))+"px";
            }
            break;
    }

	ldiv.style.zIndex = 1001;
	ldiv.style.zIndex = 9999;
	ldiv.style.zIndex = 99999;
    if (absTop)
        {
            ldiv.style.top = "150px";
        }
	ldiv.style.display = "block";
}


Popups.ClosePopup = function(hideDelay)
{
	if (Popups.PopupDiv2) {
	   	if (hideDelay) {
			setTimeout("Popups.ClosePopup(0)",hideDelay);
		} else {
        	Core.RemoveChildFromElement(document.body, Popups.PopupDiv2);
            Popups.PopupDiv2 = null;
		}
    } else if (Popups.PopupDiv) {
	   	if (hideDelay) {
			setTimeout("Popups.ClosePopup(0)",hideDelay);
		} else {
        	Core.RemoveChildFromElement(document.body, Popups.PopupDiv);
            Popups.PopupDiv = null;
       ShowGameDiv();
		}
	}
}


function Point(x,y)
{
    this.X = x;
    this.Y = y;
}
Point.FromPoint = function(pointObj)
{
    var newPoint = new Point(pointObj.X,pointObj.Y);
    return newPoint;
}