// JavaScript Document

$(document).ready(function() {
	
	var browser = jQuery.browser.msie;
	var version = jQuery.browser.version;
	
	if(browser == true && version < 7) {
		//IE6 tracking gets done with the analytics in the analytics include
	} else {
		$.gaTracker('UA-3646834-1');
	};
	
	//Add "Full details" button to Buy of the Week panel on homepage
	$('.buy_of_the_week').append('<p class="botw_fd"><a href="#" onclick="viewFullDetails();"><img src="/stat/btn_full_details.png" width="107" height="31" alt="Full details" /></a></p>');

	//Activate show/hide on news articles
	$('.news_section ul').addClass("hidden");
	$('.news_section ul:first').toggleClass("hidden");
	$('.news_section h2').append('<span><a href="#" onclick="toggleNews(this);return false;">Toggle</a></span>');
	$('.news_section h2:first a').toggleClass("minus");

	//Activate show/hide on  articles
	$('.articles_holder ul').addClass("hidden");
	$('.articles_holder h3').addClass("hidden");
	//$('.articles ul:first').toggleClass("hidden");
	$('.articles_holder h2').append('<span><a href="#" onclick="toggleArticles(this);return false;">Toggle</a></span>');
	
	$('.office .office_info').addClass("hidden");
	$('.office h4').addClass("closed");
	$('.office h4').addClass("clickable");
	
	$('.office .office_info:first').toggleClass("hidden");
	$('.office h4:first').toggleClass("closed");
	
	$('.office h4').click(function() {
		$(this).parent().find('.office_info').toggleClass("hidden");
		$(this).toggleClass("closed");
	});
	
	$('#map').addClass("active");
	
 	$('.property_search p.intro').remove();
	$('.property_search h3').after('<div id="search_selector"><p class="looking_to">Im looking to</p><p><label><input type="radio" name="search_select" checked="checked" onclick="toggleSearch(\'buy\');" value="buy" /> Buy</label> <label><input type="radio" name="search_select" onclick="toggleSearch(\'let\');" value="let" /> Let</label></p>');
	$('.property_search h3').empty().append('Property search');
	
	if($('.property_search input:checked').val() === 'buy') {
		toggleSearch('buy');
	} else {
		toggleSearch('let');
	}
	
	
	if($('.looking_for input:checked').val() === 'Buy') {
		toggleRegister('Buy');
	} else {
		toggleRegister('Let');
	}
	$('.looking_for input').click(function() { 
   		var looking_for = $(this).val();		
		if(looking_for === 'Buy') {
			toggleRegister('Buy');
		} else {
			toggleRegister('Let');
		}
    });
	
	//Open stuff in new window
	$('a.new-window').click(function(){
		window.open(this.href);
		return false;
	});

	//$('#property_to_buy').attr("href",'/estate_agency/search_map');
	
});

var searchtype = 'buy';
var bedsOptions = [ ['0','Min bedrooms'], ['0','0'],['1','1'],['2','2'],['3','3'],['4','4'],['5','5+'] ];
var minPriceOptionsBuy = [ ['0','Min price'],['50000','50,000'],['75000','75,000'],['100000','100,000'],['125000','125,000'],['150000','150,000'],['175000','175,000'],['200000','200,000'],['225000','225,000'],['250000','250,000'],['275000','275,000'],['300000','300,000'],['325000','325,000'],['350000','350,000'],['375000','375,000'],['400000','400,000'],['450000','450,000'] ];
var maxPriceOptionsBuy = [ ['999999999','Max price'],['75000','75,000'],['100000','100,000'],['125000','125,000'],['150000','150,000'],['175000','175,000'],['200000','200,000'],['225000','225,000'],['250000','250,000'],['275000','275,000'],['300000','300,000'],['325000','325,000'],['350000','350,000'],['375000','375,000'],['400000','400,000'],['450000','450,000'],['999999999','500,000+'] ];
var minPriceOptionsLet = [ ['0','Min price'],['250','250 p.c.m.'],['300','300 p.c.m.'],['350','350 p.c.m.'],['400','400 p.c.m.'],['450','450 p.c.m.'],['500','500 p.c.m.'],['550','550 p.c.m.'],['600','600 p.c.m.'],['700','700 p.c.m.'],['800','800 p.c.m.'],['900','900 p.c.m.'] ];
var maxPriceOptionsLet = [ ['999999999','Max price'],['300','300 p.c.m.'],['350','350 p.c.m.'],['400','400 p.c.m.'],['450','450 p.c.m.'],['500','500 p.c.m.'],['550','550 p.c.m.'],['600','600 p.c.m.'],['700','700 p.c.m.'],['800','800 p.c.m.'],['900','900 p.c.m.'],['999999999','1,000 p.c.m.+'] ];

