﻿Type.registerNamespace('casapadova.widgets.searchengine');

casapadova.widgets.searchengine.simpleSearchUrl = "/Search/Simple";

casapadova.widgets.searchengine.advancedSearchUrl = "/Search/Advanced";

casapadova.widgets.searchengine.originalHtml = null;

casapadova.widgets.searchengine.showEffect = {
	effect: "slide",
	options: { direction: "down" },
	speed: 250
};

casapadova.widgets.searchengine.hideEffect = {
	effect: "slide",
	options: { direction: "up" },
	speed: 250
};

casapadova.widgets.searchengine.getLocationIds = function (type) {
	var ids = [];
	var i = 0;
	$("#" + type + "LocationCheckboxes input[type='checkbox']:checked").each(function () {
		ids[i++] = $(this).val();
	});
	return ids;
}

casapadova.widgets.searchengine.getRealEstateTypeIds = function (type) {
	var ids = [];
	var i = 0;
	$("#" + type + "TypeCheckboxes input[type='checkbox']:checked").each(function () {
		ids[i++] = $(this).val();
	});
	return ids;
}

casapadova.widgets.searchengine.getSearchCount = function (type, zoneSelector) {
	if (casapadova.widgets.searchengine.originalHtml == null)
		casapadova.widgets.searchengine.originalHtml = $("#hasbeenfound").html();
	/*var realEstateTypeSelector = document.getElementById(type + "RealEstateTypes");
	var locationIdSelector = document.getElementById(type + "Locations");
	realEstateType = realEstateTypeSelector.options[realEstateTypeSelector.selectedIndex].value;
	locationId = locationIdSelector.options[locationIdSelector.selectedIndex].value;*/
	realEstateType = casapadova.widgets.searchengine.getRealEstateTypeIds(type);
	locationId = casapadova.widgets.searchengine.getLocationIds(type);
	isForSale = (type == "Sale");
	/*if (realEstateType == "") {
		realEstateType = -1;
	}
	if (locationId == "") {
		locationId = -1;
	}*/
	var refillZones = false;
	if (zoneSelector && zoneSelector.selectedIndex > 0)
		zone = zoneSelector.options[zoneSelector.selectedIndex].value || zoneSelector.options[zoneSelector.selectedIndex].text;
	else {
		refillZones = true;
		zoneSelector = document.getElementById(type + "LocationZone");
		zone = "";
	}
	if (realEstateType.length > 0 || locationId.length > 0 || zone != "") {
		if ($('#hasbeenfound').is(':visible'))
			$("#hasbeenfound").hide(
            casapadova.widgets.searchengine.hideEffect.effect,
            casapadova.widgets.searchengine.hideEffect.options,
            casapadova.widgets.searchengine.showEffect.speed);
		casapadova.common.toggleLoader("show");
		var proxy = new ajax.casapadova.it.AjaxRealEstateService();
		proxy.GetSearchCount(locationId, realEstateType, zone, isForSale, !isForSale,
        function (successResult) {
        	var proxy = new ajax.casapadova.it.AjaxResourceService();
        	proxy.GetResourceString("Widgets.SearchEngine, HasBeenFound", "~/", [successResult.Count], function (result) {
        		$("#hasbeenfound").html(result);
        		$("#hasbeenfound").show(
                casapadova.widgets.searchengine.showEffect.effect,
                casapadova.widgets.searchengine.showEffect.options,
                casapadova.widgets.searchengine.showEffect.speed);
        	}, casapadova.common.showError);
        	if (refillZones) casapadova.widgets.searchengine.fillZones(zoneSelector, successResult.Zones);
        	casapadova.common.toggleLoader("hide");
        }, casapadova.common.showError);
	} else {
		casapadova.widgets.searchengine.resetPayoff();
	}
};

casapadova.widgets.searchengine.fillZones = function (selector, elements) {
	var nullOption = selector.options[0];
	selector.options.length = 0;
	selector.options.add(nullOption);
	$(elements).each(function (index, elem) {
		selector.options.add(new Option(elem));
	});
};

