var Page_Updaters =  new Array();

function returnObjById(strId) {
	var tempObj = document.getElementById(strId);
	return tempObj;
}

/* PO object */
function CommercePO(
    LeadID,
    OrderTotal,
    ItemsCount,
    lastProductID

){
    this.leadID = LeadID;
    this.orderTotal = OrderTotal;
    this.itemsCount = ItemsCount;
    this.lastProductID = 0;
}

CommercePO.prototype.getRaster = function() {
    return this.rasterDDL;
}

CommercePO.prototype.ShowPanel = function(order) {
    objInlineBag.initializeInlineBag();
    var total = GetOrderAttribute(order, 'order_total_string');
    var price = objProduct.price;
    if (objProduct)
        objInlineBag.setInlineShoppingBagData(objProduct.name, objProduct.quantity, objProduct.size, price, total, GetOrderAttribute(order, 'order_items_count'));
    objInlineBag.positionInlineBag();
    objInlineBag.openInlineBag();
}

CommercePO.prototype.setSizeDimension = function(RasterValueID, RasterValueName) {
    if (objImg.className != "soldOut" && objImg.className != "selectedSoldOut") {
	    if (RasterValueID != this.selectedSizeDimension) {
            if (objProduct)
            {
                objProduct.rasterValueID = RasterValueID;    
                objProduct.size = RasterValueName;    
            }
		    this.selectedSizeDimension = RasterValueID;
		    this.activeSizeDimension = RasterValueID;
            this.setDropDownValue(RasterValueID);
            this.setSelectedSizeLabelValue(RasterValueID);
		    this.setSizeDimensionSwatches();
	    }
	}
}

CommercePO.prototype.swatchOver = function(index) {
	doMouseOver = false;
	if (index != this.selectedSizeDimension) {
		this.activeSizeDimension = index;
		doMouseOver = true;
	}
	if (doMouseOver) {
		objImg = returnObjById("swatch_"+index);
		if (objImg.className != "soldOut" && objImg.className != "selectedSoldOut") {
			objImg.className = "hover";
		}
        var CurrentSwatch = this.getSwatch(index);
        if (CurrentSwatch != null)
        {
	        var objLabel = returnObjById(this.sizeLabel);
            objLabel.innerHTML = CurrentSwatch.TranslatedName;    
        }
	}
}
CommercePO.prototype.swatchOut = function(index) {
    if (objImg.className != "soldOut" && objImg.className != "selectedSoldOut") {
	    doMouseOut = false;
	    if (index != this.selectedSizeDimension) 
	    {
		    doMouseOut = true;
	    }
	    if (doMouseOut) 
	    {
    		this.setSizeDimensionSwatches();
	    }
	}
    var objLabel = returnObjById(this.sizeLabel);
	if (this.selectedSizeDimension != -1)
	{
        var CurrentSwatch = this.getSwatch(this.selectedSizeDimension);
        if (CurrentSwatch != null)
            objLabel.innerHTML = CurrentSwatch.TranslatedName;    
    }
    else
        objLabel.innerHTML = '';    
}

CommercePO.prototype.setSizeDimensionSwatches = function() {
    for (i=0; i < this.arrayAllSizeDimension.length; i = i + 1) 
    {
        var CurrentSwatch = this.arrayAllSizeDimension[i];
		if (CurrentSwatch.Enabled) {
			var classState = (CurrentSwatch.ID == this.selectedSizeDimension ? "selected" : "normal");
		} 
		else 
		{
			var classState = (CurrentSwatch.ID == this.selectedSizeDimension ? "selectedSoldOut" : "soldOut");
		}    
	    var objSwatch = returnObjById("swatch_" + CurrentSwatch.ID);
	    objSwatch.className = classState;
    }
}

CommercePO.prototype.setDropDownValue = function(index) {
	    var objSwatch = returnObjById(this.rasterDDL);
        for (var i=0; i < objSwatch.options.length; i++) {
              if (objSwatch.options[i].value == index) {
                objSwatch.options[i].selected = true;
                objSwatch.options.selectedIndex = i;
              }
            }
}
CommercePO.prototype.setQuantity = function(index){
        if (objProduct)
            objProduct.quantity = index;
}

