/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2008 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/
var AddToCartReplacement = {
	INVENTORY_AVAILABLE_URL: '/v/inventory_available.asp',
	FORM_ELEMENT_NAME: 'MainForm',
	ADD_TO_CART_BUTTON_ELEMENT_NAME: 'btnaddtocart',
	ADD_TO_CART_HOVER_URL: '/v/vspfiles/templates/DayCloseout/images/buttons/btn_addtocart_over.gif',
	ADD_TO_CART_ALT: 'Hook Me Up!',
	ADD_TO_CART_SOLD_OUT_URL: '/v/vspfiles/templates/DayCloseout/images/buttons/btn_addtocart_out_of_stock.gif',
	ADD_TO_CART_SOLD_OUT_ALT: 'Sold Out',
	
	load: function() {
		var productCode = AddToCartReplacement.getProductCodeFromURL(window.location.href);
		
		//Only do work if we are on the product detail page
		if(productCode == null || productCode == '') {
			return;
		}
		
		new Ajax.Request(AddToCartReplacement.INVENTORY_AVAILABLE_URL, {
			method: 'get',
			evalJSON: true,
			sanitizeJSON: true,			
			parameters: {
				productCode: productCode
			},
			
			onSuccess: function(transport) {
				//true = inventory available
				//false = inventory unavailable
				
				if(transport.responseJSON) {
					AddToCartReplacement.inventoryAvailable(productCode);
				}
				else {
					AddToCartReplacement.noInventoryAvailable(productCode);
				}
			}
		});
	},
	
	inventoryAvailable: function(productCode) {
		var addToCartButtonElement = document.getElementsByName(AddToCartReplacement.ADD_TO_CART_BUTTON_ELEMENT_NAME);
		var originalSrc = null;
		var hoverImage = null;
		
		//We must have an element to continue
		if(addToCartButtonElement == null || addToCartButtonElement.length < 1) {
			return;
		}
		
		//Prototype it
		addToCartButtonElement = addToCartButtonElement[0];
		addToCartButtonElement = $(addToCartButtonElement);
		addToCartButtonElement.alt = AddToCartReplacement.ADD_TO_CART_ALT;
		
		//Get the original source
		originalSrc = addToCartButtonElement.src;
		
		//Load the image immediately
		hoverImage = new Element('img', {
			src: AddToCartReplacement.ADD_TO_CART_HOVER_URL
		});
		
		//Setup mouse over
		Event.observe(addToCartButtonElement, 'mouseover', function() {
			addToCartButtonElement.src = hoverImage.src;
		});
		Event.observe(addToCartButtonElement, 'mouseout', function() {
			addToCartButtonElement.src = originalSrc;
		});
	},
	
	noInventoryAvailable: function(productCode) {
		var formElement = document.forms[AddToCartReplacement.FORM_ELEMENT_NAME];
		var addToCartButtonElement = document.getElementsByName(AddToCartReplacement.ADD_TO_CART_BUTTON_ELEMENT_NAME);
		
		//We must have all elements to continue
		if(formElement == null) {
			return;
		}		
		if(addToCartButtonElement == null || addToCartButtonElement.length < 1) {
			return;
		}
		
		//Prototype it
		formElement = $(formElement);
		addToCartButtonElement = addToCartButtonElement[0];
		addToCartButtonElement = $(addToCartButtonElement);

		//Change the add to cart button
		addToCartButtonElement.src = AddToCartReplacement.ADD_TO_CART_SOLD_OUT_URL;
		addToCartButtonElement.alt = AddToCartReplacement.ADD_TO_CART_SOLD_OUT_ALT;
		//Disable the form
		formElement.onsubmit = function(){return false;};
		formElement.action = '/';
	},
	
	/*--------------------------------------------------------------------------*
	 * 
	 * Category Zoom (Whitney) - getProductCodeFromURL
	 * 
	 * Version 2.0.4
	 * 
	 * Copyright (C) 2008 Brand Labs LLC
	 * 
	 * This library is free software; you can redistribute it and/or
	 * modify it under the terms of the GNU Lesser General Public
	 * License as published by the Free Software Foundation; either
	 * version 2.1 of the License, or any later version.
	 * 
	 * This library is distributed in the hope that it will be useful,
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	 * Lesser General Public License for more details.
	 * 
	 * You should have received a copy of the GNU Lesser General Public
	 * License along with this library; if not, write to the Free Software
	 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
	 * 02110-1301  USA
	 * 
	 *--------------------------------------------------------------------------*/
	getProductCodeFromURL: function(url) {
		var matches = null;
		
		//Make sure we do not have a null
		if(url == null) {
			return null;
		}
		
		//Check non-SEO URL
		matches = url.match(/\/productdetails\.asp\?(?:[\&]?.*\=.*)*productcode=([^\&\#]+)/i);	
		if(matches != null && matches.length >= 2) {
			return unescape(matches[1]);
		}
		
		//Check SEO URL
		matches = url.match(/(?:_p|-p)\/(.+)\.htm/i);	
		if(matches != null && matches.length >= 2) {
			return unescape(matches[1]);
		}
	
		//No product code available
		return null;
	}
	/*--------------------------------------------------------------------------*/	
};

/*--------------------------------------------------------------------------*
 * Start once loaded
 *--------------------------------------------------------------------------*/
Event.observe(window, 'load', AddToCartReplacement.load);
/*--------------------------------------------------------------------------*/