/************** Rotator **************/ 
var Rotator = { 
 
    pTimer: null,
    bPlay: true,
    nIndex: 1,
    nNumItems: 0, 
    arImgParams: new Array(),
    bAnim:false,
    
    nSlideTime: 5000,
    nFadeTime: 1000,
    
    init: function() 
    {
        this.nNumItems = $("div.topnews img").length;
        if(this.nNumItems > 1)
        {
            //Set the opacity of all images to 0         
            $("div.topnews img").each(function(nIndex) 
            { 
                $(this).css({'display': 'none', 'cursor':'pointer'});
                $(this).click(function(){ location.href = $('.accenttitles a').eq(nIndex).attr('href'); });
                Rotator.arImgParams[nIndex] = Rotator.getProperty(this); 
                $(this).removeAttr('align');
            });

            $('.topnewsbottom .paging').append('<a class="left">&nbsp;</a>');
            $('.topnewsbottom .paging').append('<span>1 / ' + this.nNumItems+ '</span>');
            $('.topnewsbottom .paging').append('<a class="right">&nbsp;</a>');
            
            var pLeftArrow =  $('.topnewsbottom .paging a.left'); 
            pLeftArrow.click(function(pEvent){ Rotator.fade(-1); pEvent.preventDefault(); });     
            
            var pRightArrow =  $('.topnewsbottom .paging a.right'); 
            pRightArrow.click(function(pEvent){ Rotator.fade(1); pEvent.preventDefault(); });
            
            //Get the first image and display it (gets set to full opacity)
            $('div.topnews img:first').css({display:'block'});
            //Display text
            this.showText(1);    
            
            if(this.bPlay)
            {
                $('div.topnews img:first').css({'left':Rotator.arImgParams[0].left+'px', 'top':Rotator.arImgParams[0].top+'px'});
                
                if(Rotator.arImgParams[0].dir == 'left')
                {
                    $('div.topnews img:first').animate({'left':Rotator.arImgParams[0].size+'px'}, Rotator.nSlideTime, 'linear');
                }
                else if(Rotator.arImgParams[0].dir == 'top')
                {
                    $('div.topnews img:first').animate({'top':Rotator.arImgParams[0].size+'px'}, Rotator.nSlideTime, 'linear');
                }
                
                this.pTimer = setTimeout('Rotator.rotate()', Rotator.nSlideTime-(Rotator.nFadeTime*2));
            }
        }
        else if (this.nNumItems == 1)
        {
            $('.topnewsbottom .accenttitles .accenttitle').css({'display':'block'});
        }
        
        $('.topnews img, .accenttitle').click(function(){
            var nLinkIndex = $(this).index();
            location.href = $('.accenttitle a').eq(nLinkIndex).attr('href');
        });
    },
    
    getProperty:function(pItem)
    {
        var strDirection = $(pItem).attr('align');
        var nItemWidth = $(pItem).width();
        var nItemHeight = $(pItem).height();
        
        var nLeft = 0;
        var nTop = 0;
        var nSlideSize = 0;
        var strDir = '';
        
        switch(strDirection)
        {
            case 'top' :
                nLeft = 0;
                nTop = 0;
                nSlideSize = 430 - nItemHeight;
                strDir = 'top';
            break;
            
            case 'bottom' :
                nLeft = 0;
                nTop = 430 - nItemHeight;
                nSlideSize = 0; 
                strDir = 'top';
            break
            
            case 'left' :
                nLeft = 0;
                nTop = 0;
                nSlideSize = 760 - nItemWidth;
                strDir = 'left';
            break
            
            case 'right' :
                nLeft = 760 - nItemWidth;
                nTop = 0;
                nSlideSize = 0;
                strDir = 'left';
            break
        }
        //alert('left:'+nLeft+', top:'+nTop+', size:'+nSlideSize+', dir:'+strDir);
        return {left:nLeft, top:nTop, size:nSlideSize, dir:strDir};
    },
        
    rotate: function (nNextID) 
    {           
        Rotator.bAnim = true; 
        
        nNextID = nNextID/1;
        if(!this.bPlay && !nNextID)
        {
            return false;
        }    
        if(nNextID == this.nIndex)
        {
            return false;
        }
                                                   
        if (this.pTimer) 
        { 
            clearTimeout(this.pTimer); 
        }
        
        //alert(nNextID); 
        
        var nCurrent = this.nIndex;
        var pCurrent = $('div.topnews img').eq(this.nIndex-1);
        
        //Get next image, when it reaches the end, rotate it back to the first image
        if(nNextID > 0)
        { 
            //alert(this.nIndex + ' -> ' + nNextID);
            var pNext =  $('div.topnews img').eq(nNextID-1);
            this.nIndex = nNextID;
        }
        else
        {
            //var pNext = pCurrent.next().length ? pCurrent.next() : $('div.topnews img:first');    
            if(pCurrent.next().length)
            {
                var pNext = pCurrent.next();
                this.nIndex++;                
            }
            else
            {
                var pNext = $('div.topnews img:first');
                this.nIndex = 1;
            }
        }
        
        pCurrent.stop(true, false);
        pCurrent.css({'z-index':'2'});
        pNext.css({'z-index':'1', display:'block'});
        
        pNext.css({'left':this.arImgParams[this.nIndex-1].left+'px', 'top':this.arImgParams[this.nIndex-1].top+'px'});
        //alert(Rotator.arImgParams[this.nIndex-1].dir);
        if(Rotator.arImgParams[this.nIndex-1].dir == 'left')
        {
            pNext.stop(true, false).animate({'left':this.arImgParams[this.nIndex-1].size+'px'}, this.nSlideTime, 'linear');
        }
        else if(this.arImgParams[this.nIndex-1].dir == 'top')
        {
            pNext.stop(true, false).animate({'top':this.arImgParams[this.nIndex-1].size+'px'}, this.nSlideTime, 'linear');
        }
              
        pCurrent.stop(true, false).animate({opacity:0}, this.nFadeTime, function() { 
            $(this).removeAttr('style').css({display:'none', 'cursor':'pointer'}); Rotator.bAnim = false; 
        });
        
        this.showText(nCurrent, this.nIndex);
        
        $('.topnewsbottom .paging span').html(this.nIndex + ' / ' + this.nNumItems); 
        
        this.pTimer = setTimeout('Rotator.rotate()', Rotator.nSlideTime-Rotator.nFadeTime); 
    },
    
    fade:function(nDir)
    {
        if(!this.bAnim)
        {
            var nNext = this.nIndex;
            nNext += nDir;
            if(nNext < 1) nNext = this.nNumItems;
            if(nNext > this.nNumItems) nNext = 1;
            this.rotate(nNext);           
        }
    },
    
    showText: function(nIndex, nNext)
    {          
        if(nNext) 
        {
            $('.topnewsbottom .accenttitles .accenttitle').eq(nIndex-1).animate({'opacity': 0}, 500, function(){ 
                $(this).css({'display':'none'});
                $('.topnewsbottom .accenttitles .accenttitle').eq(nNext-1).css({'display':'block', 'opacity': 0});
                $('.topnewsbottom .accenttitles .accenttitle').eq(nNext-1).animate({'opacity': 1.0}, 500); 
            });
        }
        else
        {     
            $('.topnewsbottom .accenttitles .accenttitle').eq(nIndex-1).css({'display':'block', 'opacity': 0});
            $('.topnewsbottom .accenttitles .accenttitle').eq(nIndex-1).animate({'opacity': 1.0}, 500);
        }
    }
       
};
