var productGroupId;
function Product() {

	thisObj = this;
	

	
	this.quantityinput = $('quantity');
	stockinput = $('noInStock');
	this.addToBasketForm = $('addToBasket');
	
	if (stockinput) {
		this.noInStock = stockinput.value;
	}
	else {
	}
	
	// Mouse overs for product detail
	imagesList = $('product-images');
	if (imagesList) {

		enlargedImage = $('large-product-image').getElementsByTagName("img")[0];
		zoomLink = $('zoom');

		images = imagesList.getElementsByTagName("img");

		for (i=0; i<images.length; i++) {
			images[i].onmouseover = function() {
										imageRef = getNumberAfterDivider(this.src, "tiny_");
										zoomLink.href = ROOT +"/uploadedimages/products/large_"+imageRef;
										enlargedImage.src = ROOT + "/uploadedimages/products/medium_"+imageRef;
									}
		}
	}
	//-->
		
	// Should only be one 1 on page per product detail
	productOptionsUL = document.getElementsByClassName("product-options")[0];
	showPackageButton = $('view-package-contents');
	
	if (showPackageButton) {
		showPackageButton.href = "#";
	}
	
	if (productOptionsUL) {
		productGroupId = getNumberAfterDividerLastIndex(productOptionsUL.id, "_");
	}


	if (showPackageButton) {
  		// Show package contents on click
		Event.observe(showPackageButton, "click", thisObj.showPackageContents, false);
	}

	if (this.addToBasketForm) {
		thisObj.addToBasketForm.onsubmit = this.checkStockAgainstOrderQuantity;
	}
}

Product.prototype.showPackageContents = function(e) {
	
	
	quantityinput = $('quantity');
	
	showContentsOnClick = false
	
	clickedLink = Event.element(e);
	
	if (clickedLink.innerHTML == "Hide content summary") {
		clickedLink.innerHTML = "View package content summary";
		showContentsOnClick = false
	}
	else {
		clickedLink.innerHTML = "Hide content summary";
		showContentsOnClick = true
		HTML = "<span class=\"loading\" id=\"loadingPackageContents\"><img src=\""+ROOT+"/images/loading.gif\"><span>Please wait... loading package contents</span></span>\n";
		new Insertion.Before(quantityinput, HTML);
	}
	
	HTML = ""
	
	if (showContentsOnClick) {
	
		HTML += "<div id=\"package-contents\">\n";
		
		timestamp = new Date();
	
		url = ROOT + "/ajax/get-package-contents.asp";
		params = "productGroupId="+productGroupId+"&timestamp="+timestamp;
		
			new Ajax.Request(url, {
				method: 'get',
				parameters: params,
				onComplete: function() {
					new Effect.fade($('loadingPackageContents'));
				},
				onSuccess: function(transport) {
					HTML += transport.responseText
					HTML += "</div>\n";
					
					Element.remove($('loadingPackageContents'));
	
					new Insertion.After(productOptionsUL, HTML);
					
				}
			});
			
	}
	else {
		Element.remove($('package-contents'));
	}
		



	
}


Product.prototype.checkStockAgainstOrderQuantity = function() {
	
	if ($('loading')) {
		Element.remove($('loading'));
	}

	orderQuantity = parseInt(thisObj.quantityinput.value);
	amountInStock = parseInt(thisObj.noInStock);

	HTML = "<span class=\"loading\" id=\"loading\"><img src=\""+ROOT+"/images/loading.gif\" width=\"17\" height=\"17\" />Adding to basket</span>\n";
	new Insertion.Before(thisObj.quantityinput, HTML);

	loadingMsg = $('loading');

	if (orderQuantity > amountInStock) {
		loadingMsg.innerHTML = "<span class=\"error\">Only "+amountInStock+" left in stock!</span>\n";
		thisObj.quantityinput.value = amountInStock;
		return false; // Don't submit the form
	}
	else {
		Element.remove($('loading'));
		return true;
	}
	
}

Product.prototype.updateImage = function() {
	
}


function getNumberAfterDivider(string, dividerToken) {

	numberDivider = (string.indexOf(dividerToken)) + 5;
	len = string.length; // IE 6 doesn't like 'length' as a variable name it seems...
	
	return string.substring(numberDivider, len);

}

function getNumberAfterDividerLastIndex(string, dividerToken) {

	numberDivider = (string.lastIndexOf(dividerToken)) + 1;
	len = string.length; // IE 6 doesn't like 'length' as a variable name it seems...
	
	return string.substring(numberDivider, len);

}




Event.observe(window, "load", function() { var p = new Product(); }, false);