var $j=jQuery.noConflict();
$j(document).ready( function() {
	// change body.nojs to .js
	$j('body').removeClass('nojs').addClass('js');
	
	// initialize the menu app if not ie
	if($j("#menuInfo").length){menuApp.init();}
	
	// close button
	$j("a#closeBtn").live('click', function(){
		window.location=("../index.html")
		return false;
	});

	// mobile menu
	function setMenuNav () {
		// check to see if #selectQL has been added
		if ($j('#selectQL').length === 0) {
			var nav = $j('#nav');
			var menuQL = $j('#nav ul');
			// determine starting height of ul before hiding
			var qlHeight = menuQL.height();
			$j(menuQL).css('height','0').hide();
			// add in select and cache reference
			$j('<div id="selectQL"><em>Menu Items</em><span></span></div>').insertBefore(menuQL);
			var selectQL = $j('#selectQL');
			// show ul on click
			$j(selectQL).live('click', function (){
				$j(menuQL).show().animate({height : qlHeight}, 200);
				$j(selectQL).addClass('down');
			});
			// hide ul on mouseleave
			$j(menuQL).bind('mouseleave click', function(){
				$j(menuQL).animate({height : 0}, 150, function(){
					$j(this).css('display','none');
				});
				$j(selectQL).removeClass('down');
			});
			// hide ul on click - for touchscreens
			$j('#selectQL a').bind('click', function(){
				$j(menuQL).animate({height : 0}, 150, function(){
					$j(this).css('display','none');
				});
				$j(selectQL).removeClass('down');
			});
		}
	} // END set menu nav
	
	/* if minWindowSize (max-width in css) is changed CHANGE IT IN SKISHORES.CSS */

	var maxWindowSize = 640;
	var windowWidth = window.innerWidth || document.body.clientWidth;
	// set the menu nav on resize. Not in IE, it doesn't do @media queries
	if ($j.browser.msie !== true && $j('body.menu').length){
		// run the setMenuNav at least once
		if(windowWidth <= maxWindowSize){
			setMenuNav()
		}
		$j(window).resize(function() {
			windowWidth = window.innerWidth || document.body.clientWidth;
			if(windowWidth <= maxWindowSize){
				setMenuNav();
			} else {
			// set the menu back to the left side
				var menuQL = $j('#nav ul');
				$j('#selectQL').remove();
				$j(menuQL).css({height:'auto', display:'block'});
				$j(menuQL).unbind('mouseleave');
			}
		});
	}
	
	// only show popup once - set cookie
	if($j('#home').length){
		//console.log( $j.cookie("skishores") );
		// trigger the popup if it's the first visit then set the cookie
		if ($j.cookie("skishores") !== "beenhere"){
			triggerPopup ();
			$j.cookie("skishores","beenhere", { expires: 365 });
		}
	}
	
	
}); // END doc ready

// trigger the homepage popup
function triggerPopup (){
	$j("a#inline").fancybox(); 		
	$j("a#inline").trigger('click');
}
	
// menu app: get individual menus through ajax and display in div
var menuApp = {
	
	config : {
		menuLink : $j('#nav a'),
		otherLinks : $j('#nav a.selected').siblings(),
		whichMenu : "appetizers",
		menuPath : "../menu/"
	},
	
	init : function(config) {
		// display the default menu at start
		menuApp.displayMenu(menuApp.config.whichMenu)
		// send the menu name to displayMenu
		menuApp.config.menuLink.live('click', function(){
			whichMenu = $j(this).parent().attr("id");
			// set the body id to the menu name
			$j("body").attr("id",whichMenu + "Page");
			// clear other selected and add new selected
			menuApp.config.menuLink.removeClass('selected');
			$j(this).addClass('selected');
			// display the menu
			menuApp.displayMenu(whichMenu);
			return false; // prevent the page from refreshing 
		});
		
	},
	
	displayMenu : function(whichMenu) {
		// get the menu name and add .html
		whichMenu = menuApp.config.menuPath + whichMenu + ".html"
		// then display the menu in div#menuInfo
		$j.get(whichMenu, function(data) {
		  $j('#menuInfo').hide().html(data).fadeIn(350);
		});
	}
	
};

