﻿// YAHOO shortcuts
var $E = $E || YAHOO.util.Event;
var $CE = $CE || YAHOO.util.CustomEvent;
var $CN = $CN || YAHOO.util.Connect;
var $M = $M || YAHOO.util.Motion;
var $DD = $DD ||YAHOO.util.DD;
//var $ = $D.get;
var $S = $S || YAHOO.util.Selector;
var $$ = $$ || ($S ? $S.query : null);

window.Danbolig = window.Danbolig || {};

// Create a class to handle the widget and the interaction with it.
Danbolig.SalePriceSearchWidget = function(elm) {
	if (typeof(elm) == "string") {
		elm = $$(elm, null, true);
	}
	this.container = elm;
	this.searchBtn = $$("#salePriceHistorySearch", this.container, true);
	
	this.propertyTypeSelect = $$("[id$=c_saleHistoryPropertyTypeDropdown]", null, true);
	this.zipCodeSelect = $$("[id$=c_saleHistoryZipCodeDropdown]", null, true);
	this.streetSelect = $$("[id$=c_saleHistoryStreetDropdown]", null, true);
	
	var propertyType = "";
	var propertyTypeRegex = /(\?|&)propertytype=([^&]*)/;
	var res = propertyTypeRegex.exec(window.location.href);
	if (res) {
		propertyType = res[2];
	}
	if (propertyType != "") {
		this.setSelected(this.propertyTypeSelect, propertyType);
		if (this.propertyTypeSelect.value != propertyType) {
			this.propertyTypeSelect[0].selected = true;
		}
	}
	
	var zipcode = "";
	var zipcodeRegex = /(\?|&)zipcode=([^&]*)/;
	var res = zipcodeRegex.exec(window.location.href);
	if (res) {
		zipcode = res[2];
	}
	if (zipcode != "") {
		this.zipCodeSelect.value = zipcode;
		if (this.zipCodeSelect.value != zipcode) {
			this.zipCodeSelect.selectedIndex = 0;
		}
	}
	
	$E.addListener(this.propertyTypeSelect, 'change', this.onChange, this, true);
	$E.addListener(this.zipCodeSelect, 'change', this.onChange, this, true);
	$E.addListener(this.streetSelect, 'change', this.onChange, this, true);
	$E.addListener(this.searchBtn, 'click', this.onClick, this, true);
	
	this.onChange();
};
Danbolig.SalePriceSearchWidget.prototype.buildQuery = function() {
	var q = "?";
	if (this.propertyTypeSelect.value && this.propertyTypeSelect.value != '') {
		q += "propertytype=" + this.propertyTypeSelect.value + "&";
	}
	if (this.zipCodeSelect.value && this.zipCodeSelect.value != '') {
		q += "zipcode=" + this.zipCodeSelect.value + "&"
	}
	
	var streetRegex = /(\?|&)address=([^&]*)/;
	var street;
	var res = streetRegex.exec(window.location.href);
	if (this.streetSelect.value && this.streetSelect.value != '') {
		street = this.streetSelect.value;
	} else if (res && !this.interactive) {
		street = decodeURIComponent(res[2]);
	}
	if (street) {
		q += "address=" + encodeURIComponent(street) + "&";
	}
	/*
	if (this.streetSelect.value && this.streetSelect.value != '') {
		q += "address=" + encodeURIComponent(this.streetSelect.value) + "&";
	}
	*/
	if (q.substr(q.length-1, 1) == '&') {
		q = q.substr(0, q.length-1);
	}
	return q;
}
Danbolig.SalePriceSearchWidget.prototype.search = function(e) {
	if (e) $E.preventDefault(e);
	var q = this.buildQuery();
	return false;
}
Danbolig.SalePriceSearchWidget.prototype.setSelected = function(select, value) {
	for (var i = 0, j = select.length; i < j; i++) {
		if (select[i].value == value) {
			select[i].selected = true;
			break;
		}
	}
}
Danbolig.SalePriceSearchWidget.prototype.updateControlStates = function() {
	$D.removeClass(this.propertyTypeSelect, "invalid");
	$D.removeClass(this.zipCodeSelect, "invalid");
	$D.removeClass(this.streetSelect, "invalid");

	if (!this.zipCodeSelect.disabled && this.zipCodeSelect.value != "" && this.streetSelect.options.length > 1) {
		this.streetSelect.disabled = false;
		//this.propertyTypeSelect.disabled = false;
	} else {
		this.streetSelect.disabled = true;
		//this.propertyTypeSelect.disabled = true;
	}
	
	if (!this.streetSelect.disabled && this.streetSelect.value != "") {
		this.propertyTypeSelect.disabled = false;
	} else {
		this.propertyTypeSelect.disabled = true;
	}
	
	if (!this.streetSelect.disabled && this.streetSelect.value != "") {
		YAHOO.util.Dom.removeClass(this.searchBtn, "button-disabled");
	} else {
		YAHOO.util.Dom.addClass(this.searchBtn, "button-disabled");
	}
	
}
Danbolig.SalePriceSearchWidget.prototype.onChange = function(e) {
	this.interactive = false;
	if (e) {
		$E.preventDefault(e);
		this.interactive = true;
	}

	this.selectedStreetname = this.streetSelect.value;
	this.selectedZipCode = this.zipCodeSelect.value;
	this.selectedPropertyType = this.propertyTypeSelect.value;

	this.updateControlStates();
	if (!this.zipCodeSelect.disabled && this.zipCodeSelect.value != "") {
		var url = "/ajax/ajaxsalehistory.aspx" + this.buildQuery();
		$CN.asyncRequest('GET', url, this, null);
	} 
	
	return false;
}
Danbolig.SalePriceSearchWidget.prototype.onClick = function(e) {
	$E.preventDefault(e);
	
	if (!this.streetSelect.disabled && this.streetSelect.value != "") {
		//window.location.pathname = this.data.propertySaleHistoryUrl + this.buildQuery();
		window.location.href = window.location.protocol + "//" + window.location.host + this.data.propertySaleHistoryUrl + this.buildQuery();
		//alert("searching!");
	}
	
	var blinker = function(select, cssClass, delay, count) {
		if ($D.hasClass(select, cssClass)) {
			$D.removeClass(select, cssClass);
			count -= 1;
		} else {
			$D.addClass(select, cssClass);
		}
		if (count > 0 || $D.hasClass(select, cssClass)) {
			var func = arguments.callee;
			setTimeout(function() {
				func(select, cssClass, delay, count);
			}, delay);
		}
	}

	/*
	if (this.propertyTypeSelect.value == "") {
		blinker(this.propertyTypeSelect, "invalid", 100, 4);
	}
	*/
	if (this.zipCodeSelect.value == "" && !this.zipCodeSelect.disabled) {
		blinker(this.zipCodeSelect, "invalid", 100, 4);
	}
	if (this.streetSelect.value == "" && !this.streetSelect.disabled) {
		blinker(this.streetSelect, "invalid", 100, 4);
	}

	
	return false;
}
Danbolig.SalePriceSearchWidget.prototype.success = function(o){
	this.data = YAHOO.lang.JSON.parse(o.responseText);

	while (this.streetSelect.options.length > 1) {
		this.streetSelect.remove(1);
	}
	
	if (this.data.searchResult.distinctStreetnames.length == 0) {
		this.streetSelect.options[0].innerHTML = "Intet resultat";
	} else {
		this.streetSelect.options[0].innerHTML = "Vælg vej";
	}
	
	for (var i = 0; i < this.data.searchResult.distinctStreetnames.length; i++) {
		var option = document.createElement("option");
		option.innerHTML = this.data.searchResult.distinctStreetnames[i];
		option.value = this.data.searchResult.distinctStreetnames[i];
		this.streetSelect.appendChild(option);
	}
	
	if (this.selectedStreetname) {
		this.setSelected(this.streetSelect, this.selectedStreetname);
	}
	else if (!this.interactive) {
		var address = "";
		var addressRegex = /(\?|&)address=([^&]*)/;
		var res = addressRegex.exec(window.location.href);
		if (res) {
			address = decodeURIComponent(res[2]);
		}
		if (address != "") {
			this.setSelected(this.streetSelect, address);
			if (this.streetSelect.value != address) {
				this.streetSelect[0].selected = true;
			}
		}
	}
	this.selectedStreetname = this.streetSelect.value;
	
	while (this.propertyTypeSelect.options.length > 1) {
		this.propertyTypeSelect.remove(1);
	}
	for (var i = 0, j = this.data.searchResult.propertyTypes.length; i < j; i++) {
		var option = document.createElement("option");
		option.innerHTML = this.data.searchResult.propertyTypes[i].name;
		option.value = this.data.searchResult.propertyTypes[i].propertyTypeId;
		this.propertyTypeSelect.appendChild(option);
	}
	if (this.selectedPropertyType) {
		this.setSelected(this.propertyTypeSelect, this.selectedPropertyType);
	}

	this.updateControlStates();

	//Danbolig.salePriceHistoryData = this.data;
	return;
}
Danbolig.SalePriceSearchWidget.prototype.failure = function(o) {
	//alert("FAILURE: " + o);
}
 

$E.onDOMReady(function() {
	var widget = new Danbolig.SalePriceSearchWidget(".saleHistorySearch");
});

