// Prevent errors when Firebug or console isn't available
if (window.console === undefined) { window.console = {log: function(){}}; }
window.onerror = function() {};

/**
 * Global vars/constants
 */
	var SLIDER_MAX = 250000;
	var BATCH_SIZE = 4;
	var BATCH_TIMEOUT = 250;
	
	var isSliding = false;
	var delayLoadingOfImages = !IS_MOBILE;
	var delayedImages = [];
	var lastPanelPosition;
	var lastSliderPosition;
	var background; // global soundmanager audio object for background music
	var songs = {}; // other sound clips
	var isAudioEnabled = !IS_MOBILE;
	var audioPanels = [];
	var loaded = 0;
	
	var scrollInterval;
	var scrollDistance = 15;
	var scrollValue;
	var scrollPercentage;
	var sliderValue;
	var scrollSpeed;
	
	var panels = [
		{width: 965, skip: true},
		{id: 'intro', before_main: true, skip: true, width: 1181, image: 'images/panels/intro.jpg'},
/* 		{width: 1332, image: 'images/panels/savory.jpg'}, */
		{width: 902, image: 'images/panels/outdoors.jpg'},
		{width: 698, image: 'images/panels/shrimp.jpg'},
		{before_main: true, width: 1186, image: 'images/panels/dessert.jpg'},
		{id: 'hours', main: true, audio: true, skip: true, width: 1440, image: 'images/map.png'},
/* 		{video: true, skip: true, width: 690}, */
		{width: 690, image: 'images/panels/top10.jpg'},
		{width: 959, image: 'images/panels/oysters.jpg'},
/* 		{width: 960, image: 'images/panels/popcorn.jpg'}, */
		{width: 960, image: 'images/panels/flatbread.jpg'},
/* 		{width: 1030, image: 'images/panels/pickles.jpg'}, */
		{width: 4864, image: 'images/panels/front_sign.jpg'},
		{width: 1932, image: 'images/panels/ingredients.jpg'},
		{width: 960, image: 'images/panels/potpie.jpg'},
		{width: 1446, image: 'images/panels/meat.jpg'},
		{video: true, skip: true, width: 950},
/* 		{width: 690, image: 'images/panels/happy-hour.jpg'}, */
		{width: 1220, image: 'images/panels/napkins.jpg'},
/* 		{width: 1130, image: 'images/panels/monkfish.jpg'}, */
/* 		{width: 742, image: 'images/panels/choc_cake.jpg'}, */
		{width: 1278, image: 'images/panels/arials.jpg'},
		{before_main: true, width: 1374, image: 'images/panels/fire.jpg'},
		{id: 'reservations', main: true, skip: true, width: 960, image: 'images/private_dining_polaroid.jpg'},
		{width: 2428, image: 'images/panels/candles.jpg'},
/* 		{width: 898, image: 'images/panels/cheese.jpg'}, */
		{width: 1195, image: 'images/panels/poets.jpg'},
		{width: 690, image: 'images/panels/westside.jpg'},
		{width: 511, image: 'images/panels/forks.jpg'},
		{width: 959, image: 'images/panels/polaroids.jpg'},
		{width: 1688, image: 'images/panels/spike_cooking.jpg'},
/* 		{width: 821, image: 'images/panels/deviled_eggs.jpg'}, */
		{video: true, skip: true, width: 960},
		{id: 'menuboard', audio: true, width: 5970, image: 'images/panels/sign_and_space.jpg'},
/* 		{width: 903, image: 'images/panels/coffee001.jpg'}, */
		{width: 1211, image: 'images/panels/pancakes.jpg'},
		{width: 1042, image: 'images/panels/coffee.jpg'},
/* 		{width: 965, image: 'images/panels/time.jpg'}, */
/* 		{width: 900, image: 'images/panels/honey_ingredient.jpg'}, */
		{width: 690, image: 'images/panels/sunday.jpg'},
		{width: 1944, image: 'images/panels/waffles.jpg'},
		{width: 1891, image: 'images/panels/outdoor_sign.jpg'},
		{video: true, skip: true, width: 825},
		{id: 'restaurant', main: true, skip: true, width: 960},
		{width: 1187, image: 'images/panels/earthday.jpg'},
		{width: 533, image: 'images/panels/local_maryland.jpg'},
		{width: 1617, image: 'images/panels/people.jpg'},
		{width: 758, image: 'images/panels/have_a_seat.jpg'},
		{width: 1297, image: 'images/panels/signs.jpg'},
/* 		{width: 1133, image: 'images/panels/BLT.jpg'}, */
/* 		{width: 614, image: 'images/panels/boyfriend.jpg'}, */
/* 		{width: 840, image: 'images/panels/our_goal.jpg'}, */
		{width: 874, image: 'images/panels/pizza_oven.jpg'},
		{width: 844, image: 'images/panels/pizza.jpg'},
		{id: 'staff', skip: true, width: 1060},
		{id: 'growers', skip: true, width: 1136},
		{id: 'sketch', audio: true, width: 1130, image: 'images/panels/guy_sketch.jpg'},
		{width: 1712, image: 'images/panels/high_chair.jpg'},
		{width: 929, image: 'images/panels/black-maple-hills.jpg'},
		{width: 960, image: 'images/panels/oysters02.jpg'},
/* 		{width: 1477, image: 'images/panels/tomato.jpg'}, */
/* 		{width: 884, image: 'images/panels/lettuce.jpg'}, */
		{width: 2594, image: 'images/panels/shipping_2.jpg'},
		{before_main: true, width: 2549, image: 'images/panels/food.jpg'},
		{id: 'menu', main: true, audio: true, skip: true, width: 960},
		{width: 959, image: 'images/panels/top_ten.jpg'},
		{width: 1870, image: 'images/panels/breads.jpg'},
		{video: true, skip: true, width: 900},
		{width: 860, image: 'images/panels/photo-holder.jpg'},
		{width: 844, image: 'images/panels/wood_glass.jpg'},
		{width: 960, image: 'images/panels/fish.jpg'},
		{id: 'specials', main: true, skip: true, width: 960},
/* 		{width: 1024, image: 'images/panels/turnips.jpg'}, */
		{width: 839, image: 'images/panels/espresso.jpg'},
/* 		{width: 902, image: 'images/panels/table_2.jpg'}, */
		{width: 733, image: 'images/panels/cow_white.jpg'},
		{width: 690, image: 'images/panels/honey.jpg'},
		{width: 686, image: 'images/panels/beer_taps.jpg'},
		{width: 1260, image: 'images/panels/menu_sketch.jpg'},
		{width: 2139, image: 'images/panels/special_occasions_extended.jpg'},
		{width: 960, image: 'images/panels/pumpkin.jpg'},
		{width: 600, image: 'images/panels/chiken_waffles.jpg'},
/* 		{width: 960, image: 'images/panels/pear.jpg'}, */
		{width: 690, image: 'images/panels/happy-hour.jpg'},
		{before_main: true, width: 1254, image: 'images/panels/special-occasions.jpg'},
		{id: 'private_dining', main: true, skip: true, width: 960, image: 'images/private_dining_polaroid.jpg'},
		{width: 630, image: 'images/panels/chairs.jpg'},
		{width: 1285, image: 'images/panels/martini.jpg'},
/* 		{width: 960, image: 'images/panels/rug.jpg'}, */
/* 		{width: 960, image: 'images/panels/table.jpg'}, */
		{width: 960, image: 'images/panels/woodberry_hall.jpg'},
		{width: 1966, image: 'images/panels/wood_closeup_1.jpg'},
		{width: 2940, image: 'images/panels/wood_closeup_2.jpg'},
		{width: 797, image: 'images/panels/macchiato.jpg'},
		{width: 1076, image: 'images/panels/french-toast.jpg'},
		{width: 698, image: 'images/panels/pig.jpg'},
		{width: 477, image: 'images/panels/wood_stack_sketch.jpg'},
		{width: 775, image: 'images/panels/card.jpg'},
		{width: 959, image: 'images/panels/cheers.jpg'},
		{id: 'gift_cards', main: true, skip: true, width: 1168, image: 'images/panels/gift-cards.jpg'},
		{width: 775, image: 'images/panels/holder.jpg'},
		{width: 770, image: 'images/panels/divine_soup.jpg'},
/* 		{width: 628, image: 'images/panels/steak_3.jpg'}, */
		{id: 'menusketch', audio: true, width: 1285, image: 'images/panels/menu_sketch_2.jpg'},
		{width: 601, image: 'images/panels/fight.jpg'},
		{id: 'opportunities', main: true, skip: true, width: 960},
		{width: 629, image: 'images/panels/exit.jpg'},
	];
	
