// Bam Strategy ©2009
// By Simon-Pierre Alepin
// simon@bamstrategy.com / spalepin@gmail.com
// Last modification: 2009/11/04
// v 1.1


var TOP_MENU_HOVER_BG                 = new Object();
var TOP_MENU_CONFIG                   = new Object();
TOP_MENU_CONFIG[ 'opacity' ]          = 0.93;
TOP_MENU_CONFIG[ 'fadein_duration' ]  = 350;
TOP_MENU_CONFIG[ 'fadeout_duration' ] = 200;
//Used when rollover the hit area. Gives user enough time(ms) mouseover the rollover div
TOP_MENU_CONFIG[ 'rollover_timer' ]   = 100;

//Add to the array the prefix of the top menu id
//Script will add when necessary _rollover or _hit to access other div
//that are part of the menu
TOP_MENU_CONFIG[ 'div_list' ]         = new Array( 'top_menu_pregnancy', 'top_menu_infant', 'top_menu_baby', 'top_menu_toddler' );

//Holds the timout timer
TOP_MENU_CONFIG[ 'timeout_timer' ]     = new Object();
//The prefix of the rollover div is set here when shown
TOP_MENU_CONFIG[ 'current_rollover' ]  = '';
TOP_MENU_CONFIG[ 'previous_rollover' ] = ''

//Set the hover background color on links
TOP_MENU_HOVER_BG[ 'top_menu_pregnancy_rollover' ] = 'f6e7f6';
TOP_MENU_HOVER_BG[ 'top_menu_baby_rollover' ]      = 'dbebf5';
TOP_MENU_HOVER_BG[ 'top_menu_infant_rollover' ]    = 'dbf3df';
TOP_MENU_HOVER_BG[ 'top_menu_toddler_rollover' ]   = 'fff3b5';


includeScript( '/includes/js/jquery.valign.js' );
//Includes a Image preloader for CSS style sheets
includeScript( '/includes/js/jquery.imgpreload.js' );
includeScript( '/fr_ca/includes/navigation/js/preload_menu_images.js' );
//Product search
includeScript( '/fr_ca/includes/navigation/js/product_search.js' );

includeScript( '/includes/js/utils.js' );


/*********************************************************************************/
/* DO NOT ALTER ANYTHING BELOW THIS - Unless rollover behavior has to be changed */
/*********************************************************************************/

//Execute the functions once the page elemennts including DOMS are fully loaded
$( document ).ready( function()
{
	//Makes header clickable
	$( '#header_container').click(function()
	{
		window.location.href = '/fr_ca/';
	});
	$( '#header_container').css( { 'cursor':'pointer' } );
	//$.preloadCssImages();
	/* Not needed - fixes zindex issues in IE7
	$(function() {
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});	*/
	TOP_MENU_setup();				
});



function TOP_MENU_setup()
{
	var len          = TOP_MENU_CONFIG[ 'div_list' ].length;
	var currentIndex = 0;
	for ( var i = 0; i < len; i++ )
	{
		currentIndex = i;
		
		//Put null value to all timers
		TOP_MENU_CONFIG[ 'timeout_timer' ][ i ] = false;
		
		// R O L L O V E R
		$( '#' + TOP_MENU_CONFIG[ 'div_list' ][ i ] + '_rollover' ).hover( 
		function()
		{
			//MOUSEOVER
			TOP_MENU_stop_timer( this.id );
		},
		function()
		{
			//MOUSEOUT
			TOP_MENU_start_timer( this.id );
		});			
		
		// H I T  A R E A
		$( '#' + TOP_MENU_CONFIG[ 'div_list' ][ i ] + '_hit' ).hover( 
		function()
		{
			//MOUSEOVER
			TOP_MENU_show_rollover( this.id.replace( /_hit/, '_rollover' ) );
		},			
		function()
		{
			//MOUSEOUT
			TOP_MENU_start_timer( this.id.replace( /_hit/, '_rollover' ) );
		});	
		
		//Setup rollover menu hover effect		
		$( '#' + TOP_MENU_CONFIG[ 'div_list' ][ i ] + '_rollover a' ).hover(
		function()
		{
			//Bottom links shouldn't have any fx
			if ( !$( this ).parent().hasClass( "bottom" ) )
			{
				if( !$( this ).hasClass( 'none' ) )
				{
					var containerId = $( this ).parent().parent().parent().parent().get( 0 ).id;
						
					var bgColor     = '#' + TOP_MENU_HOVER_BG[ containerId ];
					$( this ).removeClass( "hover_off" );
					$( this ).addClass( "hover_on" );
					
					if ( $( this ).is( ':animated' ) )
					{
						$( this ).stop();
					}			
					
					$( this ).animate( { backgroundColor:bgColor }, 50 );
				}
			}
		},		
		function()
		{
			//Bottom links shouldn't have any fx
			if ( !$( this ).parent().hasClass( "bottom" ) )
			{			
				if( !$( this ).hasClass( 'none' ) )
				{			
					var containerId = $( this ).parent().parent().parent().parent().get( 0 ).id;
					var bgColor     = '#' + TOP_MENU_HOVER_BG[ containerId ];
					
					$( this ).removeClass( "hover_on" );
					$( this ).addClass( "hover_off" );
					
					if ( $( this ).is( ':animated' ) )
					{
						$( this ).stop();
					}
					
					$( this ).animate( { backgroundColor:'#ffffff' }, 1050 );
				}
			}
		});		
	}	
}

