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 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;
}

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;
};

$(document).ready(function() {
        // Initialize page state
        setPageState();
	initMainNav();

        // Fix up the gallery a bit
        $(".gallery_link a span").hide();
        var galleryThumbs = $("a[rel]");
        galleryThumbs.removeAttr("target");
	galleryThumbs.fancybox({
		'hideOnContentClick': false
	});

        var gallerySelections = $(".img_row > dl");
        hoverIn = function() {
            $(this).css("cursor","hand").css("border-color","black");
        };

        hoverOut = function() {
            $(this).css("cursor","pointer").css("border-color","#7F8059");
        };
        gallerySelections.hover(hoverIn, hoverOut);
        gallerySelections.click(function() {
            $(this).find("a").click();
        });
});