/*
	        __                   __    .__                     .___                        
	  _______/  |_ _____  _______ |  | __|__|  ____    ____    __| _/ ____    _____    ______
	 /  ___/\   __\\__  \ \_  __ \|  |/ /|  | /    \  / ___\  / __ | /  _ \  /     \  /  ___/
	 \___ \  |  |   / __ \_|  | \/|    < |  ||   |  \/ /_/  >/ /_/ |(  <_> )|  Y Y  \ \___ \ 
	/____  > |__|  (____  /|__|   |__|_ \|__||___|  /\___  / \____ | \____/ |__|_|  //____  >
	     \/             \/             \/         \//_____/       \/              \/      \/ 
	 ---------------------------------------------------------------

	 	Star Kingdoms - February 05, 2008 
	 	Website: Star Kingdoms
	 	BSG Software <http://webdev.bsgonlinegames.com/>
	 	(c) 2007

	 	License:

		The following javascript code is copyrighted and you
		DO NOT 	have permission to view, copy or alter it in
		any way.

*/

// contains:
// login.js  bubble.js  main.js

// BubbleClass -- Requires none
function BubbleClass(align) {
	
	// customizable variables
	this.zIndex = 99;
	this.backgroundColor = "#FFFFE1";
	this.color = "#000000";
	this.borderColor = "#314663";
	this.borderColor2 = "#000000";
	this.borderWidth = 2;
	this.borderStyle = "solid";
	this.padding = 8;
	this.paddingObject = 4; // distance away from object
	this.opacity = 99; // maximum bubble opacity - 99 to remove flicker glitch
	
	this.maxWidth = 365; // maximum width of bubble
	this.setWidth = 0; // auto
	this.offsetX = 0;
	this.offsetY = -10; // arrow will still be at same height
	this.fade = true; // fade in and out
	this.fadeFPS = 30;
	
	this.arrowImageDir = "images/bubble/"; // location of arrow image
	
	// required variables -- do not edit
	this.arrowWidth = 16;
	this.arrowHeight = 23;
	this.visible = false;
	this.container = this.getId("page");
	
	// default alignment
	if (align == null) {
		this.align = BubbleClass.RIGHT;
	} else {
		this.align = align;
	}
	this.align2 = BubbleClass.BOTTOM;
	
	
}

