$(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() + '&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);
					}
				});

				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);
					}
				});

				return false;
			}
			return false;
		});
	}


	// full pressroom pdf
	$('.pressroom_pdf').click(function(){
		$(".pressroom_pdf").colorbox({width:"80%", height:"80%", iframe:true});
	});

	// property images slideshow
	$('.slideshow').click(function(){
		var class_name = $(this).attr('rel');
		$("a[rel='"+ class_name +"']").colorbox({slideshow:true});
	});

	// property floorplans
	$('a.floorplan').click(function(){
		var class_name = $(this).attr('rel');
		$("a[rel='"+ class_name +"']").colorbox({slideshow:true, width:"80%"});
	});

	// property location map
	$('a.map').click(function(){
		//$("a.map").colorbox({width:"80%", height:"80%", iframe:true});
		var x = $(this).attr('rel');
		window.open("http://maps.google.com/maps?q=" + x);
	});

	// contact agent
	$('a.contact_agent').click(function(){
		$("a.contact_agent").colorbox({width:"450", height:"460px", iframe:true});
	});

	// property EPC
	$('a.epc').click(function(){
		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:"720px", height:"520px", iframe:true});
	});
});

function slideshow(x)
{
	$("a[rel='"+ x +"']").colorbox({slideshow:true});
}

/** contact us **/
function dispalyContactType(type) {
	document.getElementById('general').style.display = 'none';
	document.getElementById('requirement').style.display = 'none';
	document.getElementById('valuation').style.display = 'none';
	document.getElementById(type).style.display = 'block';
}

/** area map **/
function show_map() {
	$('#map_mask').css('display', 'block');
	$('#map_overlay_wrapper').css('display', 'block');
}

// VARIABLES
var pcodes_buf = '';

var pc_sels = new Array();
var pc_styles = new Array();

function ret_postcodes(target_id) {
	if(!target_id) {
		target_id = '#location';
	} else {
		target_id = '#' + target_id;
	}

	$(target_id).val(pcodes_buf);
	$('#map_mask').css('display', 'none');
	$('#map_overlay_wrapper').css('display', 'none');

	return false;
}

function poc(id) {
	if(pc_sels[id]) {
		pc_sels[id] = 0;
		pou(id);
	}
	else {
		pc_sels[id] = 1;
		pov(id);
	}

	buf = '';
	for(var code in pc_sels) {
		if(pc_sels[code]==1) {
			buf += code+', ';
		}
	}

	buf = buf.slice(0, -2);

	set_pcodes_htm(buf);
}

function pov(id) {
	if(!pc_styles[id]) {
		pc_styles[id] = document.getElementById(id);
	}
	pc_styles[id].style.visibility = 'visible';
}

function pou(id) {
	if(!pc_styles[id]) {
		pc_styles[id] = document.getElementById(id);
	}
	pc_styles[id].style.visibility = pc_sels[id]?'visible':'hidden';
}

function set_pcodes_htm(buf) {
	pcodes_buf = buf;
}
/** End of area map **/



/** Home page slideshow **/
var images = new Array(
'/themes/www.birchletting.com/web/images/homepage1.jpg',
'/themes/www.birchletting.com/web/images/homepage2.jpg',
'/themes/www.birchletting.com/web/images/homepage3.jpg',
'/themes/www.birchletting.com/web/images/homepage4.jpg');

var nextImage 		= 1;
var imageWidth 		= 470;
var imageHeight 	= 270;
var imageTimeout 	= 2000;

function setOpacity(el, opacity) {

	//  Which opacity method does this browser support?

	opacity /= 100;

	el.style.opacity = opacity;
	el.style.MozOpacity = opacity;
	el.style.filter = "alpha(opacity=" + (opacity*80) + ")";

}

function fadeImage(el, currentOpacity) {

	currentOpacity += 1;

	if (currentOpacity > 100) {
		setOpacity(el, 100);
		var prevEl = el.previousSibling ? el.previousSibling : el.parentNode.lastChild;
		prevEl.style.visibility = 'hidden';
		el.style.zIndex = 1;
		window.setTimeout(startFading, imageTimeout);
	}
	else {
		setOpacity(el, currentOpacity);
		window.setTimeout(function() { fadeImage(el, currentOpacity); }, 50);
	}
}

function startFading() {

	var el = document.getElementById('fading_image_container').childNodes[nextImage];

	el.style.visibility = 'visible';
	el.style.zIndex = 2;
	setOpacity(el, 0);
	fadeImage(el,0);

	nextImage = (nextImage < images.length-1) ? nextImage + 1 : 0;

}


function pageLoad() {

	if(document.getElementById('fading_image_container')) {
		var el = document.getElementById('fading_image_container');

		while (el.firstChild) { el.removeChild(el.firstChild); }

		el.style.width = imageWidth + 'px';
		el.style.height = imageHeight + 'px';

		for(var i=0; i<images.length; i++) {

			var t = document.createElement('IMG');
			t.setAttribute('src',images[i]);
			t.setAttribute('width',imageWidth);
			t.setAttribute('height',imageHeight);
			t.style.position = 'absolute';
			t.style.visibility = 'hidden';
			el.appendChild(t);

		}

		el.firstChild.style.visibility = 'visible';

		window.setTimeout(startFading, imageTimeout);
	}
}
window.onload = pageLoad;

function change(x)
{
	document.getElementById('main_image').src = x;
}

/** End of home page slideshow **/
