var contentOpen = false;
var currentPage = "";

$(document).ready(function(){
/*
	Set some variables for use in later functions
*/
	var browser = $.browser.browser();
	var tellFriendVisible = false;
/*
	Set the position of the tell a friend popup
	relative to the position of the wrapper.
*/
	//var pos = $("#wrapper").offset();
/*
	Set the position of the map container relative to the 
	position of the wrapper div.
*/
	//$("#map_container").css({ "left" : pos.left + 40 + "px" });
/*
	Load the flash container into the #movie_container div
*/
	
	var screensize = screen.width;
	var screenheight = screen.height;
	//alert(screenheight);
/*
	if(screenheight > 768){
*/
	  //if(screensize > 3000){
		$("#movie_container").flash({
	  		src: 'fl/main_version_scrubber.swf',
			version: 8,
	   		width: 930,
	   		height: 606,
			wmode: "opaque"
		});
/*
	}else{
			$("#movie_container").css({ 
				width: '760px',
				height: '495px',
				margin: 'auto'
			});
			$("#movie_container_inner").css({
				width: '760px',
				height: '495px'
			});
			$("#movie_container").html("<div id=\"movie_container_inner\"></div>");
		
			$("#movie_container_inner").css({
				height: '495'
			});
		
			$("#movie_container_inner").flash({
		  		src: 'fl/main_version_scrubber.swf',
				version: 8,
		   		width: '760',
		   		height: '495',
				align: 'top',
				wmode: "opaque"
			});
			
			$("#map_container").css( 'top', '2px');
			$("#footer_links_container").css('top', '-100px');
			$("#tell_friend_container").css('top', '310px');
			$("#tell_friend_success_container").css('top', '400px');
		
	}
*/
	
/*
	Mouse over effects for the tell a friend button.
*/
	$("#tell_friend_link").mouseover(function(){
		$(".tell_friend").css({ "background": "url(img/tell_friend.gif) no-repeat bottom" });
	});
/*
	Mouse out efects for the tell a friend button.
*/
	$("#tell_friend_link").mouseout(function(){
		if(tellFriendVisible == false){
			$(".tell_friend").css({ "background": "url(img/tell_friend.gif) no-repeat top" });
		}
	});
/*
	When the 'tell a friend' button is clicked, the following function is performed.
	It uses the above variable (broswer). If the browser is 'Internet Explorer', it will
	set the the display of the '#tell_friend_container' to 'block' this is because ie doesnt
	like fading in transparent png's. If the browser isnt IE then the div is fated in.
	
	The function uses the variable 'tellFriendVisible' to check whether the div is visible or not
	and shows/hides the div accordingly.
*/
	$("#tell_friend_link").click(function(){
		if(browser == "Internet Explorer"){
			if(tellFriendVisible == false){
				$("#tell_friend_container").css({ "display" : "block" });
				$(".tell_friend").css({ "background": "url(img/tell_friend.gif) no-repeat bottom" });
				tellFriendVisible = true;
			}else{
				$("#tell_friend_container").css({ "display" : "none" });
				tellFriendVisible = false;
			}
		}else{
			if(tellFriendVisible == false){
				$("#tell_friend_container").fadeIn("slow");
		
				$(".tell_friend").css({ "background": "url(img/tell_friend.gif) no-repeat bottom" });
				tellFriendVisible = true;
			}else{
				$("#tell_friend_container").fadeOut("slow");
				tellFriendVisible = false;
			}
		}
	});
	
/*
	the following to click functions call the openContent function
	and parse them the value for the switch statement.
*/
	$("#policy_link").click(function(){
		openContent("policy");
	});
	$("#terms_link").click(function(){
		openContent("terms");
	});
/*
	Rollover / Rollout / Click functions for the close button in the map container.
*/
	$("#close_btn").mouseover(function(){
		$("#close_btn").css({ "background-position" : "bottom" });
	});
	
	$("#close_btn").mouseout(function(){
		$("#close_btn").css({ "background-position" : "top" });
	});
	
/*
	Tell a friend stuff!
*/
	$("#tell_friend_close_btn").mouseover(function(){
	/*
		Set the close button to its hover state
	*/
		$("#tell_friend_close_btn").css({ "background-position" : "bottom" });
	});
	
	$("#tell_friend_close_btn").mouseout(function(){
	/*
		Set the close button to its default state.
	*/
		$("#tell_friend_close_btn").css({ "background-position" : "top" });
	});
	
	$("#tell_friend_close_btn").click(function(){
		if(tellFriendVisible == true){
			if(browser == "Internet Explorer"){
			/*
				hide the tell friend container and set the tell friend link to its default state.
				Then set the status variable to false.
			*/
				$("#tell_friend_container").css({ "display" : "none" });
				$(".tell_friend").css({ "background": "url(img/tell_friend.gif) no-repeat top" });
				tellFriendVisible = false;
			}else{
			/*
				Fade out the tell friend container and set the tell friend link to its default state.
				Then set the status variable to false.
			*/
				$("#tell_friend_container").fadeOut("slow");
				$(".tell_friend").css({ "background": "url(img/tell_friend.gif) no-repeat top" });
				tellFriendVisible = false;
			}
		}
	});
	
	$("#tell_friend_success_close_btn").mouseover(function(){
		$(this).css({ "background-position" : "bottom" });
	});
	
	$("#tell_friend_success_close_btn").mouseout(function(){
		$(this).css({ "background-position" : "top" });
	});
	
	$("#tell_friend_success_close_btn").click(function(){
		$("#tell_friend_success_container").css({ "display" : "none" });
		$(".tell_friend").css({ "background": "url(img/tell_friend.gif) no-repeat top" });
		tellFriendVisible = false;
	});
	
	$("#tell_friend_submit_btn").mouseover(function(){
	/*
		Set submit button to its hover state.
	*/
		$("#tell_friend_submit_btn").css({ "background-position" : "bottom" });
	});
	
	$("#tell_friend_submit_btn").mouseout(function(){
	/*
		Set the submit button to its default state
	*/
		$("#tell_friend_submit_btn").css({ "background-position" : "top" });
	});
	
	
	
	$("#tell_friend_submit_btn").click(function(){
	/*
		When the submit button is pressed.
	*/
		var email = $("#email").val(); 
		var comment = $("#comment").val();
		var emailReqChars = "@";
		var emailCharMatch = email.search(emailReqChars);
	
		if(emailCharMatch < 1){
			$("#email").css({
				"color" : "#c27979",
				"font-weight" : "bold"
			});
			$("#email").val("Invalid email");
		}else{
		/*
			If the email is valid. Use jquery ajax call to send
			an email to the address entered, and store a log in the database.
		*/
			$.post("old_website/tell_friend.php", { email: email, comment: comment },
			function(data){
				if(data == true){
				/*
					If the email was sent and the addition to the database was made then:
					~ Send a success massage.
					~ clear the form input fields
					~ close the tell_friend_container
				*/
					$("#tell_friend_success_container_left").html("<h1>Success</h1><p>Link sent to <b>"+email+"</b>.<br /> Thank you for sharing this site.</p>");
					$("#email").val(""); 
					$("#comment").val("");
					$("#tell_friend_container").css({ "display" : "none" });
					$("#tell_friend_success_container").css({ "display" : "block" });
				}else{
					alert('fail');
				}
			});
			return false;
		}	
	});
	
	$("#email").focus(function(){
		if($("#email").val() == "Invalid email"){
			$("#email").val("");
			$("#email").css({
				"color" : "#6E6E6E",
				"font-weight" : "100"
			});
			//alert('!!!');
		}
	});
	$("#content_close_btn a").click(function(){
		$("#content_container").hide();
		contentOpen = false;
		currentPage = "";
	});
});