// Bubble Class Functions
BubbleClass.prototype.getId = function(name) {
	return document.getElementById(name);
};
BubbleClass.prototype.show = function(idname, s, align, align2) {
	
	// default alignment if nothing specified
	if (align == null) {
		// use default
	} else {
		this.align = align;
	}
	
	if (align2 == null) {
		// use default
	} else {
		this.align2 = align2;
	}
	
	var tid = this.ttDiv, aid = this.arrowDiv;
	var id = this.getId(idname);
	
	// find id location
	var idXY = this.getLocation(id);
	
	// find id width and height
	var idSize = this.getSize(id);
	
	this.setVisible(this.bubble, false);
	
	if (this.setWidth == 0) {
		tid.style.width = "auto";
		tid.style.height = "auto";
	}
	
	tid.innerHTML = s;
	
	var tidSize = this.getSize(tid);
	
	if (this.setWidth != 0) {
		tidSize[0] = this.setWidth;
	} else {
		if (parseInt(tidSize[0]) > this.maxWidth) {
			tid.style.width = this.maxWidth + "px";
			tidSize[0] = this.maxWidth;
		}
	}

	// set width of bubble
	this.bubble.style.width = (this.arrowWidth + tidSize[0]) + "px";
	this.bubble.style.height = tidSize[1] + "px";
	// get screen size
	var windowSize = this.windowSize();
	
	// position bubble
	var x = idXY[0], y = idXY[1] + this.offsetY; // coords of id
	var arrowX, arrowY, ttX, ttY;
	
	if (this.align == BubbleClass.LEFT) {
		// left of object
		
		// position tooltip div
		ttX = this.borderWidth;
		ttY = 0;
		
		// left of id and try to make it below
		x = x - (this.paddingObject + this.borderWidth + tidSize[0] + this.arrowWidth + this.offsetX);
		
		// position arrow
		arrowX = tidSize[0];
		arrowY = -this.offsetY;
		
		// in middle
		arrowY = arrowY + ((idSize[1] - this.arrowHeight) / 2);
		
	} else if (this.align == BubbleClass.RIGHT) {
		// right of object
		
		// position tooltip div
		ttX = this.arrowWidth - this.borderWidth;
		ttY = 0;
		
		// right of id and try to make it below
		x = x + this.paddingObject + this.borderWidth + idSize[0] + this.offsetX;
		
		// position arrow
		arrowX = 0;
		arrowY = -this.offsetY;
		
		// in middle
		arrowY = arrowY + ((idSize[1] - this.arrowHeight) / 2);
		
		if (this.align2 == BubbleClass.TOP) {
			// position higher than target
			y = y - tidSize[1] - this.offsetY + idSize[1];
			arrowY = tidSize[1] - this.arrowHeight - ((idSize[1] - this.arrowHeight) / 2);
		}
		
	} else if (this.align == BubbleClass.TOP) {
		// above object
		
	} else if (this.align == BubbleClass.BOTTOM) {
		// below object
		
	}
	
	// position ttDiv & arrow div
	tid.style.left = ttX + "px"; tid.style.top = ttY + "px";
	aid.style.left = arrowX + "px"; aid.style.top = arrowY + "px";
	
	// position bubble container
	this.bubble.style.left = x + "px"; this.bubble.style.top = y + "px";
	
	// set visible
	this.setVisible(this.bubble, true);
	
	
	// only fade in if visible was false before
	if (!this.visible) {
		
		// start fade in
		if (this.fade) {
			this.fadeIn(this, 1);
		}
		
	}
	
	this.visible = true;
	
};
BubbleClass.prototype.hide = function() {
	this.setVisible(this.bubble, false);
	this.visible = false;
};
BubbleClass.prototype.dispose = function() {
	// dispose of all elements
	try {
		this.container.removeChild(this.bubble);
	} catch (e) { }
};
BubbleClass.prototype.fadeIn = function(self, alpha) {
	alpha = alpha + (this.opacity / (self.fadeFPS / 4)); // fadeFPS / 2 will take .5s
	
	if (alpha > this.opacity) { alpha = this.opacity; }
	
	self.setOpacity(this.bubble, alpha);
	
	if (alpha != 99) {
		setTimeout(function(){self.fadeIn(self, alpha);}, 1000 / self.fadeFPS);
	}
};
BubbleClass.prototype.setVisible = function(id, boo) {
	if (boo) {
		id.style.visibility = "visible";
	} else {
		id.style.visibility = "hidden";
	}
};

BubbleClass.prototype.getLocation = function(id) {
	
	var x = 0, y = 0;
	if (id.offsetParent) {
		x = id.offsetLeft;
		y = id.offsetTop;
		while (id = id.offsetParent) {
			x += id.offsetLeft;
			y += id.offsetTop;
		}
	}	
	return [x, y];
	
};

BubbleClass.prototype.getSize = function(id) {
	var w = 0, h = 0;
	try {
		w = id.offsetWidth;
		h = id.offsetHeight;
	} catch (e) {
	}
	return new Array(w, h);
};

