function rgb2hex(_rgb) { 
	rgb = _rgb; /* Assume array */
	if (rgb.constructor == String) {
		if (_rgb.substring(0, 1) == "#") /* Already in HEX format */
			return _rgb;
		rgb = _rgb.substring(4, rgb.length-1).split(","); /* rgb(r, g, b) format */
	}

	function hex(x) { 
		hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"); 
		return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16]; 
	} 
	return "#" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]); 
} 

function showLocationInfo(obj) {
	$(obj).stop().animate({width: "350px"}, "fast");
}

function hideLocationInfo(obj) {
	$(obj).stop().animate({width: "560px"}, "fast");
}

$().ajaxError(function(ev,xhr,o,err) {
	alert("General AJAX error. Should not happen.");
});

$(document).ready(function() {
	$.gallery.initialize();

	/* Add rounded corner elements */
	$("div.menuheader").append("<div class='menuheader_inactive_tl'>&nbsp;</div>");
	$("div.menuheader").append("<div class='menuheader_inactive_tr'>&nbsp;</div>");
	$("div.menuheader").append("<div class='menuheader_inactive_bl'>&nbsp;</div>");
	$("div.menuheader").append("<div class='menuheader_inactive_br'>&nbsp;</div>");
	$("div.menuheader").append("<div class='menuheader_active_tl'>&nbsp;</div>");
	$("div.menuheader").append("<div class='menuheader_active_tr'>&nbsp;</div>");
	$("div").each(function() {
		$("span.menuitem:last", this).append("<span class='menuitem_bl'>&nbsp;</span>");
		$("span.menuitem:last", this).append("<span class='menuitem_br'>&nbsp;</span>");
		$("span.menuitem:last", this).css("margin-bottom", "5px");
	});
	$("span.menuitem").mouseover(function() { 
		$(this).toggleClass("menuitem_hover"); 
	});
	$("span.menuitem").mouseout(function() { 
		$(this).toggleClass("menuitem_hover"); 
	});
	
	$("span.menuitem").click(function() {
		$("span.menuitem").removeClass("menuitem_hover");
		$("span.menuitem").removeClass("menuitem_current");
		$(this).addClass("menuitem_current");
	});

	$("div.menuheader").not("div.menuheader_active").next("div").hide();
	$("div.menuheader").click(function() {
		var obj = this;
		if ($(this).is(".menuheader_active"))
			return;

		$("div.menuheader_active").next("div").slideUp("slow", function() {
			$("div.menuheader_active").removeClass("menuheader_active");
			$(obj).html($(obj).html().replace(/font_color=[0-9A-F]{6}/, "font_color=FFFFFF"));
			$(obj).html($(obj).html().replace(/background_color=[0-9A-F]{6}/, "background_color=616772"));
			$(obj).addClass("menuheader_active");
			$(obj).next("div").slideDown();
			$("div.menuheader:not(.menuheader_active)").each(function() {
				$(this).html($(this).html().replace(/font_color=[0-9A-F]{6}/, "font_color=616772"));
				$(this).html($(this).html().replace(/background_color=[0-9A-F]{6}/, "background_color=FFFFFF"));
			});
		});
	});
	$("div.menuheader:first").click();

	$(".result_item").mouseover(function() {
		$(this).toggleClass("result_item_active");
	});
	$(".result_item").mouseout(function() {
		$(this).toggleClass("result_item_active");
	});
	$(".result_item").click(function() {
		$(this).toggleClass("result_item_active");
	});

	/* Add some interaction to the search bar */
	var bar_down = true;
	//var bar_timeout;
	$("#search_bar").mouseover(function() {
		if (!bar_down) {
			//clearTimeout(bar_timeout);
			bar_down = true;
			$(this).stop().animate({top: "0px"}, 400);
			$('.lang_unique').hide();
		}
		return false;
	});
	$("#center_pane").mouseover(function() {
		if (bar_down) {
			//clearTimeout(bar_timeout);
			//bar_timeout = setTimeout(function() {
				bar_down = false;
				$("#search_bar").stop().animate({top: "-200px"}, 600);
				$('.lang_unique').show();
			//}, 1500);
		}
		return false;
	});

	$(".textheader").each(function() {
		var title = $(this).html().toUpperCase();
		if ($(this).attr("id") == "") {
			var id = Math.random();
			$(this).attr("id", id + title);
		} else {
			var id = $(this).attr("id");
		}
		var fo = new FlashObject("flash/headertext.swf", "header", $(this).width() + "px", $(this).css("height"), "8", "transparent");
		$(this).width("auto");
		fo.addParam("allowScriptAccess", "sameDomain");
		fo.addParam("quality", "high");
		fo.addParam("scale", "noscale");
		fo.addParam("wmode", "transparent");
		fo.addVariable("headerText", title);
		fo.addVariable("text_pos_x", "-4");
		fo.addVariable("text_pos_y", "-4");
		fo.addVariable("text_align", $(this).css("text-align"));
		fo.addVariable("headerColor", rgb2hex($(this).css("color")));
		fo.addVariable("text_size", $(this).css("font-size").substring(0, $(this).css("font-size").length-2));
		fo.write(id+title);
	});

	$("a[class='tag_submit']").click(function() {
		clear_search_form();
		$('html,body').animate({scrollTop: 0});
		$("select#city_selector option[value="+this.rel.toLowerCase()+"]").attr('selected', 'selected');
		$('#search_bar_form_identifier').submit();
		//document.location.hash = "/"+this.rel.toLowerCase();
		//return false;
		//$("select#city_selector option[value="+this.rel.toLowerCase()+"]").attr('selected', 'selected');
		return false;
	});
	
	$("a[class!='tag_submit']").not("a.noajax").click(function(e) {
		getXUrl(this);
		//var hash = $(this).attr("href");
		//hash = hash.replace(/^.*#/, ''); /* Get only the hash part */
		//$.historyLoad(hash);
		return false;
	});
	
	$("#menu_container").show();

	$("input.prevalue, textarea.prevalue").prevalue();

	setTimeout(hideBar, 1000);

	$.historyInit(getXUrl_direct);
});