/**
 * Setup audio
 */
	soundManager.debugMode = false;
	soundManager.url = WWW_ROOT + 'swf/';
	soundManager.onerror = function()
	{
	  isAudioEnabled = false;
	  $('#audio').fadeOut();
	};
	
	soundManager.onload = function()
	{
		background = soundManager.createSound({id: 'background', url: WWW_ROOT + 'audio/Woodberry-128kbps.mp3', volume: 5});
		background.play({onfinish: function(){ this.play(); }}); // loop
		
		// #hours
		songs['hours'] = soundManager.createSound({id: 'dylan-follow', url: WWW_ROOT + 'audio/bob-dylan-follow.mp3', volume: 10});
	
		// #reservations
		songs['restaurant'] = soundManager.createSound({id: 'reservations', url: WWW_ROOT + 'audio/Woodberry_Glad_Tidings.mp3', volume: 12});
		
		// #sketch
		songs['sketch'] = soundManager.createSound({id: 'john-cash', url: WWW_ROOT + 'audio/john-cash.mp3', volume: 12});
	
		// #menuboard
		songs['menuboard'] = soundManager.createSound({id: 'george-harrison', url: WWW_ROOT + 'audio/george-harrison.mp3', volume: 12});
	
		// #menu
		songs['menu'] = soundManager.createSound({id: 'johnny-appleseed', url: WWW_ROOT + 'audio/Woodberry_Johnny_Appleseed.mp3', volume: 12});
	
		// #menusketch
		songs['menusketch'] = soundManager.createSound({id: 'beatles-i-me-mine', url: WWW_ROOT + 'audio/beatles-i-me-mine.mp3', volume: 12});
	};