function openContent(content){
	
	switch(content){
	/*
		Opening and closing the content div if the
		'policy' button is clicked.
	*/
		case "policy":
			if(currentPage == content){
				$("#content_container").hide();
				$("#privacy_policy").css({ "display":"none" });
				$("#terms_conditions").css({ "display":"none" });
				contentOpen = false;
				currentPage = "";
			}else{
				if(contentOpen == true){
					// if the container is open
					$("#privacy_policy").css({ "display":"inline", "float":"left" });
					$("#terms_conditions").css({ "display":"none" });
					currentPage = "policy";
				}else{
					// if the container is closed.
					$("#privacy_policy").css({ "display":"inline", "float":"left" });
					$("#terms_conditions").css({ "display":"none" });
					currentPage = "policy";
					$("#content_container").slideDown("slow", function(){ 
						//$.localScroll({ "target": "#bottom"});
						$("html,body").animate({ scrollTop: $("#content_container").offset().top });
					});
				}
			}
		break;
	/*
		Opening and closing the content div if the
		'terms' button is clicked.
	*/
		case "terms":
			if(currentPage == content){
				$("#content_container").hide();
				$("#privacy_policy").css({ "display":"none" });
				$("#terms_conditions").css({ "display":"none" });
				contentOpen = false;
				currentPage = "";
			}else{
				if(contentOpen == true){
					// if the container is open
					$("#privacy_policy").css({ "display":"none" });
					$("#terms_conditions").css({ "display":"inline", "float":"left" });
					currentPage = "terms";
				}else{
					// if the container is closed.
					$("#privacy_policy").css({ "display":"none" });
					$("#terms_conditions").css({ "display":"inline", "float":"left" });
					currentPage = "terms";
					$("#content_container").slideDown("slow", function(){
						$("html,body").animate({ scrollTop: $("#content_container").offset().top });
					});
				}
			}
		break;
	}
}

