(function($) {
jQuery.fn.localized = function() {
	var $input = $(this);
	$input.attr('value', $input.attr('title')).focus(function () {
		if ($input.attr('value') == $input.attr('title')) $input.attr('value', '');             
		})
		.blur(function () {
		if ($input.attr('value') == '') $input.attr('value', $input.attr('title'));
		}); 
}

})(jQuery);

(function($) {
$(function () {

	$("input.searchInput[name=s]").attr("id","autocomplete").attr("title","unde?").localized();
	
	(function() {
	    var sent = false;
	
	    $("#search form").submit(function(e) {
		if (!sent) {
		    sent = true;
    	    	    var $input = $(this).find("input[name=s]");
   	    	    if ($input.val() == $input.attr("title"))
			$input.val(" ");
	
		    $(this).submit();
	        }
	    });
	})();
	
	$("div.result").mouseover(function(){
		$(this).addClass("focus");
	}).mouseout(function(){
		$(this).removeClass("focus");
	});
	
	$("input#autocomplete").autocomplete({
		source: ["Alexandru cel Bun", "Baza 3", "Bucium", "Bularga", "Canta", "Centru", "Copou", "CUG", "Dacia", "Dancu", "Frumoasa", "Galata", "Gara", "Metalurgie", "Mircea cel Batran", "Moara de Vant", "Nicolina", "P-ta Nicolina", "Pacurari", "Podu Rosu", "Podul de Fier", "Podul de Piatra", "Poitiers", "Sararie", "Semicentral", "Sfantul Lazar", "Tatarasi", "Tg Cucu", "Tudor Vladimirescu", "Zimbru", "Zona Industriala"]
	});
	
	if ($('ul.tabs').length) { $("#content").tabs(); }
	
	$("#paginareLista").pagination();
	
	$('#coin-slider').coinslider({ width: 490, height: 240, delay: 6000, effect: 'rain' });
	
	var refreshMyList = function() {
		$list = $("#sidebar ol");
		$list.fadeOut();
		$list.find("li").remove();
		$.get("/house_list.php", function(data) {
			$list.html(data).fadeIn();
			$list.find("a.removeFromList").click(removeFromList);
		});
	}
	
	var saveInList = function (e) {
	   e.stopPropagation();
	   e.preventDefault();
	   var $a = $(this);

	   var id = $(this).attr("rel").match(/\d+$/)[0];
	   $.post("/house_list.php", { action : "add", "house_id" : id }, function(response) {
			$a.text("Sterge din lista");
			$a.removeClass("saveInList").addClass("removeFromList");
			$a.unbind().click(removeFromList);
			refreshMyList();
	   });
	}

	var removeFromList = function (e) {
		e.stopPropagation();
		e.preventDefault();
		var $a = $(this);

		var $arel = $(this).attr("rel");
		var id = $arel.match(/\d+$/)[0];
		$.post("/house_list.php", { action : "remove", "house_id" : id }, function(response) {
			$("a.removeFromList[rel=" + $arel + "]").each(function() {
				$(this).text("Salveaza in lista")
					.removeClass("removeFromList")
					.addClass("saveInList")
					.unbind().click(saveInList);
				});
			refreshMyList();
			});
	}

	$("a.saveInList").click(saveInList);
	$("a.removeFromList").click(removeFromList);
	
	$("#sidebar ol li > a").each(function(){
		var $a = $(this);
		if (document.location.href == $a.attr("href"))
			$a.closest("li").addClass("selected");
	});
	
	$("#sidebar ol a.removeFromList").click(removeFromList);
	
	$("#dialog").dialog({ autoOpen: false });
	
	$("#newsLetter, #newsLetterF").click(function(){
		$("#dialog").dialog({modal: true});
		$("#dialog").dialog('open');
		return false;
	});
	
	$("#clickMe, .contactThis").click(function(){
		$("#contactMe").dialog({modal: true});
		$("#contactMe").dialog({dialogClass: 'contactOverlay'});
		$("#contactMe").dialog('open');
		return false;
	});
	
	$("#agentTop").click(function(){
		$("#agentVirtual").dialog({modal: true});
		$("#agentVirtual").dialog({dialogClass: 'agentOverlay'});
		$("#agentVirtual").dialog('open');
		return false;
	});

})

})(jQuery);
