/*
index.js
Kingdm index Javascript library
Written by Adam Dougherty
 */



/*
Login Handler Object
*/
function Login() {  
  	var ajax = null;
  	this.form = null;
	var enabled = true;
  	this.button = null;
	this.inputVis = 0;
	this.oldHTML;
	this.iconCount;
	this.midIcon;
	this.newLogin = 0;
	this.iconState = 1;
	this.defaultWidth;
	this.loginerror = null;
	
	var me = this;
	
  	me.init = function() {
		me.loginerror = document.getElementById('login_error');
		me.iconCount = ((document.getElementById('holder').getElementsByTagName("td").length)-1);
		me.midIcon = ((me.iconCount / 2) + .5);
		var topMsg = document.getElementById("welMsg").innerHTML;
		if(topMsg == "Welcome to kingdm"){
			me.newLogin = 1;	
		}else if(topMsg == "Logged in"){
			me.newLogin = 2;
		}

  	};
    
  	me.keyup = function(e) {
		if(me.enabled == false){return false;}
		var eTarg;
    	if (!e)
		{
      		e = window.event;
    	}
		
		if(e.target){
			eTarg = e.target;	
		}else{
			eTarg = e.srcElement;
		}
		
    	if (e.keyCode == 13) {
			if (eTarg.parentNode.id == "loginput_form"){
				if(me.evalFormFieldFill(eTarg.parentNode))
				{
					me.submitData(eTarg.parentNode);
				}
			}
      	}
  	};
	

  
	me.onclick = function(e) {
		if(me.enabled == false){return false;}
		var eTarg;
    	if (!e) {
      		e = window.event;
    	}
		
		if(e.target){
			eTarg = e.target;	
		}else{
			eTarg = e.srcElement;
		}
		
		var eID = eTarg.id;
		var ePar = eTarg.parentNode.id;
		var eCls = eTarg.className;

			if(eCls == "icon"){
				me.hideAndSlideIcons(0, eTarg);
				switch(ePar)
				{
					case "icon_gloss1":
						me.showStatusPrompt("Heading to forums...");
						me.goURL("http://www.dotagaming.net/forums",1);
						break;
					case "icon_gloss2":
						if(document.getElementById('horizon').style.zIndex != -1)
						{
							opacity(document.getElementById('horizon').id, 100, 0, 150);
							document.getElementById('horizon').style.zIndex = -1;
							setTimeout("opacity('downloadDiv', 0, 100, 150)", 200);
						}
						break;
					case "icon_gloss3":
						me.showStatusPrompt("Heading to DGN Prime...");
						me.goURL("http://www.dgnprime.com",1);
						break;
				}
			}else{
				if(eTarg.className != "downloadArea" && eTarg.parentNode.className != "downloadArea" && document.getElementById('horizon').style.zIndex == -1){
					document.getElementById('horizon').style.zIndex = 99;
					opacity(document.getElementById('horizon').id, 0, 100, 150);
					setTimeout("opacity('downloadDiv', 100, 0, 150)", 200);
					me.hideAndSlideIcons(1);
					me.showStatusPrompt("");
				}
			}

  	};
	
	me.mousedown = function(e, upDown) {
		if(me.enabled == false){return false;}
		var eCls;
		var eTarg;
    	if (!e) {
      		e = window.event;
    	}
		
		if(e.target){
			eTarg = e.target;	
		}else{
			eTarg = e.srcElement;
		}
		
		eCls = eTarg.className;
		if(eCls == "icon"){
			iconClick(eTarg.id, 1);
		}
  	};
	
	me.mouseup = function(e) {
		if(me.enabled == false){return false;}
		var eCls;
		var eTarg;
    	if (!e) {
      		e = window.event;
    	}
		
		if(e.target){
			eTarg = e.target;	
		}else{
			eTarg = e.srcElement;
		}
		
		eCls = eTarg.className;
		
		if(eCls == "icon"){
			iconClick(eTarg.id, 0);
		}	
  	};
	
	me.evalFormFieldFill = function(docForm) {
		for (i = 0; i < docForm.elements.length; i++) {
			formElem = docForm.elements[i];
			if(formElem.type == "text" || formElem.type == "password" || formElem.type == "textarea"){
				if(formElem.value.length == 0){return false;}
			}
		}
		return true;
	}
  
  
	me.submitData = function(whatForm) {
    	var postData = '';
		me.ajax = new Ajax();
    	postData = formData2QueryString(whatForm);
    	me.ajax.doPost('index.php?route=login', postData, me.handleLoginResp);
    	me.showStatusPrompt("Logging in...", 0);
  	};
	
	me.showStatusPrompt = function(str, isError) {
		var promptBox = document.getElementById('loginput_prompt');
		promptBox.innerHTML=str;

  	};

	me.handleLoginResp = function(str) {
		if(str == "--gl"){
			me.goHome();
		}else{
			me.showStatusPrompt(str, 0);
			//me.form.loginput_pword.value="";
		}
  	};
	
	me.goURL = function(url, fade)
	{
		me.enabled = false;
		if(navigator.appCodeName=="Mozilla"){
			document.cookie = "refresh=1";
			window.onunload = function(){};
		}
		if(fade)opacity("mainbody", 100, 0, 450);
		setTimeout("window.location = '" + url + "'", 550);
		setTimeout("Login.enabled = true", 545);
	};
	
	me.showField = function(show, id){
		var visi = me.inputVis;

		
		if(show == 1 && visi== 0){
			me.inputVis = 1
			opacity(id, 0, 100, 150);
		}else if(show == 0 && visi == 1){
			me.inputVis = 0;
			opacity(id, 100, 0, 150);
			//document.getElementById('login_infobox').style.visibility='hidden';
			//document.getElementById('login_error').style.visibility='hidden';
		}
	};
	
	me.hideAndSlideIcons = function(showHide, iconTarget) {
		var safeIcon = document.getElementById('icon_main' + me.midIcon);
		var userForm = document.getElementById('loginput_username');
		var formDiv = document.getElementById('loginput_main');
		if(showHide == me.iconState){return false;}
		
		
		if(showHide == 1){
			safeIcon.innerHTML = me.oldHTML;
			me.oldHTML = null;
			safeIcon.style.width = me.defaultWidth;
		}else{
			me.defaultWidth = safeIcon.style.width;
			me.oldHTML = safeIcon.innerHTML;
			var mainTarg = iconTarget.parentNode.parentNode.parentNode;
			safeIcon.innerHTML = mainTarg.innerHTML;
			safeIcon.style.width = "120px";

		}
		me.iconState = showHide;
		
		var b = 0;
		var childrenCol = safeIcon.parentNode.childNodes
		
		for (b = 0; b < childrenCol.length; b++) {
			iconElem = childrenCol[b];
			if(iconElem.className == "icon_main" && iconElem.id != "icon_main" + me.midIcon){
				if(showHide == 0 || showHide == 2){
					opacity(iconElem.id, 100, 0, 150);
					iconElem.style.width = "0px";
				}else{
					opacity(iconElem.id, 0, 100, 150);
					iconElem.style.width = "120px";
				}
			}
		}
		
		
	};
  

};

Login = new Login();

/*
Login Icon Basic Functions
*/
function loadIMG() {
	gloss2 = new Image;
	gloss2.src = "images/iconGlossClick.png";
}

function iconClick(id, downUp) {
	if(downUp == 1){
		document.getElementById(id).src = "images/iconGlossClick.png";
	}else{
		document.getElementById(id).src = "images/iconGloss.png";
	}
}


function iconRollover(id, onOff) {
	var fixedId = "icon_border" + id;
	if(onOff == 1){
		changeOpac(100, fixedId);
	}else{
		changeOpac(80, fixedId);
	}
}


/*
Event inits
*/
window.onload = function() {
	//location.reload(true);
	Login.init();
	loadIMG();
	
	if(navigator.appCodeName=="Mozilla" && document.cookie.indexOf("refresh") > 0)
	{
		document.cookie = "refresh=0;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		location.reload(true);
	}
};

window.onunload = function(){
	document.cookie = "refresh=0;expires=Thu, 01-Jan-1970 00:00:01 GMT";
}; 

document.onkeyup = Login.keyup;
document.onmousedown = Login.mousedown;
document.onmouseup = Login.mouseup;
document.onclick = Login.onclick;
document.onsubmit = function(){return false;}
