function Product()
{
	this.criteria = new Array();
	this.skus = new Array();	
	this.matchedSkus = new Array();
	this.listeners = new Array();
}

Product.prototype.addSku = function(sku)
{
	this.skus[this.skus.length] = sku;
	if(this.defaultSku == null || sku.isDefault)
		this.defaultSku = sku;
}

Product.prototype.addListener = function(listener)
{
	this.listeners[this.listeners.length] = listener;
}

Product.prototype.setPropertyCriteria = function(index, value, reset)
{
    if(reset) 
        for(var i=index+1; i<this.criteria.length; ++i)
            this.criteria[i] = null;
    
	this.criteria[index] = value;
	this.matchedSkus = this.findSkus();
	this.refresh();
}

Product.prototype.findSkus = function(criteria)
{
    if(criteria == null) criteria = this.criteria;
    var skus = new Array();
	for(var a=0; a<this.skus.length; ++a)
	{
		var sku = this.skus[a];
		var match = true;
		for(var i=0; i< sku.properties.length; ++i)
		{
			if(criteria[i] != '' && criteria[i] != null && sku.properties[i] != criteria[i])
				match = false;
		}
		if(match)
		{
			skus[skus.length] = sku;
		}
	}
	return skus;
}

Product.prototype.refresh = function()
{
	for(var i=0; i<this.listeners.length; ++i) 
		this.listeners[i].notifyProductCriteriaChange();
}

Product.prototype.getPropertyCriteria = function(index)
{
	return this.criteria[index];
}

Product.prototype.getSku = function()
{
	if(this.matchedSkus.length != 1) return null;
	var sku = this.matchedSkus[0];
	for(var i=0;i<sku.properties.length;++i)
	    if(this.criteria[i] != sku.properties[i])

	        return null;
	return sku;
}

Product.prototype.getQuantity = function()
{
	if(this.matchedSkus.length > 1) return null;
	return this.matchedSkus[0].quantity;
}

Product.prototype.getMinMaxPrice = function(index)
{
	var result = new Object;
	result.min = 0;
	result.max = 0;
	result.minText = '';
	result.maxText = '';
	
	for(var i=0; i<this.matchedSkus.length; ++i) {
	    var sku = this.matchedSkus[i];
		if(result.min == 0 || result.min > sku.prices[index][0])
		{
			result.min = sku.prices[index][0];
			result.minText = sku.prices[index][1];
		}
		if(result.max == 0 || result.max < sku.prices[index][0])
		{
			result.max = sku.prices[index][0];						
			result.maxText = sku.prices[index][1];	
		}
	}
	
	if(result.min == 0) result.min = result.max;
	return result;
}

Product.prototype.getOptionsByProperty = function(index)
{
    //make new criteria from index 0 - [index-1]
    var criteria = new Array();
    for (var i = 0; i < index; ++i) criteria[i] = this.criteria[i];
    var skus = this.findSkus(criteria);
	var result = new Array();
	
	/* New */
	var tmp = {};
    for (var i = 0; i < skus.length; ++i)
		tmp[skus[i].properties[index]] = true;
	
	for (var key in tmp)
		result[result.length] = key;
		
	return result;
}

function Sku(id, code, quantity, properties, images, prices, isDefault, isActualImage)
{
	this.id = id;
	this.code = code;
	this.quantity = quantity;
	this.properties = properties;
	this.images = images;
	this.prices = prices;
	this.isDefault = isDefault;
	this.isActualImage = isActualImage;
}

function PriceControl(product, ctlNameSale, saleIndex, ourIndex, retailIndex, currencyName, text)
{
	this.product = product;
	this.saleIndex = saleIndex;
	this.ourIndex = ourIndex;
	this.retailIndex = retailIndex;
	this.control = document.getElementById(ctlNameSale);	
	this.currencyName = currencyName;
	this.text = text;
	product.addListener(this);
}

PriceControl.prototype.getPriceText = function(minmax)
{
	var price;
	if(minmax.min == minmax.max)
		price = minmax.minText;
	else {
		price = minmax.minText + " - " + minmax.maxText;
	}
	return price;
}

