function fbs_click() {
    u = location.href;
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

function sendForm() {
	var i = 0;
	$("form label:contains('*')").each(function() {
		label = $(this);
		checkField = $(this).next();
		if(checkField.val()=='') {
			label.addClass('error');
			checkField.addClass('error');
			i++;
		} else {
			label.removeClass('error');
			checkField.removeClass('error');
		}
	});
	if(i==0) {
		return true;
	} else {
		return false;
	}
}

function tablize() {
	$("table.lvWhg").each(function() {
		$(this).find("tbody tr").removeClass('gray');
		$(this).find("tbody tr:even").addClass('gray');
	});
}

function checkHaeuser() {
	$("table.lvWhg").each(function() {
		var tblTitle = $(this).find("thead th:first").text();
		$("#haus").append("<option value='"+tblTitle+"'>"+tblTitle+"</option>");
	});
}

function filter(form) {
	$("table.lvWhg").show().find("tr").show();
	var elements =  form.find("select, input");
	var query = "";
	elements.each(function() {
		var selectEle = $(this);
		var key = selectEle.attr('name');
		var value = selectEle.val();
		var prevValue = selectEle.find("option[value='"+value+"']").prev().val();
		if(prevValue=='') {prevValue=1;}
		if(value.length>0) {
			if(key!=undefined) {query += key+"="+value+"&";}
			if(key=='preis') {
				$("table.lvWhg tbody tr").each(function() {
					var preis = $(this).find(".Euro").text()
					preis = parseFloat(preis.replace(/[\.\,]/g,''));
					if(value=='openend') { 
						if(280000>preis) {
							$(this).hide();
						}
					} else {
						if(value<preis || preis<prevValue) {
							$(this).hide();
						}
					}
				});
			}
			if(key=='wohnflaeche') {
				$("table.lvWhg tbody tr").each(function() {
					var fla = parseInt($(this).find(".m2 span").text());
					if(value=='openend') {
						if(180>fla) {
							$(this).hide();
						}
					} else {						
						if(value<fla || fla<prevValue) {
							$(this).hide();
						}
					}
				});
			}
			if(key=='zimmer') {
				$("table.lvWhg tbody tr").each(function() {
					if($(this).find(".Zi").text()!=value) {
						$(this).hide();
					}
				});
			}
			if(key=='balkon') {
				if(value.toLowerCase()!='') {
					$("table.lvWhg tbody tr:visible").each(function() {
						if($(this).find('span.we').text().toLowerCase()==value.toLowerCase()) {
						} else {
							$(this).hide();
						}
					});
				}
			}
			if(key=='haus') {
				if(value!='') {
					$("table.lvWhg").each(function() {
						var tblTitle = $(this).find("thead th:first").text();
						if(tblTitle!=value) {
							$(this).hide();
						}
					});
				}
			}
		}
	});
	q(query);
	tablize();
}

function q(q) {
	$.ajax({
		type: "POST",
		url: "/_include/php/memory.php",
		data: q,
		success: function(msg){
			window.location.hash = q;
		}
	});
}

$(document).ready( function() {
	// fancybox
	$("a[href$='jpg'], a[href$='JPG'], a[href$='jpeg'], a[href$='JPEG'], a[href$='png'], a[href$='PNG'], a[href$='gif'], a[href$='GIF']").fancybox();

	// focus formular
	$("form input:first").focus();

	//projekte template bilder klick => gallery next
	$("#pic #slider img, #nextBtn").bind("click", function(e) {
		if(e.target.tagName!='A') {
			$("#nextBtn a").trigger('click');
		}
	});

	//bloecke template korrektur h1 a > kein link
	$("div.desc, div.desc_sub").find("h1 a[href='']").each(function() {
		$(this).wrapInner("<span></span>").find("span").unwrap();
	});

	//table in projektuebersicht stylen
	$("table.lvWhg").each(function() {
		if($(this).find("tbody tr").size()==0) {
			$(this).remove();
		}
	});
	tablize();
	checkHaeuser();
	$("#auswahl select").bind('change', function() {
		filter($("#auswahl"));
	});

	$("#auswahl #zimmer").bind('keyup', function() {
		filter($("#auswahl"));
	});

	$("#auswahl #reset").bind('click', function() {
		$("table.lvWhg tr").show();
		var auswahl = $("#auswahl");
		auswahl.find("select").val('');
		auswahl.find("#zimmer").val('');
		filter($("#auswahl"));
	});

});