function hideBar() {
	bar_down = false;
	$("#search_bar").stop().animate({top: "0px"}, 1000, function() {
		setTimeout(function() {
			$("#search_bar").animate({top: "-200px"}, 1000);
		}, 1000);
	});
	$('.lang_unique').show();
}

function getXUrl(obj) {
	var url = $(obj).attr("href");
	var hash = $(obj).attr("href");

	if (!hash)
		hash = "";

	hash = hash.replace(/^.*#/, ''); /* Get only the hash part */
	$.historyLoad(hash);
	//return getXUrl_direct(url);
}
	
function getXUrl_direct(url) {
	if (url == "") /* Don't do anything if url is undefined (deeplinking) */
		return false;

	// Go to hash-space is a non-hash url is set
	if (document.location.pathname.length > 3) {
		document.location.href = "/#" + url;
		return false;
	}

	$("select#city_selector option[value="+url.replace(1)+"]").attr('selected', 'selected');

	if (!url.match(/.*\/details$/)) {
		$("#related").hide();
	}

	$("#content").prepend("<div id='loader'>" + stoit.settings.loading_message + "</div>");
	$('html').animate({scrollTop: 0}, 300, function() {
		if (url.match(/.*zoeken.*/)) {
			var start = (parseInt(url.substr(url.indexOf("_")+1))-1) * 5;
			$('#search_bar_form_identifier input[name="start"]').val(start);
			$('#search_bar_form_identifier').ajaxSubmit(stoit.settings.ajax_options);
		} else {
			$.get(url+'&ajax=1', function(data) {
				$("#ajaxContainer").html(data).find("input.prevalue, textarea.prevalue").prevalue();
			});
		}

	});

	return false;
}

function setPageTitle(title) {
	$("#title").text(title);
}

function togglePrices() {
	$("#search_bar_table select[name='min_price_rent']").toggle();
	$("#search_bar_table select[name='min_price_buy']").toggle();
	$("#search_bar_table select[name='max_price_rent']").toggle();
	$("#search_bar_table select[name='max_price_buy']").toggle();

	if ($("#search_bar_table select[name='rent_or_buy'] option:selected").val() == "buy") {
		$("#search_bar_table select[name='furnished']").attr("disabled", "disabled");
	} else {
		$("#search_bar_table select[name='furnished']").removeAttr("disabled");
	}
}

function loadCities() {
	id = $("select[name='neighbourhood'] option:selected").val();
	$.getJSON("scripts/get_cities.php", {id: id}, function(data) {
		$("select[name='city']").html("<option value='0'>" + stoit.settings.no_preference + "</option>");
		for (n=0; n<data.length; n++) {
			$("select[name='city']").append("<option value='" + data[n]["city_id"] + "'>" + data[n]["name"] + "</option>");
		}
	});
}


function clear_search_form() {
	$("#search_bar_form_identifier select option:first-child").attr("selected", "selected");
	$("input[name='zip_or_address']").val("");
}



