jQuery(document).ready( function()
{
	jQuery( "#nav_categories ul li a" ).click( function( ev )
	{
		if( !jQuery( ev.target ).hasClass( 'selected' ))
		{
			strTargetClass = jQuery( ev.target ).attr('class');
			switchBanner( strTargetClass);
			switchResults( strTargetClass);
			switchDetails( strTargetClass)

			switchCategory( strTargetClass);
		}
		ev.stopPropagation();
		return false;
	});


	jQuery( "ul.subnav li a" ).click( function( ev )
	{
		if( !jQuery( ev.target ).hasClass( 'selected' ))
		{
			strTargetClass = jQuery( ev.target ).attr('class');

			switchSubTabs( ev.target);
		}
		ev.stopPropagation();
		return false;
	});
	
	jQuery(".subnav li a.all").click(function() {
		jQuery(".section").show();
	});
	
	//// Show all sunglasses on load
	jQuery(".section").show();

});

function switchBanner( strClass )
{
	jQuery( "#banner a" ).each( function(i)
	{
		if( jQuery( this ).hasClass( strClass ))
		{
			jQuery( this ).fadeIn();
			jQuery( this ).addClass( 'selected' );
		}
		else
		{
			jQuery( this ).fadeOut();
			jQuery( this ).removeClass( 'selected' );
		}
	});
}

function switchResults( strId )
{
	jQuery( "#collections>div" ).each( function(i)
	{
		if( this.id == strId )
		{
			jQuery( "#" + this.id ).show();
			jQuery( "#" + this.id ).addClass( 'selected' );
		}
		else if( this.id != "preconfigured" )
		{
			jQuery( "#" + this.id ).hide();
			jQuery( "#" + this.id ).removeClass( 'selected' );
		}
	});
}

function switchCategory( strId )
{
	jQuery( "#nav_categories ul li a" ).each( function(i)
	{
		if( jQuery( this ).hasClass( strId ))
		{
			jQuery( this ).addClass( 'selected' );
			slideHighlight( jQuery( this ));
		}
		else
		{
			jQuery( this ).removeClass( 'selected' );
		}
	});
}

function switchDetails( strId )
{
	jQuery( "#details>div" ).each( function(i)
	{
		if( jQuery( this ).hasClass( strId ))
		{
			jQuery( this ).fadeIn();
		}
		else
		{
			jQuery( this ).fadeOut();
		}
	});
}

function switchSubTabs( objTarget )
{
	strTabCat = jQuery( objTarget ).parent().parent().parent().parent().attr('id');
	strTargetClass = jQuery( objTarget ).attr('class');

	jQuery( "#" + strTabCat + " .subnav li a" ).each( function(i)
	{
		if( jQuery( this ).hasClass( strTargetClass ))
		{
			jQuery( this ).addClass( 'selected' );
		}
		else
		{
			jQuery( this ).removeClass( 'selected' );
		}
	});

	switchSubNavContent( strTabCat, strTargetClass );
}

function switchSubNavContent( strTabCat, strTargetClass )
{
	jQuery( "#collections #" + strTabCat + " .section" ).each( function(i)
	{
		if( this.id == strTargetClass || strTargetClass == "all" )
		{
			jQuery( "#" + this.id ).show();
		}
		else
		{
			jQuery( "#" + this.id ).hide();
		}
	});
}

function slideHighlight( objNavItem )
{
	// bg image for our highlight is 101px
	var intSlideTo = ((objNavItem.offset().left-jQuery("#nav_categories ul").offset().left)+30)+Math.ceil(objNavItem.width()/2)-Math.floor(101/2);

	jQuery( "#nav_categories" ).animate({backgroundPosition: '(' + intSlideTo + 'px 100%)'}, "slow");
}
