//$af = jQuery.noConflict();

var af = new (function(){});

/*** topnav nfl network navbar ***/
$(document).ready(function()
{
    var navWrap = $('#nflnetWrap');
    var nflinBar = $('#nflinBar');
    var nflinLink = $('#nflinLink');
    var dropdown = $('.dropdown');
    
    // out and in left margin settings - for aniamting in and out
    var nflinBarOutX = nflinBar.css('margin-left');
    var nflinBarInX = '445px';
    
    nflinLink.click(function(e)
    {
        e.preventDefault();
        
        if(nflinBar.hasClass('nflin-bar-off')) // change to open state
        {
            dropdown.each(function()
            {
                $(this).css('display','none');
            });
            
            nflinLink.removeClass('toleft-arrow-link');
            nflinBar.removeClass('nflin-bar-off').addClass('nflin-bar-on');	
            
            nflinBar.animate({'margin-left':nflinBarInX}, 300, function(){
                nflinLink.addClass('toright-arrow-link');
                dropdown.each(function()
                {
                    $(this).fadeIn('fast');
                });
            });

        }
        else // change to closed state
        {
            dropdown.each(function()
            {
                $(this).css('display','none');
            });
            
            nflinLink.removeClass('toright-arrow-link');
            nflinBar.removeClass('nflin-bar-on').addClass('nflin-bar-off');			
            
            nflinBar.animate({'margin-left':nflinBarOutX}, 300, function(){
                nflinLink.addClass('toleft-arrow-link');
            });
        }
                        
    });
    
    //show / hide nfl-IN dropdowns
    dropdown.each(function(index) 
    {
        $(this).mouseover(function()
        {
            $('ul', this).clearQueue();
            $('ul', this).slideDown('fast');
        });
        $(this).mouseout(function()
        {
            $('ul', this).delay('slow').slideUp('fast');
        });
    });
});


$(document).ready(function($) {


    af.topnav = new (function(){
        
        this.mainnav = {
            btns : [],
            flyout : null,
            flyoutwrap : null,
            subribbons : null,
            activeitem : null,
            
            
            init: function(){
                this.btns = $('#main_nav_ribbon li');
                this.flyout = $('#ribbon_dropdown');
                this.flyoutwrap = $('#ribbon_dropdown_container');
                this.subribbons = $('.subribbon', this.flyout);
                
                $(this.btns).each(function(){
                    //console.log(this);
                    $(this).mouseenter(function(){
                        af.topnav.mainnav.btnover(this);
                    });
                    $(this).click(function(){
                        if($('img', this).css('top') == '0px' ) {
                            af.topnav.mainnav.btnover(this);
                        }
                    });
                    $(this).mouseleave(function(){
                        af.topnav.mainnav.btnout(this);
                    });
                });
                
                this.flyout.mouseenter(function(){
                    af.topnav.mainnav.openflyout(this);
                });
                this.flyout.mouseleave(function(){
                    af.topnav.mainnav.closeflyout(this);
                });
            },
            
            btnover : function(scope){
                //console.log('btnover');
                this.swapcontent(scope);
                $(this.activeitem).stop(true,true).animate({top: '0'}, 250);
                this.activeitem = $('img', scope); 
                $('img', scope).stop(false,true).delay(250).animate({top: '-39'}, 250);
                this.openflyout(scope);
            },
            
            btnout : function(scope){
                //console.log('btnout');
                this.closeflyout(scope);
            },
            
            openflyout : function(scope){
                //console.log('openflyout');
                this.flyoutwrap.show();
                this.flyout.clearQueue();
                this.flyout.delay('750').animate({top:0}, '250');
            },
            
            closeflyout : function(scope){
                //console.log('closeflyout');
                this.flyout.clearQueue();
                this.flyout.delay('250').animate({top:-180}, '250', function(){
                    af.topnav.mainnav.flyoutwrap.hide();
                    af.topnav.mainnav.closebtn(af.topnav.mainnav.activeitem);
                });
            },
            
            swapcontent : function(scope){
                var id = scope.id;
                id = id.replace('nav_', '');
                id += '_ribbon';
                $(this.subribbons).each(function(){
                    if(this.id != id) {
                        $(this).hide();
                    } else {
                        $(this).delay(250).fadeTo(250,1);
                    }
                });
            
            },
            
            closebtn : function(btn) {
                $(btn).stop(false,true).animate({top: '0'}, 250, function() {
                    $(this).parent().removeClass('mainnav-active');
                    $(this).parent().removeAttr('style');
                });
            }
                
        };
    
        
        this.tagnav = {
            groups : [],
            tagGroups : [],
            groupIndex : 0,
            selectLabel : null,
            prevBtn : null,
            nextBtn : null, 
            
            
            init: function(){
                this.tagGroups = $('#contentTags ul');
                for(var i=0; i<this.tagGroups.length; i++){
                    this.groups.push( $(this.tagGroups[i]).attr('rel').replace('and', '&amp;').toLowerCase() );
                }
                //console.log(this.groups);
                
                this.selectLabel  = $('.tagnav-select .select-label');
                this.prevBtn = $('.tagnav-cntrl-up');
                this.nextBtn = $('.tagnav-cntrl-down');
                
                this.prevBtn.click(function(e){ e.preventDefault(); af.topnav.tagnav.moveToPrev(); });
                
                this.nextBtn.click(function(e){ e.preventDefault(); af.topnav.tagnav.moveToNext(); });
                
                //$(this.selectLabel).click(function(e){ e.preventDefault(); af.topnav.tagnav.moveToNext(); });
                
            },
            
            moveToPrev: function(){
                var old = this.groupIndex;
                this.groupIndex--;
                this.groupIndex = (this.groupIndex < 0) ? this.groups.length-1 : this.groupIndex;
                this.updateView(old);
            },
            
            moveToNext: function(){
                var old = this.groupIndex;
                this.groupIndex++;
                this.groupIndex = (this.groupIndex== this.groups.length) ? 0 : this.groupIndex;
                this.updateView(old);
            },
            
            updateView: function(old){
                
                this.selectLabel.fadeTo(250,0);
                $(this.tagGroups[old]).fadeTo(250,0, function(){
                    $(af.topnav.tagnav.selectLabel).html(af.topnav.tagnav.groups[af.topnav.tagnav.groupIndex]);
                    $(this).hide();
                });
                this.selectLabel.fadeTo(250,1);
                $(this.tagGroups[this.groupIndex]).delay(300).fadeTo(250,1).show();
                
    //			for(var i=0; i<this.tagGroups.length; i++){
    //				(i != this.groupIndex) ? $(this.tagGroups[i]).hide() : $(this.tagGroups[i]).show();
    //			}
            }
        
        };
    })();

    $(document).ready(function(){
        af.topnav.tagnav.init();
        af.topnav.mainnav.init();
    
    
        $('div#social_network_bugs li').live('mouseover mouseout', function(event) {		
            if (event.type == 'mouseover') {
                $(this).stop().animate({top: '-5'}, 250);
                $('img',this).stop().fadeTo(250,1);
            } else {
                $(this).stop().animate({top: '0'}, 250);
                $('img',this).stop().fadeTo(250,0);
            }
        });
    
        $('div#social_network_bugs li img').fadeTo(250,0);
    
    
    });

})

