
$(document).ready(function(){

	// contact us
	if($('#contact_us').length){
		$("#contact_us").validate();
		$("#submit").click(function() {

			if($("#contact_us").valid())
			{
				var data_string = $("#contact_us").serialize();

				$.ajax({
					type: "POST",
					url: "/contact/contact_agent",
					data: data_string,
					success: function() {
						$('#contact_us').html("<div id='reponse'></div>");
						$('#reponse').html("<h2>Thank you</h2>")
						.append("<p>We will be in touch shortly.</p>")
						.hide()
						.fadeIn(1500);
					}
				});

				return false;
			}
			return false;
		});
	}

	// contact agent regarding a property from full details page
	if($('#register').length){
		$("#register").validate();
		$("#submit").click(function() 
		{
			if($("#register").valid())
			{
				var data_string = $("#register").serialize();

				if($("input#firstname").val() || $("input#lastname").val())
				{
				    data_string + '&name=' + $("input#firstname").val() + ' ' +  $("input#lastname").val();
				}

				$.ajax({
					type: "POST",
					url: "/contact/contact_agent",
					data: data_string,
					success: function() {
						$('#register').html("<div id='reponse'></div>");
						$('#reponse').html("<h2>Thank you</h2>")
						.append("<p>We will be in touch shortly.</p>")
						.hide()
						.fadeIn(1500);
					},
					failure: function() {
                       $('#email_friend').html("<div id='reponse'></div>");
					   $('#reponse').html("<h2>Sorry, could not send your email, please try agains</h2>");
					}
				});

				return false;
			}
			return false;
		});
	}

	//email a friend
	if($('#email_friend').length){
		$("#email_friend").validate();
		$("#submit").click(function() {

			if($("#email_friend").valid())
			{
				var data_string = $("#email_friend").serialize();

				$.ajax({
					type: "POST",
					url: "/contact/email_friend",
					data: data_string,
					success: function() {
						$('#email_friend').html("<div id='reponse'></div>");
						$('#reponse').html("<h2>Thank you</h2>")
						.append("<p>Your message has been sent successfully.</p>")
						.hide()
						.fadeIn(1500);
					},
					failure: function() {
                       $('#email_friend').html("<div id='reponse'></div>");
					   $('#reponse').html("<h2>Sorry, could not send your email, please try agains</h2>");
					}
				});

				return false;
			}
			return false;
		});
	}
	
	// Valuation 
	if($('#valuation').length){
		$("#valuation").validate();
		$("#submit").click(function() {

			if($("#valuation").valid())
			{
				var data_string = $("#valuation").serialize();

				$.ajax({
					type: "POST",
					url: "/valuation/index",
					data: data_string,
					success: function() {
						$('#valuation').html("<div id='reponse'></div>");
						$('#reponse').html("<h2>Thank you</h2>")
						.append("<p>We will be in touch shortly.</p>")
						.hide()
						.fadeIn(1500);
					}
				});

				return false;
			}
			return false;
		});
	}

	// freetext default value
	if($('.freetext').length)
	{
		var default_value = 'Street / Postcode';
		$('.freetext').focus(function() {
			$(this).css({'color':'#000'});
			if(this.value == default_value) {
				this.value = '';
			}
		});

		$('.freetext').blur(function() {
			if(this.value == '') {
				$(this).css({'color':'#666'});
				this.value = default_value;
			}
		});
	}

	// See if featured proeprty id exists
	if($("#featured_property").length > 0)
	{
		$('ul#featured_property').innerfade({
			speed: 2000,
			timeout: 7000,
			type: 'sequence',
			containerheight: '390px'
		});
	}
	$('.telephone_number').click(function(){
		$(".telephone_number").colorbox({
		    width:"50%",
		    inline:true,
		    href:"#telephone_number"
		});
	});

	// full pressroom pdf
	$('.pressroom_pdf').click(function(){
		$(".pressroom_pdf").colorbox({
		    width:"80%",
		    height:"80%",
		    iframe:true
		});
	});

	// property images slideshow
	$('a.photo').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(){
		var class_name = $(this).attr('rel');

		$("a[rel='"+ class_name +"']").colorbox({
		    slideshow:false,
		    height:"100%",
		    maxWidth: "100%"
		});
	});

	$('#cboxTitle').click(function(){

	   $("#cboxPhoto").printElement({
            printMode:'popup',
            printBodyOptions:
            {
                styleToAdd:'padding:10px;margin:10px; !important;',
                classNameToAdd: 'floorplan_print_width'
            }
       });
	});

	// property EPC
	$('a.epc').click(function(){
		var class_name = $(this).attr('rel');
		$("a[rel='"+ class_name +"']").colorbox();
	});

	// property brochure
	$('a.brochure').click(function(){
		window.open($(this).attr('href'));
        return false;
	});

	// office map
	$('a.map_enlarge').click(function(){
		$("a.map_enlarge").colorbox({
		    width:"80%",
		    height:"80%",
		    iframe:true
		});
	});

	// email a friend
	$('a.email').click(function(){
		$("a.email").colorbox({
		    width:"600px",
		    height:"500px",
		    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
		});
	});

	// register interest from full property details
	$('a.register_interest').click(function(){
		$(".register_interest").colorbox({
		    width:"600px",
		    height:"520px",
		    iframe:true
		});
	});


	$('li.photo').mouseover(function() {
		$(this).find('span').css({'visibility': 'visible'});
	});

	$('li.photo').mouseleave(function() {
		$('li span').css({'visibility' : 'hidden'});
	});

	// back to searchs result from full property details
	$('.back_to_search').click(function(){
		history.go(-1);
	});

	if($.colorbox !== undefined){
		$('.lettings_popup').colorbox({
			width: '50%',
			inline: true,
			href: '#letting_your_property'
		});
	}

  // sales and letting search formssvn st

  //hide all content
	$(".tab_content").hide();

	$('a.properties_for_sale').click(function(){ 
		$.cookie("trans_type_id", 1, {path: '/'});
	});
	
	
	$('#menu ul li a.sales, ul.homepage_img_nav li a.sales, li a.sales, a.properties_for_sale').click(function(){ 
		$.cookie("trans_type_id", 1, {path: '/'});
	});

	$('#menu ul li a.lettings, ul.homepage_img_nav li a.lettings, li a.lettings, a.properties_to_let').click(function(){ 
		$.cookie("trans_type_id", 2, {path: '/'});
	});

	// get transaction type id from cookie
	var trans_type_id;
	if($.cookie !== undefined){
		trans_type_id = $.cookie("trans_type_id");
	} else {
		trans_type_id = 0;
	}

	if(trans_type_id == 1)
	{
		// activate first tab (sales)
		$("ul.tabs li:first").addClass("active").show();

		// show first tab content (sales form)
		$(".tab_content:first").show();
	}
	else if(trans_type_id == 2)
	{
		// activate second tab (lettings)
		$("ul.tabs li:eq(1)").addClass("active").show();

		// show second tab content (lettings form)
		$(".tab_content:eq(1)").show();
	}
	else
	{
		// activate first tab (sales)
		$("ul.tabs li:first").addClass("active").show();

		$(".tab_content:first").show();
	}

	$("ul.tabs li").click(function() {

		// set transaction type id when you switch between sales and lettings tabs
		var trans_type_id = $(this).index() + 1;
		$.cookie("trans_type_id", trans_type_id, {path: '/'});

		// remove any "active" class
		$("ul.tabs li").removeClass("active");

		// add "active" class to selected tab
		$(this).addClass("active");

		// hide all tab content
		$(".tab_content").hide();

		// find the rel attribute value to identify the active tab + content
		var activeTab = $(this).find("a").attr("href");

		// fade in the active content
		$(activeTab).fadeIn();

		return false;
	});
	
	
	$(".map-tab_content").hide();
	$("ul.map-tabs li:first").addClass("active").show();
	$(".map-tab_content:first").show();
	$("ul.map-tabs li").click(function() {

		// remove any "active" class
		$("ul.map-tabs li").removeClass("active");

		// add "active" class to selected tab
		$(this).addClass("active");

		// hide all tab content
		$(".map-tab_content").hide();

		// find the rel attribute value to identify the active tab + content
		var activeTab = $(this).find("a").attr("href");

		// fade in the active content
		$(activeTab).fadeIn();

		return false;
	});

	// slide down and up property summary and features
	$('#more').toggle(
		function() {
			$('p.description').hide();
			$('#more_details').slideDown('slow');
			$(this).html('- Fewer Details');
		},
		function() {
			$('#more_details').slideUp('slow');
			$(this).html('+ More Details');
			$('p.description').show();
		}
	);

	// home page home_page_images animation
	if($('.home_page_images').length)
	{
	/*
	    $('.home_page_images').animate({opacity: 0.5}, 1000 );
		$('.home_page_images').slideDown(3000, 'easeOutBounce', function(){});
		$('.home_page_images').animate({opacity: 1}, 3000 );
    */

		$('.home_page_images').children().hover(function() {
			$(this).siblings().stop().fadeTo(500, 0.5);
		}, function() {
			$(this).siblings().stop().fadeTo(500, 1);
		});
	}
});



