(function($) {

	$.fn.prepareLightbox = function(settings) {
		var lightboxSettings = {
			borderSize: 0,
			height: 550,
			width: 550,
			windowPadding: 50,
			windowSourceID:''
		};
		
		var config = { cloneTitle: true };
		if (settings) $.extend(config, settings);
		
		this.each(function(i) {
			var $current = $(this).addClass('js');
			var $lightboxTarget = $('<div class="click_target"><div id="LightboxContainer'+i+'" class="lightbox_container"></div></div>').prependTo($current);
			var $lightboxContainer = $('#LightboxContainer'+i, $lightboxTarget);
			$('<a href="#" class="close">Close</a>').prependTo($lightboxContainer).closeDOMWindow({eventType:'click'});
			if( config.cloneTitle ) {
				$current.children('.page_title').prependTo($lightboxTarget).clone().appendTo($lightboxContainer);
			} else {
				$current.children('.page_title').prependTo($lightboxTarget);
			}
			$current.children('.lightbox_content').appendTo($lightboxContainer);
			lightboxSettings.windowSourceID = '#LightboxContainer'+i;
			$lightboxTarget.click(function(){
				$.openDOMWindow(lightboxSettings); 
				return false;
			});
		});
		
		return this;

	};
	
	$.fn.setupTabs = function() {
		var $container = this.parent();
		var $tabHandles = $('<ul class="tab_handles"></ul>');
		this.each(function(i) {
			var $target = $(this);
			$('<li>'+$target.children('h3').hide().text()+'</li>').appendTo($tabHandles).click(function(){
				$(this).addClass('current').siblings('.current').removeClass('current');
				$target.siblings('.menu').hide();
				$target.show();
			});
		}).hide().filter(':first-child').show();
		$tabHandles.prependTo($container).children(':first-child').addClass('current');
		
		return this;
	}

})(jQuery);

$(function(){
	// Implement slide to
	$('#PagesNav a, .in_page a.more').click(function(e){
		e.preventDefault();
		$.scrollTo(this.hash,{
			duration: 500,
			easing: 'swing',
			offset: {left: -25, top: 0}
		});
	});
	$('.in_page a.back').click(function(e){
		e.preventDefault();
		$.scrollTo(this.hash,{
			duration: 500,
			easing: 'swing',
			offset: {left: -100, top: 0}
		});
	});
	// Fade the map when hovering over directions
	$('#Directions > li').hover(function(){
		$('#'+ this.getAttribute('data-map')).siblings().stop().animate({opacity:0.3},500);
	},function(){
		$('#'+ this.getAttribute('data-map')).siblings().stop().animate({opacity:1},500);
	});
	// Lightbox for the terms & conditions
	$('#TermsPage').prepareLightbox();
	$('#Menus').prepareLightbox({cloneTitle: false});
	$('#Menus .menu').setupTabs();
});

$(function(){
  $('.error_close').click(function() {
    $('.error_info').fadeOut();
    $('.error_bg').fadeOut();
  });
});
