﻿// Indici Lista Designers dDes
var des_id = 0; var des_descr = 1; var des_alpha_grp =2;

// 20/10/2009 [FP] - tipoQry Designer
var deslst_id = 0; var deslst_descr = 1; var deslst_img = 2; var deslst_srcQry = 3;
var tipoQryDes_alpha = 0;
var tipoQryDes_line = 1;
var tipoQryDes_tipologia = 2;

var tipoQryDes = tipoQryDes_alpha;
var listaDesID = "";

var fromDesSearch = false;


var imgLine=[];
var imgLineAct=[];
var imgLineOn=[];
var imgListe=[];
var imgListeAct=[];
var imgListeOn=[];
var imgAllDes;
var imgAllDesAct;
var imgAllDesOn;



function getDesigners(fromHist)
{
    viewDesigner=true;
    //  07/10/2009 [FP] - add2Log
    var objParam =
        {
            gender: curGender
            , season: curSeason
        };
    add2Log((fromHist ? LOG_PAGE_HISTORY : LOG_PAGE_MENU), LOG_ACTION_DESIGNERS, objParam);    
    var obj = document.getElementById("img_designers");
    obj.src = document.getElementById("template_img_path").value + "menu/" + document.getElementById("txtLangDir").value + "designers_act.gif";
    if (fromHist && (expectedHash.toLowerCase().indexOf('calltype=designers')!=-1))
    {
        url=expectedHash.replace("#", "")+"&fromHyst="+fromHist;
    }
    else 
    {
        url="getData.aspx|CallType=Designers&season="+curSeason+"&gender="+curGender;
        if (!fromHist) makeHistory(url);
    }
    DisplayOnLoading(1);
    makeRequest(url, "LoadDesigners");
}

//Funzione di callback della chiamata ajax per designer
function LoadDesigners(responseText, noReloadLista)
{
    var myDesigners = [];
    DisplayOnLoading(0);
    
    if (!CheckQuery(curSeason,curGender, curLine))
    {
        makeErrorMex(MEX_NOITEMS_ERROR);
        return;
    }
    // 26/03/2009 [FP] - resetAll(fromHyst, 1=noResetLine, 2=noResetDesigner, 3=noResetMenuViewedItems, 4=noResetMenuSearch, 5=noResetNewArrivals, 6=noResetListaID, 7=noResetAddThisUrl, 8=noResetStyle, 9=noResetDiv)
    resetAll(fromHyst, false, true, false, false, false, false, false, false, false);   // non resettava AddThisUrl

	DisplayDivBlock("lista_designers");
	DisplayDivBlock("ul_menu_designers");

    // 20/10/2009 [FP] - tipoQry Designer
    if (!noReloadLista)
    {
        if (!fromHyst)
        {
            tipoQryDes = tipoQryDes_alpha;
            listaDesID = "";    
        }
        
        PreloadImgSelDesigner();    // Att.ne: non fa il Preload ma imposta solo le path 
        var img = "";
        // Devo aggiustare il link della Lista Designers
        var el = document.getElementById("ul_menu_designers");
        if (el)
        {
            EliminateLIFromUL(el);
            if (dLineDisp.length > 0)
            {
                for (var i = 0; i < dLineDisp.length; i++)
                {
                    for (var j = 0; j < arrLines.length; j++)
                    {
                        if (dLineDisp[i] == arrLines[j][linearr_id])
                        {
                            addMenuDesigners(el, tipoQryDes_line, arrLines[j][linearr_id], arrLines[j][linearr_descr]);
                            break;
                        }
                    }
                }
            }
            if (arrListeDes.length > 0) // in ArrListaDes ci stanno le Liste Designers
            {
                for (var i = 0; i < arrListeDes.length; i++)
                {
                    addMenuDesigners(el, tipoQryDes_tipologia, arrListeDes[i][deslst_id], arrListeDes[i][deslst_descr]);
                }
            }
            addMenuDesigners(el, tipoQryDes_alpha, "", "VEDI TUTTI");
            if (fromHyst) changeImgSelDesigner(tipoQryDes, listaDesID, true);
        }
    }

    if (tipoQryDes == tipoQryDes_line)
    {
        myDesigners = arrDesigners1;
    }
    else if (tipoQryDes == tipoQryDes_tipologia)
    {
        myDesigners = arrDesigners2;
    }
    else
    {      
        myDesigners = arrDesigners;
    }
    if (myDesigners.length==0)
    {
        makeErrorMex(MEX_NOITEMS_ERROR);
        return;
    }
   //Viene reimpostata la posizione del menu in base a quanto presente nell'hash
   
   
   var el = document.getElementById("lista_des_col3");
   el.innerHTML="";
   var el = document.getElementById("lista_des_col2");
   el.innerHTML="";
   var el = document.getElementById("lista_des_col1");
   el.innerHTML="";
   
// 20/10/2009 [FP] - tipoQry Designer
   viewElencoDesigners(myDesigners);
}