function toggleRegister(looking_for) {
	
	var minPrice = $('.register #minimum_price');
	minPrice.empty();

	var maxPrice = $('.register #maximum_price');
	maxPrice.empty();
	
	if(looking_for === 'Buy') {
		for(i=1;i<minPriceOptionsBuy.length;i++) {
			minPrice.append('<option value="' + minPriceOptionsBuy[i][1] + '">' + minPriceOptionsBuy[i][1] + '</option>');
		}
		for(i=1;i<maxPriceOptionsBuy.length;i++) {
			if(i == (maxPriceOptionsBuy.length - 1)) {
				maxPrice.append('<option value="' + maxPriceOptionsBuy[i][1] + '" selected="selected">' + maxPriceOptionsBuy[i][1] + '</option>');	
			} else {
				maxPrice.append('<option value="' + maxPriceOptionsBuy[i][1] + '">' + maxPriceOptionsBuy[i][1] + '</option>');					
			}
		}
	} else {
		for(i=1;i<minPriceOptionsLet.length;i++) {
			minPrice.append('<option value="' + minPriceOptionsLet[i][1] + '">' + minPriceOptionsLet[i][1] + '</option>');
		}
		for(i=1;i<maxPriceOptionsLet.length;i++) {
			if(i == (maxPriceOptionsLet.length - 1)) {
				maxPrice.append('<option value="' + maxPriceOptionsLet[i][1] + '" selected="selected">' + maxPriceOptionsLet[i][1] + '</option>');	
			} else {
				maxPrice.append('<option value="' + maxPriceOptionsLet[i][1] + '">' + maxPriceOptionsLet[i][1] + '</option>');					
			}
		}
	}
	
}


function toggleSearch(type) {

	searchtype = type;

	var bed = $('.property_search #Bedrooms2');
	bed.empty();
	for(i=0;i<bedsOptions.length;i++) {
		bed.append('<option value="' + bedsOptions[i][0] + '">' + bedsOptions[i][1] + '</option>');
	}
	
	var lop = $('.property_search #MinPrice2');
	lop.empty();
	
	var hip = $('.property_search #MaxPrice2');
	hip.empty();
	
	if(searchtype === 'buy') {
		//alert(searchtype);
		for(i=0;i<minPriceOptionsBuy.length;i++) {
			lop.append('<option value="' + minPriceOptionsBuy[i][0] + '">' + minPriceOptionsBuy[i][1] + '</option>');
		}
		for(i=0;i<maxPriceOptionsBuy.length;i++) {
			hip.append('<option value="' + maxPriceOptionsBuy[i][0] + '">' + maxPriceOptionsBuy[i][1] + '</option>');
		}
		//$('.property_search #fid').val('11462');
		$('.property_search #RentalPeriod2').val('0');
		$('.property_search p.advanced_search').empty().append('<a href="/estate_agency/property_to_buy">Advanced search</a>');
	}
	
	if(searchtype === 'let') {
		//alert(searchtype);
		for(i=0;i<minPriceOptionsLet.length;i++) {
			lop.append('<option value="' + minPriceOptionsLet[i][0] + '">' + minPriceOptionsLet[i][1] + '</option>');
		}
		for(i=0;i<maxPriceOptionsLet.length;i++) {
			hip.append('<option value="' + maxPriceOptionsLet[i][0] + '">' + maxPriceOptionsLet[i][1] + '</option>');
		}
		$('.property_search #RentalPeriod2').val('4');
		$('.property_search p.advanced_search').empty().append('<a href="/estate_agency/property_to_let">Advanced search</a>');
	}
	
}

function toggleNews(ref) {
	$(ref).parent().parent().parent().find('a').toggleClass("minus");
	$(ref).parent().parent().parent().find('ul').toggleClass("hidden");
	return false;
}

function toggleArticles(ref) {
	$(ref).parent().parent().parent().find('a').toggleClass("minus");
	$(ref).parent().parent().parent().find('ul').toggleClass("hidden");
	$(ref).parent().parent().parent().find('h3').toggleClass("hidden");
	return false;
}

function viewFullDetails() {
	var full_details =  $('#featureprops a').attr('href');
	if(full_details != undefined) {
		window.location = full_details;		
	}
	return false;
};