function bind_clicked_house()
{
	$(".house").click(function()
    {
		clicked_house( $(this) );
    });
}

function clicked_house( self )
{
    var houseDetails = self.attr("id").split("-");

	//Update page title
	var temp = self.children("span").text();
	document.title = "Great Living Homes | Viewing " + temp.split(":")[0];
	
    //get the house id
    var house_id = houseDetails[1];
    show_house_by_id( house_id );
}

function show_house_by_id( house_id, effect )
{
	//log house hit
	page_hits.house_hit(house_id);
	
    var url = base_url + "home/show_house_by_id/" + house_id,
			  data = {};

    //fade old div out
    $("#content-top").slideUp(800, function()
    {
        //load new data
        setTimeout(function()
        {
            $("#content-top").load( url, data, function()
            {

				//fade the new data in
	            $("#content-top").slideDown(900);


            });
        }, 400);

    });
	
    window.scrollTo(0,0);//scroll to top
    return false;		
}

function filter_houses( url, self )
{
    	reset_search();
    	
        //reset colour of other tabs
        $(".seriesTabs").children("li").removeClass("yellowtab");

        //colour this tab
        self.parent("li").addClass("yellowtab");

        $("#houses").fadeOut();

        var data = new Array();
        setTimeout(function()
        {
            $("#houses").load( url, data, function(str)
            {
                $("#houses").fadeIn();
                bind_clicked_house();

            });
        },500);//end set timeout	
}

$(document).ready(function()
{

	//Shows the larger version of the thumb images for each elevation
    $(".imgswap").live("click", function()
    {
		var house_id = $(this).children("img").attr("id").replace("id-", ""),
						url = base_url + "home/show_house_by_id/" + house_id,
						data = {};
		
		//log house hit when they switch between elevations
		page_hits.house_hit(house_id);

        $.post(url, data, function(data)
        {
            $("#content-top").html(data);

        }, "text");//text is the response from the server

        return false;
    });

	//bind click event to houses
	bind_clicked_house();

    //attach this method to our filter tabs
    $("a.filter_houses").click( function()
    {
        var temp = $(this).attr("id").split("-");
        var id = temp[1];
        var url = base_url + "home/filterByHouseSeries/" + id;

		filter_houses( url, $(this) );

        return false;
    });

	$("a.promo-homes").click(function()
	{
		var url = base_url + "home/get_promo_homes";
		filter_houses( url, $(this) );
		return false;
		
	});

    //attach this method to our view all method
    $("a#view_all").click(function()
    {
        var url = base_url + "home/viewAll";
		filter_houses( url, $(this) );
        return false;
    });
    
});





