/************** List **************/  
var List = {

    strArticleID:'',
    strCurrentStream:'', 
    strCurrentArticle:'',
    nCurrentIndex:0,
    
    init:function(pBlock)
    {   
        /*            
        pBlock.find('.item').each(function(){
            // $(this).css({'cursor':'pointer'});
            
            $(this).click(function(pEvent)
            { 
                List.pItem = $(this); 
            
                if($('.article').length)
                {
                    List.hideShowArticle();    
                }
                else
                {
                    List.showArticle();     
                }
                
                pEvent.stopPropagation();
                 
            });
            $(this).find('a').click(function(pEvent){ pEvent.preventDefault(); });
        });
        */
        
        $('.more .btn_more a').unbind('click').bind('click', function(){ List.nextPage(this); });
        /*
        if ("onhashchange" in window) 
        {
            window.onhashchange = this.locationHashChanged; 
        }
        */
    },   
    
    showArticle:function()
    {         
        List.htmlArticle = '';         
        var strArticleLocation = $(List.pItem).attr('id'); 
         
        if(strArticleLocation.indexOf('a') > 0)   // atricle -> s1-a1 (streamID-ArticleID)
        {                     
            var strCurrentLocation = location.href.replace(/#.+$/, '');
            
            //location.href = strCurrentLocation + '#' + strArticleLocation;
             
            var strArticleURL = $(List.pItem).find('a').attr('href');
            //alert(strArticleURL);
            if(strArticleURL)
            {
                List.pListRow = $(List.pItem).parent(); 
                
                
                /*** selectItem ***/  
                var arID = List.pItem.attr('id').split("-");
                if(arID[1])
                {          
                    var nIndex = $('#'+arID[0]).find('div.item').index(List.pItem);   
                    //alert(nIndex);
                    
                    //alert(arID[0]);
                    List.selectItem(nIndex, arID[0]);    
                }
                /** * **/  
                // alert(strArticleURL);
                $.ajax({
                    cache: false,
                    url:strArticleURL,  
                    success: function(data) 
                    {
                        if(data != '')
                        {       
                            List.strArticleID = arID[1];
                            List.htmlArticle = data;  
                            Stream.bArticle = true;  
                        }
                    }
                }); 
                
                //$(document).scrollTo('#'+strArticleLocation, { duration: 400, easing:'easeInOutExpo', axis:"y", onAfter:function(){ location.href = strCurrentLocation + '#' + strArticleLocation; List.initArticle(); } });
                
                //location.href = strCurrentLocation + '#' + strArticleLocation; 
                //$(window).scrollTop($(window).scrollTop()-40);
                //alert(strArticleLocation);
                
                var nTop = parseInt($('#'+strArticleLocation).position().top);
                //$(window).scrollTop(nTop-30);
                //alert(nTop);
                if(nTop > 30)
                {
                    $(document).scrollTo(nTop-30, { duration: 400, easing:'easeInOutExpo', axis:"y" }); 
                }
                     
                List.initArticle();
                
                // pAnimBlock.animate({'opacity':1, 'left':0, 'width':List.pListRow.width(), 'height':List.pListRow.height()}, 200, List.initArticle);            
            }  
        }
        else if(strArticleLocation.indexOf('b') > 0)   // atricle -> s1-b1 (streamID-BannerID)
        {        
            var strBannerURL = $(List.pItem).find('a').attr('href');    
            if(strBannerURL.match('^http'))
            {
                location.href = strBannerURL;
            }    
        } 
        // Inquiry -> s1-i1 (streamID-InquiryID)        
    },
    
    initArticle:function()
    {
        //alert('initArticle');
        if(List.htmlArticle != '')
        {
            // $(document).scrollTo('#article', { duration: 400, axis:"y" });
            //
            
            List.pListRow.prepend(List.htmlArticle);
            var nArticleHeight = List.pListRow.find('.article').height();
            List.pListRow.find('.article').css({'opacity':1, 'height':0, 'overflow':'hidden'});
            List.pListRow.find('.article').animate({'opacity':1, 'height':nArticleHeight}, 1000, function(){ $(this).css({'overflow':'auto', 'height':'auto'}); } );
            
            var nTop = parseInt($('#article').position().top);
            //alert(nTop); 
            var strCurrentLocation = location.href.replace(/#.+$/, ''); 
            
            location.href = strCurrentLocation + '#' + List.strCurrentArticle; 

            if(nTop > 30)
            {  
                $(window).scrollTop(nTop-30);  
            } 
            
            Slideshow.init();
            Inquiry.initArticlePolls();
            Emotions.init();
            Comments.init();
            CForward.init();
            CUserPopup.init();
                 
            // $('body').bind("click", List.hideArticle );
            $('.close').bind("click", function(pEvent){ List.hideArticle(); pEvent.preventDefault(); } );  
            $('.article').bind("click", function(pEvent){ pEvent.stopPropagation(); }); 
        }
        else
        {
            setTimeout(List.initArticle, 50);
        }
    },
    
    hideArticle:function(bSlide)
    {                 
        //alert(bSlide);
        // $('.article').remove();
        if(List.pListRow)
        {
            var pArticle = List.pListRow.find('.article'); 
            if(pArticle.length)  
            {   
                pArticle.remove();
                
                var strCurrentLocation = location.href.replace(/#.+$/, '');
                location.href = strCurrentLocation + '#' + List.pListRow.parent().parent().attr('id'); 
                
                var nTop = parseInt($('#'+List.strCurrentArticle).position().top); 
                //alert('scrollTo');
                $(window).scrollTop(nTop-30);
                //$(document).scrollTo(nTop-30, { duration: 400, easing:'easeInOutExpo', axis:"y", onAfter:function(){ location.href = strCurrentLocation + '#' + List.pListRow.parent().parent().attr('id'); $(window).scrollTop(nTop-30); } }); 

                Stream.bArticle = false; 
                List.strArticleID = '';
                // List.pItem = null;
            }          
        }

        //alert(List.htmlListRow);
        //$('body').unbind("mousedown", List.hideArticle);
        //$('.article').unbind("mousedown"); 
         
        List.pListRow = null;
        List.htmlArticle = '';
        // List.htmlListRow = '';       
    },  
    
    hideShowArticle:function()
    {
        $('#article').remove();
        List.pListRow = null;
        List.htmlArticle = '';
        
        setTimeout(List.showArticle, 50);    
    },
    
    selectItem:function(nIndex, strStreamID)
    {
        // alert(nIndex + ' : ' + strStreamID); 
        if(List.strCurrentStream != '' && List.strCurrentArticle != '')
        {
           $('#'+List.strCurrentStream+' #'+List.strCurrentArticle + ' .iborder').removeClass('current'); 
        }
        
        List.strCurrentStream = strStreamID;
        List.nCurrentIndex = nIndex;
        List.strCurrentArticle = $('#'+strStreamID+' .item').eq(nIndex).attr('id');
        $('#'+List.strCurrentStream+' #'+List.strCurrentArticle + ' .iborder').addClass('current'); 
        List.pItem = $('#'+List.strCurrentStream+' #'+List.strCurrentArticle);
        
        //alert(List.strCurrentStream + ' : ' + List.strCurrentArticle);   
    },
    
    removeSelection:function()
    {
        if(List.strCurrentStream != '' && List.strCurrentArticle != '')
        {
           $('#'+List.strCurrentStream+' #'+List.strCurrentArticle + ' .iborder').removeClass('current'); 
        }
        
        List.strCurrentStream = '';
        List.nCurrentIndex = 0;    
    },
    
    selectPrev:function()
    {
        //alert(List.strCurrentIndex);
        if(List.nCurrentIndex > 0)
        {
            List.selectItem(List.nCurrentIndex-1, List.strCurrentStream);     
            var nTop = parseInt($('#'+List.strCurrentStream+' .item').eq(List.nCurrentIndex).position().top);
            //$(window).scrollTop(nTop-30);
            $(document).scrollTo(nTop-30, { duration: 300, easing:'easeInOutExpo', axis:"y" });
        }    
    },
    
    selectNext:function()
    {
        //alert(List.strCurrentIndex);
        if(List.nCurrentIndex < ($('#'+List.strCurrentStream+' .item').length - 1))
        {
            List.selectItem(List.nCurrentIndex+1, List.strCurrentStream);  
            //alert($('#'+List.strCurrentStream+' .item').eq(List.nCurrentIndex+1).attr('id'));
            //alert(parseInt($('#'+List.strCurrentStream+' .item').eq(List.nCurrentIndex+1).position().top));
            var nTop = parseInt($('#'+List.strCurrentStream+' .item').eq(List.nCurrentIndex).position().top);
            //$(window).scrollTop(nTop-30);
            $(document).scrollTo(nTop-30, { duration: 300, easing:'easeInOutExpo', axis:"y" });
            //$(window).scrollTop($('#'+List.strCurrentStream+' .item').eq(List.nCurrentIndex+1).position().top-30);   
        }
        else if(List.nCurrentIndex == ($('#'+List.strCurrentStream+' .item').length - 1) && $('#'+List.strCurrentStream+' .more').length)
        {
            List.nextPage($('#'+List.strCurrentStream+' .more'));        
        }    
    },

    nextPage:function(pButton)
    {
        var strUrl = $(pButton).attr('rel');        
        //alert(strUrl);
        List.strCurrentStream =  $(pButton).parent().parent().parent().attr('id');
        //alert(List.strCurrentStream);
        
        var strFilterParams = CFilters.getURLParams();
        //alert(strFilterParams); 
        strUrl = strUrl.replace(/\.html$/, '/'+strFilterParams+'.html');
        //alert(strUrl); 
        
        if(strUrl)
        {
            // var strBlockID = strUrl.replace("\.html", '');
            var arPageURL = strUrl.match("page_([0-9]+)");
            List.strNextPageID = 'p'+arPageURL[1];
            $.ajax({
                cache: false,
                url:Url.encode(strUrl),
                success: function(data) 
                {
                    if(data != '')
                    {        
                        List.htmlNextPage = data; 
                        setTimeout(List.initNextPage, 500);
                    }
                }
            });
            $(pButton).parent().html('<img src="'+strRootPath+'images/ajax-loader.gif" width="160" height="24">');
        }
    },
    
    initNextPage:function()
    {
        if(List.htmlNextPage != '')
        {
            $('#'+List.strCurrentStream +' .more').replaceWith(List.htmlNextPage);
            // alert('#'+List.strCurrentStream + '-'+List.strNextPageID);
            List.init($('#'+List.strCurrentStream + '-'+List.strNextPageID)); 
            
            Inquiry.initStreamPolls($('#'+List.strCurrentStream + '-'+List.strNextPageID)); 
            
            List.htmlNextPage = '';
            List.htmlNextPageID = '';
            
            // $(document).scrollTo('#'+List.strCurrentStream + '-'+List.strNextPageID, { duration: 400, easing:'easeInOutExpo', axis:"y" }); 
            //alert($('#'+List.strCurrentStream).find('div.item').length);
            var nIndex = $('#'+List.strCurrentStream).find('div.item').index($('#'+List.strCurrentStream + '-'+List.strNextPageID + ' div.item:first')); 
            //alert(nIndex);
            
            List.selectItem(nIndex, List.strCurrentStream);
            
            /*
            var nTop = parseInt($('#'+List.strCurrentStream+' .item').eq(nIndex).position().top);
            //$(window).scrollTop(nTop-30);
            $(document).scrollTo(nTop-30, { duration: 400, easing:'easeInOutExpo', axis:"y" });
            */
        }
        else
        {
            setTimeout(List.initNextPage, 200);
        }
    },
         
    locationHashChanged:function() 
    {
        var arM = document.location.hash.match(/\-a([0-9]+)/);
        if(arM && arM[1])
        {
            if($('#article').length)
            {
                if(List.strArticleID != 'a'+arM[1])
                {
                    //alert('replace article');
                    List.pItem = $(document.location.hash);
                    List.hideShowArticle();
                }
            }
            else
            {
                //alert('show article');
                List.pItem = $(document.location.hash);
                List.showArticle(); 
            }    
        }
        else
        {
            if(document.location.hash.match(/^#comment[0-9]+$/))
            {
                $(window).scrollTop($(window).scrollTop()-30);
            }
            else
            {
                if($('#article').length)
                {
                    //alert('hide article');  
                    List.hideArticle(); 
                }         
            }
        }
    }
};
           