mapVisible = false; // set the var that represents map visibility as false at default.

function openMap(searchValue, printed){
/*
	This function cannot be called on the document ready function because
	it is being called from flash and parsed the postcode.
	
	use the var 'browser' to check that the user is not IE then 
	fade in #map_container or set its css to block.
*/  
	if(printed == true){
		mapVisible = false;
	}

	if(checkPostCode(searchValue) != false){
		pcode = checkPostCode(searchValue); // call the check-postcode function and get a valid postcode format from it, 
		setTimeout("initSearch(pcode)", 1000); // delay the search so the animations have time to complete.
	}else{
	/*
		If the search value is no good:
		Send the map to a default position (overlooking the UK).
		and display and error message.
	*/
		pcode = false;
		mapviewer.goToPosition( new MMLocation( new MMLatLon( 55, -4 ) ,6 ) );
		mapviewer.removeAllOverlays();
		$("#recordListDiv").html("<div class=\"error_container\"><h1>Invalid search criteria</h1><p>Please ensure that you have entered a valid postcode.</p></div>");
	}

/*
	If the map is not visible (mapVisible is set to false), check against the browser.
*/
	if(mapVisible == false){
		if(pcode != false){
			if(navigator.appName != "Microsoft Internet Explorer"){
				$("#map_container").fadeIn("slow");
				//setTimeout("show()", 1500);
				mapVisible = true;
			}else{
				//initSearch(pcode);
				$("#map_container").css({ "display" : "block" });  
				//setTimeout("showIE()", 1500);  
				mapVisible = true;
			}
		}else{
			alert("Invalid search criteria. Please enter a valid postcode.");
		}
	}else{
		//initSearch(pcode);
	}
}

function closeMap(){
/*
	This function cannot be placed inside the document ready function because
	it needs scope of var: mapVisible, which also had to be set outside the doc ready
	function.
	
	Hide the map container.
*/
	if(mapVisible == true){
		if(navigator.appName != "Microsoft Internet Explorer"){
			$(document).ready(function(){
				$("#map_container").fadeOut("slow");
				mapVisible = false;
			});
		}else{
			$(document).ready(function(){
				$("#map_container").css({ "display" : "none" });
				mapVisible = false;
			});
		}
	}
}

function show(){ $("#map_container").fadeIn("slow"); }
function showIE(){ $("#map_container").css({ "display" : "block" }); }