casapadova.widgets.searchengine.toggleAdvanced = function (type) {
	$("#advancedSearch" + type).toggle("blind", {}, 500, function () {
		if ($("#advancedSearch" + type + ":visible").length > 0) {
			$("#" + type + "Form").attr("action", casapadova.widgets.searchengine.advancedSearchUrl);
		}
		else {
			$("#" + type + "Form").attr("action", casapadova.widgets.searchengine.simpleSearchUrl);
		}
	});
	$("#SimpleCommand" + type).toggle();
	$("#AdvancedCommand" + type).toggle();
	return false;
}


casapadova.widgets.searchengine.setFromTo = function (type) {
	var from = parseInt($("select#" + type + "From").val());
	var to = parseInt($("select#" + type + "To").val());

	if (from > 0 && (to > 0 && to <= from)) {
		$("select#" + type + "To option").each(function (i, elem) {
			if (elem.value > from) {
				$(elem).attr("selected", "selected");
				return false;
			}
		});
	}
}

casapadova.widgets.searchengine.resetPayoff = function () {
	if (casapadova.widgets.searchengine.originalHtml == null)
		casapadova.widgets.searchengine.originalHtml = $("#hasbeenfound").html();
	$("#hasbeenfound").hide(
            casapadova.widgets.searchengine.hideEffect.effect,
            casapadova.widgets.searchengine.hideEffect.options,
            casapadova.widgets.searchengine.showEffect.speed, function () {
            	$("#hasbeenfound").html(casapadova.widgets.searchengine.originalHtml);
            	$("#hasbeenfound").show(
                            casapadova.widgets.searchengine.showEffect.effect,
                            casapadova.widgets.searchengine.showEffect.options,
                            casapadova.widgets.searchengine.showEffect.speed);
            });
}


casapadova.widgets.searchengine.showSale = function () {
	casapadova.widgets.searchengine.resetPayoff();
	var imgtabvendita = $("#imgtabvendita")
	var imgtabaffitto = $("#imgtabaffitto");
	var tmpDivAffi = $("#divricercaaffitto");
	var tmpDivVend = $("#divricercavendita");

	imgtabvendita.attr("src", "/Content/Images/tabvendita_on.gif");
	imgtabaffitto.attr("src", "/Content/Images/tabaffitto_off.gif");
	tmpDivAffi.fadeOut("fast", function () {
		tmpDivVend.fadeIn("fast", function () {
			if ($.browser.msie)
				this.style.removeAttribute('filter');
		});
	});
};

casapadova.widgets.searchengine.showRent = function () {
	casapadova.widgets.searchengine.resetPayoff();
	var imgtabvendita = $("#imgtabvendita")
	var imgtabaffitto = $("#imgtabaffitto");
	var tmpDivAffi = $("#divricercaaffitto");
	var tmpDivVend = $("#divricercavendita");

	imgtabvendita.attr("src", "/Content/Images/tabvendita_off.gif");
	imgtabaffitto.attr("src", "/Content/Images/tabaffitto_on.gif");
	tmpDivVend.fadeOut("fast", function () {
		tmpDivAffi.fadeIn("fast", function () {
			if ($.browser.msie)
				this.style.removeAttribute('filter');
		});
	});
};

casapadova.widgets.searchengine.toggleLocationBox = function (type) {
	//$("#" + type + "LocationCheckboxes").toggle("slide", { direction: "up" }, 150);
	casapadova.widgets.searchengine.toggleSelectBox(type, "Location");
}

casapadova.widgets.searchengine.toggleTypeBox = function (type) {
	//$("#" + type + "TypeCheckboxes").toggle("slide", { direction: "up" }, 150);
	casapadova.widgets.searchengine.toggleSelectBox(type, "Type");
}

casapadova.widgets.searchengine.locationChecked = function (type, check, emptyValue) {
	casapadova.widgets.searchengine.processChecked(type, "Location", check, emptyValue);
}