BubbleClass.prototype.windowSize = function () {
	// get innerWidth and Height and window
	var w = 0, h = 0;
	if(typeof(window.innerWidth) == 'number' ) {
	    // Not IE
	    w = window.innerWidth; h = window.innerHeight;
	} else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in compliant mode
		w = document.documentElement.clientWidth; h = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4
		w = document.body.clientWidth; h = document.body.clientHeight;
	}
	
	return new Array(w, h);
};
BubbleClass.prototype.setOpacity = function(id, alpha) {
	try { id.style.opacity = (alpha / 100); } catch (e) { }
	try { id.style.MozOpacity = (alpha / 100); } catch (e) { }
	try { id.style.KhtmlOpacity = (alpha / 100); } catch (e) { }
	try { id.style.filter = "alpha(opacity=" + alpha + ")"; } catch (e) { }
};
BubbleClass.prototype.init = function() {
	
	// create elements
	this.bubble = document.createElement("div");
	this.bubble.style.position = "absolute";
	this.bubble.style.width = (this.maxWidth) + "px";
	this.bubble.style.height = "100px";
	this.bubble.style.zIndex = this.zIndex;
	this.bubble.style.visibility = "hidden";
	this.bubble.style.cursor = "default";
	
	this.rel = document.createElement("div");
	this.rel.style.position = "relative";
	this.rel.style.width = "100%";
	this.rel.style.height = "100%";
	
	// text-tooltip div
	this.ttDiv = document.createElement("div");
	this.ttDiv.style.position = "absolute";
	this.ttDiv.style.top = "0px";
	this.ttDiv.style.left = "0px";
	this.ttDiv.style.padding = this.padding + "px";
	this.ttDiv.style.backgroundColor = this.backgroundColor;
	this.ttDiv.style.color = this.color;
	this.ttDiv.style.border = this.borderColor + " " + this.borderStyle + " " + this.borderWidth + "px";
	this.ttDiv.style.borderColor = this.borderColor + " " + this.borderColor + " " + this.borderColor2 + " " + this.borderColor2;
	
	// arrow div - draws completely in CSS
	this.arrowDiv = document.createElement("div");
	this.arrowDiv.style.position = "absolute";
	
	var holder = document.createElement("div");
	holder.style.position = "relative";
	holder.style.width = this.arrowWidth + "px";
	holder.style.height = this.arrowHeight + "px";
	holder.style.overflow = "hidden";
	
		// draw arrow point
	var l = Math.floor(this.arrowHeight / 2);
	for (var i=1;i<l;i++) {
		var tdiv = document.createElement("div");
		
		tdiv.style.position = "absolute";
		tdiv.style.top = (l - i) + "px";
		tdiv.style.left = (this.align == BubbleClass.RIGHT? i: this.arrowWidth - i) + "px";
		tdiv.style.width = "1px";
		tdiv.style.height = ((i * 2) - 1) + "px";
		tdiv.style.overflow = "hidden";
		
		tdiv.style.backgroundColor = this.backgroundColor;
		tdiv.style.borderBottom = this.borderColor + " solid 1px";
		tdiv.style.borderTop = this.borderColor + " solid 1px";
		
		holder.appendChild(tdiv);
	}
	
		// draw arrow box
	var tdiv = document.createElement("div");
	tdiv.style.position = "absolute";
	tdiv.style.top = "1px";
	tdiv.style.left = (this.align == BubbleClass.RIGHT? l: 0) + "px";
	tdiv.style.width = ((this.arrowWidth - l) + 1) + "px";
	tdiv.style.height = (this.arrowHeight - 4) + "px";
	tdiv.style.overflow = "hidden";
	tdiv.style.backgroundColor = this.backgroundColor;
	tdiv.style.borderBottom = this.borderColor + " solid 1px";
	tdiv.style.borderTop = this.borderColor + " solid 1px";
	holder.appendChild(tdiv);
	
		// append to arrowDiv
	this.arrowDiv.appendChild(holder);
	
	if (this.fade) {
		// start as completely faded
		this.setOpacity(this.bubble, 1);
	}
	
	// append elements to document
	this.rel.appendChild(this.ttDiv);
	this.rel.appendChild(this.arrowDiv);
	
	this.bubble.appendChild(this.rel);	
	
	try {
		this.container.appendChild(this.bubble);
	} catch(e) {
		// error creating bubble
	}
	
};
// static constants
BubbleClass.LEFT = 0; BubbleClass.RIGHT = 1; BubbleClass.TOP = 2; BubbleClass.BOTTOM = 3;