/**
 * Event bindings
 */
	$(document).ready(function()
	{
		$('#slider').slider({step: 1, min: 1, max: SLIDER_MAX, animate: true, slide: slideDrag, stop: slideStop});
	
		if ($('.container_error').length < 1)
		{
			$('.trigger').click(routeClickURL);
		}
	  
		$('#audio').click(playPauseAudio);
	
		// cache data about audio enabled panels
		$('#panels .has-audio').each(function()
		{
			var start = $(this).position().left;
			audioPanels.push({id: $(this).attr('id'), start: start, end: start + $(this).width()});
		});
	
		//var lastPanel = $('#panels .panel:last');
		if ($('#panels .panel').length > 1)
		{
			var lastPanel = $('#panels .panel').eq($('#panels .panel').length - 2);
			var lastImage = $('#panels .panel:last');
			lastPanelPosition = lastPanel.position().left + (lastImage.width() - (($(window).width() - 960) / 2) - (960 - lastPanel.width()));
		}
	
		//menu actions	
		$(".main_menu a").click(swapMainMenu);
		$(".submenu a").click(swapSubMenu);
	
		$('.scroll_area img').each(function() {
			if (parseInt($(this).outerWidth()) > 420)
			{
				var w = $(this).outerWidth();
				var h = $(this).outerHeight();
				var r = h / w;
				$(this).css({width: '420px', height: (420 * r) + 'px'});
			}
		});
		
		$(".scroll_area").scrollbar({buttonHeight: 0});

		$(window).resize(resetScrollOverlays);	
		if ($(window).width() > 1024)
		{
			//scroll overlays
			var scrollOverlayWidth = parseInt($(window).width() - 1024) / 2;	
			$('.scroll_right_overlay, .scroll_left_overlay').show().css({width: scrollOverlayWidth + 'px'});
		}
		else
		{
			$('.scroll_right_overlay, .scroll_left_overlay').hide();
		}
		
		$('.scroll_right_overlay').mouseover(scrollRightEnter);
		$('.scroll_right_overlay').mousemove(scrollRightMove);
		$('.scroll_right_overlay').mouseout(clearScrollCarriage);
		
		$('.scroll_left_overlay').mouseover(scrollLeftEnter);
		$('.scroll_left_overlay').mousemove(scrollLeftMove);
		$('.scroll_left_overlay').mouseout(clearScrollCarriage);
		
		$('a._email').each(function() {
			$(this).attr('href', $(this).attr('href') + '@woodberrykitchen.com');
		});
	
		$('form').each(function() {
			$(this).attr('target', '_blank');
		});
	
		if (window.location.href != WWW_ROOT && $('.container_error').length < 1)
		{
			routeURL();
		}
		
		if (IS_MOBILE)
		{
			$('.panel').not('.main').not('#intro').remove();
			$('#audio, #slider_container').hide();
			
			var lastPanel = $('#panels .panel').eq($('#panels .panel').length - 2);
			var lastImage = $('#panels .panel:last');
			lastPanelPosition = lastPanel.position().left + (lastImage.width() - (($(window).width() - 960) / 2) - (960 - lastPanel.width()));
		}
	});
	
	// Load images after full page is loaded
	if (delayLoadingOfImages)
	{
		$(window).load(function()
		{
			setTimeout(loadImages, 500);
		});
	}
	