casapadova.widgets.searchengine.typeChecked = function (type, check, emptyValue) {
	casapadova.widgets.searchengine.processChecked(type, "Type", check, emptyValue);
}


casapadova.widgets.searchengine.toggleSelectBox = function (type, what) {
	$("#" + type + what + "Checkboxes").toggle("slide", { direction: "up" }, 150);
}

casapadova.widgets.searchengine.processChecked = function (type, what, check, emptyValue) {
	var title = "";

	$("#" + type + what + "Checkboxes input[type='checkbox']:checked").each(function () {
		if (title != "") title += ", ";
		title += $("#" + type + what + "Checkboxes label[for='" + this.id + "']").html();
		//alert(this.id);
	});
	$("#" + what + what + "Title").attr("title", title)

	casapadova.widgets.searchengine.adjustTitle(type, what, title, emptyValue);

	if (type != null && type != "")
		casapadova.widgets.searchengine.getSearchCount(type);
}

casapadova.widgets.searchengine.adjustTitle = function (type, what, title, emptyValue) {
	var ellispsis = "&hellip;";
	var fontSize = 12;
	var minfontSize = 8;
	if (title == "") title = emptyValue;

	$("#" + type + what + "Title").html(title);
	var t = $("#" + type + what + "Title").clone();

	t.css({
		'position': 'absolute',
		'width': 'auto',
		'overflow': 'visible',
		'top': '0',
		'left': '-10000px',
		'font-size': fontSize + "px"
	});

	$("body").append(t);

	var oWidth = $("#" + type + what + "Title").width();

	while (t.width() > oWidth && fontSize > minfontSize) {
		t.css("font-size", --fontSize);
	}

	var text = t.html();

	while (t.width() > oWidth && text.length > 0) {
		text = text.substring(0, text.length - 1);
		t.html(text + ellispsis);
	}

	$("#" + type + what + "Title").css("font-size", t.css("font-size"));
	$("#" + type + what + "Title").html(t.html());

	//$("#" + type + what + "Title").html(title);
	t.remove();
}

$(document).ready(function () {
	$(document).click(function (e) {
		if ($(e.target).is('#SaleLocationTitle, SaleLocationTitle *,#SaleLocationCheckboxes, #SaleLocationCheckboxes *, #RentLocationTitle, RentLocationTitle *,#RentLocationCheckboxes, #RentLocationCheckboxes *')) return;
		if ($(e.target).is('#SaleTypeTitle, SaleTypeTitle *,#SaleTypeCheckboxes, #SaleTypeCheckboxes *, #RentTypeTitle, RentTypeTitle *,#RentTypeCheckboxes, #RentTypeCheckboxes *')) return;
		if ($(e.target).is('#LocationTitle, LocationTitle *,#LocationCheckboxes, #LocationCheckboxes *, #LocationTitle, LocationTitle *,#LocationCheckboxes, #LocationCheckboxes *')) return;
		if ($(e.target).is('#TypeTitle, TypeTitle *,#TypeCheckboxes, #TypeCheckboxes *, #TypeTitle, TypeTitle *,#TypeCheckboxes, #TypeCheckboxes *')) return;
		if ($('#SaleLocationCheckboxes').is(":visible"))
			$('#SaleLocationCheckboxes').hide("slide", { direction: "up" });
		if ($('#SaleTypeCheckboxes').is(":visible"))
			$('#SaleTypeCheckboxes').hide("slide", { direction: "up" });
		if ($('#RentLocationCheckboxes').is(":visible"))
			$('#RentLocationCheckboxes').hide("slide", { direction: "up" });
		if ($('#RentTypeCheckboxes').is(":visible"))
			$('#RentTypeCheckboxes').hide("slide", { direction: "up" });
		if ($('#LocationCheckboxes').is(":visible"))
			$('#LocationCheckboxes').hide("slide", { direction: "up" });
		if ($('#TypeCheckboxes').is(":visible"))
			$('#TypeCheckboxes').hide("slide", { direction: "up" });
	});
});
