$(window).load(function(){
	$('.featured div img').each(function(){
		fh = $(this).height();
		fa = (fh-275)/2;
		if(fa<0) fa = fa.positive();
		else fa = '-'+fa;
		$(this).css({ position:'relative', top:fa+'px' });
	});
	
	if(jQuery.fn['slideshow'] == undefined) jQuery.fn['slideshow'] = function(){};
	$('.featured').slideshow();
});


$(document).ready(function(){
	
	$('.more-details-view').click(function(){
		a = ($(this).html() == "More Information") ? "Less Information" : "More Information";
		$(this).html(a);
		$('.more-details').slideToggle(500);
		return false;
	});
	

	$('.thumbnail').css({ 'cursor':'pointer' });
	
	$('.thumbnail').bind("click", function(){
		$('.image').css({ width: '495px', height: 'auto' }).attr("src", $(this).attr("src"));
	});

	$('.cssdropdown').addClass('dropdown');
   	$('.dropdown').removeClass('cssdropdown');
	$('.dropdown').html('<p><small>Your Interest List</small><big>0 Properties</big></p><div><p></p></div>');
	$('.dropdown div p').html("Loading..");
	$('.listing span').addClass("apply-interest");
	
	$(document).bind("click", function (event) {
    	if($(event.target).closest('.dropdown').length){
			if($('.dropdown').find("div > div").length == 0) return false;
        	$('.dropdown').addClass("dropdown-active");
			$('.dropdown').removeClass("dropdown");
		}else if($(event.target).closest('.dropdown-active').length < 1){
			$('.dropdown-active').addClass("dropdown");
			$('.dropdown-active').removeClass("dropdown-active");
			return true;
		};
    });
	
	
	$('.search').find('select').sSelect();
	
	$('.thedropdown').find('.delete').live("click", function(){
		id = $(this).parent().attr("id").replace(/property\-list\-/gi, "");
		property.remove(id);
	});
		
	$('.listing > div').find("span").bind("click", function(){
		property.add($(this).parent().attr("id"));
	});
	
	$('.add-property').live("click", function(){
		property.add($(this).attr("id"));
	});
	
	property = {
		load: function(id){
			id = id.replace(/property\-/gi, "");
			id = id.replace(/,$/gi, "");
			ids = id.split(",");
			$('.thedropdown div').attr("properties", id);
			if(id == ""){
				$('.thedropdown > div').html("");
				property.count(0);
				$.get("inc_interest_list_actions.asp?action=clear");
				return;
			}
			$.get("inc_interest_list_actions.asp?action=load&properties="+id, {}, function(data){

				properties = data.split("#");
				out = "";
				for(i=0;i<properties.length;i++){
					properties[i] = properties[i].replace(/^(\s)*/, '');
					properties[i] = properties[i].replace(/(\s)*$/, '');
					
					if(properties[i] == "")continue;
					properties[i] = properties[i].replace(/^(\s)*/, '');
					properties[i] = properties[i].replace(/(\s)*$/, '');
					properties[i] = properties[i].split(",");
					out += property.interest({ id:properties[i][0], name:properties[i][1], price:properties[i][2], image:properties[i][3] });

					property.added(properties[i][0]);
				}
				$('.thedropdown > div').html(out);
				property.count(properties.length-1);
			});
		},
		
		add: function(id){
			id = id.replace(/property\-/gi, "");
			start= new RegExp("^"+id);
			if(!start.test($('.thedropdown div').attr("properties")) && $('.thedropdown div').attr("properties").indexOf(","+id) < 0){
				p = $('.thedropdown div').attr("properties");
				if(p == "") property.load(id);
				else property.load($('.thedropdown div').attr("properties")+','+id);
			}
		},
		
		remove: function(id){
			id = id.replace(/property\-/gi, "");
			a = $('.thedropdown div').attr("properties");
			a = a.replace(","+id+",", ",");
			start= new RegExp("^"+id+",");
			a = a.replace(start, "");
			end = new RegExp(","+id+"$");
			a = a.replace(end, "");
			if(a == id) a = "";
			property.load(a);
			property.removed(id);
		},
		
		added: function(id){
			id = "#property-"+id;
			$(id).find("span").html("-");
			$(id).find("span").attr("title", "Remove from Interest List");
			$(id).find("span").unbind();
			$(id).find("span").bind("click", function(){
				property.remove($(this).parent().attr("id"));
			});
			
			$('.add-property'+id).html("- Remove from Interest List");
			$('.add-property'+id).unbind();
			$('.add-property'+id).bind("click", function(){
				property.remove($(this).attr("id"));
				return false;
			});
		},
		
		removed: function(id){
			id = "#property-"+id;
			$(id).find("span").html("+");
			$(id).find("span").attr("title", "Add to Interest List");
			$(id).find("span").unbind();
			$(id).find("span").bind("click", function(){
				property.add($(this).parent().attr("id"));
			});
			
			$('.add-property'+id).html("+ Add to Interest List");
			$('.add-property'+id).unbind();
			$('.add-property'+id).bind("click", function(){
				property.add($(this).attr("id"));
				return false;
			});
		},
		
		counter: $('.thedropdown > p > big'),
		
		count: function(value){
			$(property.counter).animate({ opacity:0.2 }, 200).
			animate({ opacity:1 }, 200);
			$(property.counter).html(value+' Properties');
		},
		
		view: 'property_detail.asp?PropertyID=',
	
		interest: function(info){

			out  = '<div id="property-list-'+info.id+'">';
			out += '<a href="'+property.view+info.id+'">';
			if(info.image)
			out += '<img src="/propertypics/'+info.image+'" alt="'+info.name+'" />';
			out += '<strong>'+info.name+'</strong>';
			out += '<small>£'+info.price+'</small>';	//info.price.substring(0, info.price.length-3)+','+info.price.substring(info.price.length-3, info.price.length)
			out += '</a>';
			out += '<span class="delete" title="Remove">X</span>';
			out += '</div>';

			return out;
		}
	}
	
	properties = $('.dropdown').attr("id");
	property.load(properties);
	$('.dropdown div p').remove();
});

ignore = function(){
	return false;
};