/*
 * All java script logic for the Demandware reference application.
 *
 * The code relies on the prototype.js and scriptaculous.js libraries to
 * be also loaded.
 */

/*
 * Register more initializations here
 */
window.onload = function()
{
}

function preloadImg(imgSrc)
{
	var img = new Image;
	img.src = imgSrc;
}

function ajaxPopup(popupURL)
{
	Dialog.alert({url: popupURL}, {windowParameters: {className: 'alphacube'}, okLabel: 'Close'}); 
	return false;
}

function textPopup(popupText)
{
	Dialog.alert(popupText, {windowParameters: {className: 'alphacube'}, okLabel: 'Close'}); 
	return false;
}

/*
	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=yes, resizable=yes");
		}
		else
		{
			window.open(url, "", "scrollbars=yes, 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;

			if( MiniCart.suppressSlideDown && MiniCart.suppressSlideDown() )
			{
				// do nothing
				// the hook 'MiniCart.suppressSlideDown()' should have done the refresh
			}
			else
			{
				// show the item				
				var minicartcontent = $('minicartcontent');
				minicartcontent.style.width = dim.width + 'px';	
				minicartcontent.scrollTo();
				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');
		}
	},

	// hook which can be replaced by individual pages/page types (e.g. cart)
	suppressSlideDown: function()
	{
		return false;
	}
}
/*
 * Functionality from rounded CSS divs.
 */
var makeRound = function(){var els = document.getElementsByTagName('div'); for(var i=0; el=els[i]; i++) if(el.className.indexOf('round')>-1 && el.firstChild && el.firstChild.className!='t') el.innerHTML = '<b class="t"><b class="r"></b></b><div class="c"><b class="br"></b>'+el.innerHTML+'<b class="br"></b></div><b class="b"><b class="r"><!----></b></b>';}

	window.onload = makeRound;

/*
 * Dynamically add CSS styles to <head> using javascript function
 * http://www.tomhoppe.com/2008/03/dynamically-adding-css-through.html
 */	
function addCss(cssCode) {
var styleElement = document.createElement("style");
  styleElement.type = "text/css";
  if (styleElement.styleSheet) {
    styleElement.styleSheet.cssText = cssCode;
  } else {
    styleElement.appendChild(document.createTextNode(cssCode));
  }
  document.getElementsByTagName("head")[0].appendChild(styleElement);
}
