/**
* File: product_detail.js
* Author: Brian Schemp
* 
*
* History: 09/26/2009 Brian - Created.
*
*
*/

/**
* Called when setting the product detail link.  Set the click event for the product image and product number
* links.
*
* @param {Object} productId
* @param {Object} productNumber
* @param {Object} catalogName
*/
function setupProductDetailLink(productId, productNumber, catalogName, catalogId) {
	$("#product_" + productId).append('<a id="product_detail_link_' + productId + '" class="product-search-results-number-link">' + productNumber + '</a>');
	
	//set product number link.
	$("#product_detail_link_" + productId).click(function(e){
		checkProductForMultipleModels(productId, productNumber, catalogName, catalogId, e.pageY);
	});
	
	//set product image link.
	$("#product_detail_image_link_" + productId).click(function(e){
		checkProductForMultipleModels(productId, productNumber, catalogName, catalogId, e.pageY);
	});	
}

/**
* Check if the product belongs to multiple models.  Display a window for the user
* to chose a model if it does belong to multiple models.
*
* @param {Object} productId
* @param {Object} productNumber
* @param {Object} catalogName
* @param {Object} yScrollPosition
*/
function checkProductForMultipleModels(productId, productNumber, catalogName, catalogId, yScrollPosition) {
	//catalog exists in multiple catalogs.
	if(catalogName.toUpperCase() == 'MULTIPLE') {
		var productModelChoiceContainer = $("#productModelChoiceContainer");
		var backgroundPopup = $("#backgroundPopup");		
		
		//set the onClick event for the background image.
		backgroundPopup.unbind().click(function() {
			closeProductModelChoicePopup();										
		});
		
		//set the onClick event for the close window link.
		$("#productModelChoiceCloseWindowLink").click(function(){
			closeProductModelChoicePopup();												  
		});
		
		//call a function to get the product models/categories.
		$.post(
			"/object/Product.cfc",
			{
				method: "getProductCatalogs",
				returnFormat: "json",
				productId : productId
			},
			function(response){
				//variable to store the top postion.
				var topPosition = yScrollPosition;
				
				//variable to store the product model list.
				var productList = $("#productModelList");
				
				if(yScrollPosition > 100) {
					topPosition = yScrollPosition - 100;
				}
								
				//set the top position.
				productModelChoiceContainer.css("top", topPosition + "px");
								
				//remove all list children.
				productList.children().remove();			
				
				//loop over each product model.
				$.each(response.DATA, function(index, optionData) {
					var catalogId = optionData[0];
					var catalogName = optionData[1];
					var catalogLongName = optionData[2];
					
					//productList.append('<li><a href="/' + catalogName + '/' + productNumber + '.html">' + catalogLongName.toUpperCase() + '</a></li>');
					productList.append('<li><a href="/controller.cfm?type=product&action=productDetail&productId=' + productId + '&productSearchCatalogId=' + catalogId + '">' + catalogLongName.toUpperCase() + '</a></li>');
				});
				
				//set click event for product model choice list item.
				$("#productModelList li").click(function(){
					productModelChoiceContainer.fadeOut("slow");
					backgroundPopup.fadeOut("slow");
					window.location = $(this).find("a").attr("href");return false;
				});
				
				//display the pop-up window.
				backgroundPopup.css({"opacity": "0.7"});  
				backgroundPopup.fadeIn("slow"); 	 
				productModelChoiceContainer.fadeIn("slow");				
			},
			"json"
		);
	}
	
	//product only exist in one catalog.
	else {
		//window.location = "/" + catalogName + "/" + productNumber + ".html";
		top.location.href = "/controller.cfm?type=product&action=productDetail&productId=" + productId + "&productSearchCatalogId=" + catalogId;
	}
}

/**
* Close the product model choice window.
*/
function closeProductModelChoicePopup() {
	$("#productModelChoiceContainer").fadeOut("slow");
	$("#backgroundPopup").fadeOut("slow");
}