majestic_js = {
	
	init : function(){
		
		if( $( "#faceted-navigation" ) ) majestic_js.facet_navigation_show_hide();
		
		/*if( $( ".subnav" ).length )*/ majestic_js.dynamic_subnav_width();

	},
	
	facet_navigation_show_hide : function(){
	
		$facet_h4 = $( '#faceted-navigation' ).find( 'h4' );
		
		// Add show/hide button dynamically via js so users without js will not see it
		$facet_h4.css({ "cursor" : "pointer" }).append( '<span class="facet-toggle-bt facet-up"></span>' )
				 .live( 'click', function(){
						$( this ).find( ".facet-toggle-bt" )
								 .removeClass('rotate')
								 .addClass('rotate')
								 .toggleClass( "facet-up" )
								 .toggleClass( "facet-down" )
								 .end()
								 .next( "ul" )
								 .slideToggle(200);
							
				 });
	},
	
	// Alter the width of our subnavs dependant on the amount of columns
	dynamic_subnav_width : function(){

		$( document ).bind( "navigation_loaded", function(){ // subscribe to the navigation_loaded event so our script doesn't execute until the subnav has been retrieved

			var $inner_page_content, inner_page_content_right_edge;

			$inner_page_content = $( ".inner-page-content" );
			inner_page_content_margin_right = $inner_page_content.css( "margin-right" );
			inner_page_content_right_edge = $inner_page_content.offset().left + $inner_page_content.outerWidth() ;
			
			
			$( ".subnav" ).each(function(){
						
				 /////////////////////////////////////////////////////////////////////
				// Dynamically adjust the width of each .subnav to match that of the total contained columns
						
				var $subnav, $subnav_column_li, subnav_column_li_width, subnav_column_li_length, subnav_right_edge, subnav_left_margin;
				
				$subnav = $( this );
				$subnav_column_li = $subnav.children( "li" );
				subnav_column_li_width = $subnav_column_li.outerWidth();
				subnav_column_li_length = $subnav_column_li.length;
				
				$subnav.css({ "width" : (subnav_column_li_length * subnav_column_li_width) + "px"});
				
				
				 /////////////////////////////////////////////////////////////////////
				// Stop subnav overflow over the right edge of the doc
				
				subnav_left_margin = parseInt( $subnav.css("margin-left").replace('px', '') );
				subnav_right_edge = $subnav.offset().left + $subnav.outerWidth() + ( subnav_left_margin * -1 ); // Calculate the right edge of our subnav - we must grab the left margin and add it into the equation as we are hiding it to the far left when not in use

				
				if( subnav_right_edge > inner_page_content_right_edge ){
					
					var difference = subnav_right_edge - inner_page_content_right_edge;
					
					$subnav.css({"left" : "-" + difference + "px"});
				}							 
			});
		});
	}
	
}

$(function(){

	majestic_js.init();
		   
});
