/**
 * Javascript for elizabethgrant.com
 *
 */



// do this stuff once the DOM is ready
$(document).ready(function() {
						   
	$("div.roundbox").each(function() { RoundBox(this) });		// apply rounded corners if necessary

	initRollovers($(".rollover"));		// pass where to apply rollover events
	preloadImages($(".rollover"));		// preload rollover images

});


// firebug logging
//$.fn.log = function (msg) {
//      console.log("%s: %o", msg, this);
//      return this;
//};





/* Functions
----------------------------------------------------------------------*/

/**
 * Go to a URL
 */
function goTo(url) {
	window.location.href = url;
}



/**
 * Binds event handlers to swaps images for rollovers.
 *
 *	Expects: 			one or more DOM elements.
 *
 *  Elements should be:	an IMG node,
 *						an INPUT node of type 'image',
 *						an A or DIV node containing either of the above.
 *
 *  Image names must end with _over.* and _off.* to qualify for swapping.
 *		(ex. "btn-home_off.gif" triggers "btn-home_over.gif" on mouseover)
 *
 */
function initRollovers(domChunk) {

	var elems;
	var overSrc;

	// assign event handlers
	domChunk.bind("mouseover", function () {
		// find images that need to swapped
		if (this.tagName === "A" || this.tagName === "DIV") {
			elems = $(this).find("img[@src*='_off.']"); // for A tags look for suitable child images
		} else {
			elems = $(this);
		}

		// do the swap
		elems.each(function () {
			overSrc = this.src.replace(/\_off\./, "_over.");
			this.src = overSrc;
		});
	});
	domChunk.bind("mouseout", function () {
		// find images that need to swapped
		if (this.tagName === "A" || this.tagName === "DIV") {
			elems = $(this).find("img[@src*='_over.']"); // for A tags look for suitable child images
		} else {
			elems = $(this);
		}
		
		// do the swap
		elems.each(function () {
			overSrc = this.src.replace(/\_over\./, "_off.");
			this.src = overSrc;
		});
	});
}


/**
 * Preload images used in rollovers.
 */
function preloadImages(domChunk) {
	
	var elems;
	var preloadSrc;
	var preloadObj;
	
	domChunk.each(function () {
		
		// find images inside element that need to be preloaded
		if (this.tagName === "A" || this.tagName === "DIV") {
			elems = $(this).find("img[@src*='_off.']"); // for A tags look for suitable child images
		} else {
			elems = $(this);
		}
		
		// preload them
		elems.each(function () {
			preloadSrc = this.src.replace(/\_off\./, '_over.');
			preloadObj = new Image();
			preloadObj.src = preloadSrc;
		});
		
	});
}


/**
 * Clear preset text in textfield once clicked on
 */
function clearTextfield(domChunk) {
	domChunk.bind("click", function() {
		this.value = "";
	});
}


/**
 * Update display of shopping bag item count
 */
function updateItemCount() {
	$.getJSON("wire/shopping-bag-item-count.json", function(json) {
//alert('UPDATING ITEM COUNT {' + json.shoppingBagItemCount + '}');


	$("div.itemCount").html(json.shoppingBagItemCount);
	});
}


/**
 * Add to Wish List
 */
function addToWishList(productId) {

	$.getJSON("wire/is-logged-in.json", function(json) {
		var dest;
		
		dest = 'account/addToWishList.do?_layer=overlay&productId=' + productId;

		dest += '&TB_special&modal=true';

      tb_show(null,dest,false);

      return false;
	});
}
