//=== GLOBAL VALUE SECTION ==================================================

/** Global Values
 */
var		products = new Array();
var		bSelected = true;
var		productIdx = new Array();
var		IMAGE_BASE_LOCATION = "../../images/";

var img_blank_src = IMAGE_BASE_LOCATION + "site/buttons/blank.gif";
var img_blank = null;
if (document.images) {
	img_blank = new Image();
	img_blank.src = img_blank_src;
}

//=== OBJECT DEFINITION SECTION ===============================================

/** Object::Product
 */
function Product() {
	this.id = "";
	this.name = "";
	this.thumbnail = null;
	this.thumbnailsrc = "";
	this.thumbnaildescription ="";
	this.thumbnaillink = "";
	this.detail = null;
	this.detailsrc = "";
	this.detaildescription = "";
	this.information ="";
	this.price = "";
	
	this.toHTMLParams = function() {
		alert(this.thumbnaildescription);
		return "?Product.id=" + this.id + "?Product.name=" + this.name 
			+ "?Product.thumbnailsrc=" + this.thumbnailsrc + "?Product.thumbnaildescription =" +this.thumbnaildescription
			+ "?Product.detailsrc=" + this.detailsrc + "?Product.detaildescription=" + this.detaildescription+ "?Product.information=" + this.information
			+ "?Product.price=" + this.price;
	}
	
	this.fromHTMLParams = function() {
		this.id = getUrlParameter("Product.id");
		this.name = getUrlParameter("Product.name");
		this.thumbnailsrc = IMAGE_BASE_LOCATION + getUrlParameter("Product.thumbnailsrc");
		this.thumbnaildescription = getUrlParameter("Product.thumbnaildescription");
		this.detailsrc = IMAGE_BASE_LOCATION + getUrlParameter("Product.detailsrc");
		this.detaildescription = getUrlParameter("Product.detaildescription");
		this.information= getUrlParameter("Product.information");
		this.price = getUrlParameter("Product.price");

		this.thumbnail = new Image();
		this.thumbnail.src = this.thumbnailsrc;
		this.detail = new Image();
		this.detail.src = this.detailsrc;
	}
}		


//=== COLLECTION FUNCTIONS SECTION ===========================================

/**	initialiseProductList:
 */
function initialiseProductList() {
	products = new Array();
	productIdx = new Array();
}

/**	preloadImages:
 */
function preloadImages() {
	if (document.images) {
		for(var i = 0; i < products.length;i++ ) {
			products[i].thumbnail = new Image();
			products[i].thumbnail.src = IMAGE_BASE_LOCATION + products[i].thumbnailsrc;
			products[i].detail = new Image();
			products[i].detail.src = IMAGE_BASE_LOCATION + products[i].detailsrc;
		}
	}
}	

/** addProduct:
 */
function addProduct(oProduct) {
	products[products.length] = oProduct;
	productIdx[oProduct.id] = oProduct;
}

/**	getUrlParameter:
 */
function getUrlParameter(sName) {
	try {
		var sHref = window.location.href;
		var iBegin = sHref.indexOf(sName + "=") + sName.length+1;
		var iEnd = sHref.indexOf("?",iBegin+1);
		if ( iEnd == -1) iEnd = sHref.length;
		var sValue = sHref.substring(iBegin, iEnd);
		return sValue;
	}
	catch (e) {
		alert(e);
	}
}

//=== DHTML FUNCTIONS SECTION ==============================================

/**	createSiteNavigation:
 */
function createSiteNavigation() {
	document.write("<TABLE class='navigator'>");
	document.write("<TR><TD HEIGHT='200'><IMG SRC='" + img_blank_src + "' WIDTH='100' HEIGHT='150'></TD></TR>");
	writeNavigator("about-us", "about-us", "Welcome to Luz de Laguna");
	writeNavigator("collections", "jewellery collection", "Collections");
	writeNavigator("stockists", "stockists", "Stockists");
	writeNavigator("press", "press", "Press");
	writeNavigator("shop", "shop", "Visit our e-Boutique");
	writeNavigator("contact-us", "contact-us", "Contact Us");
	writeNavigator("links", "Links");
	document.write("</TABLE>");
}

/**	writeNavigator:
 */