function selDesigner(node)
{
// 26/03/2009 [FP] - Serve curIDCat="" ?
//    curIDCat="";
   
    var idDesNew = node.id.replace("des", ""); // 26/03/2009 [FP] - gestione "des"+IDDes
    var searchQuery = "";   // 26/10/2009 [FP]
      
// 26/03/2009 [FP] - Serve changeImgLineMenu ?
//    changeImgLineMenu(false); 
    
    // 26/03/2009 [FP] - resetImgMenuDes
    resetImgMenuDes();
    lineDesSel = "";
    if (tipoQryDes == tipoQryDes_line)
        lineDesSel = listaDesID;
    curIDDes = idDesNew;
    curDescrDes = getAInnerText(node);
    // 26/10/2009 [FP]
    fromDesSearch = false;
    if (tipoQryDes == tipoQryDes_tipologia)
    {
        for (var i = 0; i < arrListeDes.length; i++)
        {
            if (arrListeDes[i][deslst_id] == listaDesID)
            {
                fromDesSearch = true;
                searchQuery = arrListeDes[i][deslst_srcQry];
                break;
            }
        }
    }
    //  07/10/2009 [FP] - add2Log
    skipLog = true;
    var objParam =
        {
            gender: curGender
            , season: curSeason
            , designer: curIDDes
        };
    add2Log(LOG_PAGE_DESIGNER, LOG_ACTION_SELDESIGNER, objParam);

    getListaProd(false, false, false, searchQuery);
}

function selDesignerFromList(idDesNew, descrD)
{
    lineIdFromList=""//curLine; [BV] 02/09/2009 - come da attività sito riga 69
    curIDDes = idDesNew;
    curDescrDes = descrD;
    // 26/03/2009 [FP] - resetAll(fromHyst, 1=noResetLine, 2=noResetDesigner, 3=noResetMenuViewedItems, 4=noResetMenuSearch, 5=noResetNewArrivals, 6=noResetListaID, 7=noResetAddThisUrl, 8=noResetStyle, 9=noResetDiv)
    resetAll(false, false, true, false, false, false, false, false, false, false);
    //  07/10/2009 [FP] - add2Log
    skipLog = true;
    // 26/10/2009 [FP]
    fromDesSearch = false;
    var objParam =
        {
            gender: curGender
            , season: curSeason
            , designer: curIDDes
        };
    add2Log(LOG_PAGE_LISTPROD, LOG_ACTION_SELDESIGNER, objParam);
    getListaProd(false);
}

function viewListaProdDes(prodDesId,prodDesDescr)
{
    curIDDes = prodDesId;
    curDescrDes = prodDesDescr;
    curDescrCat="";
    // 26/03/2009 [FP] - serve ?
//    searchLine="";
//    searchCateg="";
//    searchDes="";
//    searchColor="";
//    searchSize="";
//    searchPrice="";
//    searchDiscount="";
    // 26/03/2009 [FP] - resetAll(fromHyst, 1=noResetLine, 2=noResetDesigner, 3=noResetMenuViewedItems, 4=noResetMenuSearch, 5=noResetNewArrivals, 6=noResetListaID, 7=noResetAddThisUrl, 8=noResetStyle, 9=noResetDiv)
    resetAll(false, false, true, false, false, false, false, false, false, false);
    // 26/10/2009 [FP]
    fromDesSearch = false;
    //  07/10/2009 [FP] - add2Log
    var objParam =
        {
            gender: curGender
            , season: curSeason
            , designer: curIDDes
        };
    add2Log(LOG_PAGE_PRODUCT, LOG_ACTION_SELDESIGNER, objParam);

    getListaProd(false);
}

// 20/10/2009 [FP] - tipoQry Designer
function selListaDes(selTipoQry, listaDesigner, fromHist)
{
    if (tipoQryDes != selTipoQry || listaDesID != listaDesigner)
    {
        changeImgSelDesigner(tipoQryDes, listaDesID, false);
        tipoQryDes = selTipoQry;
        listaDesID = listaDesigner;
        changeImgSelDesigner(tipoQryDes, listaDesID, true);
        //[BV] 02/11/2009
        if (!fromHist)
        {
            url=expectedHash.replace("#", "");
            url+="&listaDesID="+listaDesID+"&tipoQryDes="+tipoQryDes;
            makeHistory(url);
        }
        LoadDesigners("", true);
    }
}