/**
 * Mouse hover scrolling
 */
	//RESET OVERLAYS
	function resetScrollOverlays()
	{	
		if ($(window).width() > 1024)
		{
			var scrollOverlayWidth = parseInt($(window).width() - 1024) / 2;	
			$('.scroll_right_overlay, .scroll_left_overlay').show().css({width: scrollOverlayWidth + 'px'});
		}
		else
		{
			$('.scroll_right_overlay, .scroll_left_overlay').hide();
		}
		
		//RESETS SCROLL POSITION
		var lastPanel = $('#panels .panel').eq($('#panels .panel').length - 2);
		var lastImage = $('#panels .panel:last');
		lastPanelPosition = lastPanel.position().left + (lastImage.width() - (($(window).width() - 960) / 2) - (960 - lastPanel.width()));
		scrollCarriageRight();
	}
	
	//SCROLL RIGHT
	function scrollRightEnter()
	{
		if (!isSliding)
		{
			sliderValue = $('#slider').slider('value');
			scrollInterval = setInterval(scrollCarriageRight, 10);
		}
	}
	
	function scrollRightMove(e)
	{
		var x = e.pageX - this.offsetLeft;
		scrollSpeed = Math.round( (x / $(this).width()) * 10 );
	}
	
	function scrollCarriageRight()
	{
		$('.scroll_right_overlay').css({opacity: scrollSpeed / 10});
		sliderValue = sliderValue + (scrollDistance * scrollSpeed);
		scrollCarriage();
	}
	
	//SCROLL LEFT
	function scrollLeftEnter()
	{
		if (!isSliding)
		{
			sliderValue = $('#slider').slider('value');
			scrollInterval = setInterval(scrollCarriageLeft, 10);
		}
	}
	
	function scrollLeftMove(e)
	{
		var x = $(this).width() - e.pageX;
		scrollSpeed = Math.round( (x / $(this).width()) * 10 );
	}
	
	function scrollCarriageLeft()
	{
		$('.scroll_left_overlay').css({opacity: scrollSpeed / 10});
		sliderValue = sliderValue - (scrollDistance * scrollSpeed);
		scrollCarriage();
	}
	
	//SCROLL
	function scrollCarriage()
	{
		scrollPercentage = sliderValue / SLIDER_MAX;
		$('#slider').slider('value', sliderValue);
		var leftPos = Math.round(lastPanelPosition * scrollPercentage * -1);
				
		if(leftPos >= 0)
		{
			leftPos = 0;
		}
		else if(leftPos <= -lastPanelPosition)
		{
			leftPos = -lastPanelPosition;
		}
		
		$('#panels').css({left: leftPos});
		
		if (isAudioEnabled)
		{
			var currentPosition = parseInt($('#panels').css('left')) * -1 + 480;
		
			// loop through audio panels checking if any are in view
			$.each(audioPanels, function(i, obj)
			{
				if (currentPosition >= obj.start && currentPosition <= obj.end)
				{
					if (songs[obj.id].playState === 0)
					{
						stopAllSongs();
						songs[obj.id].play();
					}
				}
			});
		}
	}
	
	function clearScrollCarriage()
	{
		$('.scroll_right_overlay, .scroll_left_overlay').animate({opacity: 0});
		clearInterval(scrollInterval);
	}


