var GlobalState = {submitCancelled : false}

function BinaryAddiction() {
	this.ME = "David Smith";
	this.CO = "binary_addiction";
	this.EMAIL = "davidATbinaryaddictionDOTnet";
	this.URL = "www.binaryaddiction.net";
	this.BACKDROP_DIV = "baBackdrop";
	this.INFO_DIV = "baInfo";
}

BinaryAddiction.prototype.formatEmail = function() {
	var email = this.EMAIL;
	email = email.replace(/AT/,"@");
	email = email.replace(/DOT/,".");
	return email;
};

BinaryAddiction.prototype.createBackdropNode = function() {
	if(!document.createElement) return null;
	var backdropDiv = document.createElement("div");
	backdropDiv.setAttribute("id",this.BACKDROP_DIV);
	backdropDiv.onclick = function() {
		var ba = new BinaryAddiction();
		ba.removeInfo();
		delete ba;
	};
	return backdropDiv;
};

BinaryAddiction.prototype.createInfoNode = function() {
	if(!document.createElement || !document.createTextNode) return null;
	var baDiv = document.createElement("div");
	baDiv.setAttribute("id",this.INFO_DIV);
	baDiv.onclick = function() {
		var ba = new BinaryAddiction();
		ba.removeInfo();
		delete ba;
	};
	var infoTitle = document.createElement("h1");
	infoTitle.setAttribute("id","baInfoTitle");
	var title = document.createTextNode("Designed and Implemented By:");
	var pCo = document.createElement("p");
	var pCoTxt = document.createTextNode(this.CO);
	pCo.appendChild(pCoTxt);
	var pMe = document.createElement("p");
	pMe.setAttribute("id", "myba_info");
	var pMeTxt = document.createTextNode(this.ME);
	var br = document.createElement("br");
	var pEmailTxt = document.createTextNode(this.formatEmail());
	pMe.appendChild(pMeTxt);
	pMe.appendChild(br);
	pMe.appendChild(pEmailTxt);
	var pClose = document.createElement("p");
	var em = document.createElement("em");
	var emTxt = document.createTextNode("click anywhere to close");
	em.appendChild(emTxt);
	pClose.appendChild(em);
	infoTitle.appendChild(title);
	baDiv.appendChild(infoTitle);
	baDiv.appendChild(pCo);
	baDiv.appendChild(pMe);
	baDiv.appendChild(pClose);
	return baDiv;
};

BinaryAddiction.prototype.getBackdropHeight = function() {
	var cHeight = 0;
	var sHeight = 0;
	if(self.innerHeight) {
		cHeight = self.innerHeight;
		sHeight = document.getElementById("main_wrap").scrollHeight;
	}
	else if(document.documentElement && document.documentElement.clientHeight) {
		cHeight = document.documentElement.clientHeight;
		sHeight = document.documentElement.scrollHeight;
			
	}
	else {
		cHeight = document.body.clientHeight;
		sHeight = document.body.scrollHeight;
	}
	return sHeight > cHeight ? sHeight : cHeight;
};

BinaryAddiction.prototype.hideInfo = function() {
	var success = false;
	if(document.getElementById) {
		var backdropDiv = document.getElementById(this.BACKDROP_DIV);
		var infoDiv = document.getElementById(this.INFO_DIV);
		if(backdropDiv !== null && infoDiv !== null) {
			backdropDiv.style.display = "none";
			infoDiv.style.display = "none";
			success = true;
		}
	}
	return success;
};

BinaryAddiction.prototype.installAddictionNode = function(baNode) {
	var success = false;
	if(document.getElementById) {
		var pageWrapperDiv = document.getElementById("main_wrap");
		if(pageWrapperDiv) {
			pageWrapperDiv.appendChild(baNode);
			success = true;
		}
	}
	return false;
};

BinaryAddiction.prototype.removeInfo = function() {
	var success = false;
	if(document.getElementById) {
		var pageWrapperDiv = document.getElementById("main_wrap");
		var backdropDiv = document.getElementById(this.BACKDROP_DIV);
		var infoDiv = document.getElementById(this.INFO_DIV);
		if(backdropDiv !== null && pageWrapperDiv !== null) {
			this.hideInfo();
			pageWrapperDiv.removeChild(backdropDiv);
			pageWrapperDiv.removeChild(infoDiv);
			success = true;
		}
	}
	return success;
};

