$(document).ready(function() {
	var finalWidth = '704px';
	var vertWidth = '23px';
	
	// navigation items
	var menu = '#navigation li.menu a, #sidebar .our-food a, #sidebar a.sub';
	var specials = '#navigation li.specials a';
	var coldsubs = '#navigation li.coldsubs a';
	var hotsubs = '#navigation li.hotsubs a';
	var salads = '#navigation li.salads a';
	var sides = '#navigation li.sides a';
	var allnav = '#navigation li ul li a';
	var subnav = '#navigation ul';
	var menuChildren = '#sidebar a.sub div, li.menu a div, div.our-food a div';
	
	// functions for later use
	function resizeContent() {
		var contentHeight = $('').height();
		$('#container, #menu, #surfing, #story, #contact, #sidebar').css({height:contentHeight});
	}

	function mainHeight() {
		var docHeight = $(document).height();
		$('.main').css({height:'100%'});
		$('.bottom').css({'bottom':'0'});
	}
	
	function menuAnim() {
		var docHeight = $(document).height();
		$(allnav).css({'background-position':''});
		$('#container').css({height:docHeight});
	}
	
	// global animations for opening interior pages
	function animateAll() {
		$('.vert-nav, #sidebar .our-food, #lw').show();
		$('#menu, #sidebar').css({backgroundColor:'#e5e1df',backgroundImage:'url(assets/img/global/bg-container.png)'});
		$('#surfing').css({backgroundColor:'#59350c',backgroundImage:'url(assets/img/surfing/bg-surfing.png)'});
		$('#story').css({backgroundColor:'#4a580b',backgroundImage:'url(assets/img/story/bg-story.png)'});
		$('#contact').css({backgroundColor:'#30718d',backgroundImage:'url(assets/img/contact/bg-contact.png)'});
		$('#sidebar .our-food').animate({width:'238px'}, 700);
		$('.main, #sand, #sidebar a.sub, '+menuChildren).hide();
	}
	
	// main menu hovers
	$('#sidebar a.sub, li.menu a, div.our-food a').hover(function(){
		if (jQuery.browser.msie) { $(menuChildren).show(); } 
			$(menuChildren).stop(true, true).fadeIn(700); 
		}, function() { 
		if (jQuery.browser.msie) { $(menuChildren).hide(); }
			$(menuChildren).stop(true, true).fadeOut(300);
	});
	
	// main hovers (text)
	$('#surfing a.sub, #story a.sub, #contact a.sub, li.specials a').hover(function(){
		if (jQuery.browser.msie) { $(this).children('div').show(); } 
			$(this).children('div').stop(true, true).fadeIn(700); 
		}, function() { 
		if (jQuery.browser.msie) { $(this).children('div').hide(); }
			$(this).children('div').stop(true, true).fadeOut(300);
	});

	// main hovers (images)
	$('a.sub').hover(function(){
		$(this).siblings('div').children('img').stop(true, true);
		$(this).siblings('div').children('img.front').animate({top:'-2px'}, 300).animate({top:'0'}, 50)
		$(this).siblings('div').children('img.design').animate({top:'-4px'}, 400).animate({top:'0'}, 100);
		$(this).siblings('div').children('img.back').delay(150).animate({top:'-4px'}, 300).animate({top:'0'}, 100);
			}, function() {
		$(this).siblings('div').children('img').stop(true, true);
		$(this).siblings('div').children('img.front').animate({top:'190px'}, 300);
		$(this).siblings('div').children('img.design').animate({top:'190px'}, 400);
		$(this).siblings('div').children('img.back').animate({top:'190px'}, 400);
	});
	
	// opens menu to default (cold subs)
	$(menu).click(function(){
		animateAll();
		$('#surfing .header, #surfing .content, #story .header, #story .content, #contact .header, #contact .content, #hotsubs, #salads, #sides, #specials').hide();
		$('#menu').css({left:'232px',width:finalWidth});
		$('#menu').children('.header, .content').show();
		$('#coldsubs').show();
		$('#surfing').animate({left:'913px',width:vertWidth});
		$('#story').animate({left:'936px',width:vertWidth});
		$('#contact').animate({left:'959px',width:vertWidth});
		$(subnav).slideDown(500);
		menuAnim();
		$(coldsubs).css({'background-position':'-140px 0'});
	});	
	
	// opens suring
	$('#surfing .vert-nav, #surfing .sub').click(function(){
		animateAll();
		$('#menu .header, #menu .content, #story .header, #story .content, #contact .header, #contact .content, #surfing .vert-nav').hide();
		$('#surfing').animate({left:'232px',width:finalWidth});
		$('#surfing .header, #surfing .content').show();
		$('#story').animate({left:'936px',width:vertWidth});
		$('#contact').animate({left:'959px',width:vertWidth});
		$('#story .vert-nav').css({left:'0',right:''});
		$(subnav).slideUp(300);
		resizeContent();
	});
	
	// opens story
	$('#story .vert-nav, #story .sub').click(function(){
		animateAll();
		$('#menu .header, #menu .content, #surfing .header, #surfing .content, #contact .header, #contact .content, #story .vert-nav').hide();
		$('#story').animate({left:'255px',width:finalWidth});
		$('#story .header, #story .content').show();
		$('#surfing').animate({left:'232px',width:vertWidth});
		$('#contact').animate({left:'959px',width:vertWidth});
		$(subnav).slideUp(300);
		resizeContent();
	});
	
	// opens contact
	$('#contact .vert-nav, #contact .sub').click(function(){
		animateAll();
		$('#menu .header, #menu .content, #surfing .header, #surfing .content, #story .header, #story .content, #contact .vert-nav').hide();
		$('#contact').animate({left:'278px',width:finalWidth});
		$('#contact .header, #contact .content').show();
		$('#surfing').animate({left:'232px',width:vertWidth});
		$('#story').animate({left:'255px',width:vertWidth});
		$('#story .vert-nav').css({left:'',right:'0'});
		$(subnav).slideUp(300);
		resizeContent();
	});
	
	// menu sub-navigation
	$(coldsubs).click(function(){
		menuAnim();
		$('#hotsubs, #salads, #sides, #specials').hide();
		$('#coldsubs').show();
		$(this).css({'background-position':'-140px 0'});
	});	
	$(hotsubs).click(function(){
		menuAnim();
		$('#container').css({height:'3000px'});
		$('#coldsubs, #salads, #sides, #specials').hide();
		$('#hotsubs').show();
		$(this).css({'background-position':'-140px 0'});
	});	
	$(salads).click(function(){
		menuAnim();
		$('#coldsubs, #hotsubs, #sides, #specials').hide();
		$('#salads').show();
		$(this).css({'background-position':'-140px 0'});
	});	
	$(sides).click(function(){
		menuAnim();
		$('#coldsubs, #hotsubs, #salads, #specials').hide();
		$('#sides').show();
		$(this).css({'background-position':'-140px 0'});
	});
	$(specials).click(function(){
		animateAll();
		$('#surfing .header, #surfing .content, #story .header, #story .content, #contact .header, #contact .content, #hotsubs, #salads, #sides, #specials').hide();
		$('#menu').css({left:'232px',width:finalWidth});
		$('#menu').children('.header, .content').show();
		$('#surfing').animate({left:'913px',width:vertWidth});
		$('#story').animate({left:'936px',width:vertWidth});
		$('#contact').animate({left:'959px',width:vertWidth});
		$(subnav).slideDown(500);
		menuAnim();
		$('#coldsubs, #hotsubs, #salads, #sides').hide();
		$('#specials').show();
		$(this).css({'background-position':'0 -49px'});
	});
	
	// vertical navigation hover
	var vertSpeed = 300;
	$('#surfing .vert-nav').hover(function(){
		$(this).css({'background-color':'#a26116'});
		}, function() { 
	    $(this).css({'background-color':'#925713'});
	});
	$('#story .vert-nav').hover(function(){
		$(this).css({'background-color':'#809713'});
		}, function() { 
	    $(this).css({'background-color':'#778d11'});
	});
	$('#contact .vert-nav').hover(function(){
		$(this).css({'background-color':'#65cefe'});
		}, function() { 
	    $(this).css({'background-color':'#58c0ef'});
	});
	
	mainHeight();
	$(window).bind("resize", function() { resizeContent(); });
	
});