/**
 * Event Handlers
 */
 	function updateTracker(route)
 	{
 		if(pageTracker)
 		{
 			pageTracker._trackPageview('/' + route);
 		}
 	}
 
 	function routeURL()
 	{
 		var route = window.location.href;
 		route = route.replace(WWW_ROOT, '');
 		route = route.replace('-', '_');
 		updateTracker(route);
 		route = route.split('/');
 		gotoPanel(route[0]);
 	}
 
	function routeClickURL()
 	{
 		var route = $(this).attr('href');
 		route = route.replace(WWW_ROOT, '');
 		route = route.replace('-', '_');
 		updateTracker(route);
 		route = route.split('/');
 		gotoPanel(route[0]);

 		if (route[0] == 'menu')
 		{
 			if (route[1] != '')
			{
				loadMenuRoute(route[1], route[2]);
			}
 		}
 		return false;
 	}

	function gotoPanel(panelId)
	{
		if (panelId == 'site_credits')
		{
			panelId = 'opportunities';
		}
	
		if (!isSliding)
		{
			isSliding = true;
			var panel = $('#' + panelId);
							
			if (panelId != '#' && panel.length > 0)
			{				
				$('#nav .active').removeClass('active');
				$('#nav a.' + panelId + '').addClass('active');
				
				if (!IS_MOBILE)
				{
					Cufon.replace('#nav a');
				}
				
				var panelPosition = panel.position().left;
				var percentage = panelPosition / lastPanelPosition;
				var delta = Math.abs((panelPosition) - (parseInt($('#panels').css('left')) * -1)) ;
				var duration = (delta / 25000) * 1000;
							
				$('#slider .ui-slider-handle').animate({left: (percentage * 100) + '%'}, {duration: duration, easing: 'easeInOutQuad'});
				
				$('#panels').animate({left: panelPosition * -1}, {duration: duration, easing: 'easeInOutQuad', complete: function()
				{
					if (isAudioEnabled)
					{
						if (panelId == 'hours')
						{
							stopAllSongs();
							songs['hours'].play();
						}
						else if (panelId == 'menu')
						{
							stopAllSongs();
							songs['menu'].play();
						}
						else if (panelId == 'restaurant')
						{
							stopAllSongs();
							songs['restaurant'].play();
						}
					}
					
					$('#slider').slider('value', percentage * SLIDER_MAX);
					isSliding = false;
				}});
			}
			else
			{
				isSliding = false;
			}
		}
	}
 
	// Toggle audio on/off globally
	function playPauseAudio()
	{
		// Play
		if ($(this).hasClass('off'))
		{
			$(this).removeClass('off');
			background.play();
			isAudioEnabled = true;
		}
		else
		{
			isAudioEnabled = false;
			$(this).addClass('off');
			background.stop();
			
			stopAllSongs();
		}
		
		return false;
	}
	
	function stopAllSongs()
	{
		// Stop all playing songs
		$.each(songs, function(i, val)
		{
			songs[i].stop();
		});
	}
 