BinaryAddiction.prototype.showInfo = function() {
	var success = false;
	if(document.getElementById) {
		var backdropDiv = document.getElementById(this.BACKDROP_DIV);
		var infoDiv = document.getElementById(this.INFO_DIV);
		if(backdropDiv !== null && infoDiv !== null) {
			backdropDiv.style.display = "block";
			backdropDiv.style.height = this.getBackdropHeight()+"px";
			infoDiv.style.display = "block";
			success = true;
		}
	}
	return success;
};

BinaryAddiction.prototype.displayInfo = function() {
	var backdrop = this.createBackdropNode();
	var info = this.createInfoNode();
	if(backdrop !== null && info !== null) {
		this.installAddictionNode(backdrop);
		this.installAddictionNode(info);
		this.showInfo();
	}
};

function SimartCookies() { this.cookieName = "cTab"; }

SimartCookies.prototype.getCookieValue = function(cName) {
	var cValue = "";
	var cookie = document.cookie;
	if(cookie.length > 0) {
		cValStart = cookie.indexOf(cName + "=");
		if(cValStart != -1) {
			cValStart = cValStart + cName.length + 1;
			cValEnd = cookie.indexOf(";",cValStart);
			if(cValEnd == -1) cValEnd = cookie.length;
			cValue = unescape(cookie.substring(cValStart,cValEnd));
		}
	}
	return cValue;
};

SimartCookies.prototype.setCookie = function(cName,cVal,cExp,cDomain) {
	if(cDomain == null || cDomain == "") cDomain = "/";
	if(cExp == null || cExp == "") cExp = 1; // one day default
	var expDate = new Date();
	expDate.setDate(expDate.getDate() + cExp);
	var fullCookie = cName + "=" + escape(cVal) + 
	 				 ";expires=" + expDate.toGMTString() +
	 				 ";path="+cDomain;
	document.cookie = fullCookie;
};

SimartCookies.prototype.isTabStateCurrent = function(tabClicked) {
	var success = false;
	if(document.getElementsByTagName) {
		var cState = this.getCookieValue(this.cookieName);
		var tabHref = tabClicked.href;
		success = tabHref.indexOf(cState+".") != -1;
	}
	return success;
};

SimartCookies.prototype.setTabState = function() {
	var tabName = "";
	if(document.getElementsByTagName) {
		var body = document.getElementsByTagName("body")[0];
		tabName = body.getAttribute("id");
	}
	this.setCookie(this.cookieName,tabName);
};

function SimartGuests() { 
	this.MAX_COMMENT_LENGTH = 300;
}

SimartGuests.prototype.resetCommentForm = function() {
	var cForm = document.forms[0];
	cForm.gname.value = "";
	cForm.gname.style.borderColor = "#D4E6EC";
	cForm.gname.style.borderWidth = "1px";
	cForm.gemail.value = "";
	cForm.gemail.style.borderColor = "#D4E6EC";
	cForm.gemail.style.borderWidth = "1px";
	cForm.gremark.value = "";
        cForm.captcha_entry.value = "";
        cForm.captcha_entry.style.borderColor = "#D4E6EC";
        cForm.captcha_entry.style.borderWidth = "1px";
	if(document.getElementById) {
		var charCntVal = document.getElementById("char_cnt_val");
		charCntVal.firstChild.nodeValue = 0;
	}
};

SimartGuests.prototype.cancelSubmit = function() {
	GlobalState.submitCancelled = true;
};

SimartGuests.prototype.hideCommentForm = function() {
        $("#gbook_entry").slideUp();
        this.resetCommentForm();
};

SimartGuests.prototype.initActions = function() {
        $("#makeComment").click(function() {
            $("#gbook_entry").slideDown("normal", function() {
                $("#makeComment").blur();
                $("#gname").focus();
            });
            return false;
        });
        $("#captcha_regen").click(function() {
            $("#captcha").attr({ src: "captcha_image.php?"+Math.random() });
            return false;
        });
        // Kill the cached captcha
        $("#captcha_regen").click();
};