function addMenuDesigners(el, selTipoQry, listaDesigner, descr)
{
    var added = false;
    var id = getNameSelDesigner(selTipoQry, listaDesigner);
    var attiva = false;
    if (selTipoQry == tipoQryDes && listaDesigner == listaDesID)
        attiva = true;
    var li = document.createElement("li");
    var anc = document.createElement("a");
    anc.setAttribute("id",id);
    anc.setAttribute("href", "javascript:;");
    setEventCode(anc,"onclick","javascript:selListaDes(" + selTipoQry + ", '" + listaDesigner + "');" );
    var img_menu = "";
    if (selTipoQry == tipoQryDes_line)
    {
        for (var idx = 0; idx < dLineDisp.length; idx++)
        {
            if (dLineDisp[idx] == listaDesigner)
            {
                img_menu = imgLine[idx];
                break;
            }
        }
    }
    else if (selTipoQry == tipoQryDes_tipologia)
    {
        for (var idx = 0; idx < arrListeDes.length; idx++)
        {
            if (arrListeDes[idx][deslst_id] == listaDesigner)
            {
                img_menu = imgListe[idx];
                break;
            }
        }
    }
    else
        img_menu = imgAllDes;
    if (img_menu != "")
    {
        added = true;
        var img = document.createElement("img");
        img.setAttribute("id","img_"+id);
        img.setAttribute("alt","");
        img.src = img_menu;
        anc.appendChild(img);
    }
    else
    {
        anc.className = "lista_des_select";
        anc.innerHTML = descr;
    }
    li.appendChild(anc);
    el.appendChild(li);
    if (added)  // utilizzo added per evitare che in alcuni casi si valorizzi succ
        changeImgSelDesigner(selTipoQry, listaDesigner, attiva);
}

function viewElencoDesigners(myDesigners)
{
    var myGroup = "";
    var numDesigners = 0;
    var idxGroup = -1;
    var groupAlpha = [];
    var groupNum = [];
    var stringaAlfa = "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
    for (var i = 0; i < myDesigners.length; i++)
    {
        if (listaDesID==myDesigners[i][des_alpha_grp] || tipoQryDes == tipoQryDes_alpha)
        {
            numDesigners ++;
            var fstCh = myDesigners[i][des_descr].substring(0,1).toUpperCase();
            if (stringaAlfa.indexOf(fstCh) == -1)
                fstCh = "0-9";
            if (myGroup != fstCh)
            {
                myGroup = fstCh;
                idxGroup ++;
                groupAlpha[idxGroup] = myGroup;
                groupNum[idxGroup] = 1;
            }
            else
                groupNum[idxGroup] ++;
        }
        
    }
    // provo a calcolare le colonne per lettera
    var col1 = 0;
    var idxGroup2 = -1;
    var idxGroup3 = -1;
    var numColonna = ((numDesigners + (idxGroup*2)) /3);
    for (var i = 0; i <= idxGroup; i++)
    {
        col1 += groupNum[i]+2;
        if (col1 >= numColonna)
        {
            if (idxGroup2 == -1)
            {
                idxGroup2 = i+1;
                col1 = 0;
            }
            else 
            {
                idxGroup3 = i+1;
                break;
            }
        }
    }
    myGroup = "";
    idxGroup = -1;
    var el;
    for (var i = 0; i < myDesigners.length; i++)
    {
        if (listaDesID==myDesigners[i][des_alpha_grp] || tipoQryDes == tipoQryDes_alpha)
        {
            var fstCh = myDesigners[i][des_descr].substring(0,1).toUpperCase();
            if (stringaAlfa.indexOf(fstCh) == -1)
                fstCh = "0-9";
            if (myGroup != fstCh)
            {
                myGroup = fstCh;
                idxGroup ++;
                if (idxGroup < idxGroup2)
                    el = document.getElementById("lista_des_col1");
                else if (idxGroup < idxGroup3)
                    el = document.getElementById("lista_des_col2");
                else
                    el = document.getElementById("lista_des_col3");
                if (idxGroup != 0 && idxGroup != idxGroup2 && idxGroup != idxGroup3) 
                {
                    // Aggiunge spazio fra Lettera e Lettera
                    br = document.createElement("br");
                    el.appendChild(br);                    
                }
                var img = document.createElement("img");
                img.setAttribute("alt", "");
                img.className="alpha_des";
                img.src = "immagini/a-z/"+fstCh+".gif"
                el.appendChild(img);
                br = document.createElement("br");
                el.appendChild(br);
            }
            var a = document.createElement("a");
            a.setAttribute("id", "des"+myDesigners[i][des_id]);
            a.setAttribute("href", "javascript:;");
            setAInnerText(a, myDesigners[i][des_descr]);
            setEventCode(a,"onclick","javascript:selDesigner(this);" );
            el.appendChild(a);
            br = document.createElement("br");
            el.appendChild(br);
        
        }

    }
   
}

