var page = {
	
	animationMultiplier: 10,
	contentPosition: 'hidden',
	backgroundImage: '',
	currentPage: 'index',
	ajaxURLprefix: '',
	ajaxURLpostfix: '',
	loadPage: '',
	pathStart: 'http://',
	fallback: 0,
	pageInitialized: false,
	initialLoad: true,
	hashChangeDetection: true,
	loadQueue: new Array(),
	
	init: function()
	{
		//console.log('------------------');
		//console.log('INIT - PAGE RELOAD');
		//console.log('------------------');
		page.setInitPage();
		page.checkHashString(location.href);
		page.bindEvents();
		page.footer.init();
		//page.setLogoAlt();
	},
	
	setInitPage: function()
	{
		var href = location.href,
			href_elements = new Array();
			
		href_elements = href.split('#');
		href = href_elements[0];
		href_elements = href.split('/');
		href = href_elements.pop();
		
		href = href.replace(page.ajaxURLpostfix,'');
		
		if( href == '' )
		{
			href = 'index';
		}
		
		page.currentPage = href;
		
		//console.log('current page: '+page.currentPage);
		
		// set full url
		href = location.href;
		href = href.replace(page.pathStart,'');
		href_elements = href.split('/');
		href = href_elements.pop();
		href = '';
		
		for(i=0; i<href_elements.length; i++)
		{
			if( href_elements[i] != '' )
			{
				href += href_elements[i]+'/';
			}
		}
		href = page.pathStart+href;
		page.ajaxURLprefix = href;
	},
	
	checkHashString: function(href)
	{
		var link_elements = new Array(),
			ajax_hash = '';
		
		link_elements = href.split('#');
		
		if( link_elements.length > 1 )
		{
			ajax_hash = link_elements[1].split("?")[0];	
		}
		else
		{
			link_elements = link_elements[0].split('/');
			ajax_hash = link_elements.pop();
			ajax_hash = ajax_hash.replace(page.ajaxURLpostfix, '');
		}
		
		//console.log('check hash: '+ajax_hash);
		
		
		if( ajax_hash.length > 0 && ajax_hash != page.currentPage )
		{
			
			
			if( page.loadQueue.length == 0 )
			{
				page.loadQueue.push(ajax_hash);
				//console.log('load new page');
				// a new page has to be loaded via AJAX
				page.updateCurrentLink(ajax_hash);
				page.updateURL(ajax_hash);
				page.ajaxLoadPage(ajax_hash);
			}
			else if( page.loadQueue.length == 1 )
			{
				page.loadQueue.push(ajax_hash);
			}
			else
			{
				page.loadQueue[1] = ajax_hash;
			}
		}
		else
		{
			//console.log('show current page');
			page.loadBackgroundImage();
			page.updateCurrentLink(ajax_hash);
		}
	},
	
	updateCurrentLink: function(url)
	{
		var newCurrentLink = $('#menu_wrap ul#mainnav').find('a[href$="/'+url+page.ajaxURLpostfix+'"]'),
			this_navset = newCurrentLink.parent().parent();
		// $('#menu_wrap ul').find('a').removeClass('current');
		// newCurrentLink.addClass('current');
		// console.log('updateCurrentLink: '+newCurrentLink.length+', url: '+url);
		page.highlightCurrentLink(newCurrentLink);
		if( this_navset.hasClass('secondary_nav') )
		{
			page.showSubnav(this_navset.siblings('a'));
		}
		else
		{
			page.checkHidingSubnav(newCurrentLink);
		}
		
	},
	
	loadBackgroundImage: function()
	{
		//console.log('loadBackgroundImage');
		
		var link_to_bg = $('body').children('.content_wrap').children('.bg_image_link').children('a').eq(0);
			bg_url = link_to_bg.attr('href'),
			bg_img = '<img src="'+bg_url+'">',
			bg_img_existing = $('#bg_image').children('img[src$="'+bg_url+'"]');
		
		if( link_to_bg.length < 1 )
		{
			// url for bg image not found
			page.showContentWrap();
		}	
		else if( bg_img_existing.length > 0 )
		{
			// bg image has already been inserted
			//console.log('img exists');
			
			if( bg_img_existing.is(':visible') )
			{
				// bg image is also visible, just slide in content then
				//console.log('img is visible');
				page.showContentWrap();
			}
			else
			{
				// bg image not visible
				//console.log('img not visible');
				
				var vis_img = $('#bg_image').children('img:visible');
				
				if( vis_img.length == 0 )
				{
					//console.log('no other images, just fade in');
					bg_img_existing.fadeIn(150*page.animationMultiplier, function(){
						page.showContentWrap();
					});
				}
				else
				{
					if( vis_img.index() > bg_img_existing.index() )
					{
						//console.log('img below, fade out others');
						bg_img_existing.show();
						vis_img.fadeOut(150*page.animationMultiplier, function(){
							page.showContentWrap();
						});
					}
					else
					{
						//console.log('img above, fade in, hide others');
						bg_img_existing.fadeIn(150*page.animationMultiplier, function(){
							vis_img.hide();
							page.showContentWrap();
						});
					}
				}
			}
		}
		else
		{
			// new bg image -> insert
			//console.log('insert new image, fade in, hide others');
			$('#bg_image').append(bg_img);
			$('#bg_image').children('img').last().load(function(){
				page.resizeBackgroundImages();
				$(this).fadeIn(150*page.animationMultiplier, function(){
					$(this).siblings('img').hide();
					page.showContentWrap();
				});
			});
		}
		
		
		// Call content slide-in-animation anyways after x ms, even if load-event doesn't fire
		setTimeout(page.slideInFallback,6000);

	},
	
	resizeBackgroundImages: function()
	{
		var window_height = $(window).height(),
			window_width  = $(window).width() - $('#menu_wrap').width(),
			window_ratio  = 1.5,
			img_width     = 0,
			img_height    = 0,
			img_ratio     = 1.5;
		
		if( window_width > 0 && window_height > 0 )
		{
			window_ratio = window_width/window_height;
		}
		
		$('#bg_image').children('img').each(function(){
			img_width  = $(this).width();
			img_height = $(this).height();
			
			if( img_width > 0 && img_height > 0 )
			{
				img_ratio = img_width/img_height;
				
				if( img_ratio > window_ratio )
				{
					$(this).css({
						width: 'auto',
						height: window_height+'px'
					});
				}
				else
				{
					$(this).css({
						width: window_width+'px',
						height: 'auto'
					});
				}
			}
		});
		
		page.stretchContent();
	},
	
	stretchContent: function()
	{
		// stretch content if menu wrap is taller
		var menu_wrap_height = $('#menu_wrap').height(),
			content_wrap = $('body > .content_wrap'),
			content_wrap_height = content_wrap.height();
			
		if( menu_wrap_height > content_wrap.height() )
		{
			content_wrap.height(menu_wrap_height);
		}
	},
	
	showContentWrap: function()
	{
		//console.log('showContentWrap');
		page.loadQueue.shift();
		
		if( page.loadQueue.length > 0 )
		{
			var nextLoad = ''+page.loadQueue.shift();
			//console.log('SKIP FADING IN, NEXT LOAD: '+nextLoad);
			page.checkHashString(nextLoad);
		}
		else
		{
			//console.log('NOTHING in CUE');
			page.checkTemplate();
			page.stretchContent();
			window.scrollTo(0,0);
			$('body > .content_wrap').animate({left: '260px'}, 120*page.animationMultiplier, 'easeOutCirc', function(){
				page.contentPosition = 'visible';
				page.hideAjaxLoader();
			});
			page.stripImageAlts($('body > .content_wrap'));
		}
	},
	
	slideInFallback: function()
	{
		//console.log('slideInFallback');
		
		if( page.contentPosition != 'visible' )
		{
			//console.log('fallback slides in content');
			page.showContentWrap();
		}
	},
	
	hideContentWrap: function()
	{
		//console.log('hideContentWrap');
		$('body > .content_wrap').animate({left: '-440px'}, 100*page.animationMultiplier, 'easeInCirc', function(){
			page.contentPosition = 'hidden';
		});
	},
	
	bindEvents: function()
	{
		//console.log('ajaxLoadEventListener');
		
		// Form event first live event // IE BUGFIX
		$('body > .content_wrap .standard_form').live('submit',function(event){

			if( page.form.validate() )
			{
				// Form ok, show thank you page
				page.form.sendForm();
			}
			else
			{
				// form didn't validate
			}
			
			return false;
		});
		
		
		// FETCH >>>ALL<<< LINK CLICKS
		$('a:not("#footer_toggle a")').live('click', function(){
			
			if( $(this).parent().hasClass('media_region') )
			{
				return true;
			}
			else if( $(this).hasClass('close_content')) {
				page.hideContentWrap(); return false;
			}
			else if( $(this).hasClass('current') && !$(this).hasClass('folder')) {
				page.showContentWrap(); return false;
			}
			else
			{
				page.hashChangeDetection = false;
				page.parseLink($(this));
				return false;
			}
			
		});
		
		
		// WINDOW RESIZE + HASH CHANGE
		$(window).resize(function(){
			page.resizeBackgroundImages();
		})
		.hashchange(function(){
			if( page.hashChangeDetection )
			{	
				page.pageInitialized = false;
				page.checkHashString(location.href);
			}
		});
		
		
		// IMAGE LISTING, CLICK-EVENT ON IMAGES
		$('.image_listing .left img, .image_listing .right h3').live('click', function(){
			if( $(this).parent().parent().find('.bottom_link a').length )
			{
				var link_to = $(this).parent().parent().find('.bottom_link a');
				if(link_to.attr('href') != '#' )
				{
					page.hashChangeDetection = false;
					page.parseLink(link_to);
				}
			}
		});

	},
	
	parseLink: function(link)
	{
		// decide what to depending on HREF
		var _href = link.attr('href');
		
		if( link.parent().parent().hasClass('navset') )
		{
			// this is a menu link
			if( link.hasClass('folder') )
			{
				// it's a folder, show subnav
				page.showSubnav(link);
			}
			else if( link.hasClass('backlink') )
			{
				// going back up 1 nav level
				page.hideSubnav(link);
			}
			else
			{
				// it's a page link, load content
				if( !link.hasClass('current') )
				{
					// not the current page, load in new
					page.pageInitialized = false;
					page.showAjaxLoader();
					// page.highlightCurrentLink(link); // reset styling for menu links to highlight this
					page.checkHashString(_href);
				}
			}
			
		}
		else
		{
			if( _href != '#' )
			{
				if( page.isLocalLink(_href) )
				{
					page.pageInitialized = false;
					page.showAjaxLoader();
					// page.highlightCurrentLink(link); // reset styling for menu links to highlight this
					page.checkHashString(_href);
				}
				else
				{
					//console.log('open link in new window');
					page.hashChangeDetection = true;
					window.open(_href,'_blank');
				}
			}
		}
		
		
	},
	
	isLocalLink: function(href)
	{
		// check if it is a link pointing to the website itself
		
		for(i=0; i < DOMAINS.length; i++)
		{
			var pattern = new RegExp(DOMAINS[i],"i");			
			
			if( href.match(pattern) )
			{
				return true;
			}
		}
		return false;
	},
	
	helperInArray: function(needle,haystack)
	{
		for( i=0; i < haystack.length; i++ )
		{
			if(haystack[i]==needle)
			{
				return true;
			}
		}
		return false;
	},
	
	highlightCurrentLink: function(link)
	{
		$('#menu_wrap').find('ul a').removeClass('current');
		link.addClass('current');
		// if link in folder, highlight folder as well
		if( link.parent().parent().hasClass('secondary_nav') )
		{
			link.parent().parent().siblings('a').addClass('current');
		}
	},
	
	ajaxLoadPage: function(link)
	{
		//console.log('ajaxLoadPage: '+link);
		
		var container_name = link;
		page.loadPage = container_name;
		link = page.ajaxURLprefix + link + page.ajaxURLpostfix;
		
		page.hideContentWrap();
		
		if( $('#aj_container_'+container_name).length == 0 )
		{
			//console.log('appending new container (link: '+link+')(container: '+container_name+')');
			$('#aj_container').append('<div id="aj_container_'+container_name+'"></div>');
			
			$('#aj_container_'+container_name).load(link +' .content_wrap', function(){
				page.fallback = 0;
				page.ajaxSwitchContent();
			});
		}
		else
		{
			//console.log('container exists, not loading page again!');
			page.fallback = 0;
			page.ajaxSwitchContent();
		}
	},
	
	ajaxSwitchContent: function()
	{
		//console.log('ajaxSwitchContent: '+page.loadPage +'/'+page.fallback);
		
		if( page.contentPosition == 'hidden' || page.fallback > 50 )
		{
			var new_content = $('#aj_container_'+page.loadPage).find('.content_wrap');
			if(new_content.length == 1)
			{
				$('body').children('.content_wrap').replaceWith(new_content.clone());
				
				page.initialLoad = false;
			}
			else
			{
				//alert('no content found! #aj_container_'+page.loadPage);
			}
			//$('#aj_container').html('');
			page.loadBackgroundImage();
		}
		else
		{
			page.fallback++;
			setTimeout(page.ajaxSwitchContent, 50);
		}
	},
	
	updateURL: function(link)
	{
		//console.log('ajax_hash: #'+link);
		page.currentPage = link;
		location.hash = link;
		page.googleVirtualPageView('/'+link);
	},
	
	googleVirtualPageView: function(pageName)
	{
		_gaq.push(['_trackPageview', pageName]);
	},
	
	showSubnav: function(navitem)
	{
		//console.log('showSubnav '+navitem);
		$('#mainnav').animate({left: '-260px'}, 60*page.animationMultiplier, 'easeInCirc', function(){
			navitem.parent().children('ul').show().animate({left: '260px'}, 60*page.animationMultiplier, 'easeOutCirc');
		});
	},

	hideSubnav: function(navitem)
	{
		//console.log('hideSubnav '+navitem);
		navitem.parent().parent().animate({left: '0px'}, 60*page.animationMultiplier, 'easeInCirc', function(){
			navitem.parent().parent().hide();
			$('#mainnav').animate({left: '0px'}, 60*page.animationMultiplier, 'easeOutCirc');
		});
	},
	
	checkHidingSubnav: function(link)
	{
		// if subnav is showing but link is on 1st level
		if( link.parent().parent().attr('id') == 'mainnav' )
		{
			if( $('#mainnav').find('.secondary_nav:visible').length > 0 )
			{
				var openedSubnavFirstLink = $('#mainnav').find('.secondary_nav:visible').find('a').first();
				page.hideSubnav(openedSubnavFirstLink);
			}
		}
	},
	
	showAjaxLoader: function(link)
	{
		//link.append('<img src="/templates/oscars_table/images/loader.gif" class="loader_img">');
		// console.log('adding SHOW class');
		$('#menu_wrap').find('li.loading_graphic').addClass('show');
	},
	
	hideAjaxLoader: function()
	{
		//$('#menu_wrap').find('.loader_img').remove();
		// console.log('removing SHOW class');
		$('#menu_wrap').find('li.loading_graphic').removeClass('show');
	},
	
	checkTemplate: function(){
		// Check what template is loaded and attach specific events to it
		// this neccessary for hoverIntent having no .live() functionality
		//console.log('checkTemplate');
		if( page.pageInitialized )
		{
			// Events have already been bound for this load sequence
			// console.log('page has been initialized!');
		}
		else
		{
			// console.log('init page');
			page.pageInitialized = true;
			
			var content_wrap = $('body').children('.content_wrap');
			
			if( content_wrap.children('.image_text').length > 0 )
			{
				//console.log('templateType: image_text');
				page.adjustImageOverlays();
				page.bindImageOverlayEvents();
			}
			else if( content_wrap.children('.gallery_items').length > 0 )
			{
				page.gallery.init();
			}
			else if( content_wrap.find('.standard_form').length > 0 )
			{
				page.form.init();
			}
			else
			{
				// console.log('no init found');
			}
			
			page.hashChangeDetection = true;
		}
	},
	
	stripImageAlts: function(parent)
	{
		parent.find('img').removeAttr('alt');
	},
	
	adjustImageOverlays: function()
	{
		
		// show captain on image hover
		$('.content_wrap').children('.image_text').find('.overlay').each(function(){
			// set overlay height to image height (-margin)
			var _this = $(this),
				overlay_height = _this.siblings('img').attr('height') - 24,
				html_content = $.trim(_this.children('.text').html());
			
			_this.css('height', overlay_height+'px');
			_this.parent().css('cursor','pointer');
			
			if( html_content.length < 10 )
			{
				_this.remove();
			}
		});
	},
	
	bindImageOverlayEvents: function()
	{
		// attach hover event to parent
		$('.left').hoverIntent(
				function(){
					$(this).children('.overlay').fadeIn((page.animationMultiplier * 60));
					$(this).children('.overlay').children('.overlay_bg').fadeTo((page.animationMultiplier * 50), 0.6);
				},
				function(){
					$(this).children('.overlay').fadeOut((page.animationMultiplier * 40));
					$(this).children('.overlay').children('.overlay_bg').fadeTo((page.animationMultiplier * 30), 0.0);
				}
			);
	},
	
	gallery: 
	{
		currentGallery: 'body > .content_wrap > .gallery',
		autoPlayMode: true,
		currentSlide: 1,
		maxSlides: 1,
		loaded: false,
		autoPlayTime: 5000,
		autoPlayRunning: false,
		fadeOutTime: 100,
		fallback: 50,
		hover: false,
		eventsBound: false,
		isHoveringControls: false,
		
		init: function()
		{
			//console.log('GALLERY INIT');
			
			page.gallery.bindEvents();
			page.gallery.autoPlayMode = false;
			page.gallery.loaded = false,
			page.gallery.resetPagination();
			page.gallery.resetSlides();
			page.gallery.fallback = 50,
			page.gallery.checkLoadStatus();
		},
		
		checkLoadStatus: function()
		{
			//console.log('checking load status/'+page.gallery.fallback);
			var notLoaded = 0;
			
			$(page.gallery.currentGallery).find('.item').children('img').each(function(){
				if( !$(this)[0].complete )
				{
					notLoaded++;
				}
			});
			
			if( notLoaded > 0 && page.gallery.fallback > 0 )
			{
				page.gallery.fallback--;
				setTimeout(page.gallery.checkLoadStatus, 100);
			}
			else
			{
				// all images loaded, let's go
				//console.log('GALLERY OK GO');
				$(page.gallery.currentGallery).find('.controls').fadeIn();
				page.gallery.autoPlayMode = true;
				if( !page.gallery.autoPlayRunning )
				{
					setTimeout(page.gallery.autoPlay, page.gallery.autoPlayTime);
				}
				
			}
		},
		
		bindEvents: function()
		{
			if( !page.gallery.eventsBound )
			{
				// catch all control events
				$('body > .content_wrap .gallery .control').live('click', function(){
					var _this = $(this);

					if( _this.hasClass('full') )
					{
						// toggle full screen mode
						$(page.gallery.currentGallery).toggleClass('full');
						$(page.gallery.currentGallery).find('.control.full').find('span').toggleClass('off');
						
						if( $.browser.msie && $.browser.version < 8)
						{
							$('#menu_wrap').toggle();
							$('#black_bar').toggle();
						}
					}
					else if( _this.hasClass('next') )
					{
						page.gallery.autoPlayMode = false;
						$('body > .content_wrap .gallery .control').removeClass('pause');
						page.gallery.nextImg();
					}
					else if( _this.hasClass('prev') )
					{
						page.gallery.autoPlayMode = false;
						$('body > .content_wrap .gallery .control').removeClass('pause');
						page.gallery.prevImg();
					}
					else if( _this.hasClass('pause') )
					{
						_this.removeClass('pause')
						page.gallery.pause();
					}
					else if( _this.hasClass('play') )
					{
						_this.addClass('pause')
						page.gallery.play();
					}
				});
				
				$('body > .content_wrap .gallery').live('click', function(){
					if( $(this).hasClass('full') && !page.gallery.isHoveringControls )
					{
						// toggle full screen mode
						$(page.gallery.currentGallery).toggleClass('full');
						$(page.gallery.currentGallery).find('.control.full').find('span').toggleClass('off');
					}
				});
				
				page.gallery.eventsBound = true;
			}
			
			$('body > .content_wrap .gallery .controls, body > .content_wrap .gallery .gallery_items')
				.live('mouseenter', function(){ page.gallery.isHoveringControls = true; })
				.live('mouseleave', function(){ page.gallery.isHoveringControls = false;  });
			
			$('body > .content_wrap .gallery .gallery_items .text').each(function(){
				var this_content = $(this).html();
				this_content = $.trim(this_content);
				if( this_content.length < 10 )
				{
					$(this).parent().remove();
				}
			});

			$('body > .content_wrap .gallery .gallery_items').hoverIntent(
				function()
				{
					var _overlays = $(this).find('.overlay');
					page.gallery.hover = true;
					
					_overlays.css('display','block');
					_overlays.children('.text').fadeIn((page.animationMultiplier * 100));
					_overlays.children('.overlay_bg').fadeTo((page.animationMultiplier * 80), 0.6);
				},
				function()
				{
					var _overlays = $(this).find('.overlay');
					
					_overlays.children('.text').fadeOut((page.animationMultiplier * 80), function(){
						$(this).parent().css('display','none');
						page.gallery.hover = false;
					});
					_overlays.children('.overlay_bg').fadeTo((page.animationMultiplier * 70),0.0);
				}
			);
		},
		
		resetSlides: function()
		{
			var slides = $(page.gallery.currentGallery).children('.gallery_items').children('.item');
			slides.hide();
			slides.first().show();
		},
		
		resetPagination: function()
		{
			page.gallery.currentSlide = 1;
			page.gallery.maxSlides = $(page.gallery.currentGallery).children('.gallery_items').children('.item').length;
			$(page.gallery.currentGallery).children('.controls').children('.pagination').children('.current_slide').html('1');
			$(page.gallery.currentGallery).children('.controls').children('.pagination').children('.slide_count').html(page.gallery.maxSlides);
		},
		
		setPagination: function(slide)
		{
			page.gallery.currentSlide = slide;
			$(page.gallery.currentGallery).children('.controls').children('.pagination').children('.current_slide').html(slide);
		},
		
		play: function()
		{
			page.gallery.autoPlayMode = true;
			if( !page.gallery.autoPlayRunning )
			{
				page.gallery.autoPlay();
			}
		},
		
		pause: function()
		{
			page.gallery.autoPlayMode = false;
		},
		
		prevImg: function()
		{
			var slides = $(page.gallery.currentGallery).find('.item');

			if( page.gallery.currentSlide == 1 )
			{
				page.gallery.setPagination(page.gallery.maxSlides);
				slides.last().fadeIn(page.gallery.fadeOutTime*page.animationMultiplier,function(){
					$(this).prevAll().hide();
				});
			}
			else
			{
				slides.eq(page.gallery.currentSlide-2).show();
				slides.eq(page.gallery.currentSlide-1).fadeOut(page.gallery.fadeOutTime*page.animationMultiplier);
				page.gallery.setPagination(page.gallery.currentSlide-1);
			}
		},
		
		nextImg: function()
		{
			var slides = $(page.gallery.currentGallery).find('.item');
			
			if( page.gallery.currentSlide == page.gallery.maxSlides )
			{
				page.gallery.setPagination(1);
				
				slides.first().show();
				slides.last().fadeOut(page.gallery.fadeOutTime*page.animationMultiplier, function(){
				});
			}
			else
			{
				page.gallery.setPagination(page.gallery.currentSlide+1);
				slides.eq(page.gallery.currentSlide-1).fadeIn(page.gallery.fadeOutTime*page.animationMultiplier, function(){
					$(this).prev().hide();
				});
			}
		},
		
		autoPlay: function()
		{
			if( page.gallery.autoPlayMode && !page.gallery.hover )
			{
				page.gallery.autoPlayRunning = true;
				
				page.gallery.nextImg();
				setTimeout(page.gallery.autoPlay, page.gallery.autoPlayTime);
			}
			else if( page.gallery.autoPlayMode && page.gallery.hover )
			{
				page.gallery.autoPlayRunning = true;
				setTimeout(page.gallery.autoPlay, page.gallery.autoPlayTime);
			}
			else
			{
				page.gallery.autoPlayRunning = false;
			}
		}
	},
		
	form: {
		
		formJustSent: false,
		
		init: function()
		{
			page.form.bindEvents();
			$('body > .content_wrap .standard_form').find('input[type=radio], input[type=checkbox]').ezMark();
		},
		
		bindEvents: function()
		{
			// $('body > .content_wrap .standard_form').live('submit',function(){
			// 				
			// 				if( page.form.validate() )
			// 				{
			// 					// Form ok, show thank you page
			// 					page.form.sendForm();
			// 				}
			// 				else
			// 				{
			// 					// form didn't validate
			// 				}
			// 				
			// 				return false;
			// 			});
			
			$('.back_to_form').click(function(){
				page.form.resetForm();
			});
		},
		
		thankYou: function()
		{
			var _wrap = $('body > .content_wrap');

			_wrap.animate({left: '-440px'}, 100*page.animationMultiplier, 'easeInCirc', function(){
				_wrap.find('div.form').hide();
				_wrap.find('.thankyou').show();
				
				_wrap.animate({left: '260px'}, 120*page.animationMultiplier, 'easeOutCirc', function(){
					page.hideAjaxLoader();
				});
			});
			
			page.googleVirtualPageView('/form/'+ _wrap.find('.standard_form').attr('id'));
		},
		
		sendForm: function()
		{			
			if( page.form.formJustSent )
			{
				// alert('You have just submitted the form. Please wait for your enquiry to be sent to us. Thank you.')
			}
			else
			{
				page.form.formJustSent = true;
				
				setTimeout(page.form.allowFormSubmits, 5000);
				
				var _form = $('body > .content_wrap').find('.standard_form'),
					aj_action = _form.attr('action'),
					aj_values = _form.serialize();

				page.showAjaxLoader();

				$.ajax({
					type: 'GET',
					url: aj_action+'?callback=?',
					data: aj_values,
					success: function()
					{
						page.form.thankYou();
						page.form.allowFormSubmits();
					}
				});
			}
		},
		
		allowFormSubmits: function(){
			page.form.formJustSent = false;
		},
		
		resetForm: function()
		{
			var _content = $('body > .content_wrap'),
				_form = _content.find('.standard_form');
			
			_form.find('input[type=text]').val('');
			_form.find('textarea').val('');
			_form.find('input[type=checkbox]').removeAttr('checked');
			_form.find('input[type=radio]').removeAttr('checked');
			_form.find('.ez-checkbox, .ez-radio').removeClass('ez-selected').removeClass('ez-checked').children('input').removeAttr('checked');
			
			if( _content.children('.thankyou').is(':visible') )
			{
				_content.animate({left: '-440px'}, 100*page.animationMultiplier, 'easeInCirc', function(){
					_content.find('div.form').show();
					_content.find('.thankyou').hide();

					_content.animate({left: '260px'}, 120*page.animationMultiplier, 'easeOutCirc');
				});
			}
			else
			{
				_content.find('div.form').show();
				_content.find('.thankyou').hide();
			}
		},
		
		validate: function()
		{
			var _form = $('body > .content_wrap').find('.standard_form');
			
			if( _form.length > 0 )
			{
				var everything_ok = true,
					email_invalid = false,
					error_message = '<span class="error"> - this field is required!</span>';

				_form.find('.error').remove();

				_form.find(".required").each(function(){

					var 
						this_type = $(this).attr('type'),
						this_name = $(this).attr('name'),
						this_val  = $(this).val();
										
					if( this_type == "checkbox" || this_type == "radio" )
					{
						if( _form.find("input[name="+this_name+"]:checked").length == 0 )
						{
							everything_ok = false;
							_form.find('.for_'+this_name).after(error_message);
						}
					}
					else
					{
						if( $(this).hasClass("email") )
						{
							if( !this_val.match(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/) )
							{
								// everything_ok = false;
								email_invalid = true;
								_form.find('.for_'+this_name).after(error_message);
							}
						}	
						else
						{
							if( this_val == "" )
							{
								everything_ok = false;
								_form.find('.for_'+this_name).after(error_message);
							}
						}
					}
				});
				
				if( _form.find('input[name="siteprompt_other"]').val() == "" && _form.find('input[name="siteprompt"]:checked').length == 0 )
				{
					everything_ok = false;
					_form.find('.for_siteprompt').after(error_message);
				}
				
				if( everything_ok && !email_invalid )
				{
					return true;
				}
				else
				{
					if( email_invalid )
					{
						alert('The email address you entered is not valid.');
					}
					else
					{
						alert('Please fill out all required fields.');
					}

					return false;
				}
			}
		}
	},
	
	footer: {
		
		init: function()
		{
			setTimeout(page.footer.hideFooter,1500);
			page.footer.bindEvents();
		},
		
		bindEvents: function()
		{
			$('#footer_toggle span').click(function(){
				if( $('#footer_toggle').hasClass('off') )
				{
					page.footer.showFooter();
				}
				else
				{
					page.footer.hideFooter();
				}
			});
		},
		
		hideFooter: function()
		{
			$('#footer_toggle').addClass('off');
			$('#footer').animate({bottom: '-138px'}, 100*page.animationMultiplier, 'easeOutExpo');
		},
		
		showFooter: function()
		{
			$('#footer_toggle').removeClass('off');
			$('#footer').animate({bottom: '0px'}, 120*page.animationMultiplier, 'easeOutExpo');
		}
	},

	setLogoAlt:function(){
			$("#logolink img").attr("alt","Oscars Table");
		}
};


$(function(){
	page.init();
});


