function initialise()
{
	do_corners();
	map_initialise();
	contact_form();
}

function do_corners()
{
	$("div#topbar a").corner("3px");
	$("p.copyright a").corner("3px");
	$("p.care-quality").corner("3px");
	$("p.directions a").corner("3px");
	$("p.phone a").corner("3px");
	$("p.back a").corner("3px");
	$("div#opening-times").corner("5px");

	//$("#form-wrap").corner("3px");
	//$("input.submit-button").corner("3px");
}

function map_initialise()
{
	if (GBrowserIsCompatible()) 
	{
		var elem = document.getElementById("map");
		
		if(elem != null)
		{
			var map = new GMap2(elem);
			var location = new GLatLng(53.143397, -1.196286);
			
			map.setCenter(location, 15);
			
			map.addControl(new GSmallMapControl());
			
			map.addOverlay(new GMarker(location));
		}
	}
}

function contact_form()
{
	$(".email-button").click(function() 
	{
		$("#contact-form").fadeIn("fast");
	});
	
	$(".close").click(function() 
	{
		$("#contact-form").fadeOut("fast");
	});
}

function process_form()
{	
	$("p.sending").fadeIn("fast");
	
	clear_form();
	
	$.post
	(
		"http://mansfieldelectrolysisandlaser.co.uk/includes/mail.php", 
		{ 
			name: $("#your-name").val(), 
			email: $("#your-email").val(), 
			comments: $("#your-comments").val(), 
			captcha: $("input#captcha").val()
		},
		function (data) { process_form_result(data); return true; },
		"json"
	);
}

function clear_form()
{
	$("p.success").fadeOut("fast");
	$("p.error").fadeOut("fast");

	$("#your-name").css("border-color", "#ccc");
	$("#your-email").css("border-color", "#ccc");
	$("#your-comments").css("border-color", "#ccc");
	$("input#captcha").css("border-color", "#ccc");
}

function process_form_result(data)
{	
	if(data.status)
	{
		$("p.sending").fadeOut("fast", function()
		{
			$("p.success").fadeIn("fast");
		});
	}
	else
	{
		$("p.sending").fadeOut("fast", function()
		{
			var first = true;
			var i = 0;
			do
			{
				// Only set the first error message, but highlight all the others using the red border.
				if(first)
				{
					$("span.error-text").html(data.errors[i].message);
					first = false;
				}
				
				$(data.errors[i].selector).css("border-color", "#EF352C");
				
				++i;
			}
			while(i < data.size);
			
			$("p.error").fadeIn("fast");
		});
	}
	
	return true;
}

function refresh_captcha()
{
	$("div#captcha-image").fadeOut("fast", function()
	{
		$(this).html('<img src="/includes/captcha.php?refresh=' + Math.random() + '" id="captcha" alt="Anti-Spam Captcha Image" />');
		
		$(this).fadeIn(700);
	});
}

function next_hr_picture()
{
	$.getJSON("http://www.mansfieldelectrolysisandlaser.co.uk/includes/pictures.js", function(data) 
	{
		current_hair_removal = (current_hair_removal >= (data.hair_removal.length - 1)) ? 0 : current_hair_removal + 1;
	
		$("div#hair-removal div.wrap").fadeOut("slow", function () 
		{
			$("div#hair-removal img.before").attr("src", data.hair_removal[current_hair_removal].before);
			$("div#hair-removal img.after").attr("src", data.hair_removal[current_hair_removal].after);
			$("div#hair-removal p.blurb").html(data.hair_removal[current_hair_removal].description);
			
			$(this).fadeIn("slow");
		});
		
		$("span.hair-removal").fadeOut("slow", function () 
		{
			$(this).html((current_hair_removal + 1) + " of " + data.hair_removal.length);
			
			$(this).fadeIn("slow");
		});
	});
}

function next_sr_picture()
{
	$.getJSON("http://www.mansfieldelectrolysisandlaser.co.uk/includes/pictures.js", function(data) 
	{
		current_skin_rejuvenation = (current_skin_rejuvenation >= (data.skin_rejuvenation.length - 1)) ? 0 : current_skin_rejuvenation + 1;
	
		$("div#skin-rejuvenation div.wrap").fadeOut("slow", function () 
		{
			$("div#skin-rejuvenation img.before").attr("src", data.skin_rejuvenation[current_skin_rejuvenation].before);
			$("div#skin-rejuvenation img.after").attr("src", data.skin_rejuvenation[current_skin_rejuvenation].after);
			$("div#skin-rejuvenation p.blurb").html(data.skin_rejuvenation[current_skin_rejuvenation].description);
			
			$(this).fadeIn("slow");
		});
		
		$("span.skin-rejuvenation").fadeOut("slow", function () 
		{
			$(this).html((current_skin_rejuvenation + 1) + " of " + data.skin_rejuvenation.length);
			
			$(this).fadeIn("slow");
		});
	});
}

function next_tv_picture()
{
	$.getJSON("http://www.mansfieldelectrolysisandlaser.co.uk/includes/pictures.js", function(data) 
	{
		current_thread_vein = (current_thread_vein >= (data.thread_vein.length - 1)) ? 0 : current_thread_vein + 1;
	
		$("div#thread-vein div.wrap").fadeOut("slow", function () 
		{
			$("div#thread-vein img.before").attr("src", data.thread_vein[current_thread_vein].before);
			$("div#thread-vein img.after").attr("src", data.thread_vein[current_thread_vein].after);
			$("div#thread-vein p.blurb").html(data.thread_vein[current_thread_vein].description);
			
			$(this).fadeIn("slow");
		});
		
		$("span.thread-vein").fadeOut("slow", function () 
		{
			$(this).html((current_thread_vein + 1) + " of " + data.thread_vein.length);
			
			$(this).fadeIn("slow");
		});
	});
}
