
var NAVIMAGETOPMARGIN = '-39px';
var NAVIMAGEHIGHLIGHTMARGIN = '-78px';

function preload_images()
{
  var imgs = ['breakfast-solutions','our-products','school-foodservice','community-foodservice','health-nutrition'];
	var cache = [];
  var run = imgs.length;
	for( var i = run; i--; )
	{
		var cacheImage = document.createElement('IMG');
		cacheImage.src = 'http://www.esfoods.com/wp-content/themes/esf/images/home-main-nav/bg-'+imgs[i]+'.png';
		//console.log( cacheImage.src );
		cache.push(cacheImage);
		cacheImage = document.createElement('IMG');
		cacheImage.src = 'http://www.esfoods.com/wp-content/themes/esf/images/home-main-nav/bottom-'+imgs[i]+'.png';
		cache.push(cacheImage);
	}
}

function toggle( id )
{
	//console.log( id );
	if( $('#'+id).is(':visible') )
	{
		$( '#' + id + '-beacon' ).attr( 'src', 'http://www.esfoods.com/wp-content/themes/esf/images/beacon-right.gif' );
		$( '#' + id + '-beacon' ).attr( 'alt', '>' );
	}
	else
	{
		$( '#' + id + '-beacon' ).attr( 'src', 'http://www.esfoods.com/wp-content/themes/esf/images/beacon-down.gif' );
		$( '#' + id + '-beacon' ).attr( 'alt', '&darr;' );
	}
	$('#'+id).toggle();
}

function barndoor( hit, img, amt )
{
	hit.hover( 
		function()
		{
			img.css({ marginTop:0 });
		},
		function()
		{
			img.css({ marginTop:-amt });
		}
	);
}

function setup_menus()
{
	//-breakfast-solutions
	$('#breakfast-solutions').hover
	(
		function()
		{
			$('#'+this.id+'-subnav-container').show();
			$('#'+this.id+'-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$('#'+this.id+'-subnav-container').hide();
			$('#'+this.id+'-image').css( 'margin-top', $(this).attr('current') == 'true'? NAVIMAGEHIGHLIGHTMARGIN : NAVIMAGETOPMARGIN );
		}
	);
	$('#breakfast-solutions-subnav-container').hover
	(
		function()
		{
			$(this).show();
			$('#breakfast-solutions-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$(this).hide();
			$('#breakfast-solutions-image').css( 'margin-top', NAVIMAGETOPMARGIN );
		}
	);
	//-our-products
	$('#our-products').hover
	(
		function()
		{
			$('#'+this.id+'-subnav-container').show();
			$('#'+this.id+'-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$('#'+this.id+'-subnav-container').hide();
			$('#'+this.id+'-image').css( 'margin-top', $(this).attr('current') == 'true'? NAVIMAGEHIGHLIGHTMARGIN : NAVIMAGETOPMARGIN );
		}
	);
	$('#our-products-subnav-container').hover
	(
		function()
		{
			$(this).show();
			$('#our-products-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$(this).hide();
			$('#our-products-image').css( 'margin-top', NAVIMAGETOPMARGIN );
		}
	);
	//-school-foodservice
	$('#school-foodservice').hover
	(
		function()
		{
			$('#'+this.id+'-subnav-container').show();
			$('#'+this.id+'-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$('#'+this.id+'-subnav-container').hide();
			$('#'+this.id+'-image').css( 'margin-top', $(this).attr('current') == 'true'? NAVIMAGEHIGHLIGHTMARGIN : NAVIMAGETOPMARGIN );
		}
	);
	$('#school-foodservice-subnav-container').hover
	(
		function()
		{
			$(this).show();
			$('#school-foodservice-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$(this).hide();
			$('#school-foodservice-image').css( 'margin-top', NAVIMAGETOPMARGIN );
		}
	);
	//-community-foodservice
	$('#community-foodservice').hover
	(
		function()
		{
			$('#'+this.id+'-subnav-container').show();
			$('#'+this.id+'-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$('#'+this.id+'-subnav-container').hide();
			$('#'+this.id+'-image').css( 'margin-top', $(this).attr('current') == 'true'? NAVIMAGEHIGHLIGHTMARGIN : NAVIMAGETOPMARGIN );
		}
	);
	$('#community-foodservice-subnav-container').hover
	(
		function()
		{
			$(this).show();
			$('#community-foodservice-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$(this).hide();
			$('#community-foodservice-image').css( 'margin-top', NAVIMAGETOPMARGIN );
		}
	);
	//-health-nutrition
	$('#health-nutrition').hover
	(
		function()
		{
			$('#'+this.id+'-subnav-container').show();
			$('#'+this.id+'-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$('#'+this.id+'-subnav-container').hide();
			$('#'+this.id+'-image').css( 'margin-top', $(this).attr('current') == 'true'? NAVIMAGEHIGHLIGHTMARGIN : NAVIMAGETOPMARGIN );
		}
	);
	$('#health-nutrition-subnav-container').hover
	(
		function()
		{
			$(this).show();
			$('#health-nutrition-image').css( 'margin-top', 0 );
		}, 
		function()
		{
			$(this).hide();
			$('#health-nutrition-image').css( 'margin-top', NAVIMAGETOPMARGIN );
		}
	);
}

$(document).ready(function()
{
	//-menus
	setup_menus();
	preload_images();
/*
	if( typeof(IESIX) == 'boolean' && IESIX )
	{
		barndoor( $('#our-products'), $('#our-products-image'), 42 );
		barndoor( $('#school-foodservice'), $('#school-foodservice-image'), 42 );
		barndoor( $('#community-foodservice'), $('#community-foodservice-image'), 42 );
		barndoor( $('#breakfast-solutions'), $('#breakfast-solutions-image'), 42 );
		barndoor( $('#health-nutrition'), $('#health-nutrition-image'), 42 );
	}
*/
});