PriceControl.prototype.notifyProductCriteriaChange = function()
{
	var saleResult = this.product.getMinMaxPrice(this.saleIndex);
	var ourResult = this.product.getMinMaxPrice(this.ourIndex);
	var retailResult = this.product.getMinMaxPrice(this.retailIndex);
    var headingPrice;
    var headingPriceName;
    // if sale price equal ourprice show our price instead
    if((saleResult.min !=0 || saleResult.max!=0 )&&(this.getPriceText(saleResult) != this.getPriceText(ourResult)) ) {
        headingPrice = saleResult;
        headingPriceName = this.text[2]; //sale price
    } else if(ourResult.min !=0 || ourResult.max != 0) {
        headingPrice = ourResult;
        headingPriceName = this.text[0]; //our price
    } else {
        headingPrice = retailResult;
        headingPriceName = this.text[1]; //Retail Price
    }
	var from = '';
	var price;
	if(headingPrice.min != headingPrice.max) from = this.text[3];
	var headingPriceText = '';
	if(headingPriceName == this.text[2])
		headingPriceText = '<SPAN class="itemDetail_price" id="salePrice">' + this.getPriceText(headingPrice) + '</SPAN>';	
		
	else headingPriceText = '<SPAN class="itemDetail_priceSmBold" id="salePrice">' + this.getPriceText(headingPrice) + '</SPAN>';	
	


    var costPriceText = '';
    if(headingPriceName != this.text[1] && (retailResult.min != 0 || retailResult.max != 0)) // if headingPrice is our price
	{
        costPriceText = '';
            /*'<SPAN class="itemDetail_priceSmBold">'
            + '<SPAN class="itemDetail_priceStrikeout">' 
            + this.getPriceText(retailResult) 
            + '</SPAN></SPAN><BR>';*/
           
    }
	else if(headingPriceName == this.text[2]){ // if headingPrice is sale price, cost price is ourprice
	
	    costPriceText = 
	    
            '<SPAN class="itemDetail_priceSmBold">'
            + '<SPAN class="itemDetail_priceStrikeout">' 
            + this.getPriceText(ourResult) 
            + '</SPAN></SPAN><BR>';
    }
    var priceVaryText = '<BR><BR>';
    if(headingPrice.min != headingPrice.max) 
        priceVaryText = 
            '<BR><SPAN style="COLOR: #999999">' + this.text[4] + '</SPAN>';
	this.control.innerHTML = costPriceText + headingPriceText + priceVaryText; 
}

function PropertySelectorControl(product, ctlName, index, text, order, ctlDivName)
{
	var me = this;
	
	function onChange()
	{
		var value = me.control.options[me.control.selectedIndex].value;	
		me.product.setPropertyCriteria(me.criteriaIndex, value, true);
	}
	this.product = product;
	this.criteriaIndex = index;
	this.control = document.getElementById(ctlName);	
	this.control.onchange = onChange;
	this.text = text;
	this.order = order;
	
	this.divControl = document.getElementById(ctlDivName);

	product.addListener(this);
}

PropertySelectorControl.prototype.notifyProductCriteriaChange = function()
{
	var me = this;
    function ordinalSort(a, b) 
    { 
        a = me.order[a];
        b = me.order[b];
        if(a > b) 
            return 1;
        if(a == b) 
            return 0;
        return -1;
    }
	//clear current options
	for (var i = this.control.options.length - 1; i >= 0; i--)
		this.control.options[i] = null;

    //get selectable options
	var properties = this.product.getOptionsByProperty(this.criteriaIndex);   
    properties.sort(ordinalSort);
		
	//add blank option and selectable options
	if(this.text != null)
	    this.control.options[this.control.options.length] = new Option(this.text, '');
	    
	for (i=0; i<properties.length; ++i)
	{
		var j = this.control.options.length;
		this.control.options[j] = new Option(properties[i], properties[i]);
		if(properties[i] == this.product.criteria[this.criteriaIndex]) 
		{
			this.control.selectedIndex = this.control.options.length-1;
		}

		// Set div over dropdown
		this.divControl.style.display = 'block';
		this.divControl.innerHTML  = this.control.options[this.control.selectedIndex].value;					
		this.control.style.display = 'none';
	}
	if(this.divControl.innerHTML == '')
	{
		this.divControl.innerHTML = this.text;
	}
    var selectedValue = '';
	if(this.control.selectedIndex != -1) 
		selectedValue = this.control.options[this.control.selectedIndex].value;
    var criteriaValue = this.product.criteria[this.criteriaIndex];
    if(criteriaValue == null) criteriaValue = '';
	if(selectedValue != criteriaValue && criteriaValue!='')
	    this.product.setPropertyCriteria(this.criteriaIndex, selectedValue, true);
	
}