/* HashTable Class */
function HashTableClass() {
	this.hash = new Array();
	this.keys = new Array();
	this.i = 0;
}
HashTableClass.prototype.get = function(key) {
	if (typeof this.hash[key] == "undefined") {
		return false;
	} else {
		return this.hash[key];
	}
};
HashTableClass.prototype.put = function(key, value) {
	if (this.hash[key] == null) {
		this.keys[this.keys.length] = key;
	}
	this.hash[key] = value;
};
HashTableClass.prototype.moveFirst = function() {
	this.i = 0;
};
HashTableClass.prototype.getNext = function() {
	if (this.i < this.keys.length) {
		return this.keys[this.i++];
	} else {
		return false;
	}
};
/* End HashTable Class */


/* 	XMLget Class */
/* requires -None- */
/* last update: December 20, 2007 */
function XMLgetClass() {
	// load xmlhttp
	/**/
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this,xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				this.xmlhttp = false;
			}
		}
	@end @*/
	if (!this.xmlhttp && typeof XMLHttpRequest!="undefined") {
		this.xmlhttp = new XMLHttpRequest();
	}
	/**/
	
	this.iXML = 0;
	if (this.xmlhttp == null) {
		this.supported = false;
	} else {
		this.supported = true;
	}
	
}

	// Functions
XMLgetClass.prototype.openXML = function(id, url) {
	var xml = this.xmlhttp;
		
	if (!this.supported) {
		return true;
	} else {
	
		var c; if (url.indexOf("?") == -1) { c = "?"; } else { c = "&"; }
		url = url + c + "a=" + escape(this.iXML++);
		
		id.innerHTML = "Loading... " + url;
		xml.open("GET", url);
	    xml.onreadystatechange = function() {
		    if (xml.readyState == 4 && xml.status == 200) {
				buffer = xml.responseText;
				id.innerHTML = buffer;
			}
		};
		xml.send(null);
		
		return false;
		
	}		
};
XMLgetClass.prototype.returnXML = function(self, f, loadingid, url) {

	var xml = this.xmlhttp;
	if (!this.supported) {
		return true;
	} else {
	
		var c; if (url.indexOf("?") == -1) { c = "?"; } else { c = "&"; }
		url = url + c + "a=" + escape(this.iXML++);
		
		if (loadingid == "" || loadingid == null) {
		} else {
			loadingid.innerHTML = "Loading... " + url;
		}
		
		xml.open("GET", url);
		xml.onreadystatechange = function() {
		    if (xml.readyState == 4 && xml.status == 200) {
				buffer = xml.responseText;
				f(self, buffer);
			}
		};
		xml.send(null);
		
		return false;
					
	}	
		
};

XMLgetClass.prototype.getObject = function () {
	return this.xmlhttp;
};

/* End XMLget Class */





/* Main Class */
/* requires XMLgetClass */
function MainClass() {
	
	this.XMLget = new XMLgetClass();
	
	this.h = 0;
	this.m = 0;
	this.s = 0;
	
	this.universe = 'terranova';
	
	this.monthsArray = Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	
}

	// Copied Functions
MainClass.prototype.getId = function(idname, scope) {
	if (scope != null) {
		return scope.document.getElementById(idname);
	} else {
		return document.getElementById(idname);
	}
};
MainClass.prototype.time = function() {
	// get current unix timestamp (seconds)
	var tdate = new Date();
	return parseInt(tdate.getTime() / 1000);
};
MainClass.prototype.trim = function(s) {
	return s.replace(/^\s+|\s+$/g,"");
};


	// Functions
