var settings;
var pane = new Object();
var api = new Object();
var allDataLoaded = false;
var enquiry;

var index;

var isSiteLoaded = false;

function supersizeImage(image) {
	/*
	var imagewidth = 1477;
	var imageheight = 7511;
	browserwidth = $(window).width();
	browserheight = $(window).height();

	var ratio = imageheight/imagewidth;

	if ((browserheight/browserwidth) > ratio){
		$(image).height(browserheight);
		$(image).width(browserheight / ratio);
	} else {
		if(browserwidth < 1800){
			browserwidth = 1800;
		}
		$(image).width(browserwidth);
		$(image).height(browserwidth * ratio);
	}
	$(image).css('left', (browserwidth - $(image).width())/2);
	$(image).css('top', (browserheight - $(image).height())/2);
	*/
}

//Force resize all the fullscreen image
function forceSupersize(){
	$('img#supersize').each(function(){
		//supersizeImage($(this));
		$(this).css('visibility','visible');
	});
	
	//$('.content').css('height', $(window).height() - 130);
	if(index > 0){
		$('#all_content').css("top",-1*index*$(window).height());
	}
}

function setScrollPanes(){
	pane = new Object();
	api = new Object();
	
	settings = {
		autoReinitialise : false
	};

	i = 0;
	$('.menu').each(function(){
		i++;
		
		name = i;
		if($(this).attr('id')){
			name = $(this).attr('id');
		}
		
		pane[name] = $(this);
		pane[name].jScrollPane(settings);
		api[name] = pane[name].data('jsp');	
	});

}

function showContentFromUrl(url){
	
	$('#nav li').removeClass('on');
	$('#nav li a[href="' + url + '"]').parents('li').addClass('on');
	
	if(allDataLoaded){
		makeContentVisible();
		
		content = $('#all_content div[data-href="' + url + '"]');

		if(content.html()){
			document.title = content.attr('data-title');
			forceSupersize();
			$('html,body').animate({
				scrollTop: content.position().top
			}, 500);
			
			/*
			$('#all_content, #supersize').animate({
				top: (-1 * content.position().top)
			},400);
			*/
		}
		else {
			window.location.reload();
		}
	}
	else {
		$('#all_content').fadeOut('fast',function(){
			$('#loading').fadeIn('fast');
		});	
	}
}

function makeContentVisible(){
	if($('#loading').is(":visible") || !$('#all_content').is(":visible")){
		$('#all_content').hide();
		$('#all_content').css('visibility','visible');
		$('#loading').fadeOut('fast');
		$('#all_content').fadeIn('fast');
		
		$('a.ajaxnav').click(function(){
			$('a.dinnermenuBtn').trigger('click');
			return false;
		});
		
		setScrollPanes();
		$('.slideshow').cycle();
		$('.menusub li').click(function(){
			$(this).parent().children('li').removeClass('on');
			$(this).addClass('on');
			var pos = $(this).index();
			var imageLoc = $(this).parentsUntil('cms').next('.privatemenu-sub');
			$(imageLoc).animate({
				opacity: 0
			},200, function(){
				$(imageLoc).children("img").hide();
				$(imageLoc).children("img").eq(pos).show();
				$(imageLoc).animate({
					opacity: 1
				},200);
			});
			var reference = $(this).parent().next().find('.menu').attr('id');
			if(api[reference]){
				if($(this).index() > 0){
					api[reference].scrollToY($('.menu #'+$(this).attr('data-anchor')).position().top);
				} else {
					api[reference].scrollToY(0);
				}
			}
		});
	}	
}


var historyStartup = true;

$(document).ready(function(){
	
	$('#seeretailspecial').click(function(){
		$('#nav li a[href="our-shops"]').trigger('click');
		return false;
	});
	if($('#all_content').attr('data-href')){
		$('#nav li a[href="' + $('#all_content').attr('data-href') + '"]').parents('li').addClass('on');
	}
	
	$('#nav li').hover(
		function(){
			$(this).children('ul').fadeIn(100);
		}, function(){
			$(this).children('ul').fadeOut(100);
		}
	);
	
	loc = window.location.href.split("#");
	if(loc[1]){
		if(loc[1] == "index"){
			window.location.href = $('base').attr('href');
		}
		else {
			window.location.href = $('base').attr('href') + loc[1];	
		}
		$('body').html('');
		return;
	}
	
	
	$.history.init(function(url){
		if(url && !historyStartup){
			showContentFromUrl(url);
		}
		historyStartup = false;
	},
	{ unescape: "/,&,=,?" });
	
	$('#nav li a').click(function(){	
		if(allDataLoaded && !$('#all_content div[data-href="' + $(this).attr("href") + '"]').html()){
			return true;	
		}

		jQuery.history.load($(this).attr('href'));
		
		return false;
	});
	
	$.ajax({
		type: "POST",
		url: "full-site",
		data: {"href" : $('#all_content').attr('data-href') },
		dataType:"json",
		success: function(resp){
			if($('#loading').is(":visible")){
				$('#all_content').css('visibility','hidden');
				$('#all_content').show();
			}
						
			//$('#all_content').append(resp.html);
			$('#all_content').html($('#all_content').html()+resp.html);
			setAjaxFunctions();

			forceSupersize();
			//setScrollPanes();
			allDataLoaded = true;
			
			//we need to check the hash again incase they clicked links while all the content was still loading
			loc = window.location.href.split("#");
			if(loc[1]){
				showContentFromUrl(loc[1]);
			}
			else {
				startupHref = $('#startup .content:first').attr('data-href');
				if(startupHref && $('#all_content div[data-href="' + startupHref + '"]')){
					//if the startup content is replicated we want to set the position to where it should be and remove the startup
					$('#startup').remove();
					var indexPos = $('#all_content div[data-href="' + resp.href + '"]').index();
					$('html,body').animate({
						scrollTop: $('#all_content div[data-href="' + resp.href + '"]').position().top
					}, 500);
					//$('#all_content, #supersize').css('top',(-1 * $('#all_content div[data-href="' + resp.href + '"]').position().top));
					//supersizeImage($('#supersize'));
				}
				makeContentVisible();
			}
		}
	});
	setAjaxFunctions();
	
});

