
function IndexProduct(prdtNum, style, shouldShowAlternateView) {
	this.productNum = prdtNum;
	this.styleNum = style;
	this.showAlternateView = shouldShowAlternateView;
	this.currentColorCode = "";
	this.currentColorName = "";
	this.frontImageSet = new ImageSet();
	this.alternateImageSet = new ImageSet();
	this.productViews = new ProductViews();
	
	this.switchColor = function(clrCode, colorName) {
		var productView = this.productViews.getProductView(clrCode);
		var colorImageName = this.frontImageSet.getColorImage(clrCode);
		
		doDebug("[switchColor] productView = " + productView);
		if (productView !== null) {
			this.linkToProductPage(productView.productPageURL);
		}

		var imgElement = document.getElementById("prdtImg_"+prdtNum);
		if (imgElement === null) {
			doDebug("null...");
			return;
		}
		var swatchContainer = document.getElementById("sw_"+prdtNum+"_"+this.currentColorCode);
		if (swatchContainer !== null) {
			swatchContainer.className = "swatch";	
		}
		
		this.currentColorCode = clrCode;
		this.currentColorName = colorName;

		swatchContainer = document.getElementById("sw_"+prdtNum+"_"+this.currentColorCode);
		if (swatchContainer !== null) {
			swatchContainer.className = "swatchSelected";	
			this.preload();
		}
		
		if (colorImageName === null) {
			this.doColorError(colorName);
			return;
		} else {
			imgElement.src = colorImageName;
		}
				
		//made it to the end, clear the error if there is one
		this.clearError();
	};
	
	this.showAlternate = function() {	
		if (this.showAlternateView === false || this.alternateImageSet.colorImages.length === 0) {
			return;
		}
		var colorImageName = this.alternateImageSet.getColorImage(this.getCurrentColorCode());
		if (colorImageName === null) {
			return;
		}
		var imgElement = document.getElementById("prdtImg_"+prdtNum);
		if (imgElement === null) {
			doDebug("null...");
			return;
		}
		doDebug(colorImageName);
		imgElement.src = colorImageName;
		doDebug(imgElement.src);
	};
	this.showFront = function(clrCode) {
		var colorImageName = this.frontImageSet.getColorImage(this.getCurrentColorCode());
		if (colorImageName === null) {
			this.doColorError(this.getCurrentColorName());
			return;
		}
		var imgElement = document.getElementById("prdtImg_"+prdtNum);
		if (imgElement === null) {
			doDebug("null...");
			return;
		}
		doDebug(colorImageName);
		imgElement.src = colorImageName;
		doDebug(imgElement.src);
	};
	this.doColorError = function (colorName) {
		var errorDiv = document.getElementById("prodError_"+prdtNum);
		var errorTextDiv = document.getElementById("prodErrorText_"+prdtNum);
		var msg = "Sorry, this photograph is not color changeable to " + colorName;
		errorTextDiv.innerHTML = msg;
		errorDiv.style.display="block";
		setTimeout("document.getElementById('prodError_"+prdtNum+"').style.display='none'", 3000);
	};
	
	this.clearError = function () {
		setTimeout("document.getElementById('prodError_"+prdtNum+"').style.display='none'", 1);
	};	
	
	this.getCurrentColorCode = function () {
		if (this.currentColorCode === "") {
			this.currentColorCode =  this.frontImageSet.colorImages[0].colorCode;
		}
		return this.currentColorCode;
	};
	this.getCurrentColorName = function () {
		if (this.currentColorName === "") {
			this.currentColorName =  this.frontImageSet.colorImages[0].colorName;
		}
		return this.currentColorName;
	};
	this.linkToProductPage = function(ppLink) {
		var link = document.getElementById("pp_link_name_"+prdtNum);
		var imgLink = document.getElementById("pp_link_img_"+prdtNum);
		doDebug("[linkToProductPage] ppLink =" + ppLink);
		doDebug("[linkToProductPage] " + link.href);
		link.href = ppLink;
		imgLink.href = link.href;
		doDebug("[linkToProductPage] " + link.href);
	};
	this.preload = function() {
		var altImage = this.alternateImageSet.getColorImage(this.getCurrentColorCode());
		var imgLoader = document.getElementById("imageloader");
		if (altImage !== null && imgLoader !== null) {
			imgLoader.src = altImage;
		}
	};
}

function ProductList () {
	this.productArray = new Array();
	this.addProduct = function (prdt) {
		this.productArray.push(prdt);
	};
	this.getProduct = function (prdtNum) {
		for (var i=0; i<this.productArray.length; i++) {
			if (this.productArray[i].productNum == prdtNum) {
				return this.productArray[i];
			}
		}
		return null;
	};
	this.swatchClicked = function (prdtNum, clrCode, colorName) {
		doDebug("[swatchClicked] prdtNum = " + prdtNum + ", clrCode = "  + clrCode + ", colorName = " + colorName);
		var prdt = this.getProduct(prdtNum);
		if (prdt === null) {
			doDebug("[swatchClicked] invalid product number");
			return;
		}
		prdt.switchColor(clrCode, colorName);
	};
	this.mouseOver = function (prdtNum) {
		var prdt = this.getProduct(prdtNum);
		if (prdt === null) {
			doDebug("[mouseOver] invalid product number");
			return;
		}
		prdt.showAlternate(prdtNum);
	};
	this.mouseOut = function (prdtNum) {
		var prdt = this.getProduct(prdtNum);
		if (prdt === null) {
			doDebug("[mouseOver] invalid product number");
			return;
		}
		prdt.showFront(prdtNum);
	};
	this.tryItOn = function (prdtNum) {
		var prdt = this.getProduct(prdtNum);
		if (prdt === null) {
			doDebug("[tryItOn] prdt is null");
			return;
		}
		doDebug("[tryItOn] style:color="+prdt.styleNum +":"+prdt.getCurrentColorCode());  
		MVMModelTag.tryOn(prdt.styleNum, prdt.getCurrentColorCode(), 'leus');
		return false;
	};
	this.linkToProductPage = function (prdtNum) {
		doDebug("[linkToProductPage] prdtNum=" + prdtNum);
		var prdt = this.getProduct(prdtNum);
		if (prdt === null) {
			doDebug("[linkToProductPage] prdt is null");
			return;
		}
		prdt.linkToProductPage();
	};
	this.preload = function() {
		for (var i=0; i<this.productArray.length; i++) {
			this.productArray[i].preload();
		}
	};
}

function ProductViews() {
    this.productViewArray = new Array();
    
    this.getProductView = function(colorCode) {
    	var i;
    	for (i=0; i<this.productViewArray.length; i++) {
    		if (this.productViewArray[i].colorCode == colorCode) {
    			return this.productViewArray[i];
    		}
    	}
    	return null;
    };       	
}

function ProductView(colorCode, productPageURL) {
	this.colorCode = colorCode;
	this.productPageURL = productPageURL;
}