MainClass.prototype.getId = function(idname, scope) {
	if (scope != null) {
		return scope.document.getElementById(idname);
	} else {
		return document.getElementById(idname);
	}
};
MainClass.prototype.time = function() {
	// get current unix timestamp (seconds)
	var tdate = new Date();
	return tdate.getTime();
};
MainClass.prototype.trim = function(s) {
	return s.replace(/^\s+|\s+$/g,"");
};
MainClass.prototype.strpad = function(s, i, padchr, padleft) {
	if (padchr == null || padchr == "") { padchr = " "; }
	
	while (s.length < i) {
		if (padleft) {
			s = padchr + s;
		} else {
			s = s + padchr;
		}
	}
	return s;
};
MainClass.prototype.startClock = function(time, h) {
	
	var self = this;
	
	var d = new Date();	
	var offh = (h - d.getHours());
	var offms = time - d.getTime();
	
	
	
	this.tickClock(self, offms, offh);
	
};
MainClass.prototype.tickClock = function(self, offms, offh) {
	
	var time = self.time() + offms; // add seconds
	var d = new Date(time + (offh * 3600000)); // add hours
	
	var gid = self.getId("time"), sid = self.getId("stardate");
	
	// gregorian
	gid.innerHTML = "<b>Gregorian:</b> " + self.monthsArray[d.getMonth()] + " " + d.getDate() + ", " + (d.getFullYear()) + " - " + 
		self.strpad(new String(d.getHours()), 2, "0", true) + ":" + 
		self.strpad(new String(d.getMinutes()), 2, "0", true) + ":" + 
		self.strpad(new String(d.getSeconds()), 2, "0", true);
	
	// stardate
	
	var stime = new String(((time - 630720000000) / 86400000));
	var s = stime.split(".");
	s[1] = self.strpad(s[1].substr(0, 5), 5, "0");
	sid.innerHTML = "<b>Stardate:</b> " + s[0] + "." + s[1];
	
	setTimeout(function(){self.tickClock(self, offms, offh);}, 200);
	
};
MainClass.prototype.setVisible = function(id, vis) {
	if (vis) {
		id.style.visibility="visible";
	} else {
		id.style.visibility= "hidden";
	}
};
MainClass.prototype.setOpacity = function(id, alpha) {
	try { id.style.opacity = (alpha / 100); } catch (e) { }
	try { id.style.MozOpacity = (alpha / 100); } catch (e) { }
	try { id.style.KhtmlOpacity = (alpha / 100); } catch (e) { }
	try { id.style.filter = "alpha(opacity=" + alpha + ")"; } catch (e) { }
};
MainClass.prototype.focus = function(id) {
	// highlight class
	id.className = id.className + " focus";
};
MainClass.prototype.blur = function(id) {
	// remove highlight class
	id.className = id.className.replace(/focus/, "");
};
MainClass.prototype.getSize = function(id) {
	var Size = new SizeClass(0, 0);
	try {
		Size.width = id.offsetWidth;
		Size.height = id.offsetHeight;
	} catch (e) {
	}
	return Size;
};
MainClass.prototype.windowSize = function () {
	// get innerWidth and Height of window
	var w = 0, h = 0;
	if(typeof(window.innerWidth) == 'number' ) {
	    // Not IE
	    w = window.innerWidth; h = window.innerHeight;
	} else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in compliant mode
		w = document.documentElement.clientWidth; h = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4
		w = document.body.clientWidth; h = document.body.clientHeight;
	}
	
	var size = new SizeClass(w, h);
	return size;
};
MainClass.prototype.screenSize = function() {
	// get screen resolution
	var size = new SizeClass(window.screen.width, window.screen.height);
	return size;
};
function SizeClass(width, height) {
	this.width = width;
	this.height = height;
}
MainClass.prototype.startTicker = function (id, tickerArray, interval) {
	id.innerHTML = "&nbsp;";
	this.setVisible(id, true);
	var self = this;
	setTimeout(function(){self.updateTicker(self, id, interval, 0, tickerArray, 0)}, interval);
};
MainClass.prototype.updateTicker = function(self, id, interval, ci, tickerArray, ti) {
	
	var currentText = tickerArray[ti] + " ";
	
	if (ci == currentText.length) {
		// already at max
		id.innerHTML = "&nbsp;";
	} else {
		ci++;
		
		// html entity
		if (currentText.substring(ci - 1, ci) == "&") {
			// loop until ; found
			while (currentText.substring(ci - 1, ci) != ";" && ci < currentText.length) {
				ci++;
			}
		}		
		id.innerHTML = currentText.substring(0, ci - 1) + "<b>" + currentText.substring(ci - 1, ci) + "</b>";
	}
	if (ci == currentText.length) {
		// longer break -- whole word is shown
		
		// cycle ti
		if (++ti >= tickerArray.length) { ti = 0; } // back to beginning of array
		ci = 0;
		setTimeout(function(){self.updateTicker(self, id, interval, ci, tickerArray, ti)}, interval * 50);
	} else {
		setTimeout(function(){self.updateTicker(self, id, interval, ci, tickerArray, ti)}, interval);
	}
	
};
MainClass.prototype.popup = function(url, width, height, name) {
	if (name == null || name == "") {
		name = new Date().getTime();
	}
	
	window.open(url, name, "width=" + width + ",height=" + height + ",scrollbars=yes");
	return false;
};
MainClass.prototype.link = function(url) {
	
	window.open(url, "_parent");
	return false;
	
};
MainClass.prototype.premiumForm = function() {

	var email = this.trim(this.getId("email").value);
	
	BubbleRight.hide();
	
	if (email == "") {
		BubbleRight.show("email", "Your E-Mail Address is blank.");
		return false;
	} else if (email != "" && (email.indexOf("@") == -1 || email.indexOf(".") == -1)) {
		BubbleRight.show("email", "Please enter in a valid E-Mail Address. eg. yourname@domain.com");
		return false;
	}
	
	return true;
		
};



