﻿var InventoryCount = 0;
var QLProds = null
var QLProdSelectedIndex = 0;
function GetQuickLook(id){
    QLProdSelectedIndex = 0;
    $get("ModProdLoader").style.visibility="visible";
    $get('ProdSku').innerHTML = "";
    $get('ProdTitle').innerHTML = "";
    $get('ProdPrice').innerHTML = "";
    $get('ProdSalePrice').innerHTML = "";
    $get('ProdUrl').href = "#";
    $get('ProdImage').src = "#";
    $get('ProdColor').innerHTML = "Black --";
    $get('ProdClass').innerHTML = "";
    $find('ModDynamicProduct').show();
    setIdField(id);
    StefaniBags.QuickLook.GetQuickLook(id,CallBackGetQuickLookSuccess, CallBackGetQuickLookFailure);    
}
function CallBackGetQuickLookSuccess(results){
    try{
        var arr = new Array();
        eval(results);    
        QLProds = arr; 
        if(getIdField()>0){
            for(var p=0; p<QLProds.length; p++){
                if(QLProds[p][0]==getIdField()){
                    QLProdSelectedIndex=p;
                }
            } 
        }else{
            QLProdSelectedIndex=0;
        }
        LoadQLProd(QLProdSelectedIndex);    
    }catch(err){
        //alert(err.message);
        //alert(results);
    }
}
function LoadQLProd(index){
    QLProdSelectedIndex = index;
    
//    for(var p=0; p<QLProds.length; p++){
//        if(p==index){
//            setIdField(QLProds[p][0]);
//        }
//    }
    
    setIdField(QLProds[QLProdSelectedIndex][0]);
    
    InventoryCount = QLProds[QLProdSelectedIndex][1][13];
    
    $get('ProdSku').innerHTML = QLProds[QLProdSelectedIndex][1][0];
    $get('ProdTitle').innerHTML = QLProds[QLProdSelectedIndex][1][1];
    $get('ProdPrice').innerHTML = QLProds[QLProdSelectedIndex][1][2];
    $get('ProdSalePrice').innerHTML = QLProds[QLProdSelectedIndex][1][3];
    
    var regPrice = QLProds[QLProdSelectedIndex][1][2].replace("$","");
    var salePrice = QLProds[QLProdSelectedIndex][1][3].replace("$","")
    
    var IsPromo = QLProds[QLProdSelectedIndex][1][10];
    var IsClearance = QLProds[QLProdSelectedIndex][1][11];
    
    var IsSale = false
    if(IsClearance==true || IsPromo==true){
        IsSale = true;
    }
    
    if(IsSale==true){
        $get('ProdPrice').style.textDecoration="line-through";
        $get('ProdSalePrice').style.display="inline";
    }else{
        $get('ProdPrice').style.textDecoration="";
        $get('ProdSalePrice').style.display="none";
    }
    
    
    $get('ProdUrl').href = QLProds[QLProdSelectedIndex][1][4];
    $get('ProdImage').src = QLProds[QLProdSelectedIndex][1][7];
    $get('ProdColor').innerHTML = QLProds[QLProdSelectedIndex][1][9];
    $get('ProdClass').innerHTML = 'by ' + QLProds[QLProdSelectedIndex][1][15];
    $get('ProdColors').innerHTML = GetColors();
    $get('ProdGallery').innerHTML = GetGallery(QLProds[QLProdSelectedIndex][1][8]);
    
    //Reset Product State
    BuyBtn.style.visibility='visible';
    $get('ProdPrice').className='';
    $get('ProdSoldOut').style.display='none';
    $get('ProdInventoryState').innerHTML ='';
    $get('ProdFinalSale').innerHTML ='';
    $get('ProdPreOrder').innerHTML ='';
    //Set Product State
    
    $get('ProdInventoryState').style.display="block";
    $get('ProdFinalSale').style.display="block";
    $get('ProdPreOrder').style.display="none";
    
    $get('ProdPreOrder').innerHTML = "Available for PreOrder";
    if(QLProds[QLProdSelectedIndex][1][16] == "true"){
        $get('ProdPreOrder').style.display="block";
    }
    
    if(QLProds[QLProdSelectedIndex][1][13] == 0){
        $get('ProdSoldOut').style.display="inline";
    }
    
    if(InventoryCount == 0  ){
        $get('ProdInventoryState').innerHTML = QLProds[QLProdSelectedIndex][1][12];

        BuyBtn.style.visibility='hidden';
        $get('ProdPrice').className='strike';
        $get('ProdSalePrice').style.display="none";
    }else if(IsPromo==true && IsClearance==true){
        $get('ProdInventoryState').innerHTML ='FURTHER REDUCED';
    }else if(IsPromo==true && IsClearance==false){
        $get('ProdInventoryState').innerHTML ='ON SALE';
    }else if(IsPromo==false && IsClearance==true){
        $get('ProdInventoryState').innerHTML ='ON CLEARANCE';
    }else{
        $get('ProdInventoryState').style.display="none";
        $get('ProdSalePrice').style.display="none";
    }
        
    $get('ProdFinalSale').innerHTML = '';
    if (QLProds[QLProdSelectedIndex][1][14] == "true"){
     $get('ProdFinalSale').innerHTML = "/ FINAL SALE"
    }
    
    $get('ModProdLoader').style.visibility='hidden';
    
    return;
    
}
function CallBackGetQuickLookFailure(err){
    //alert(err.get_message());
    $get('ModProdLoader').style.visibility='hidden';
    $find('ModDynamicProduct').hide();
}
function GetColors(){
    var rtrnHtml = "";
    for(var t=0; t<QLProds.length; t++){
        if(t==QLProdSelectedIndex){
            rtrnHtml += "<div style='border: solid 1px #B366AC; cursor: pointer; float:left'><img src='/images/leading.gif' width='25' height='15' alt='"+QLProds[t][1][9]+"' title='"+QLProds[t][1][9]+"' style='border: solid 2px #FFFFFF; background-color: "+QLProds[t][1][5][1]+";' /></div>";
        }else{
            rtrnHtml += "<div style='border: solid 1px #ffffff; cursor: pointer; float:left' onmouseover='this.style.borderColor=\"#B366AC\";' onmouseout='this.style.borderColor=\"#ffffff\";'><a href=\"javascript:LoadQLProd("+t+");\"><img src='/images/leading.gif' width='25' height='15' alt='"+QLProds[t][1][9]+"' title='"+QLProds[t][1][9]+"' style='border: solid 2px #FFFFFF; background-color: "+QLProds[t][1][5][1]+";' /></a></div>";
        }
    }
    return rtrnHtml + "<div class='clear'></div>";
    
}
var GalArray = null
function GetGallery(arr){
    GalArray = arr;
    var str = ""
    var arrLen = 0;
    if(arr.length>5){
        arrLen = 5;
    }else{
        arrLen = arr.length;
    }
    for(a=0; a<arrLen; a++){
        str += "<div style='margin-bottom: 15px;'><img src='"+GalArray[a]+"' style='cursor: pointer; width:45px; height: 45px;' onclick='SetImage("+a+");' /></div>";
    }
    return str
}
function SetImage(index){
	GalleryItemSelectedIndex=index;
	$get("ProdImage").src=GalArray[index].replace('/thumb_', '/mid_');
}
var GalArray = new Array();
var GalleryItemSelectedIndex = 0;

