﻿

var _compareBasket = new ProductCompareBasket("_compareBasket");


function ProductCompareBasket(variableName){
    this._variableName = variableName;
    this._basketKey = null;     // must be set by the page
    this._basketDiv = null;
    this._built = false;
    this._loaded = false;
    this._basket = null;
}
ProductCompareBasket.prototype.build = function(){
    var mainDivID = "compareBasketMainDiv";
    /*
    var mainDivID = this._variableName + "_MainDiv";
    var html = "<div id=\"" + mainDivID + "\"></div>";
    document.write(html);
    */
    this._basketDiv = document.getElementById(mainDivID);
    if(this._basketDiv != null){
        this._built = true;
    }
}
ProductCompareBasket.prototype.setHtml = function(html){
    if(!this._built) return;
    this._basketDiv.innerHTML = html;
}
ProductCompareBasket.prototype.updateHtml = function(){
    var html = "";

    //html += "<h3>Product Comparison</h3>";
    
    var compareUrl;
    var findUrl;
    switch(this._basketKey){
        case "Broadcast":
            compareUrl = "/Broadcast/ProductComparison.aspx";
            findUrl = "/Broadcast/ProductFinder/";
            break;
        case "DigitalCinema":
            compareUrl = "/DigitalCinema/ProductComparison.aspx";
            findUrl = "/DigitalCinema/ProductFinder/";
            break;
        case "Security":
            compareUrl = "/Security/ProductComparison.aspx";
            findUrl = "/Security/ProductFinder/";
            break;
    }
    
    html += "<a href=\"" + findUrl + "\"><b>Product Quick Finder >></b></a><br/><br/>";
    
    
    html += "<b>Comparison Cart:&nbsp;&nbsp;</b>" + this._basket.ItemList.length;
    html += " item" + ((this._basket.ItemList.length == 1) ? "" : "s");
    html += "<br/>";
    
    
    //html += "<tr><td colspan=\"2\" align=\"left\"><b>Comparison Cart: " + this._basket.ItemList.length;
    //html += " item" + ((this._basket.ItemList.length == 1) ? "" : "s");
    //html += "</b></td></tr>";
    if(this._basket.ItemList.length == 0){
        //html += "<tr><td colspan=\"2\" align=\"left\">(no products added)</td></tr>";
        html += "<p class=\"comparisonCartInsructions\">Find products and then add them to the comparison cart for a side by side feature comparison.</p>";
    }else{
        html += "<table cellpadding=\"0\" cellspacing=\"10\" border=\"0\">";
        for(var i=0; i<this._basket.ItemList.length; i++){
            var item = this._basket.ItemList[i];
            html += "<tr>";
            html += "<td><a href=\"javascript:" + this._variableName + ".removeItem(" + item.ProductID + ");\">X</a></td>";
            html += "<td><span onmouseover=\"javascript:onPeekableMouseOver(this, 'prod_" + item.ProductID + "', 'right');\" style=\"cursor:default;\">" + item.Product.Name + "</span></td>";
            html += "</tr>";
        }
        html += "<tr><td colspan=\"2\" align=\"right\"><a href=\"" + compareUrl + "\">Compare Products >></a></td></tr>";
        html += "</table>";
    }
    

    
    
    html += "<br/>";

    //html


    this.setHtml(html);
}


ProductCompareBasket.prototype.loadBasket = function(basketKey){
    if(basketKey == null || basketKey == ""){
        alert("basket key not set.");
        return;
    }
    if(!this._built) this.build();
    if(!this._built) return;
    this._basketKey = basketKey;
    var me = this;
    ProductCompareBasketService.GetBasket(basketKey, function(result){me.loadBasketSuccess(result);}, function(error, userContext, methodName){me.loadBasketFailure(error, userContext, methodName);});  
}
ProductCompareBasket.prototype.loadBasketSuccess = function(result){
    if(result == null) return;
    this._loaded = true;
    this._basket = result.Basket;
    this.updateHtml();
}
ProductCompareBasket.prototype.loadBasketFailure = function(error, userContext, methodName){
    //alert("failed");
}


ProductCompareBasket.prototype.addItem = function(productID){
    if(!this._loaded) return;
    var me = this;
    ProductCompareBasketService.AddItem(this._basketKey, productID, function(result){me.addItemSuccess(result);}, function(error, userContext, methodName){me.addItemFailure(error, userContext, methodName);});  
}
ProductCompareBasket.prototype.addItemSuccess = function(result){
    if(result == null) return;
    if(result.IsSuccessful){
        this._basket = result.Basket;
        this.updateHtml();
    }else{
        alert(result.ResultMessage);
    }
}
ProductCompareBasket.prototype.addItemFailure = function(error, userContext, methodName){
    //alert("failed");
}


ProductCompareBasket.prototype.removeItem = function(productID){
    if(!this._loaded) return;
    var me = this;
    ProductCompareBasketService.RemoveItem(this._basketKey, productID, function(result){me.removeItemSuccess(result);}, function(error, userContext, methodName){me.removeItemFailure(error, userContext, methodName);});  
}
ProductCompareBasket.prototype.removeItemSuccess = function(result){
    if(result == null) return;
    if(result.IsSuccessful){
        this._basket = result.Basket;
        this.updateHtml();
    }else{
        alert(result.ResultMessage);
    }
}
ProductCompareBasket.prototype.removeItemFailure = function(error, userContext, methodName){
    //alert("failed");
}