function ImageControl(product, ctlName, index, ctlActualImgTxt, actualImgTxt, ctlNameProdZoom, ctlNameDragImage, ctlThumbnail1,ctlThumbnail2,ctlThumbnail3, noImageUrl)
{
	this.product = product;
	this.control = document.getElementById(ctlName);
	this.imgIndex = index;
	this.ctlActualImgTxt = document.getElementById(ctlActualImgTxt);
	this.actualImgTxt = actualImgTxt;
	this.controlZoom = document.getElementById(ctlNameProdZoom);
	this.controlDragImage =  document.getElementById(ctlNameDragImage);
	this.controlThumbnail1 = document.getElementById(ctlThumbnail1);
	this.controlThumbnail2 = document.getElementById(ctlThumbnail2);
	this.controlThumbnail3 = document.getElementById(ctlThumbnail3);
	this.noImgUrl = noImageUrl;
		
	product.addListener(this);
}

ImageControl.prototype.notifyProductCriteriaChange = function()
{
	var imgIndex = (arguments.length > 0) ? arguments[0] : this.imgIndex;

    this.ctlActualImgTxt.innerHTML = "";
	if(this.product.matchedSkus.length >= 1) {
	    this.control.src = this.product.matchedSkus[0].images[imgIndex];
		this.controlZoom.src = this.product.matchedSkus[0].images[imgIndex + 1];
		this.controlDragImage.src = this.product.matchedSkus[0].images[imgIndex];
		
		
			//set small thumbnail image1
			if(this.product.matchedSkus[0].images[this.imgIndex] != this.noImgUrl)
			{
				this.controlThumbnail1.src = this.product.matchedSkus[0].images[this.imgIndex];
				this.controlThumbnail1.style.display = "inline";
			}
			else this.controlThumbnail1.style.display = "none";
			
			if(this.product.matchedSkus[0].images[this.imgIndex+3] != this.noImgUrl)
			{
				this.controlThumbnail2.src = this.product.matchedSkus[0].images[this.imgIndex+3];
				this.controlThumbnail2.style.display = "inline";
			}
			else this.controlThumbnail2.style.display = "none";
			
			if(this.product.matchedSkus[0].images[this.imgIndex+5] != this.noImgUrl)
			{
				this.controlThumbnail3.src = this.product.matchedSkus[0].images[this.imgIndex+5];
				this.controlThumbnail3.style.display = "inline";
			}
			else this.controlThumbnail3.style.display = "none";
	

        if(this.product.matchedSkus[0].isActualImage)
	        this.ctlActualImgTxt.innerHTML = "&nbsp;";
	    else
	        this.ctlActualImgTxt.innerHTML = this.actualImgTxt;
	    
	} else {
	    this.control.src = this.product.defaultSku.images[this.imgIndex];
    }	    
}

function SkuIdControl(product, ctlName)
{
	this.product = product;
    
	this.control = document.getElementById(ctlName);
	product.addListener(this);
}

SkuIdControl.prototype.notifyProductCriteriaChange = function()
{
    var sku = this.product.getSku();
    if(sku==null)
	    this.control.value = "";
	else 
	    this.control.value = sku.id;
}

function QuantityControl(product, ctlName, defaultText, defaultValue, ctlDivName)
{
	var me = this;
	function onChange()
	{
		var value = me.control.options[me.control.selectedIndex].value;
		me.divControl.style.display = 'block';
		me.divControl.innerHTML  = me.control.options[me.control.selectedIndex].value;
		me.control.style.display = 'none';
		if(me.divControl.innerHTML == '') me.divControl.innerHTML = defaultText;
	}
	
	this.product = product;
	this.control = document.getElementById(ctlName);
	this.control.onchange = onChange;
	this.defaultText = defaultText;
	this.defaultValue = defaultValue;
	this.divControl = document.getElementById(ctlDivName);
	product.addListener(this);
}

QuantityControl.prototype.notifyProductCriteriaChange = function()
{
    while(this.control.options.length > 0)
      this.control.options[0] = null;

    var sku = this.product.getSku();    
	this.control.options[0] = new Option(this.defaultText,this.defaultValue);
	this.divControl.innerHTML = this.defaultText;
    if(sku != null)
	{
      for(i=1;i<=sku.quantity && i<=5;++i)
        this.control.options[this.control.options.length] = new Option(i, i);
	}
	
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

