/**
 * @author		Jonathan Cochran <jono.cochran@gmail.com> 
 * 				http://www.fabriccreative.com
 * --------------------------------------------------------- */
$(document).ready(function() {
	
	var badBrowser = (/MSIE ((5\.5)|6|7|8)/.test(navigator.userAgent) && navigator.platform == "Win32");

	var $objContent		= $('#content'); 	// main content nest
	var $objGallery 	= $('.images');
	var $objNav			= $('#navigation a').not('.videos');
	
/**
 * Navigation
 * --------------------------------------------------------- */
	var docLoc  = window.location.href;
	var getHash = docLoc.indexOf('#') != "-1" ? true : false; // hash # present in uri
	var split = docLoc.split('#');
	var $hashPage = $('.'+ split[1]);
	
	$objNav.each(function() { 
		var url = $(this).attr('class');
		$(this).attr('href', '#'+ url);
		eval(url)(name);
	});
	$objNav.bind('click', function() {
		var url = $(this).attr('class');
		
		// animate page...
		var $objChild 	= $('div:first', $objContent);
		var $iChildH	= $objChild.height() * -1;
		$iChildH -= 60;
		
		// check to see if it's the same page
		//console.log(url);
		if ($objChild.hasClass(url)) return false;
		
		hideLoadFooterImages( { image : url });
		
		// animate loader
		if (!badBrowser) $('body').css('overflow', 'hidden');
		$objChild.after('<div class="loader">Loading</div>');
		
		$objChild.stop().animate({ 'marginTop' : $iChildH }, 1200, 'easeOutExpo', function() { 
			
			// load page (ajax)			
			$.post(
				hostname +'view/'+ url,
				{ },
				function(data)
				{
					var $newPage = $(data);
					$newPage.css('opacity', 0);
					$('.loader').after($newPage);
					
					$('.loader').stop().fadeTo(500, 0, function() {
						//$objContent.html($newPage);
						$newPage.fadeTo(1000,1, function() { eval(url)(name); });
						
						$(this).remove();
						$objChild.remove();
						if (!badBrowser)	$('body').css('overflow', 'auto');
					});
				}
			);
		
		});
		
		//return false; // prevents browsers from following links
	});
	
	// # Hash is present, load page...
	if (getHash)
	{
		if ( split[1] != "" && $hashPage.length > 0 )
		{
			hideLoadFooterImages({ image : split[1] });
			$hashPage.trigger('click');
		}
	}
	else 
	{
		hideLoadFooterImages( { image : 'home' });
	}
	
	// Top right (In the kitchen drop down)
	$('#kitchen div').hover(function() {
		$(this).stop().animate({ 'marginTop' : 0 }, 500, 'easeInExpo');
	}, function() {
		$(this).stop().animate({ 'marginTop' : -227 }, 1000, 'easeOutExpo');
	});
	$('#kitchen a').click(function() {
		$('#kitchen div').animate({ 'marginTop' : -227 }, 500, 'easeOutExpo');
		$('.blog').trigger('click');
		return false;
	});
	
/**
 * Pages (Don't delete empty functions!!! Produces error...)
 * --------------------------------------------------------- */	
	function home()
	{
		if ( $('.home').length > 0 )
		{
			if ( $('.home-images').length > 0 )
			{
				$('.home-images').click(function() { $('.images').trigger('click'); });				
			}
			
			// News & Events click
			$('.home-news-events a').click(function() { 
				$('.events').trigger('click');
			})
		
			// Footer resize
			$('#main').css('padding-bottom', 94);	
			$('#footer').css({ 'height' : 94, 'margin-top' : -94 });
		}
	}
	
	function restaurant()
	{
		if ( $('.restaurant:first').length > 0 )
		{
			//$('.restaurant-columns').columnize({ width: 200, columns : 2 });  		
			var i = 0;
			$('.next-page').click(function() {
				if ( i < $('.restaurant-page').length )
					i++;
				$('.restaurant-shell').scrollTo('.restaurant-page:eq('+ i +')', 1000, { easing : 'easeOutExpo' });

			});
			$('.prev-page').click(function() {
				i--;
				$('.restaurant-shell').scrollTo('.restaurant-page:eq('+ i +')', 1000, { easing : 'easeOutExpo' });
			});
		
			// Footer resize
			$('#main').css('padding-bottom', 99);	
			$('#footer').css({ 'height' : 99, 'margin-top' : -99 });
		}		
	}
	function menu()
	{
		if ( $('.menu').length > 0 )
		{
			// Footer resize
			$('#main').css('padding-bottom', 119);	
			$('#footer').css({ 'height' : 119, 'margin-top' : -119 });
		}
	}
	function history()
	{		
		if ( $('.history').length > 0 )
		{
			var i = 0;
			$('.next-page').click(function() {
				if ( i < $('.history-page').length )
					i++;
				$('.history-shell').scrollTo('.history-page:eq('+ i +')', 1000, { easing : 'easeOutExpo' });

			});
			$('.prev-page').click(function() {
				i--;
				$('.history-shell').scrollTo('.history-page:eq('+ i +')', 1000, { easing : 'easeOutExpo' });
			});
		
			// Footer resize
			$('#main').css('padding-bottom', 119);	
			$('#footer').css({ 'height' : 119, 'margin-top' : -119 });
		}
	}
	
	function blog() {		
		
		if ( $('.blog').length > 0 )
		{
			var $objMedia = $('.post-media'); // photo frames
			if ( $objMedia.length > 0 )
			{
				$objMedia.each(function() {
					// It's a video...
					if ( $('object', $(this)).length > 0 && !badBrowser ) 
					{
						//$(this).wrapInner('<div class="video-frame" />');
						//$(this).wrap('<div class="frame_scrapbook">');
						//$(this).after('<div class="upper_left"></div>');
						//$(this).after('<div class="upper_right"></div>');
						//$(this).after('<div class="lower_left"></div>');
						//$(this).after('<div class="lower_right"></div>');
					}
					else if ( $('embed', $(this)).length > 0 && badBrowser ) 
					{
						$('.post-media').css({ 'margin' : 20, 'padding' : 0, 'width' : 'auto', 'height' : 'auto', 'border' : '10px solid #d9d9cf' })
						$(this).css({ 'padding' : 0 });
					}
				});
			}
		
			// Photo frames
			$objImages = $('.entry img');
			if ( $objImages.length > 0 )
			{
				$objImages.each(function() {
					$parent = $(this).parents('.frame_scrapbook');
					if ( $parent.length == 0 )
					{
						$(this).wrap('<div class="frame_scrapbook">');
						$(this).after('<div class="upper_left"></div>');
						$(this).after('<div class="upper_right"></div>');
						$(this).after('<div class="lower_left"></div>');
						$(this).after('<div class="lower_right"></div>');
					}
				});
			}
		
			// Footer resize
			$('#main').css('padding-bottom', 57);	
			$('#footer').css({ 'height' : 57, 'margin-top' : -57 });
		
		}		
	}
	function images() {
		if ( $objGallery.length > 0 )
		{
			$objGalleryNest = $('.images-gallery-nest');
			$triggerPrev = $('.images-prev');
			$triggerNext = $('.images-next');
					
			// Food gallery
			$('.gallery-food').bind('click', function() {
				$('.gallery-restaurant').removeClass('current');
				$(this).addClass('current');
				var clone = $('.images-food').clone()
				clone.css('display', 'block');
				$objGalleryNest.html(clone);
				//$('.images-gallery-nest ul:first').slideImages('horizontal', 506, 361, 1200, 'easeOutExpo', 5000, $triggerPrev, $triggerNext);
				initImages();
			});
	
			// Restaurant gallery
			$('.gallery-restaurant').bind('click', function() {
				$('.gallery-food').removeClass('current');
				$(this).addClass('current');
				
				var clone = $('.images-restaurant').clone()
				clone.css('display', 'block');
				$objGalleryNest.html(clone);
				//$('.images-gallery-nest ul:first').slideImages('horizontal', 506, 361, 1200, 'easeOutExpo', 5000, $triggerPrev, $triggerNext);
				initImages();
			});
		
			$('.gallery-food').trigger('click');			
			
			// Footer resize
			$('#main').css('padding-bottom', 127);	
			$('#footer').css({ 'height' : 127, 'margin-top' : -127 });			
		}		

	}
	window.t = ""; // Timer for auto-cycle gallery images
	function initImages() {		
		var $objLi = $('.images-gallery-nest li');
		var totalCount = $objLi.length;
		var current = totalCount;
		var $prev = $('.images-prev');
		var $next = $('.images-next');
		
		$prev.click(function() {
			if (current == totalCount) {
				current = 0;				
			} 
			current++;
			$('#current').val(current);
			tempId = 'img'+current;
			$objLi.each(function() {
				if ($(this).hasClass(tempId)) {
					$(this).css({'display':'block','opacity':'0'});
					$(this).fadeTo(800,1);	
				} else {
					$(this).css({'display':'none','opacity':'0'});
				}
			});		
		});
		$next.click(function() {
			if (current == 1) {
				current = totalCount;
				current++;				
			} 
			current--;
			$('#current').val(current);
			tempId = 'img'+current;
			$objLi.each(function() {
				if ($(this).hasClass(tempId)) {
					$(this).css({'display':'block','opacity':'0'});
					$(this).fadeTo(800,1);	
				} else {
					$(this).css({'display':'none','opacity':'0'});
				}
			});
		});
		
		// Auto-play cycle images
		clearInterval(window.t);
		window.t = setInterval(function() { $next.trigger('click') }, 2000)
		$next.mousedown(function() { clearInterval(window.t) });
		$prev.mousedown(function() { clearInterval(window.t) });
	}
	
	function awards()
	{
		if ( $('.press').length > 0 )
		{
			// Footer resize
			$('#main').css('padding-bottom', 94);	
			$('#footer').css({ 'height' : 94, 'margin-top' : -94 });
		}
	}
	
	function events()
	{
		if ( $('.events').length > 0 )
		{
			var $objMedia = $('.post-media'); // photo frames
			if ( $objMedia.length > 0 )
			{
				$objMedia.each(function() {
					// It's a video...
					if ( $('object', $(this)).length > 0 && !badBrowser ) 
					{
						//$(this).wrapInner('<div class="video-frame" />');
						//$(this).wrap('<div class="frame_scrapbook">');
						//$(this).after('<div class="upper_left"></div>');
						//$(this).after('<div class="upper_right"></div>');
						//$(this).after('<div class="lower_left"></div>');
						//$(this).after('<div class="lower_right"></div>');
					}
					else if ( $('embed', $(this)).length > 0 && badBrowser ) 
					{
						$('.post-media').css({ 'margin' : 20, 'padding' : 0, 'width' : 'auto', 'height' : 'auto', 'border' : '10px solid #d9d9cf' })
						$(this).css({ 'padding' : 0 });
					}
				});
			}

			// Photo frames
			$objImages = $('.entry img');
			if ( $objImages.length > 0 )
			{
				$objImages.each(function() {
					$parent = $(this).parents('.frame_scrapbook');
					if ( $parent.length == 0 )
					{
						$(this).wrap('<div class="frame_scrapbook">');
						$(this).after('<div class="upper_left"></div>');
						$(this).after('<div class="upper_right"></div>');
						$(this).after('<div class="lower_left"></div>');
						$(this).after('<div class="lower_right"></div>');
					}
				});
			}

			// Footer resize
			$('#main').css('padding-bottom', 57);	
			$('#footer').css({ 'height' : 57, 'margin-top' : -57 });
		}
	}
	
	function resources()
	{
		
	}
	
	function functions()
	{
		if ( $('.functions').length > 0 )
		{
			var i = 0;
			$('.next-page').click(function() {
				if ( i < $('.functions-page').length )
					i++;
				$('.functions-shell').scrollTo('.functions-page:eq('+ i +')', 1000, { easing : 'easeOutExpo' });

			});
			$('.prev-page').click(function() {
				i--;
				$('.functions-shell').scrollTo('.functions-page:eq('+ i +')', 1000, { easing : 'easeOutExpo' });
			});
		
			// Footer resize
			$('#main').css('padding-bottom', 108);	
			$('#footer').css({ 'height' : 108, 'margin-top' : -108 });
		}
	}
	
	function contact()
	{
		if ( $('.contact').length > 0 )
		{
			// Footer resize
			$('#main').css('padding-bottom', 140);	
			$('#footer').css({ 'height' : 140, 'margin-top' : -140 });
		}
	}
	
	function videos() { }


/**
 * Footer
 * --------------------------------------------------------- */
	function hideLoadFooterImages(ar)
	{
		$('#footer img:visible').stop().css('z-index', 0).hide().fadeTo(1,0, function() {
			objFooter = $('.footer-'+ ar.image);
			objFooter.attr('src', hostname +'images/footer-'+ ar.image +'.jpg');
			iLeft = (objFooter.attr('width') / 2)*-1;
			objFooter.stop().css({ 'z-index' : 999, 'marginLeft' : iLeft }).show().fadeTo(1500, 1);
		});
	}
	
	
	
});
