// JavaScript Document
$(document).ready(function(){
	
	
	
	if($('#submenu').length > 0){
		var submenu_top_position = $('#submenu').position().top;
	}
	
	if($('#menu').length > 0){
		var menu_top_position = $('#menu').position().top-5;
	}
	
	$(window).scroll(function(e){
		if($('#menu').length > 0 && menu_top_position-$(window).scrollTop() < 0 && $("#menu_fixer").length == 0){
			$('#menu').before("<div id='menu_spacer' style='width:"+$('#menu').width()+"px; height:"+($('#menu').outerHeight()+5)+"px;'></div>")
			$('#menu').wrap("<div id='menu_fixer' style='width:"+$('#menu').width()+"px; position:fixed; top:0px; border-top:5px solid white;'><div>");
			$('#menu_bg_left').css({"backgroundPosition":"left"});
			$('#menu_bg_right').css({"backgroundPosition":"left"});
		}
		if($('#menu').length > 0 && menu_top_position-$(window).scrollTop() >= 0 && $('#menu').closest('#menu_fixer').length > 0){
			$('#menu_spacer').remove();
			$('#menu').insertAfter("#menu_fixer");
			$("#menu_fixer").remove();
			$('#menu_bg_left').css({"backgroundPosition":"right"});
			$('#menu_bg_right').css({"backgroundPosition":"right"});
		}
	});
	
	$(window).scroll(function(e){
		if($('#submenu').length > 0 && submenu_top_position-$(window).scrollTop() < 0 && $("#submenu_fixer").length == 0){
			$('#submenu').before("<div id='submenu_spacer' style='width:"+$('#submenu').width()+"px; height:"+($('#submenu').outerHeight()+5)+"px;'></div>")
			$('#submenu').wrap("<div id='submenu_fixer' style='width:"+$('#submenu').width()+"px; position:fixed; top:"+ $('#menu').outerHeight()+"px; border-top:5px solid white;'><div>");
		}
		if($('#submenu').length > 0 && submenu_top_position-$(window).scrollTop() >= 0 && $('#submenu').closest('#submenu_fixer').length > 0){
			$('#submenu_spacer').remove();
			$('#submenu').insertAfter("#submenu_fixer");
			$("#submenu_fixer").remove();
		}
	});
							  
	$("a.js_fancybox_single,area.js_fancybox_single")
		.fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'hideOnContentClick' : true,
			'showNavArrows' : true,
			'autoDimensions': false}
		);
	
	$(".maklerauftrag")
		.click(function(){
			var ma_id = $(this).siblings('input[name=maklerauftrag_id]').val();			
			var makler_dialog = 
					  $("<div id='maklerauftrag_form'>"+
						"	<label>Anschrift des Kunden:</label>"+
						"	<br /><textarea class='maklerauftrag_anschrift' name='maklerauftrag_anschrift' ></textarea>"+
						"</div>");
			makler_dialog.dialog({	modal:true,
								 	title: "Expos&eacute; mit Maklerauftrag",
									width: 330,
									buttons:{
										Herunterladen: function(){
											window.location.replace("dwl/expose.php?object="+ma_id+"&expose_maklerauftrag=1&ma_anschrift_kunde="+$(this).find(".maklerauftrag_anschrift").val().replace(/\r?\n|\r/g,"<br />"));
											$(this).dialog("destroy");
										},
										Abbrechen:function(){
											$(this).dialog("destroy");
										}
									}
								});
			return false;
		});
	
	$("input[name=edit_objekt_del_submit]")
		.click(function(){
			return $('input[name=edit_objekt_select\[\]]:checked').length && confirm("Wollen Sie die Objekte wirklich löschen?");
		});
		
	$(".js_autocomplete")
		.autocomplete({
			source: function(request, response) {
				$.ajax({
					url: "ajx/richSearch.php",
					type: 'POST',
					dataType: "json",
					data: {
						maxRows: 5,
						searchTerm: request.term
					},
					success: function(data) {
						response($.map(data, function(item) {
							return {
								label: item.bezeichnung,
								value: item.nummer
							}
						}))
					}
				})
			},
			minLength: 2,
			open: function(e,ui) {},
			change: function(e,ui) {},
			close: function() {},
			search: function() {}
		});
	
	$(".form_sort_objekt").change(function(){
		$(this).submit();
	});
});
