
var site = {
		
	/* Initialize Application Variables
	**************************/
	'currentPage' 	: '',
	'menuLinks' 	: new Array(),
	'menuPages' 	: new Array(),
	'scrollLoaded'	: false,
	
	'speed'			: {
		'close'		: 250,
		'open'		: 250
	},
	
	'init'			: function()
	{
		/* Check Window Location
		**************************/
		var location = window.location.pathname.toString();
		
		/* AJAX Functionality & Image Slider
		**************************/
		// If Home Page
		if(location == '/'){
			
			// Enable AJAX
			site.enableAjax();
			
			// Start Image Rotation
			$('#slider').cycle();
		}
		
			
		/* Normalize Column Heights
		**************************/
		$('.elements').equalHeight();
		
		/* Adjust Background When Window is Resized
		**************************/
		$(window).resize(function(){ 
			var htmlHeight = $('html').height();
			var windowHeight = $(window).height();
			$('#body-bg-container').height(htmlHeight > windowHeight ? htmlHeight : windowHeight); 
		});
		// */
	},
	
	'enableAjax'	: function(){
		
		/* Modify Element Display
		**************************/
		$('.menu').hide();
		$('#content').css({
			'overflow-y'	: 'hidden',
			'opacity'		: 0,
			'display'		: 'block'
		}).append('<div id="ajax"></div>');
		$('#loading').css({
			'visibility'	: 'visible',
			'opacity'		: 0
		});
		
		/* Modify Link Behavior
		**************************/
		/* Add 'external' class to all links beginning with 'http'
		$('a[href^="http"]').addClass('external').click(function(){
			location.href="#";
			return true;
		});
		// */
		
		$('#nav a,#footer a').each(function(){ 
			var href = $(this).attr('href');
			if(href != '' && href != '#' && !$(this).hasClass('external')) $(this).attr('href', '#' + href.replace('https','http').replace('http://www.glacierbrewhouse.com',''));
		});
		$('#branding a').attr('href','#');
		$('.external').attr('target','_blank');
		
		/* Set Current Page
		**************************/
		site.currentPage = window.location.hash;
		
		/* Create Array of Menu Links
		**************************/
		$('#nav li > .menu').siblings('a').each(function(){
			site.menuLinks.push($(this).attr('href'));
		});
		
		/* Create Array of Menu Pages
		**************************/
		$('#nav li .menu a').each(function(){
			site.menuPages.push($(this).attr('href'));
		});
		
		/* Load Page If Not Home
		**************************/
		site.loadPage(site.currentPage,true);
		
		/* Set Current Page Classes
		**************************/
		if(site.currentPage != ''){
			$('#nav li > a,#nav .menu li > a').each(function(){
				hash = $(this).attr('href');
				
				if(site.currentPage.search(hash) >= 0){
					$(this).addClass('current');
				}
			});
		}
		
		/* Add Navigation Click Behavior
		**************************/
		$('#nav li > a').click(function(){
			$(this).parent().siblings().children('a').removeClass('current');
			$(this).addClass('current');
			
			site.loadPage();
		});
		
		/* Add Content Close Behavior
		**************************/
		$('#ajax .close').live('click', function(event) {
			var openMenu = $('.menu.open');
			if(openMenu.length > 0){
				openMenu.find('a.current').removeClass('current');
				var hash = openMenu.siblings('a').attr('href');
				window.location = hash;
			}else{
				window.location = '#';
			}
			return false;
		});
		
		/* Add Class To Current Menu Item
		**************************/
		$('.menu li a').click(function (){
			
			// Remove Current Class From Other Menu Links
			$(this).parent().siblings().children('a').removeClass('current');
			$(this).addClass('current');
			
			site.loadPage();
		});
		
		/* Check For Page Change
		**************************/
		site.loadPage();
		setInterval(function(){
			site.loadPage();
		},site.speed.close);
	},
	
		
	/* Open or Close Selected Menu
	**************************/
	'hideMenu'		:function(m){
		if(m.hasClass('open')){
			m.removeClass('open');
			m.find('.close').animate({
				opacity	:	'hide'
			},site.speed.close,function(){
				m.animate({
					width		:	'toggle',
			   		left		:	'-=211px',
					opacity		:	0
			 	},site.speed.close,function(){
					m.find('a').removeClass('current');
				});
			});
		}
	},
	
	'showMenu'		:function(m){
		if(!m.hasClass('open')){
			var siblingMenu = m.parent('#nav li').siblings().children('.menu.open'); //select other menus
			if (siblingMenu.length > 0){ //if they are open, close them					
				site.hideMenu(siblingMenu);
			}
			
			m.css({
				'height': $('#subContent').height()
			});
			
			$('#content').addClass('narrow');
			m.addClass('open').animate({
	   			width		:	'toggle',
	   			left		:	'+=211px',
				opacity		:	1
	 		},site.speed.open,function(){
	  				m.children('.close').animate({
	  					opacity : 'show'
	  				},site.speed.close);
	 		});
		}
	},
	
	'loadPage'		: function(hash,forceLoad)
	{	
		if(!hash) hash=window.location.hash;
		if(hash != site.currentPage || forceLoad)	// if the hash value has changed
		{
			site.currentPage=hash;	//update the current hash
			if(hash == '' || hash == '#'){
				$('#content').animate({
					'opacity' : 0
				},site.speed.open,function(){
					site.hideMenu($('.menu.open'));
					$('#nav li > a').removeClass('current');
				});
			}else if(hash in site.utilities.objectArray(site.menuLinks)){
				$('#content').animate({
					'opacity' : 0
				},site.speed.open,function(){
					site.showMenu($('a[href="' + site.currentPage + '"] + .menu'));
				});
			}else{
			    url = hash.replace('#','');    //strip the #page part of the hash and leave only the page number
			
				if(url == '/gift-cards/' && location.protocol != 'https:')
				{
					window.location = 'https://' + location.hostname + '/' + location.hash;
				}
				else if(url != '/gift-cards/' && location.protocol == 'https:')
				{
					window.location = 'http://' + location.hostname + '/' + location.hash;
				}
	 
				$('#loading').remove().appendTo($('a[href="' + site.currentPage + '"]')).end().animate({'opacity' : 1 });  //show the rotating gif animation
	 			
			    $.ajax({    //create an ajax request to load_page.php
			        type: 'POST',
			        url: url,
			        data: 'page='+url,  //with the page number as a parameter
			        dataType: 'html',   //expect html to be returned
			        success: function(msg){
			            if(parseInt(msg)!=0)    //if no errors
			            {
			            	$('#content').animate({
								'opacity' : 0
							},site.speed.close,function(){
								$('#content').removeClass('narrow');
								if(hash in site.utilities.objectArray(site.menuPages)){
									$('#content').addClass('narrow');
									
									var menu = $('.menu:has(a[href="' + site.currentPage + '"])');
									if(!menu.hasClass('open')) site.showMenu($('.menu:has(a[href="' + site.currentPage + '"])'));
								}else if($('.menu.open').length > 0){
									site.hideMenu($('.menu.open'));
								}
								$('#ajax').html(msg).append('<a href="#" class="close">X Close</a>');								
								if(site.scrollLoaded == false){
									fleXenv.fleXcrollMain('content');
									site.scrollLoaded = true;
								}else{
									fleXenv.updateScrollBars();
								}
								$('#content').animate({
									'opacity' : 1
								},site.speed.open);	//load the returned html into pageContet
				                $('#loading').animate({'opacity' : 0});	//and hide the rotating gif
							});
			            }
			        }

			    });
			}
		}
	},
	
	/* Utility Functions
	**************************/
	'utilities'		: {
		
		// Convert Array to Object
		'objectArray' : function(a){ 
			var o = {}; 
			for(var i=0;i<a.length;i++){ 
				o[a[i]]=''; 
			} 
			return o; 
		}
	}
};
$(function(){ site.init(); });
