var last_url;
var ajaxRequest;

function supersizeImage() {
	var imagewidth = 550;
	var imageheight = 366;
	var browserwidth = $(window).width();
	var browserheight = $(window).height();

	var ratio = imageheight/imagewidth;

	$('img.supersize').each(function(){
		image = $(this);
		if ((browserheight/browserwidth) > ratio){
			$(image).height(browserheight);
			$(image).width(browserheight / ratio);
		} else {
			$(image).width(browserwidth);
			$(image).height(browserwidth * ratio);
		}
		$(image).css('left', (browserwidth - $(image).width())/2);
		$(image).css('top', (browserheight - $(image).height())/2);
	});
	
	$('.wrapper').height($(window).height() - $('.footer').height() - $('.header').height());
}

function currUrl(){
	url=document.location.href.split("#");
	return url[0];
}

function deepUrl(){
	url=document.location.href.split("#");
	if(url[1]){ 
		checkurl=url[1].toLowerCase();
		if(checkurl.indexOf('http://')<0 && checkurl.indexOf('https://')<0 && checkurl.indexOf('www.')<0){ 
			pageSwitch(checkurl)
		}
	}
}

function pageSwitch(target) {	
	if(last_url==target){
		return;
	}
	else {
		last_url=target;
		var newHash;
		filename = currUrl().split('/');
		filename = filename[filename.length-1];
		datatarget = currUrl().replace(filename,"")+target+".php";
		
		if(ajaxRequest){
			ajaxRequest.abort();	
		}
		
		ajaxRequest = $.ajax({
			type: "POST",
			url: datatarget,
			data: ({"json":true}),
			dataType: "json",
			error: function(){
//				location.reload(true);
			},
			success: function(json){		
				title = "Craven Insurance Services";
				if(json.title){
					title += " - "+json.title;
				}
				document.title = title;
				populatePage(json.content);
			}
		});
	}
}

function populatePage(data) {
	$(".contentCopy").fadeOut(400, function(){
		$(".contentCopy").html(data);
		$(".bodycopy").animate({
			height: $(".contentCopy").height()+40
		}, 500, function(){
			$(".contentCopy").fadeIn(400);
			pageload();
		});
	});
}

function pageload(){
	if($('.ourteam').length > 0){
		$('.ourteam a').click(function(){
			var target = $(this).attr('rel');
			$('.teaminfo').fadeOut(300, function(){
				$('.teaminfo div').hide();
				$('.teaminfo div.'+target).fadeIn(0);
				$('.teaminfo').fadeIn(300);
			});
		});
	}
	if($('.deepurl').length > 0) {
		$('.deepurl').click(function(){
			var target = $(this).attr('href').split('/');
			var filename = target[target.length-1].split('.');
			if(filename[0] != "business-quote"){
				document.location.href=currUrl()+'#'+filename[0];
				return false;
			}
		});
	}
}

$(window).load(
	function() {
		$('.loading').fadeOut(500);
		pageload();
		//RESIZE - all fullscreen image to be fullscreen
		supersizeImage();
		//LISTENER - in window that will resize, reposition everything
		$(window).bind("resize", supersizeImage);
		
		$('ul#nav a').click(function(){
			if($(this).attr('href') != "business-quote.php"){
				var target = $(this).attr('href').split('/');
				var filename = target[target.length-1].split('.');
				document.location.href=currUrl()+'#'+filename[0];
				return false;
			}
		});
		
		$('ul#nav').superfish({ 
			hoverClass:  'over',
			delay:       50,                            // one second delay on mouseout 
			animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
			speed:       'fast',                          // faster animation speed 
			autoArrows:  false                           // disable generation of arrow mark-up 
		});
		
		$('ul#nav li').hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});
		
		deepUrl();
		setInterval("deepUrl();",100);
		
		/* PROMAIL FORM ELEMENTS */
		
		document.em_subscribe_form.em_wfs_formfield_2447183.value = "Email:";
		document.em_subscribe_form.em_wfs_formfield_2457017.value = "Name:";		
		document.em_subscribe_form.em_wfs_formfield_2457022.value = "Company:";
		document.em_subscribe_form.em_wfs_formfield_2457023.value = "Phone:";
		document.em_subscribe_form.em_wfs_formfield_2457024.value = "Enquiry:";
		
		$('#em_wfs_formfield_2457017').focus(
			function() {
				if($(this).val()=="Name:") {
					$(this).val('');
				}
			}
		);
		$('#em_wfs_formfield_2457017').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Name:');
				}
			}
		);
		
		$('#em_wfs_formfield_2457022').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Company:');
				}
			}
		);
		$('#em_wfs_formfield_2457022').focus(
			function() {
				if($(this).val()=="Company:") {
					$(this).val('');
				}
			}
		);
		
		$('#em_wfs_formfield_2457023').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Phone:');
				}
			}
		);
		$('#em_wfs_formfield_2457023').focus(
			function() {
				if($(this).val()=="Phone:") {
					$(this).val('');
				}
			}
		);
		
		$('#em_wfs_formfield_2447183').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Email:');
				}
			}
		);
		$('#em_wfs_formfield_2447183').focus(
			function() {
				if($(this).val()=="Email:") {
					$(this).val('');
				}
			}
		);
		
		$('#em_wfs_formfield_2457024').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Enquiry:');
				}
			}
		);
		$('#em_wfs_formfield_2457024').focus(
			function() {
				if($(this).val()=="Enquiry:") {
					$(this).val('');
				}
			}
		);
		
		$('#em_subscribe_form').submit(function() {
			if($('#em_wfs_formfield_2457017').val()=="Name:" || $('#em_wfs_formfield_2457017').val()=="" || $('#em_wfs_formfield_2457017').val()==" "){
				alert("Please fill in your name");
				return false;
			} else if ($('#em_wfs_formfield_2457023').val()=="Phone:" || $('#em_wfs_formfield_2457023').val()=="" || $('#em_wfs_formfield_2457023').val()==" "){
				alert("Please fill in your phone");
				return false;
			} else if ($('#em_wfs_formfield_2447183').val()=="Email:" || $('#em_wfs_formfield_2447183').val()=="" || $('#em_wfs_formfield_2447183').val()==" "){
				alert("Please fill in your email");
				return false;
			} else if ($('#em_wfs_formfield_2457024').val()=="Enquiry:" || $('#em_wfs_formfield_2457024').val()=="" || $('#em_wfs_formfield_2457024').val()==" "){
				alert("Please fill in your enquiry");
				return false;
			} else {
				if(!checkForm(this)){
					return false;	
				}
				if ($('#em_wfs_formfield_2457022').val()=="Company:"){
					$('#em_wfs_formfield_2457022').val("");
				}
				window.open('', 'newsletterpopup', 'scrollbars=no,menubar=no,height=200,width=500,resizable=yes,toolbar=no,status=no');
				this.target = 'newsletterpopup';	
			}
		});
	}
);