function getNameSelDesigner(selTipoQry, listaDesigner)
{
    var nome = "lista_des_alpha";
    if (selTipoQry == tipoQryDes_line)
        nome = "lista_des_line_" + listaDesigner;
    else if (selTipoQry == tipoQryDes_tipologia)
        nome = "lista_des_tipo_" + listaDesigner;
    return nome;
}

function changeImgSelDesigner(selTipoQry, listaDesigner, attiva)
{
    var curId = "img_"+getNameSelDesigner(selTipoQry, listaDesigner);
    var img = document.getElementById(curId);
    if (img)
    {
        var src = "";
        var src_over = "";
        if (selTipoQry == tipoQryDes_line)
        {
            for (var idx = 0; idx < dLineDisp.length; idx++)
            {
                if (dLineDisp[idx] == listaDesigner)
                {
                    if (attiva)
                    {
                        src = imgLineAct[idx];
                        src_over = imgLineAct[idx];
                    }
                    else
                    {
                        src = imgLine[idx];
                        src_over = imgLineOn[idx];
                    }
                    break;
                }
            }
        }
        else if (selTipoQry == tipoQryDes_tipologia)
        {
            for (var idx = 0; idx < arrListeDes.length; idx++)
            {
                if (arrListeDes[idx][deslst_id] == listaDesigner)
                {
                    if (attiva)
                    {
                        src = imgListeAct[idx];
                        src_over = imgListeAct[idx];
                    }
                    else
                    {
                        src = imgListe[idx];
                        src_over = imgListeOn[idx];
                    }
                    break;
                }
            }
        }
        else 
        {
            if (attiva)
            {
                src = imgAllDesAct;
                src_over = imgAllDesAct;
            }
            else
            {
                src = imgAllDes;
                src_over = imgAllDesOn;
            }
        }
        img.src = src;
        setEventCode(img,"onmouseout","javascript:this.src='" + src + "';" );
        setEventCode(img,"onmouseover","javascript:this.src='" + src_over + "';" );
    }

}


function PreloadImgSelDesigner()
{
    imgLine=new Array();
    imgLineAct=new Array();
    imgLineOn=new Array();
    if (dLineDisp.length > 0)
    {
        for (var i = 0; i < dLineDisp.length; i++)
        {
            for (var j = 0; j < arrLines.length; j++)
            {
                if (dLineDisp[i] == arrLines[j][linearr_id])
                {
                    var imgName = document.getElementById("line_img_path").value + "small/" + document.getElementById("txtLangDir").value + arrLines[j][linearr_id] + ".gif";
                    imgLine[i] = imgName;
                    imgLineAct[i] =  imgName.replace(".gif", "_act.gif");
                    imgLineOn[i] = imgName.replace(".gif", "_on.gif");
                    break;
                }
            }
        }
    }
    imgListe=new Array();
    imgListeAct=new Array();
    imgListeOn=new Array();
    if (arrListeDes.length > 0) // in ArrListaDes ci stanno le Liste Designers
    {
        for (var i = 0; i < arrListeDes.length; i++)
        {
            if (arrListeDes[i][deslst_img] != "")
            {
                var imgName = document.getElementById("des_img_path").value + "liste/" + document.getElementById("txtLangDir").value + arrListeDes[i][deslst_img];
                imgListe[i] = imgName;
                imgListeAct[i] = imgName.replace(".gif", "_act.gif");
                imgListeOn[i] = imgName.replace(".gif", "_on.gif");
            }
            else
                imgListe[i] = "";
              
        }
    }
    var imgName = document.getElementById("cat_img_path").value + document.getElementById("txtLangDir").value + "view_all_big.gif";;
    imgAllDes = imgName;
    imgAllDesAct = imgName.replace(".gif", "_act.gif");
    imgAllDesOn = imgName.replace(".gif", "_on.gif");

}