SimartGuests.prototype.enforceCommentRange = function(textarea) {
	if(document.getElementById) {
		var charCntVal = document.getElementById("char_cnt_val");
		var charCnt = parseInt(charCntVal.firstChild.nodeValue);
		if(charCnt >= this.MAX_COMMENT_LENGTH) {
			charCntVal.firstChild.nodeValue = this.MAX_COMMENT_LENGTH;
			textarea.value = textarea.value.substring(0,this.MAX_COMMENT_LENGTH);
		}
		else {
			var textLen = textarea.value === "" ? 0 : textarea.value.length;
			charCntVal.firstChild.nodeValue = textLen;
		}
	}
};

SimartGuests.prototype.containsData = function(field) {
	field.style.borderWidth = "1px";
	field.style.borderColor = "#D4E6EC";
	return field.value.length > 0 && field.value !== field.defaultValue;
};

SimartGuests.prototype.validEmail = function(field) {
	field.style.borderWidth = "1px";
	field.style.borderColor = "#D4E6EC";
	return field.value.indexOf("@") !== -1 && field.value.indexOf(".") !== -1;
};

SimartGuests.prototype.notifyUserOfError = function(field) {
	field.style.borderColor = "red";
	field.style.borderWidth = "2px";
};

SimartGuests.prototype.validateCommentForm = function() {
	var cForm = document.forms[0];
	var nameField = cForm.gname;
	var valid = true;
	var pass = this.containsData(nameField);
	if(!pass) {
		this.notifyUserOfError(nameField);
		valid = false;
	}
	var emailField = cForm.gemail;
	pass = this.validEmail(emailField);
	if(!pass) {
		this.notifyUserOfError(emailField);
		valid = false;
	}
        var captchaEntryField = cForm.captcha_entry;
        pass = this.containsData(captchaEntryField);
        if(!pass) {
            this.notifyUserOfError(captchaEntryField);
            valid = false;
        }
	return valid;
};

SimartGuests.prototype.initCommentEntry = function() {
	if(document.getElementById) {
		var commentTA = document.getElementById("gremark");
		commentTA.onkeydown = function(e) {
			var guests = new SimartGuests();
			guests.enforceCommentRange(this);
			delete guests;
			return true;
		};
		commentTA.onkeyup = function(e) {
			var guests = new SimartGuests();
			guests.enforceCommentRange(this);
			delete guests;
			return true;
		};
		var nmbutton = document.getElementById("gnmbutton");
		nmbutton.onclick = function() {
			GlobalState.submitCancelled = true;
			return true;
		};
		var cForm = document.getElementById("gbook_entry");
		cForm.onsubmit = function() {
			var submitForm = true;
			var guests = new SimartGuests();
			if(GlobalState.submitCancelled) {
				guests.hideCommentForm();
				GlobalState.submitCancelled = false;
				submitForm = false;
			}
			else {
				submitForm = guests.validateCommentForm();
			}
			delete guests;
			return submitForm;
		};
	}
};

function initMainNav() {
	if(document.getElementById && document.getElementsByTagName) {
		var mainNav = document.getElementById("navsite");
		var footerNav = document.getElementById("footer_nav");
		var navLinks = mainNav.getElementsByTagName("a");
		var fNavLinks = footerNav.getElementsByTagName("a");
		for(var i = 0; i < navLinks.length; i++) {
			navLinks[i].onclick = function() {
				var cookies = new SimartCookies();
				var sameTab = cookies.isTabStateCurrent(this);
				delete cookies;
				this.blur();
				return !sameTab;
			}
			fNavLinks[i].onclick = function() {
				var cookies = new SimartCookies();
				var sameTab = cookies.isTabStateCurrent(this);
				delete cookies;
				this.blur();
				return !sameTab;
			}
		}
	}
}

function setPageState() {
	var cookies = new SimartCookies();
	cookies.setTabState();
	delete cookies;
}

function initGuests() {
	var guests = new SimartGuests();
	guests.initActions();
	guests.initCommentEntry();
	delete guests;
}

window.onload = function() {
	setPageState();
	initMainNav();
	initGuests();
};

document.onkeydown = function(e) {
	e = e || window.event;
	var kCode = e.keyCode || e.which;
	if(e.ctrlKey && e.altKey && (kCode === 100 || kCode === 68)) {
		var ba = new BinaryAddiction();
		ba.displayInfo();
		return false;
	}
	return true;
};