function writeNavigator(sButtonName, sLocation, sAlt) {
	if ( sButtonName == "shop" ) {
		document.write("<tr><td><a href='" + sLocation + ".html'><img src='" + IMAGE_BASE_LOCATION + "site/buttons/" 
		+ sButtonName + ".gif' class='ldl_img_button' alt='" + sAlt + "' onclick=\"window.open('http://www.fashioncapital.co.uk/index.php?option=com_boutique&task=boutiqueRESULTS&boutique_search_id=3532','eBoutique','width=800,height=600,toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes')\"" 
		+ "></a></td></tr>");
	}
	else {
		document.write("<tr><td><a href='" + sLocation + ".html'><img class='ldl_img_button' src='" + IMAGE_BASE_LOCATION + "site/buttons/" + sButtonName + ".gif'' alt='" + sAlt + "'/></a></td></tr>");
	}
}

/**	writeThumbnailImage:
 */
function writeThumbnailImage(sProductID, bDescription) {
	if ( sProductID == "blank" ) {
		document.write("<img class=\"ldl_img_thumbnail\" src=\"" + img_blank_src + "\"/>");
	}
	else {
		if ( !bDescription) bDescription = false;
		var oPrd = productIdx[sProductID];
		document.write("<a href=\"#\" onMouseover=\"handleOnMouseover('" + sProductID + "'," + bDescription + ")\" onMouseout=\"handleOnMouseout(" + bDescription + ")\">");
		document.write("<img class=\"ldl_img_thumbnail\" onclick=\"onDetailClicked('" + oPrd.id + "');\" title=\"" + oPrd.name + "\"  src=\"" + oPrd.thumbnail.src + "\" alt=\""+oPrd.name+"\"/></a>");
	}
}


/**	getThumbnailDescription:
 */
function getThumbnailDescription(oProduct) {
	return "<span class='ldl_prd_dtl_name'>" + oProduct.name 
	+ "</span><p/><span class='ldl_prd_dtl_desc'>" + oProduct.thumbnaildescription
	+ "</span><p/><span class='ldl_price'>Price: £" + oProduct.price 
	+ "</span>";
}

/**	getDetailDescription:
 */
function getDetailDescription(oProduct) {
	return "<span class='ldl_prd_dtl_name'>" + oProduct.name 
	+ "</span><p/><span class='ldl_prd_dtl_desc'>" + oProduct.thumbnaildescription 
	+ "</span><p/><span class='ldl_prd_thm_desc'>" + oProduct.detaildescription 
	+ "</span><p/><span class='ldl_prd_thm_desc'>" + oProduct.information 
	+ "</span><p/><span class='ldl_price'>Price: £" + oProduct.price 
	+ "</span>";
}

//=== EVENT HANDLER FUNCTIONS SECTION ============================================

/**	handleOnMouseover:
 */
function handleOnMouseover(sProductID, bDescription) {
	var oPrd = productIdx[sProductID];
	document.detail.src=oPrd.detail.src;
	if ( bDescription ) {
		document.getElementById("detailDescription").innerHTML = getThumbnailDescription(oPrd);
	}
}

/**	handleOnMouseout:
 */
function handleOnMouseout(bDescription) {
	if ( bSelected == false ) {
		document.detail.src= img_blank_src;
		if ( bDescription ) {
			document.detailDescription.innerText = "";
		}
	}
}

/**	onDetailClicked:
 */
function onDetailClicked(sProductID) {
	var sHref = window.location.href;
	var oProduct = productIdx[sProductID];

	var sURL = sURL = sHref.substring(0,sHref.lastIndexOf("/")) + "/detail.html?Collection=" + getCollectionName()+ "?Product.id=" + oProduct.id;
	window.location = sURL;
}

/**	handleOnDetailLoad:
 */
function handleOnDetailLoad() {
	var oProduct = productIdx[getUrlParameter("Product.id")];

	document.detail.src = IMAGE_BASE_LOCATION + oProduct.detailsrc;
	document.getElementById("detailDescription").innerHTML = getDetailDescription(oProduct);
}

function home() {
	window.location = "../site/home.html";
}

//=== COLLECTION MANANGEMENT FUNCTIONS SECTION ============================================

function defineCollection(sCollectionName) {
	try {
		sCollectionName = sCollectionName.replace(/-/g, "_");
		var fn = "defineProducts_" + sCollectionName + "()";
		eval(fn);
	}
	catch(e) {
		alert("defineCollection::" + e + " when attempting to eval: " + fn);
	}
}
function getCollectionName() {
	var sHref = window.location.href;
	var sCollectionName = sHref.substring(sHref.lastIndexOf("/")+1, sHref.indexOf(".html"));
	if ( "detail" == sCollectionName ) {
		sCollectionName = "";
	}
	return sCollectionName;
}

function defineCollectionProducts() {
	var sCollectionName = getCollectionName();
	if ( sCollectionName == "" || sCollectionName == null ) {
		sCollectionName = getUrlParameter("Collection");
	}
	defineCollection(sCollectionName);
}
