/*
 * All java script logic for the Demanware reference application.
 *
 * The code relies on the prototype.js and scriptaculous.js libraries to
 * be also loaded.
 */


/*
 * Register more initializations here
 */
window.onload = function()
{
}


/*
	Opens a new window with the provided url and dimension. Used
	for Scene7 and other situations.

	@param url the url to open
	@param width the window width
	@param height the window height
*/
function openPopup( url, width, height )
{
	if (url != null)
	{
		if (width != null && height != null)
		{
			window.open(url, "", "width=" + width +", height=" + height +", scrollbars=no, resizable=yes");
		}
		else
		{
			window.open(url, "", "scrollbars=no, resizable=yes");
		}
	}
}


/*
 * Support for the compare window
 */
ProductCompare = {
	openPopup: function( url ) {
		window.open(
			url,
			'product_compare',
			'width=800,height=600,scrollbars=yes,resizable=yes',
			true /* replace history in the popped up window */
		).focus();
	}
}


/*
 * Functionality around the mini cart.
 */
var MiniCart = {
	// flag, whether cart is open or not
	state: 0,

	// during page loading, the Demandware URL is stored here
	url: '',

	// timer for automatic close of cart item view
	timer: null,

	cartAdd: function( form, progressImageSrc )
	{
		// get the data of the form as serialized string
		var postdata = Form.serialize(form);
	
		// get button reference
		var addButtons = Form.getInputs(form, 'image', 'add');
	
		// the button to update
		var addButton = null;
		
		// disable form
		Form.disable(form);

		// it is an array of buttons, but we need only one all
		// other combinations are strange so far
		if (addButtons.length == 1)
		{
			addButton = addButtons[0];	
		}
	
		var previousImageSrc = null;
	
		// show progress indicator
		if (addButton != null)
		{
			previousImageSrc = addButton.src;
			addButton.src = progressImageSrc;
		}
	
		var handlerFunc = function(req)
		{
			// hide progress indicator
			if (addButton != null)
			{
				addButton.src = previousImageSrc;
			}
			Form.enable(form);
		
			// Get the position before the innerHTML is set. After
			// the set the elements have no coordinates. A display
			// refresh from the browser is necessary.
			var minicarttotal = $('minicarttotal');
			var pos = Position.cumulativeOffset(minicarttotal);
			var dim = Element.getDimensions(minicarttotal);

			// replace the content
			var minicart = $('minicart');
			minicart.innerHTML = req.responseText;

			// show the item
			// this display is optimized for Firefox. IE6/7 display the 
			// sliding down cart with an offset of -1
			var minicartcontent = $('minicartcontent');

			minicartcontent.style.left = pos[0] + 0 + 'px';
			minicartcontent.style.top = pos[1] + dim.height + 1 + 'px';
			minicartcontent.style.width = dim.width - 2 + 'px';

			new Effect.SlideDown('minicartcontent');

			// after a time out automatically close it
			MiniCart.timer = setTimeout( 'MiniCart.cartClose()', 6000 );
		}

		var errFunc = function(req)
		{
			// hide progress indicator
			if (addButton != null)
			{
				addButton.src = previousImageSrc;
			}
			Form.enable(form);
		}

		// close a product QuickView
		// if ( QuickView ) QuickView.closeQuickView();

		// cloes a previous mini cart
		MiniCart.cartClose();

		// add the product
		new Ajax.Request( MiniCart.url, {method:'post', postBody:postdata, onSuccess:handlerFunc, onFailure:errFunc});
	},

	cartClose: function()
	{
		if ( MiniCart.timer != null )
		{
			clearTimeout( MiniCart.timer );
			MiniCart.timer = null;
			Effect.SlideUp('minicartcontent');
		}
	}
}
