$af = jQuery.noConflict();
var af = new (function(){});

/*** topnav ***/
$af(document).ready(function()
{
	var navWrap = $af('#nflnetWrap');
	var nflinBar = $af('#nflinBar');
	var nflinLink = $af('#nflinLink');
	var dropdown = $af('.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()
			{
				$af(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()
				{
					$af(this).fadeIn('fast');
				});
			});

		}
		else // change to closed state
		{
			dropdown.each(function()
			{
				$af(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');
			});
		}
						
	});
	

	dropdown.each(function(index) 
	{
		$af(this).mouseover(function()
		{
			$af('ul', this).clearQueue();
			$af('ul', this).slideDown('fast');
		});
		$af(this).mouseout(function()
		{
			$af('ul', this).delay('slow').slideUp('fast');
		});
	});
});

/*** topnav ***/
af.topnav = new (function(){
	
	this.mainnav = {
		btns : [],
		flyout : null,
		flyoutwrap : null,
		subribbons : null,
		activeitem : null,
		
		
		init: function(){
			this.btns = $af('#main_nav_ribbon li');
			this.flyout = $af('#ribbon_dropdown');
			this.flyoutwrap = $af('#ribbon_dropdown_container');
			this.subribbons = $af('.subribbon', this.flyout);
			
			$af(this.btns).each(function(){
				//console.log(this);
				$af(this).mouseenter(function(){
					af.topnav.mainnav.btnover(this);
				});
				$af(this).click(function(){
					if($af('img', this).css('top') == '0px' ) {
						af.topnav.mainnav.btnover(this);
					}
				});
				$af(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);
			$af(this.activeitem).stop(true,true).animate({top: '0'}, 250);
			this.activeitem = $af('img', scope); 
			$af('img', scope).stop(false,true).delay(250).animate({top: '-50'}, 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';
			$af(this.subribbons).each(function(){
				if(this.id != id) {
					$af(this).hide();
				} else {
					$af(this).delay(250).fadeTo(250,1);
				}
			});
		
		},
		
		closebtn : function(btn) {
			$af(btn).stop(false,true).animate({top: '0'}, 250, function() {
		  		$af(this).parent().removeClass('mainnav-active');
		  		$af(this).parent().removeAttr('style');
		  	});
		}
			
	};

	
	this.tagnav = {
		groups : [],
		tagGroups : [],
		groupIndex : 0,
		selectLabel : null,
		prevBtn : null,
		nextBtn : null, 
		
		
		init: function(){
			this.tagGroups = $af('#contentTags ul');
			for(var i=0; i<this.tagGroups.length; i++){
				this.groups.push( $af(this.tagGroups[i]).attr('rel').replace('and', '&amp;').toLowerCase() );
			}
			//console.log(this.groups);
			
			this.selectLabel  = $af('.tagnav-select .select-label');
			this.prevBtn = $af('.tagnav-cntrl-up');
			this.nextBtn = $af('.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(); });
			
			//$af(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);
			$af(this.tagGroups[old]).fadeTo(250,0, function(){
				$af(af.topnav.tagnav.selectLabel).html(af.topnav.tagnav.groups[af.topnav.tagnav.groupIndex]);
				$af(this).hide();
			});
			this.selectLabel.fadeTo(250,1);
			$af(this.tagGroups[this.groupIndex]).delay(300).fadeTo(250,1).show();
			
//			for(var i=0; i<this.tagGroups.length; i++){
//				(i != this.groupIndex) ? $af(this.tagGroups[i]).hide() : $af(this.tagGroups[i]).show();
//			}
		}
	
	};
})();

$af(document).ready(function(){
	af.topnav.tagnav.init();
	af.topnav.mainnav.init();


	$af('div#social_network_bugs li').live('mouseover mouseout', function(event) {		
		if (event.type == 'mouseover') {
			$af(this).stop().animate({top: '-5'}, 250);
			$af('img',this).stop().fadeTo(250,1);
		} else {
			$af(this).stop().animate({top: '0'}, 250);
			$af('img',this).stop().fadeTo(250,0);
		}
	});

	$af('div#social_network_bugs li img').fadeTo(250,0);


});