/**
 * Callbacks
 */
	function slideDrag(event, ui)
	{
		var percentage = ui.value / SLIDER_MAX;
		var left = lastPanelPosition * percentage * -1;
		$('#panels').css({left: Math.round(left)});
	}
	
	function slideStop(event, ui)
	{
		if (isAudioEnabled)
		{
			var currentPosition = parseInt($('#panels').css('left')) * -1 + 480;
		
			// loop through audio panels checking if any are in view
			$.each(audioPanels, function(i, obj)
			{
				if (currentPosition >= obj.start && currentPosition <= obj.end)
				{
					if (songs[obj.id].playState === 0)
					{
						stopAllSongs();
						songs[obj.id].play();
					}
				}
			});
		}
	}


/**
 * Helper functions
 */
	function delayedLoad()
	{
		$('#panels .delay').each(function()
		{
			var panel = $(this);
			var image = $(this).find('img').eq(0);
			
			// skip images that already got loaded or are cached
			if (!image[0].complete)
			{
				delayedImages.push({panel: panel, image: image.attr('src')});
				image.hide().attr('src', '');
			}
		});
	}
	
	// Batch load images of BATCH_SIZE at a time
	function loadImages()
	{
		var all_panels = $('.panel');
		var max = Math.min(all_panels.length, loaded + BATCH_SIZE);

		for (var i = loaded; i < max; i++)
		{
			if (!panels[i].skip)
			{
				loadImage(panels[i].image, all_panels.eq(i));
			}
			
			loaded++;
		}
		
		// load next batch
		if (all_panels.length != max)
		{
			setTimeout(loadImages, BATCH_TIMEOUT);	
		}
		else
		{
			//alert('All images loaded');
		}
	}
	
	/**
	 * Load single image into panel
	 * @param image - source of image
	 * @param panel - jQuery object of DOM panel element
	 */
	function loadImage(image, panel)
	{
		var src = WWW_ROOT + image;
		var img = new Image();

		$(img).load(function()
		{
			$(this).hide().prependTo(panel).fadeIn(250);
		}).error(function()
		{
			console.log('error loading image', this);
		}).attr('src', src);
	}
	
	function currentPanel()
	{
		
	}
	
	function computeActualWidth()
	{
		var lastPanel = $('#panels .panel:last');
		console.log(lastPanel.position().left + lastPanel.width());
	}
	
	
	/**
	 * FS player
	 */
	(function($){

		var fsPlayer;
		var isReady = false;
		var player_div = "";

		$.fn.fsPlayer = function(settings)
		{
			player_div = $(this);
			
			var flashvars = { 
				id: player_div.attr("id"),
				domId: player_div.attr('id'), 
				autoPlay: false, 
				autoLoad: true,
				sourceFile: '',
				thumbnailFile: '',
				controls: 'min',
				fullscreen: true,
				width: '400', 
				height: '300', 
				loop: false,
				fitting: 'fit',
				videoBackgroundColor: 'decdba', // decdba
				uiBackgroundColor: 'decdba',
				buttonColor: 'FFFFFF',
				highlightColor: '22120c',
				trackBackgroundColor: '666666',
				trackColor: 'CCCCCC',
				accentColor: 'E20A16'
			};
			
			for (var key in settings) {
				flashvars[key] = settings[key];
			}

			var params = {
				quality: "high",
				allowscriptaccess: "always",
				allowfullscreen: "true"
			};

			var attributes = {
				id: flashvars.domId
			};

			swfobject.embedSWF(WWW_ROOT + "swf/fsPlayer.swf", flashvars.domId, flashvars.width, flashvars.height, "10.0.0", null, flashvars, params, {});
		};
	})(jQuery);
	
	
	jQuery.extend( jQuery.easing,
	{
		def: 'easeOutQuad',
		swing: function (x, t, b, c, d) {
			//alert(jQuery.easing.default);
			return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
		},
		easeInQuad: function (x, t, b, c, d) {
			return c*(t/=d)*t + b;
		},
		easeOutQuad: function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		},
		easeInOutQuad: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t + b;
			return -c/2 * ((--t)*(t-2) - 1) + b;
		},
		easeInCubic: function (x, t, b, c, d) {
			return c*(t/=d)*t*t + b;
		},
		easeOutCubic: function (x, t, b, c, d) {
			return c*((t=t/d-1)*t*t + 1) + b;
		},
		easeInOutCubic: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t + b;
			return c/2*((t-=2)*t*t + 2) + b;
		},
		easeInQuart: function (x, t, b, c, d) {
			return c*(t/=d)*t*t*t + b;
		},
		easeOutQuart: function (x, t, b, c, d) {
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		},
		easeInOutQuart: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
			return -c/2 * ((t-=2)*t*t*t - 2) + b;
		},
		easeInQuint: function (x, t, b, c, d) {
			return c*(t/=d)*t*t*t*t + b;
		},
		easeOutQuint: function (x, t, b, c, d) {
			return c*((t=t/d-1)*t*t*t*t + 1) + b;
		},
		easeInOutQuint: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
			return c/2*((t-=2)*t*t*t*t + 2) + b;
		},
		easeInSine: function (x, t, b, c, d) {
			return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
		},
		easeOutSine: function (x, t, b, c, d) {
			return c * Math.sin(t/d * (Math.PI/2)) + b;
		},
		easeInOutSine: function (x, t, b, c, d) {
			return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
		},
		easeInExpo: function (x, t, b, c, d) {
			return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
		},
		easeOutExpo: function (x, t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		},
		easeInOutExpo: function (x, t, b, c, d) {
			if (t==0) return b;
			if (t==d) return b+c;
			if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
			return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
		},
		easeInCirc: function (x, t, b, c, d) {
			return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
		},
		easeOutCirc: function (x, t, b, c, d) {
			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
		},
		easeInOutCirc: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
			return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
		},
		easeInElastic: function (x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		},
		easeOutElastic: function (x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		},
		easeInOutElastic: function (x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
			return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
		},
		easeInBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 1.70158;
			return c*(t/=d)*t*((s+1)*t - s) + b;
		},
		easeOutBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 1.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		},
		easeInOutBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 1.70158; 
			if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
			return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
		},
		easeInBounce: function (x, t, b, c, d) {
			return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
		},
		easeOutBounce: function (x, t, b, c, d) {
			if ((t/=d) < (1/2.75)) {
				return c*(7.5625*t*t) + b;
			} else if (t < (2/2.75)) {
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			} else if (t < (2.5/2.75)) {
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			} else {
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		},
		easeInOutBounce: function (x, t, b, c, d) {
			if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
			return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
		}
	});
	
	function outputPanels()
	{
		$('.panel').each(function()
		{ 
			var panel = '{';

			if ($(this).attr('id'))
			{
				panel += 'id: \'' + $(this).attr('id') + '\', ';
			}
			
			if ($(this).hasClass('main'))
			{
				panel += 'main: true, ';
			}
			
			if ($(this).hasClass('before_main'))
			{
				panel += 'before_main: true, ';
			}
			
			if ($(this).hasClass('has-audio'))
			{
				panel += 'audio: true, ';
			}
			
			if ($(this).hasClass('has-video'))
			{
				panel += 'video: true, ';
			}
			
			if (!$(this).hasClass('delay'))
			{
				panel += 'skip: true, ';
			}
			
			panel += 'width: ' + $(this).width();
		
			if ($(this).find('img').length > 0)
			{
				panel += ', image: \'' + $(this).find('img').eq(0).attr('src') + '\'';
			}
			
			panel += '},';
		});
	}
	
	
	
	function swapMainMenu()
	{
		if(!$(this).hasClass('active'))
		{
			
			$(".main_menu li.active").removeClass('active').find(".active").removeClass('active');
			$(this).addClass('active').parents('li').addClass('active');
					
			if (!IS_MOBILE)
			{
				Cufon.replace('.main_menu a');
			}
			
			var type = $(this).attr('href'); 
			type = type.substring(1, type.length);
			$("#menu_holder").animate({opacity: 0}, 500, function() {
				$(this).load(WWW_ROOT + "ajax/get-menu/", { type: type }, function() {
					$(this).animate({opacity: 1}, 500).find(".scroll_area").scrollbar({ buttonHeight: 0 });
			})});
			
			$("#submenu_wrapper .submenu.active").fadeOut(500, function() {
				$(this).removeClass('active').find('.active').removeClass('active');
				$("#submenu_wrapper .submenu").find('li:eq(0) a').addClass('active');
				$("#submenu_wrapper .submenu").eq(type-1).fadeIn(500, function() {
					$(this).addClass('active');
					if (!IS_MOBILE)
					{
						Cufon.replace('.submenu a');
					}
				});
				
				if(type == '3')
				{
					$('#update_date').fadeIn(500);
				}
				else
				{
					$('#update_date').fadeOut(500);
				}				
			});
		}
		
		return false;
	}
	
	function swapSubMenu()
	{
		if(!$(this).hasClass('active'))
		{
			$(".submenu a.active").removeClass('active');
			$(this).addClass('active');
			
			if (!IS_MOBILE)
			{
				Cufon.replace('.submenu a');
			}
			
			var cat = $(this).attr('href'); 
			cat = cat.substring(1, cat.length);
			var type = $(".main_menu li a.active").attr('href'); 
			type = type.substring(1, type.length);
			
			$("#menu_holder").animate({opacity: 0}, 500, function() {
				$(this).load(WWW_ROOT + "ajax/get-menu/", { category: cat, type: type }, function() {
					$(this).animate({opacity: 1}, 500).find(".scroll_area").scrollbar({ buttonHeight: 0 });
			})});
		}
		
		return false;
	}
	
	function loadMenuRoute(type, cat)
	{
		$(".main_menu li.active").removeClass('active').find(".active").removeClass('active');
		$(".main_menu li a[href=#" + type + "]").addClass('active').parents('li').addClass('active');
				
		if (!IS_MOBILE)
		{		
			Cufon.replace('.main_menu a');
		}
			
		if(!cat)
		{
			$("#menu_holder").animate({opacity: 0}, 500, function() {
				$(this).load(WWW_ROOT + "ajax/get-menu/", { type: type }, function() {
					$(this).animate({opacity: 1}, 500).find(".scroll_area").scrollbar({ buttonHeight: 0 });
			})});
		}
		else
		{
			$("#menu_holder").animate({opacity: 0}, 500, function() {
				$(this).load(WWW_ROOT + "ajax/get-menu/", { category: cat, type: type }, function() {
					$(this).animate({opacity: 1}, 500).find(".scroll_area").scrollbar({ buttonHeight: 0 });
			})});
		}
		
		$("#submenu_wrapper .submenu.active").fadeOut(500, function() {
			$(this).removeClass('active').find('.active').removeClass('active');
			
			if(cat)
				$("#submenu_wrapper .submenu").find('li a[href=#' + cat + ']').addClass('active');
			else
				$("#submenu_wrapper .submenu").find('li:eq(0) a').addClass('active');
			$("#submenu_wrapper .submenu").eq(type-1).fadeIn(500, function() {
				$(this).addClass('active');
				if (!IS_MOBILE)
				{
					Cufon.replace('.submenu a');
				}
			});
		});
		
		return false;
	}