//css image preloader
;jQuery.preloadCssImages=function(settings){settings=jQuery.extend({statusTextEl:null,statusBarEl:null,errorDelay:999,simultaneousCacheLoading:2},settings);var allImgs=[],loaded=0,imgUrls=[],thisSheetRules,errorTimer;function onImgComplete(){clearTimeout(errorTimer);if(imgUrls&&imgUrls.length&&imgUrls[loaded]){loaded++;if(settings.statusTextEl){var nowloading=(imgUrls[loaded])?'Now Loading: <span>'+imgUrls[loaded].split('/')[imgUrls[loaded].split('/').length-1]:'Loading complete';jQuery(settings.statusTextEl).html('<span class="numLoaded">'+loaded+'</span> of <span class="numTotal">'+imgUrls.length+'</span> loaded (<span class="percentLoaded">'+(loaded/imgUrls.length*100).toFixed(0)+'%</span>) <span class="currentImg">'+nowloading+'</span></span>');}
if(settings.statusBarEl){var barWidth=jQuery(settings.statusBarEl).width();jQuery(settings.statusBarEl).css('background-position',-(barWidth-(barWidth*loaded/imgUrls.length).toFixed(0))+'px 50%');}
loadImgs();}}
function loadImgs(){if(imgUrls&&imgUrls.length&&imgUrls[loaded]){var img=new Image();img.src=imgUrls[loaded];if(!img.complete){jQuery(img).bind('error load onreadystatechange',onImgComplete);}else{onImgComplete();}
errorTimer=setTimeout(onImgComplete,settings.errorDelay);}}
function parseCSS(sheets,urls){var w3cImport=false,imported=[],importedSrc=[],baseURL;var sheetIndex=sheets.length;while(sheetIndex--){var cssPile='';if(urls&&urls[sheetIndex]){baseURL=urls[sheetIndex];}else{var csshref=(sheets[sheetIndex].href)?sheets[sheetIndex].href:'window.location.href';var baseURLarr=csshref.split('/');baseURLarr.pop();baseURL=baseURLarr.join('/');if(baseURL){baseURL+='/';}}
if(sheets[sheetIndex].cssRules||sheets[sheetIndex].rules){thisSheetRules=(sheets[sheetIndex].cssRules)?sheets[sheetIndex].cssRules:sheets[sheetIndex].rules;var ruleIndex=thisSheetRules.length;while(ruleIndex--){if(thisSheetRules[ruleIndex].style&&thisSheetRules[ruleIndex].style.cssText){var text=thisSheetRules[ruleIndex].style.cssText;if(text.toLowerCase().indexOf('url')!=-1){cssPile+=text;}}else if(thisSheetRules[ruleIndex].styleSheet){imported.push(thisSheetRules[ruleIndex].styleSheet);w3cImport=true;}}}
var tmpImage=cssPile.match(/[^\("]+\.(gif|jpg|jpeg|png)/g);if(tmpImage){var i=tmpImage.length;while(i--){var imgSrc=(tmpImage[i].charAt(0)=='/'||tmpImage[i].match('://'))?tmpImage[i]:baseURL+tmpImage[i];if(jQuery.inArray(imgSrc,imgUrls)==-1){imgUrls.push(imgSrc);}}}
if(!w3cImport&&sheets[sheetIndex].imports&&sheets[sheetIndex].imports.length){for(var iImport=0,importLen=sheets[sheetIndex].imports.length;iImport<importLen;iImport++){var iHref=sheets[sheetIndex].imports[iImport].href;iHref=iHref.split('/');iHref.pop();iHref=iHref.join('/');if(iHref){iHref+='/';}
var iSrc=(iHref.charAt(0)=='/'||iHref.match('://'))?iHref:baseURL+iHref;importedSrc.push(iSrc);imported.push(sheets[sheetIndex].imports[iImport]);}}}
if(imported.length){parseCSS(imported,importedSrc);return false;}
var downloads=settings.simultaneousCacheLoading;while(downloads--){setTimeout(loadImgs,downloads);}}
parseCSS(document.styleSheets);return imgUrls;};
