var CFilters = { 
     
    strTop: 'new',
    strEmotion: '0',
    strPeriod: '0',
    strType: '0',
    
    nTimer: null,
    nDelay: 500, 
    pCurrnet: null,
    arLabels:new Array(), 
    
    init: function() 
    {
        $('.filters >ul>li').each(function(){   
            var pItem = $(this).find('>a'); 
            if($(this).find('ul').length)
            {
                var strParenLabel = pItem.attr('rel'); 
                CFilters.arLabels.push({label:pItem.text(), value:''});
                
                pItem.bind('mouseover', function() {
                    if($(this).hasClass('parent'))
                    { 
                        if(CFilters.nTimer) { clearTimeout(CFilters.nTimer); CFilters.nTimer = null; } 
                        CFilters.hideMenu(); 
                        CFilters.pCurrnet = this;
                        $(this).addClass('over');
                        $(this).next().css({'display':'block'});
                    }
                }); 
                pItem.bind('mouseout', function(){ CFilters.nTimer = setTimeout(CFilters.hideMenu, CFilters.nDelay); });       
                pItem.bind('click', function(e){
                    if($(this).hasClass('cparent')) 
                    {
                        $(this).removeClass('cparent').addClass('parent'); 
                        //var strLabel = CFilters.setLabel($(this).text());
                        //alert(strLabel); 
                        // alert(strParenLabel + ' : ' + 'nqma'); 
                        CFilters.setValue(strParenLabel, '0');
                        $(this).text(CFilters.setLabel($(this).text()));

                        //alert('load content'); 
                        CFilters.setFilters(); 
                    }
                    e.preventDefault(); 
                });       
                
                var pSubItem = $(this).find('>ul>li>a');
                pSubItem.bind('mouseover', function(){ if(CFilters.nTimer) { clearTimeout(CFilters.nTimer); CFilters.nTimer = null; } }); 
                pSubItem.bind('mouseout', function(){ CFilters.nTimer = setTimeout(CFilters.hideMenu, CFilters.nDelay); });    
                pSubItem.bind('click', function(e){ 
                    CFilters.setLabel(pItem.text(), $(this).text()); 
                    
                    // alert(strParenLabel + ' : ' + $(this).attr('rel')); 
                    CFilters.setValue(strParenLabel, $(this).attr('rel')); 
                    
                    pItem.text($(this).text()); 
                    pItem.removeClass('parent').addClass('cparent'); 
                    CFilters.hideMenu(); 
                    
                    //alert('load content'); 
                    CFilters.setFilters();
                    e.preventDefault(); });
            }
            else
            {
                pItem.bind('click', function(e){
                    var strFilter = $(this).attr('rel');
                    //alert(strFilter); 
                    CFilters.setValue('top', strFilter);
                    switch(strFilter)
                    {
                         case 'new' :
                             $(this).addClass('current'); 
                             $(this).parent().next().find('a').removeClass('current');  
                         break;
                         
                         case 'top' :
                            $(this).addClass('current');
                            $(this).parent().prev().find('a').removeClass('current'); 
                         break;
                    } 
                    //alert('load content'); 
                    CFilters.setFilters();  
                    e.preventDefault(); 
                }); 
            }   
        });
        
        //Bind click on the search button
        $('#SearchBtn').click(function(event){
                //var strSearch = escape($('#Tag').val());
                var strSearch = $('#Tag').val(); 
                window.location = strRootPath + 'търсене/' + strSearch + '.html';
        });
    },
    
    setLabel:function(strLabel, strValue)
    {
        if(strValue)
        {
            $.each(CFilters.arLabels, function(){
                if(this.label == strLabel)
                {
                    this.value = strValue; 
                    return;
                }
            });
        }
        else
        {
            // alert(strLabel);
            var strParentLabel = ''; 
            $.each(CFilters.arLabels, function(nIndex){   
                if(this.value == strLabel)
                {
                    strParentLabel = this.label;
                    return;
                }
            });  
            return strParentLabel;         
        } 
        /*
        var strarray = '';
        $.each(CFilters.arLabels, function(){
            strarray += this.label + ':' + this.value + ', '    
        });
        alert(strarray); 
        */  
    },
    
    setValue: function(strLabel, strValue)
    {
        switch(strLabel)
        {
            case 'top' :
                CFilters.strTop = strValue;
            break;
            
            case 'emotion' :
                CFilters.strEmotion = strValue;
            break;
            
            case 'period' :
                CFilters.strPeriod = strValue;
            break;
            
            case 'type' :
                CFilters.strType = strValue;
            break;
        }    
    },

    hideMenu: function()
    {
        if(CFilters.pCurrnet)
        {
            $(CFilters.pCurrnet).removeClass('over');
            $(CFilters.pCurrnet).next().css({'display':'none'});
        }
    },
    
    setFilters: function()
    {        
        //CFilters.showLoading();
        
        var strParams = CFilters.getURLParams();

        var strTargetURL = location.href;
        strTargetURL = strTargetURL.replace(/#.+$/, '');   

        if(strTargetURL.match(/\/f-/))
        {
            strTargetURL = strTargetURL.replace(/\/f-[a-z0-9\-]+/i, strParams);
        }
        else if(strTargetURL.match(/\/articles\//))
        {
            strTargetURL = strTargetURL.replace(/\/articles\/.+/, '/index/'+strParams+'.html'); 
        }
        else if(strTargetURL.match(/\.html/))
        {
            strTargetURL = strTargetURL.replace(/\.html/, '/'+strParams+'.html');     
        }
        else if(strTargetURL.match(/\/$/)) 
        {
            strTargetURL = strTargetURL.replace(/\/$/, '/index/'+strParams+'.html');
        }
        //alert(strTargetURL); 
        
        $.ajax({
            cache: false,
            type: "GET",
            url: strTargetURL,
            success: function(data){   
                //alert(data);
                if(data != '')
                {
                    $('#streams').html(data);
                    Stream.initList();
                    // CFilters.hideLoading();
                }    
            }
        });
    },
    
    getURLParams:function()
    {
        return 'f-'+CFilters.strTop+'-'+CFilters.strEmotion+'-'+CFilters.strPeriod+'-'+CFilters.strType;     
    },
    
    showLoading: function()
    {
        $(window).bind("resize", CFilters.resizeWindow); 
        
        $('body').append('<div class="popupbg"></div>');
        var pPopupBG = $('.popupbg');
        pPopupBG.width($(window).width());
        pPopupBG.height($(window).height());
        pPopupBG.css({'opacity': 0});
         
        $('body').append('<div id="CPopupForm" class="popup"><div class="popupcontent"><div><img src="'+strRootPath+'images/ajax-loader.gif" /></div></div></div>');
        
        CPopup.bLoading = true;
        CPopup.nTimer = setTimeout(CPopup.showLoading, 500);
        
        var pPopup = $('.popup'); 
        pPopup.center();
        
        pPopupBG.animate({'opacity': 0.5}, 200);   
    },
    
    hideLoading: function()
    {
        $(window).unbind("resize", CFilters.resizeWindow); 
        var pPopupBG = $('.popupbg');
        var pPopup = $('.popup');
        var pPopupData = $('.popupcontent'); 
        
        // pPopup.remove();
        var nWidth = pPopupData.width() - 80;
        var nHeight = pPopupData.height() - 80;
        var nTop = parseInt(pPopup.css('top')) + 40;
        var nLeft = parseInt(pPopup.css('left')) + 40;
        
        $('.popupcontent div:first').animate({'opacity':0}, 200, function(){ 
            pPopup.animate({'top':nTop, 'left':nLeft, 'opacity':0}, 200);
            pPopupData.animate({'width':nWidth, 'height':nHeight}, 200, function(){ pPopup.remove(); pPopupBG.animate({'opacity': 0}, 200, function(){ pPopupBG.remove(); }); });      
        });
    },
    
    resizeWindow: function(e)
    {
        var pPopupBG = $('.popupbg');
        pPopupBG.width($(window).width());
        pPopupBG.height($(window).height());
        $('.popup').center();
    }
};

$(document).ready(function()
{ 
    CFilters.init(); 
});