CommercePO.prototype.setSelectedSizeLabelValue = function(index) {
        var CurrentSwatch = this.getSwatch(this.selectedSizeDimension);
        if (CurrentSwatch != null)
        {
	        objLabel = returnObjById(this.selectedSizeLabel);
            objLabel.innerHTML = CurrentSwatch.TranslatedName;    
            objTxt = returnObjById(this.selectedSizeTxt);
            objTxt.value = CurrentSwatch.TranslatedName;    
            objValidator = returnObjById(this.selectedSizeValidator);
            objValidator.style.display = "none";
        }
}
CommercePO.prototype.getSwatch = function(index) {
        for (var i=0; i < this.arrayAllSizeDimension.length; i++) 
        {
            var CurrentSwatch = this.arrayAllSizeDimension[i];
	    	if (CurrentSwatch.ID == index)
			    return CurrentSwatch;
        }
        return null;
}


/* AJAX */
function CommerceAddToShoppingCart(productID)
{
    if (objCommercePO)
    {
        if (productID)
        {
            objCommercePO.lastProductID = productID;
            WattBike.CommerceLive.WebService.ShoppingCart.Add(objCommercePO.leadID, productID, 0, 1, CommerceAddToShoppingCart_SucceededCallback, CommerceAddToShoppingCart_FailedCallback);
        }
    }
}

function CommerceAddToShoppingCart_SucceededCallback(result)
{
    if (objCommercePO)
    {
        objCommercePO.orderTotal = GetOrderAttribute(result.value, 'order_total_string');
        objCommercePO.itemsCount = GetOrderItemCount(result.value);
        Page_ClientUpdate();
    }
}
function CommerceAddToShoppingCart_FailedCallback(result, userContext, methodName)
{
    alert('failed');
}

function GetOrderItemCount(order)
{
    var orders = order.getElementsByTagName('Order');
    if (orders.length == 1)
        orderItemsNode = orders[0].getElementsByTagName('OrderItems');
    if (orderItemsNode.length == 1)
        orderItems = orderItemsNode[0].getElementsByTagName('OrderItem');
    if (orderItems != null)
        return orderItems.length;    
}
function GetOrderAttribute(order, attribute)
{
    var orders = order.getElementsByTagName('Order');
    for (var i=0; i < orders.length; i++)
    {
        var orderAttibutes = orders[i].getElementsByTagName('OrderAtributes');
        for (var j=0; j < orderAttibutes.length; j++)
        {
            var attributes = orderAttibutes[j].getElementsByTagName('Attribute');
            for (var k=0; k < attributes.length; k++)
            {
                if (attributes[k].getAttribute('name') == attribute)
                    return attributes[k].getAttribute('value');
            }	            
        }
    }
    return null;
}

function View(
    LeadID,
    ListControlID,
    ListControlGridClass,
    ListControlListClass,
    ListButtonControlID,
    GridButtonControlID,
    ListButtonImageOn,
    ListButtonImageOff,
    GridButtonImageOn,
    GridButtonImageOff
){
    this.leadID = LeadID;
    this.listControlID = ListControlID;
    this.listControlGridClass = ListControlGridClass;
    this.listControlListClass = ListControlListClass;
    this.listButtonControlID = ListButtonControlID;
    this.gridButtonControlID = GridButtonControlID;
    this.listButtonImageOn = ListButtonImageOn;
    this.listButtonImageOff = ListButtonImageOff;
    this.gridButtonImageOn = GridButtonImageOn;
    this.gridButtonImageOff = GridButtonImageOff;

    this.SetView = function(view)
    {
        WattBike.CommerceLive.WebService.Lead.SetView(this.leadID, view);
        this.SetView_SucceededCallback(view);
    }

    this.SetView_SucceededCallback = function(view)
    {
        var objList = returnObjById(this.listControlID);
        
        var objListButton = returnObjById(this.listButtonControlID);
        var objGridButton = returnObjById(this.gridButtonControlID);
        
        if (view == 'list')
        {
            objList.className = this.listControlListClass;
            objListButton.src = this.listButtonImageOn;
            objGridButton.src = this.gridButtonImageOff;
        }
        else if (view == 'grid')
        {
            objList.className = this.listControlGridClass;
            objListButton.src = this.listButtonImageOff;
            objGridButton.src = this.gridButtonImageOn;
        }
    }
}

/* prodji kroz sve prijavljene Updatere i izvrsi ih */
function Page_ClientUpdate() {
    var i;
    for (i = 0; i < Page_Updaters.length; i++) {
        UpdaterUpdate(Page_Updaters[i]);
    }
}
function UpdaterUpdate(val) {    
    if (typeof(val.updatefunction) == "function") {
            val.updatefunction(objCommercePO); 
    }
}


