/* addEvent crossbrowser function */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function getWindowHeight() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	} else {
		if (document.documentElement&& document.documentElement.clientHeight) {
			windowHeight= document.documentElement.clientHeight;
		} else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function setFooter() {
	if (document.getElementById) {
		var extraPadding = 150;
		if (document.getElementById("root")) {
			extraPadding = 300;
		}
		var windowHeight=getWindowHeight();
		if (windowHeight>0) {
			var contentElement = cssQuery("div.central")[0];
			var contentHeight = contentElement.offsetHeight;
			var footerElement = document.getElementById("footer");
			var footerHeight=footerElement.offsetHeight;
			if (windowHeight-(contentHeight+footerHeight+extraPadding)>=0) {
				footerElement.className="attach_to_bottom";
			} else {
				footerElement.className="below_content";
			}
		}
	}
}

/* Handles the Find Tully's search box */
var storeFind = {
	sfDefText : "Enter a Zip Code",
	storeFindTB : null,
	
	init : function() {
		this.storeFindTB = document.getElementById("tcHeader_tb_StoreFind");
		if (this.storeFindTB.value == "") {
			this.storeFindTB.value = this.sfDefText;
		}
		addEvent(this.storeFindTB,"focus",this.selectStoreFind);
		addEvent(this.storeFindTB,"blur",this.markStoreFind);
	},

	selectStoreFind : function() {
		if (storeFind.storeFindTB.value == storeFind.sfDefText) {
			storeFind.storeFindTB.value = "";
		}
	},

	markStoreFind : function() {
		if (storeFind.storeFindTB.value == "") {
			storeFind.storeFindTB.value = storeFind.sfDefText;	
		}
	}
}

function storeFindLoader() {
	storeFind.init();
}

/* Add events */
window.addEvent(window,'load',setFooter);
window.addEvent(window,'resize',setFooter);
window.addEvent(window,"load",storeFindLoader);