function TOP_MENU_hide_IE6_select()
{
	if( BAM_AD_get_IE_version() == 6 )
	{	
		try
		{	
			$( '#fj_header_select select' ).hide();
		}
		catch(e){}
		try
		{
			$( '#header_option select' ).hide();
		}
		catch(e){}
	}
}

function TOP_MENU_show_IE6_select()
{
	if( BAM_AD_get_IE_version() == 6 )
	{
		var notNullCount = 0;
		for( var i in TOP_MENU_CONFIG[ 'timeout_timer' ] )
		{
			if( TOP_MENU_CONFIG[ 'timeout_timer' ][i] == false )
			{
				notNullCount++;
			}
		}
		
		if( notNullCount == 4 )
		{
			try
			{
				$( '#fj_header_select select' ).show();
			}
			catch(e){}
			try
			{
				$( '#header_option select' ).show();
			}
			catch(e){}
		}
	}
}

function TOP_MENU_start_timer( id )
{	
	//Had to be changed for IE6, IE7
	TOP_MENU_CONFIG[ 'timeout_timer' ][ id ] = window.setTimeout( function(){ TOP_MENU_hide_current( id );TOP_MENU_show_IE6_select(); }, TOP_MENU_CONFIG[ 'rollover_timer' ] ); 
}

function TOP_MENU_stop_timer( id )
{
	if ( TOP_MENU_CONFIG[ 'timeout_timer' ][ id ] != false )
	{
		window.clearTimeout( TOP_MENU_CONFIG[ 'timeout_timer' ][ id ] );
		TOP_MENU_CONFIG[ 'timeout_timer' ][ id ] = false;
	}
}

function TOP_MENU_hide_current( id )
{
	TOP_MENU_hide_rollover( id );
}

function TOP_MENU_show_rollover( id )
{
	TOP_MENU_stop_timer( id );
	$( '#' + id ).css( { 'opacity':TOP_MENU_CONFIG[ 'opacity' ] } );
	$( '#' + id ).fadeIn( TOP_MENU_CONFIG[ 'fadein_duration' ] );	
	TOP_MENU_hide_IE6_select();
}

function TOP_MENU_hide_rollover( id )
{
	$( '#' + id ).fadeOut( TOP_MENU_CONFIG[ 'fadeout_duration' ] );	
}

function includeScript( file ) 
{
	if ( document.createElement && document.getElementsByTagName ) 
	{
		var head  = document.getElementsByTagName( 'head' )[ 0 ];	
		var script = document.createElement( 'script' );
		script.setAttribute( 'type', 'text/javascript' );
		script.setAttribute( 'src', file );	
		head.appendChild( script );
	} else 
	{
		alert( 'Your browser can\'t deal with the DOM standard.' );
	}
}


function getClassRule( classname, rulename ) 
{
	var retval;
	for ( var i = 0; i < document.styleSheets.length; i++ ) 
	{
		var style_sheet = document.styleSheets[ i ];
		if ( !style_sheet ) 
		{ 
			continue; 
		}
		
		var sheet_rules = style_sheet.cssRules || style_sheet.rules;
		
		for ( var j = 0; j < sheet_rules.length; j++ ) 
		{
			var rule = sheet_rules[ j ];
			if ( rule.selectorText ) 
			{
				var class_name = rule.selectorText;
				if (class_name == classname) 
				{
					if ( rule.style[ rulename ] ) 
					{
						retval = rule.style[ rulename ];
					}				
				}
			}
		}
	}
	return retval;
} 