$(document).ready(function(){

	$('.error').hide();

	$(".submit").click(function() {
		$('.error').hide();

		var action = $("input#action").val();
		var id 	   = ($("input#id").val()) ? $("input#id").val() : '';
		var contact_type = $("input#contact_type").val();
		var success_message = '<p>We will be in touch shortly.</p>';


		if(action == 'email_friend')
		{
			var email = $("input#email").val();
			if (email == "") {
				$("label#email_error").show();
				$("input#email").focus();
				return false;
			}
			else if (!is_email_valid(email))
			{
				$("label#email_error").show();
				$("label#email_error").html('Please enter a valid email address');
				$("input#email").focus();
				return false;
			}

			var subject = $("input#subject").val();
			if (subject == "") {
				$("label#subject_error").show();
				$("input#subject").focus();
				return false;
			}

			if(contact_type == 'request_appraisal'){
				var message = $("textarea#postcode").val();
			} else {
				var message = $("textarea#message").val();
				if (message == "") {
					$("label#message_error").show();
					$("input#message").focus();
					return false;
				}
			}

			var data_string = 'email='+ email + '&subject=' + subject + '&message=' + message + '&id=' + id;
			var success_message = '<p>Your message has been sent successfully.<p>';
		}

		if(action == 'contact_agent'){

			var name = $("input#name").val();
			if (name == "") {
				$("label#name_error").show();
				$("input#name").focus();
				return false;
			}

			var contact_number = $("input#contact_number").val();
			if (contact_number == "") {
				$("label#contact_number_error").show();
				$("input#contact_number").focus();
				return false;
			}

			var email = $("input#email").val();
			if (email == "") {
				$("label#email_error").show();
				$("input#email").focus();
				return false;
			}
			else if (!is_email_valid(email))
			{
				$("label#email_error").show();
				$("label#email_error").html('Please enter a valid email address');
				$("input#email").focus();
				return false;
			}

			var message = ($("textarea#message").val()) ? $("textarea#message").val() : '';

			var data_string = 'name='+ name  + '&contact_number=' + contact_number + '&email=' + email + '&message=' + message + '&id=' + id;

			var address = ($("textarea#address").val()) ? $("textarea#address").val() : '';
			if(address != ''){
				data_string += '&address=' + address;
			}

			var postcode = ($("input#post_code").val()) ? $("input#post_code").val() : '';
			if(postcode != ''){
				data_string += '&postcode=' + postcode;
			}

			if(contact_type != ''){
				data_string += '&contact_type=' + contact_type;
			}
		}

		$.ajax({
			type: "POST",
			url: "/contact/" + action,
			data: data_string,
			success: function() {
				$('#'+action+'').html("<div id='reponse'></div>");
				$('#reponse').html("<h2>Thank you</h2>")
				.append(success_message)
				.hide()
				.fadeIn(1500);
			}
		});
		return false;
	});


	$('a.disable').click(function(){
        var delete_user = confirm('Do you really want to disable this user?');
        if(delete_user == true)
        {
            return true;
        } else {
            return false;
        }
 	});

	// See if featured proeprty id exists
	if($("#featured_property").length > 0)
	{
		$('ul#featured_property').innerfade({
			speed: 2000,
			timeout: 7000,
			type: 'sequence',
			containerheight: '390px'
		});
	}

	// full pressroom pdf
	$('.pressroom_pdf').click(function(){
		$(".pressroom_pdf").colorbox({width:"80%", height:"80%", iframe:true});
	});

	// property images slideshow
	 $('a.slideshow').click(function(){
	 	var class_name = $(this).attr('rel');
		$("a[rel='"+ class_name +"']").colorbox({slideshow:true});
	});

	// property location map
	$('a.map').click(function(){
		$("a.map").colorbox({width:"80%", height:"80%", iframe:true});
	});

	// contact agent
	$('a.contact_agent').click(function(){
		$("a.contact_agent").colorbox({width:"450", height:"460px", iframe:true});
	});

	// property floorplans
	 $('a.floorplan').click(function(){

	 	/**
		 * Floorplan could be in jpg or PDF format, if it's a PDF format then it will
		 * open it in iFrame otherwise it will not use iFrame.
		 */

	 	var is_PDF = $(this).attr('href').toString().search( new RegExp(/.pdf/));

		if(is_PDF > 0){
			$("a.floorplan").colorbox({width:"80%", height:"80%", iframe:true});
		} else {
			var class_name = $(this).attr('rel');
			$("a[rel='"+ class_name +"']").colorbox({width:"80%", slideshow:true});
		}
	});

	// property EPC
	$('a.epc').click(function(){

		/**
		 * EPC could be in jpg or PDF format, if it's a PDF format then it will
		 * open it in iFrame otherwise it will not use iFrame.
		 */

		var is_PDF = $(this).attr('href').toString().search( new RegExp(/.pdf/));

		if(is_PDF > 0){
			$("a.epc").colorbox({width:"80%", height:"80%", iframe:true});
		} else {
			var class_name = $(this).attr('rel');
			$("a[rel='"+ class_name +"']").colorbox({slideshow:true});
		}

	});

	// property brochure
	$('a.brochure').click(function(){
		$("a.brochure").colorbox({width:"80%", height:"80%", iframe:true});
	});

	// email a friend
	$('a.email').click(function(){
		$("a.email").colorbox({width:"600px", height:"450px", iframe:true});
	});

	// owner's insight
	$('a.owners_insight').click(function(){
		var owners_insight_id = $(this).attr('rel');
		$('a.owners_insight').colorbox({width:"50%", inline:true, href:"#" + owners_insight_id + ""});
	});

	// request appraisal
	$('a.request_appraisal').click(function(){
		$(".request_appraisal").colorbox({width:"450", height:"400px", iframe:true});
	});

	// bookmark stanely website
	if(window.opera)
	{
		if ($("a.bookmark").attr("rel") != "")
		{
			$("a.bookmark").attr("rel","sidebar");
		}
	}
	$("a.bookmark").click(function(event)
	{
		// prevent the anchor tag from sending the user off to the link
		event.preventDefault();
		if (window.sidebar)
		{ 	// Mozilla Firefox Bookmark
			window.sidebar.addPanel(this.title, this.href, "");
		}
		else if(window.external)
		{	// IE Favorite
			window.external.AddFavorite(this.href, this.title);
		}
		else if(window.opera)
		{
			return false;
		}
		else
		{
			alert('Unfortunately, this browser does not support the requested action,' + ' please bookmark this page manually.');
		}
	});

	// commented out google map due to displaying wronge address
	// till stanley chelsea resolve the matter with google
});

function brochure_status(id, active)
{
	$.ajax({
		type: "POST",
		url: "/admin/brochure/edit",
		data: 'id=' + id + '&active=' + active,
		success: function() {
			window.location.reload();
		}
	});
}

function  is_email_valid(email)
{
	var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return filter.test(email);
}