/* Login Class */
/* requires XMLgetClass, MainClass */
function LoginClass() {
	
	this.XMLget = new XMLgetClass();
	
}
LoginClass.prototype.submit = function() {
	
	// complete login process
	var bsgid = Main.getId("account"), password = Main.getId("password"), remember = Main.getId("remember");
	
	// hide any messages
	BubbleRight.hide();
	
	if (Main.trim(bsgid.value) != "" && password.value != "") {
		
		// show loading
		Main.setVisible(Main.getId("loading"), true);
		
		var url = "/login/?bsgid=" + escape(Main.trim(bsgid.value)) + "&password=" + escape(password.value) + "&remember=" + (remember.checked?1:0);
		return this.XMLget.returnXML(this, this.returnLogin, null, url);
	} else {
	
		BubbleRight.show("loading", "BSGid or password is blank. Please complete the form before pressing \"Login to Kingdom\".");
		
		return false;
			
	}
	

};
LoginClass.prototype.returnLogin = function(self, buffer) {
	
	// hide loading
	Main.setVisible(Main.getId("loading"), false);
	
	result = buffer.split("\r");
	
	if (result[0] == "1") {
		// good - show list
		
		// update content
		Main.getId("logindiv").className = Main.getId("logindiv").className + " list";
		Main.getId("logindiv").innerHTML = buffer.substr(1, buffer.length);		
		
		// update link
		Main.getId("loginSignup").href = "/login/?form=login";
		Main.getId("loginSignup").innerHTML = "Sign in as a different BSGid.";
		
	} else if (result[0] == "302") {
		// direct link
		
		Main.link(result[1]);
		
	} else {
		// bad
		
		Main.getId("password").value = "";
		Main.getId("password").focus();
		
		BubbleRight.show("loading", result[1]);
		
	}
	
};



// Load classes
Main = new MainClass();
Login = new LoginClass();

var BubbleLeft;
var bubbleRight;

// Init script
function pageInitialize(tickerArray, time, h, universe) {

	// set universe
	Main.universe = universe;
	
	// initialize bubbles
	BubbleLeft = new BubbleClass(BubbleClass.LEFT);
	BubbleLeft.init();
	
	BubbleRight = new BubbleClass(BubbleClass.RIGHT);
	BubbleRight.setWidth = BubbleRight.maxWidth;
	BubbleRight.init();
	
	// start ticker
	Main.startTicker(Main.getId("ticker"), tickerArray, 100);
	
	// start clock
	Main.startClock(time, h);
	
	// page resize event
	pageResize();
	
	if(window.addEventListener) {
		window.addEventListener("resize", pageResize, true);
	} else {
		window.attachEvent('onresize', pageResize);
	}
	
}
function pageResize() {
	var size = Main.getSize(document.body), statusid = Main.getId("status");
	statusid.style.bottom = "";
	statusid.style.top = (size.height - 24) + "px";
}


