﻿//<!--
/************
This file is part of Genwi JS Lib.
All Rights Reserved.

Desc: filter_dropdown. 
      Control to display filter drop-down on header across all the pages.

************/

function FilterDrodown()
{
    this.oPageObj = null;
    this.aDDData =  [];
    this.oFilterLyr = this.oFilterTextLyr = this.oCatLyr = this.oTray = this.oSubCatTray = null;
    this.aDDLayers = {'cat':{'bShow':false,'lyr':null,'btn':'by-source'},'subcat':{'bShow':false,'lyr':null,'btn':'by-subcat'},'filter':{'bShow':false,'lyr':null,'btn':'by-filter'}};
    this.bDDShow = false;
    this.aItemLinkNames = ['snapShotLink:snap:Snapshot','allItemsLink:-1:Most Recent','popularItemsLink:popular:Most Popular','ratingsItemsLink:ratings:Highest Rated'];
    
    this.init = function(pPageObj)
    {
        with(this)
        {
            if ( typeof aFilterData != 'undefined')
                aDDData = aFilterData;
            
            oPageObj = pPageObj;
            
            oFilterLyr = new HTMLLayer('pfilter_popFilter');
            oFilterTextLyr = new HTMLLayer('sFilterText');
            var oFilterBtn = _GL.getUIElem('by-filter');
            oFilterBtn.parent = this;
            aDDLayers['filter'].lyr = oFilterLyr;
            /*
            oFilterBtn.onmouseover = function()
            { return;this.parent.setMouseOver(this,'filter') }
                
            oFilterBtn.onmouseout = function()
            { this.parent.setActiveClass(this,false,'filter'); }
            */
            oFilterBtn.onclick = function(pEvent)
            { this.parent.onClick(this,'filter',pEvent); }
                
            var aByFilterDims = _GL.getOffsetLT('by-filter'), 
                aContentDims = _GL.getOffsetLT('content'), oFilterElem = oFilterLyr.elem;
            
            if(oFilterElem)
            {
                oFilterElem.style.left = (aByFilterDims[0] - aContentDims[0]) + 'px';
                oFilterElem.onclick = function(pEvent){var e = pEvent ? pEvent : event;e.cancelBubble = true;}
            }
            
            oCatLyr = new HTMLLayer('catUL');
            oTray = new HTMLLayer('sourcesTray');
            aDDLayers['cat'].lyr = oTray;
            var oSourceTab = _GL.getUIElem('by-source'), oTrayElem = oTray.elem;
            if (oTrayElem)
            {
                oTrayElem.style.left = -20 + 'px';
                oTrayElem.onclick = function(pEvent){var e = pEvent ? pEvent : event;e.cancelBubble = true;}
            }
            oSourceTab.parent = this;
            /*
            oSourceTab.onmouseover = function()
            { this.parent.setMouseOver(this,'cat') }
            
            oSourceTab.onmouseout = function()
            { this.parent.setActiveClass(this,false,'cat');}
            */
            oSourceTab.onclick = function(pEvent)
            { this.parent.onClick(this,'cat',pEvent); }
            
            if (aDDData.length > 0)
                this.oCatLyr.setValue(this.getFilterHTML(aDDData['Category'],'filter'));
            
            var oSubCatTab = _GL.getUIElem('by-subcat');
            if (oSubCatTab)
            {
                oSubCatTray = new HTMLLayer('subcatTray');
                aDDLayers['subcat'].lyr = oSubCatTray;
                oSubCatTab.parent = this;
                
                oSubCatTab.onmouseover = function()
                { this.parent.setMouseOver(this,'subcat')}
                
                oSubCatTab.onmouseout = function()
                { this.parent.setActiveClass(this,false,'subcat');}
                
                oSubCatTab.onclick = function(pEvent)
                { this.parent.onClick(this,'subcat',pEvent); }
                
                oSubCatTray.elem.onclick = function(pEvent){var e = pEvent ? pEvent : event;e.cancelBubble = true;}
            }
        }
    }
    
    this.getFilterHTML = function(pData, pParam)
    {
        var oD = pData, len = oD.length, s = '';
        for (var i=0;i<len;i++)
        {
            var t = oD[i].title;
            if (t.length >= 20)
                t = t.substr(0,20) + '...';
            s += '<li onmouseover="this.className=\'active\'" onmouseout="this.className=\'\'">'; 
            s += '<a href="'+sPageName+'/filter/'+oD[i].id+'">'+t+'</a>';
            s += '</li>';
        }
        return s;
    }
    
    this.hideDD = function()
    {
        if (!this.bDDShow)
            return;
            
        var aD = this.aDDLayers;
        for (var i in aD)
        {
            var oBtn = _GL.getUIElem(aD[i].btn);
            if (oBtn && aD[i].bShow)
            {
                aD[i].lyr.show(false);
                aD[i].bShow = false;
                this.setActiveClass(oBtn,false,i);
            }
        }
        this.bDDShow = false;
    }
    
    this.hideAutoSug = function()
    {
        if (oAutoSug)
            oAutoSug.hide();
    }
    
    this.setActiveClass = function(pObj, pActive, pDD)
    {
        var b = pActive, aDD = this.aDDLayers[pDD];
        if (!b && aDD && aDD.bShow)
            return;
            
        pObj.className = b ? 'filter-widget active' : 'filter-widget';
    }
    
    this.setMouseOver = function(pObj,pDD)
    {
        if (!this.bDDShow)
            this.setActiveClass(pObj,true,pDD);   
    }
    
    this.onClick = function(pObj, pDD,pEvent)
    {
        var aDD = this.aDDLayers[pDD];
        this.hideDD();
        this.hideAutoSug();
        this.setActiveClass(pObj,true);
        aDD.bShow = true;
        aDD.lyr.show(true);
        this.bDDShow = true;
        
        if (sPageName != 'home')
        {
            var tabDims = _GL.getOffsetLT(pObj.id);
            if (tabDims)
            {
               // var yOff =  _GL.oClient.bIE ? 54 : 51;
               // aDD.lyr.elem.style.top = (tabDims[1]-yOff+25) + 'px';
            }
        }
        
        
        var e = pEvent ? pEvent : event;e.cancelBubble = true;
    }
    
}

var oFilterDD = new FilterDrodown();

