
$(document).ready(function(){
	
	$("#team-menu").css({ 'display':'block' });
	$(".team").css({ 'display':'none' });
	
	$("#team-menu > a").click(function(){
		var id = $(this).attr("rel");
		$(".team").css({ 'display':'none' });
		$("#"+id).css({ 'display':'block' });
		$("body").animate({'scrollTop':'1000px'});
		
		for(var i=0; i<5; i++){
			if(id != $("#team-menu").find("a").eq(i).attr("rel")){
				img = $("#team-menu").find("a > img").eq(i).attr("src");
				img = img.replace("_select", "_deselect");
				$("#team-menu").find("a > img").eq(i).attr("src", img);
			}else{
				img = $("#team-menu").find("a > img").eq(i).attr("src");
				img = img.replace("_deselect", "_select");
				$("#team-menu").find("a > img").eq(i).attr("src", img);
			}
		}
		return false;
	});

	$("#search").wrap("<div id='search-box'></div");
	$("#search").css({ 'margin-bottom':'0px' });
	$("#search-box").css({ 'height':$("#search").css("height"), 'overflow':'hidden', 'margin-bottom':'10px' });
	$("#search-box").append("<img src='images/search-up.png' alt='Collapse' />");
	$("#search-box > img:last").css({ 'float':'right', 'margin-right':'10px', 'position':'relative', 'top':'-30px', 'cursor':'pointer' });
	
	$("#search-box").attr("state", $("#search").attr("class") || 'enabled');
	
	if($("#search-box").attr("state") != 'enabled'){
		$("#search-box").animate({ 'height':'36px', 'scrollTop':'89px' }, 1, function(){
			$("#search-box").css({ 'overflow':'hidden' });
			$("#search-box").find("div").eq(0).css({ 'background-image':'url(images/search-background-up.png)', 'background-position':'bottom'});
			$("#search-box").find("img:last").attr("src", "images/search-down-small.png");
		});
		$("#search-box").find("div").eq(0).children().slideUp(1);
		$("#search-box").attr("state", 'disabled');
	}
	
	$("#search-box > img:last").click(function(){
		if($("#search-box").attr("state") == 'enabled'){
			$("#search-box").animate({ 'height':'36px', 'scrollTop':'53px' }, 800, function(){
				$("#search-box").css({ 'overflow':'hidden' });
				$("#search-box").find("div").eq(0).css({ 'background-image':'url(images/search-background-up.png)', 'background-position':'bottom'});
				$("#search-box").find("img:last").attr("src", "images/search-down.png");
			});
			$("#search-box").find("div").eq(0).children().slideUp(1300);
			$("#search-box").attr("state", 'disabled');
		}else{
			$("#search-box").find("div").eq(0).css({ 'background-image':'url(images/search-down-small.png)', 'background-position':'top'});
			$("#search-box").animate({ 'height':'89px', 'scrollTop':'0px' }, 800, function(){
				$("#search-box").css({ 'overflow':'hidden' });
				$("#search-box").find("img:last").attr("src", "images/search-up.png");
			});
			$("#search-box").find("div").eq(0).children().slideDown(700);
			$("#search-box").attr("state", 'enabled');
		}
	});
	
	for(var i=0; i<$("#viewing-list > dd").size(); i++){
		var me = $("#viewing-list > dd > a").eq(i).attr('rel');
		$("#"+me+" > div > p > a:last").html("Remove from watch list");
		$("#"+me+" > div > p > a:last").click(function(){
			me = $(this).parent().parent().parent().attr("id");
			removeProperty(me);
			return false;
		});
	}

	$("#right > dl > dd > a > span").click(function(){
		me = $(this).parent().attr("rel");
		removeProperty(me);
		return false;
	});
	
	$("div.details > p").find("a:last").click(function(){
		me = $(this).parent().parent().parent().attr("id");
		if(me == ''){ me = $(".property").attr("id"); }
		addProperty(me);
		return false;
	});
});

addProperty = function(me){
	$(".empty-list").remove();
	thelink = $('#'+me).find(".details > p:last-child").find("a:last");
	
	$(thelink).html("Remove from watch list");
	name = $("#"+me+" > .details > h2 > a").html();
	if(name == 'null'){ name = $("#property-name").html(); };

	img = $("#"+me+" > .image > img").attr("src");

	$("#right > dl").append("<dd style='display:none'><a href='propertydetails.asp?PropertyID="+me+"' rel='"+me+"'><img src='"+img+"' alt='"+name+"' /><span>X</span>"+name+"</a></dd>");
	$("#right > dl > dd:last").slideDown("slow");
	$("#right > dl:last").find("a > span").click(function(){
		me = $(this).parent().attr("rel");
		removeProperty(me);
		return false;
	});
	$(thelink).unbind();
	$(thelink).click(function(){
		me = $(this).parent().parent().parent().attr("id");
		removeProperty(me);
		return false;
	});
	$.get("viewing-list.asp", { 'editWatchList':'add', 'id':me }, function(data){ }, "text");
}

removeProperty = function(me){
	thelink = $('#'+me).find(".details > p:last-child").find("a:last");
	
	$(thelink).html("Add to watch list");
	$("#right > dl").find("a[rel='"+me+"']").parent().slideUp("slow", function(){ $(this).remove(); });
	$("#"+me+" > div > p > a:last").unbind();
	$("#"+me+" > div > p > a:last").click(function(){
		me = $(this).parent().parent().parent().attr("id");
		addProperty(me);
		return false;
	});
	$.get("viewing-list.asp", { 'editWatchList':'remove', 'id':me }, function(data){  }, "text");
	if($("#viewing-list > dd").length == 1){

		$("#viewing-list").append("<dd class='empty-list'>Nothing here!</dd>");
	}
}

$(window).load(function(){
	$('#carousel').carousel({
		transition: 0.3,
		scrolling: true,
		menu:'carousel-menu'
	});
	$('a[rel="lightbox"]').lightBox();				   

});

