$(function()
{

	var category = "";
	
	$("#proplist li:last").addClass("last");
	
	// Hide the search entries initially                    
	$("li#prop-commercial .search2, li#prop-commercial .search3").hide();

	$("li#prop-commercial dt.search1 a").click(function()
	{

		$(this).addClass("on"). 								// Add on to me
		parent().parent().find("dd.search1 a").removeClass("on").			// Get rid of on the other
		parent().parent().find(".search2, .search3").show();	// Sliding the next search
		category = "sale";
		
		reload_search();
		
		return false;

    });               
	     
	$("li#prop-commercial dd.search1 a").click(function()
	{

		$(this).addClass("on"). 								// Add on to me
		parent().parent().find("dt.search1 a").removeClass("on").			// Get rid of on the other
		parent().parent().find(".search2, .search3").show();	// Sliding the next search
		category = "rent";

		reload_search();
		
		return false;

    });               
     
     				
	// Hide the lot 
	$("div#prop-search ul#proplist").hide();
	
	// Mouse over the property search button
	property_open = false;
	news_open = true;
	
	$("div#prop-search h3").click(function()
	{
	
		if(property_open)
			return;
		
		$(this).find("a").addClass("on");
		$("div#news h3 a").removeClass("on");
		
		$("div#prop-search ul#proplist").slideDown();
		$("div#news ul#news-items").slideUp();		

		property_open = true;
		news_open = false;
		
		return false;
		
	});
	
	// Mouse over news
	$("div#news h3").click(function()
	{

		if(news_open)
			return;
			
		$(this).find("a").addClass("on");
		$("div#prop-search h3 a").removeClass("on");
		
		$("div#prop-search ul#proplist").slideUp();
		$("div#news ul#news-items").slideDown();		

		news_open = true;
		property_open = false;
		
		return false;
		
	});
   
   
	// Lightboxin
	$('a[@rel*=lightbox]').lightBox();
	
	// Searching
	function reload_search()
	{
	
		var type = $("select#type").val();
		var location = $("select#location").val();
		var area = $("select#area").val();

		result_count = 0;
				
		$("div#content").load("search/?category="+category+"&type="+type+"&location="+location+"&area="+area, "", function()
			{
				document.title = "Office space, Industrial Units and Residential Property Search Results";
				more_results_page = "search";
				result_click_handle();
				do_blank_page_links();
			});
	
	}
	
	$("select#type, select#location, select#area").change(function(){
		reload_search();
	});
	
	// Team page
	var open_team = null;
	var sliding = false;
	
	$("body.team div#content li a").mouseover(function(){
			
		var name = $(this).attr("id");

		if(open_team == name || sliding == true)
			return;

		$(this).find("img").attr("src", "media/images/team/"+name+"_select.gif");
		
		if(open_team == null)
		{
			$("body.team div#content div#"+name+"_info").slideDown("slow", function(){
					$('body.team div#content ul').scrollTo(1000);		
					sliding = false;			
			});
			sliding = true;	
			open_team = name;
		}
		else
		{
		
			$("body.team div#content a#"+open_team+" img").attr("src", "media/images/team/"+open_team+"_deselect.gif");
			
			$("body.team div#content div#"+open_team+"_info").hide();
			open_team = name;
			$("body.team div#content div#"+name+"_info").show();
			
		}
		
	}).click(function(){ return false; });
	
	$("body.team div#content div.info").hide();
	
	// Development more results
	var result_count = 0;
	var more_results_page = document.body.className;
	
	function result_click_handle()
	{
	
		$("a[rel=more_results]").click(function(){
		
			result_count += 5;

			if(more_results_page == "search")
			{
			
				var type = $("select#type").val();
				var location = $("select#location").val();
				var area = $("select#area").val();
		
				var url = "search/?category="+category+"&type="+type+"&location="+location+"&area="+area+"&count="+result_count;
			
			}
			else
				var url = more_results_page+"/"+result_count;
				
			$.get(url, function(data){
				$("a[rel=more_results]").before(data).remove();
				result_click_handle();
			});
			
			return false;
						
		});

	}
	
	result_click_handle();
	
	// New links in a new window
	function do_blank_page_links()
	{
		$("a[rel=external]").each(function(){
			this.target = "_blank";
		});
	}
	
	do_blank_page_links();
		
});

	


jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = $(this).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});
