﻿function ProductShopByCategories(data){ 
        this.itemData = data.split("^");
        this.CategoryId = function (){  return this.itemData[0]; };
        this.Name = function (){  return this.itemData[1]; }; 
    }
   
    
  function LoadProductShopByCategories(){
        var shopByCategoryContainer = $get('shopByCategory');
        shopByCategoryContainer.innerHTML = "<h3><%=Title %></h3></br><div style='color:gray;'><img src=\"images/ajax-loader.gif\"/>Loading....</div>";
       
  
       if(getQueryVariable('c') != null)
         BMW.Lifestyles.Web.Services.Products.ProductShopByCategories(getQueryVariable('c'), CompletedLoadProductShopByCategories, FailedLoadProductShopByCategories);
       else
         shopByCategoryContainer.innerHTML = "<h3>Shop By Category</h3>";
    }
    
    function FailedLoadProductShopByCategories(result, eventArgs){
        var shopByCategoryContainer = $get('shopByCategory');
        shopByCategoryContainer.innerHTML = "<h3>Shop By Category</h3>";
        }
    
    function CompletedLoadProductShopByCategories(result, eventArgs){
    
        var shopByCategoryHTMLData = "";
        var shopByCategoryContainer = $get('shopByCategory');
        var shopByCategoryTitle ="<h3>Shop By Category</h3>";
        var shopByCategoryFormat  ="<li class='{2}'><a href='ProductList.aspx?c={0}'>{1}</a></li>"
        if ( result.length == 0 ){
            shopByCategoryContainer.innerHTML = "";
        }
        else
        {
            shopByCategoryContainer.innerHTML = "";
            var currentCategoryId = getQueryVariable('c');
            
            for(i=0; i < result.length; i++){
               var sbc = new ProductShopByCategories(result[i]);
               
               var className="";
               if(currentCategoryId == sbc.CategoryId()){className="current";}
               
                shopByCategoryHTMLData = shopByCategoryHTMLData + String.format(shopByCategoryFormat,sbc.CategoryId(),sbc.Name(),className );
            }
            shopByCategoryContainer.innerHTML =  shopByCategoryTitle + "<ul>" + shopByCategoryHTMLData + "</ul>";
        }
    }

