/* HomeGoods ~ JAVASCRIPT header

	Author: Joel Nagy | dotglu
	Last Modified: May 15th, 2009 12:12:12 PM EST

	Dependency: prototype 1.7 and scriptaculous 1.8 
*/

// NAMESPACE
if (dotglu == null) // check for existence of namespace
var dotglu = {};

dotglu.CurrentMenu = null;
dotglu.CurrentMenuTimer = null;
dotglu.timerAvail = false;

// Populate Query String Object, iterate over each name-value pair
window.query = [];
window.query.length = 0;
window.location.search.replace(
	new RegExp('([^?=&]+)=([^&#]*)?', 'g'),
	// populate the object with the values by name
	function($0, $1, $2) { window.query[$1] = $2; window.query.length++; }
);

// Tracking / Sharing
addthis_pub             = 'HomeGoods';
addthis_logo_background = 'FFFFFF';
addthis_logo_color      = '797979';
addthis_brand           = 'HomeGoods';
addthis_options         = 'favorites, email, twitter, delicious, facebook, google, myspace, live, more';
addthis_offset_left     = 15;
addthis_offset_top      = 11;

dotglu.isString = function(o) {
   return (typeof o).toLowerCase() == 'string';
}

dotglu.modMenu = function(menu, mod) {
	try {
		if (menu == null) return;
		menu = dotglu.isString(menu)? $(menu): menu;
		mod = mod? mod: 'none';
		dotglu.CurrentMenu = menu.id;

		if (mod == 'none') {
			if (dotglu.timerAvail)
				Effect.BlindUp(menu, {duration: 0.25}); //, afterFinish: function () { if (parent.noMenu && mod == 'none') parent.noMenu(); } });
			else
				menu.style.display = mod;
			if (parent.noMenu && mod == 'none')
				parent.noMenu();
		} else {
			if (dotglu.timerAvail)
				Effect.BlindDown(menu, {duration: 0.5});
			else
				menu.style.display = mod;
		}
	} catch (e) { }
}

dotglu.setPointer = function() {
	$('Nav').getElementsBySelector('a').each(function(a) { a.style.cursor = 'pointer'; a.blur(); });
}

dotglu.setFrameBaseHeight = function() {
	try {
		var dim = $('HeaderWrapper').getDimensions();
		if (parent.setFrameBaseHeight)
			parent.setFrameBaseHeight(dim.width, dim.height);
	} catch (e) { }
}

dotglu.prepULNavigation = function(id) {
	try {
		$(id).getElementsBySelector('li').each(function(LI) {
			LI.observe('mouseover', function () {
				LI.style.cursor = 'pointer';
				if (dotglu.CurrentMenu != null && (LI.descendantOf(dotglu.CurrentMenu) || LI.select('ul#'+dotglu.CurrentMenu) != '')) {
					clearTimeout(dotglu.CurrentMenuTimer);
					dotglu.CurrentMenuTimer = null;
				}
				LI.getElementsBySelector('ul').each(function (UL) {
					if (UL != null) {
						if (UL.style.display == 'none') {
							// Get height
							UL.style.visibility = 'hidden';
							UL.style.display = 'block';
							var A = UL.getElementsBySelector('a');
							UL.style.height = (A.length * A[0].getDimensions().height) +'px';
							UL.style.display = 'none';
							UL.style.visibility = 'visible';
							// open frame if needed
							try {
								if (parent && parent.goMenu)
									parent.goMenu(parseInt(UL.style.height));
							} catch (e) { }
							dotglu.modMenu(UL, 'block');
							setTimeout('dotglu.setPointer();', 100);
						}
						return;
					}
				}); //: UL.first()
			}); //: mouseover
			LI.observe('mouseout', function () {
				LI.style.cursor = 'pointer';
				LI.getElementsBySelector('ul').each(function (UL) {
					if (UL != null)
						return (dotglu.CurrentMenuTimer = setTimeout('dotglu.modMenu("'+ UL.id +'")', 100));
				}); //: UL.first
			}); //: mouseout
			LI.observe('click', function () {
				LI.getElementsBySelector('ul').each(function (UL) {
					if (UL != null)
						return dotglu.modMenu(UL.id);
				}); //: UL.first
			}); //: mouseout
		}); //: LI.each()
	} catch (e) { }
}

dotglu.inputSet = function(e, def) {
	def = def == null? '': def;
	if (e.value == '') e.value = def;
	if (def == '' && e.value == e.title) e.value = '';
	return false;
}

dotglu.cookie = function(name, value, days) {
	// SET / DELETE (if value == '')
	if (value != null) {
		var D = new Date();
		D.setTime(D.getTime()+(days*24*60*60*1000));
		var expires = (days? expires = "; expires="+ D.toGMTString(): expires = "");
		document.cookie = name +"="+ value + expires +"; path=/";
		return value;
	// GET
	} else {
		var x = new RegExp("\\b" + name + "=([^;]*)");
		var value = x.exec(document.cookie);
		return (value) ? unescape(value[1]) : "";
	}
}

dotglu.randomizer = function(start, end) {
	return Math.floor(Math.random()*end)+start;
}

document.observe('dom:loaded', function() {
	// menu prep
	dotglu.prepULNavigation('Nav');

	setTimeout('dotglu.timerAvail = true', 10);
	
	dotglu.setFrameBaseHeight();

	// clean all forms on full page refresh
	$$('form').each(function(F) {
		if (F.postcode) F.postcode.value = '';
		if (F.state) F.state.value = '';
		F.reset();
	});
	
	// Set the Active Link on the side nav for Footer Pages
	if ($('intNav') != null) {
		var strPage = document.location.pathname.toString().replace(/-/g, '_');
		if (document.location.hash != '') strPage = document.location.hash.slice(1)
			else strPage = strPage.slice(1, strPage.length-4);
		shortPage = strPage.slice(0, strPage.indexOf('_'));
		$('intNav').getElementsBySelector('img').each(function(IMG) {
			var l = IMG.ancestors()[0].href;
			var force = false;
			switch (strPage) { // Highlight Specific pages
				case 'product_images':
					force = true;
			}
			if (force) {
				if (l != null && l.indexOf('press') >= 0)
					return IMG.parentNode.addClassName('current');
				IMG = null;
			} else if (l) {
				var h = l.lastIndexOf('#');
				l = l.toString();
				if (h > 0) l = l.slice(h+1);
				else {
					l = l.slice(l.indexOf('://')+3);
					l = l.toString().slice(l.indexOf('/')+1, l.toString().length-4);
				}
			}
			if ((strPage == l) || (IMG != null && IMG.name.slice(0, IMG.name.indexOf('_')) == shortPage)) {
				return IMG.parentNode.addClassName('current');
			}
		}); //: #intNav IMG.each()
	}
	
	$$('input.zipcode').each(function(e) {
		dotglu.inputSet(e, e.title);
		// In Firefox the inputs are different sizes when in the blog vs the main site
		e.style.width = '60px';
		e.style.height = '19px';
	});

	if ($('PreferredLogin'))
		dotglu.inputSet($('PreferredLogin'), $('PreferredLogin').title);
}); //: dom:loaded