function setAjaxFunctions(){
	$('form.ajax-submit').each(function(){
		if($(this).hasClass('isset')){
			return;	
		}
		else {
			var form = $(this);
			form.addClass('isset');
			form.submit(function(){
				if(!form.hasClass('loading')){					
					form.addClass('loading');
				
					form.find('.submit').fadeOut('fast',function(){
						form.find('.submit-loading').fadeIn('fast',function(){
							submitData = new Object();
							
							submitData['json'] = true;
							
							form.find('input, textarea, select').each(function(){
								if($(this).attr('name')){
									type = $(this).attr('type');
									if(type=='radio' || type=='checkbox'){
										if($(this).is(":checked")){
											submitData[$(this).attr('name')] = $(this).val();
										}
									}
									else {
										submitData[$(this).attr('name')] = $(this).val();
									}
								}
							});
												
							$.ajax({
								type: "POST",
								url: form.attr('action'),
								data:submitData,
								dataType:"json",
								error: function(){
									alert('An unexpected error occurred. Please try again');
									
									form.removeClass('loading');
									form.find('.submit-loading').fadeOut('fast',function(){
										form.find('.submit').fadeIn('fast');
									});
								},
								success: function(response){
									alertMsg = "";
																
									if(response.errors){
										for(i in response.errors){
											alertMsg += " - " + response.errors[i] + "\n";		
										}
									}
									
									if(alertMsg){
										alert("The following errors occurred:\n" + alertMsg);	
									}
									else {
										if(response.form_replace_html){
											form.slideUp('fast',function(){
												form.html(response.form_replace_html);
												form.slideDown('fast');
											});
										}
										else {
											if(response.evaluate){
												eval(response.evaluate);	
											}
											else {
		//										form.find('input, textarea, select').val('');
												alert('Thank you. Your details have been saved.');	
											}
										}
									}
									
									form.removeClass('loading');
									form.find('.submit-loading').fadeOut('fast',function(){
										form.find('.submit').fadeIn('fast');
									});
		
								}
							});
						});
					});
				}
				
				return false;
			});
		}
	});

	$("a.fancyboxMe").fancybox({
		overlayOpacity: 0.6,
		overlayColor: '#000',
		speedIn: 400,
		autoDimensions: true,
		autoScale: false,
		titleShow: false,
		padding: 15
	});	
	
	$('a.enquiry-form').each(function(){
		if($(this).hasClass('isset')){
			return;	
		}
		else {
			$(this).addClass('isset');
			$(this).click(function(){			
				if(enquiry){
					enquiry.abort();	
				}
				
				var href = $(this).attr('href');
				
				$('#loading').fadeIn('fast');
				
				enquiry = $.ajax({
					type: "POST",
					url: href,
					data:{ "mode" : "getForm" },
					dataType:"json",
					error: function(){
						alert('An unexpected error occurred. Please try again.');
					},
					success: function(resp){
						$.fancybox(resp.html,
						{
							'onComplete':function(){
								setAjaxFunctions();
								$('#loading').stop().hide();
							}
						});
					}
				});
				
				return false;
			});
		}
	});	
}

//LISTENER - in window that will resize, reposition everything
$(window).bind("resize", function(){
	forceSupersize();
	//setScrollPanes();
	loc = window.location.href.split("#");
	if(loc[1]){
		showContentFromUrl(loc[1]);
	}
	else {
		showContentFromUrl($('#all_content').attr('data-href'));
	}
});

$(window).load(function() {
	forceSupersize();
		
	if(!allDataLoaded){
		//setScrollPanes();
		
		$('#all_content').hide();
		$('#all_content').css('visibility','visible');
		$('#loading').fadeOut('fast');
		$('#all_content').fadeIn('fast');
	}
});
