$(document).ready(function() {
	// open external links in new window
	$("a.link-external").attr('target', '_blank');

	// mouseover/out for headers
	$("#col3 h2.hdr a").mouseover(function(){
		$(this).parent().css('background-image', 'url(/images/ui/hdr-green-over.gif)');
	});
	
	$("#col3 h2.hdr a").mouseout(function(){
		$(this).parent().css('background-image', 'url(/images/ui/hdr-green-off.gif)');
	});

	// No available listings
	$("table#visningsliste tbody").each(function(){
		if ($(this).children('tr').length < 2) {
			$(this).parent().parent().append('Beklager, fortiden ingen visninger.');
			$(this).parent().remove();
		}
	});
	
	// update region fields
	$("select#wm_fylkesliste1").change(function(){
		$.ajax({
		    url: '/eiendom/sok',
		    type: 'GET',
		    data: $("select#wm_fylkesliste1").serialize(),
		    dataType: 'html',
		    timeout: 1000,
		    error: function(){
		        $("select#wm_fylkesliste1").after('<p class="error">Feil: kan ikke opptatt fylkkeliste.</p>');
						// $('p.error').fadeOut("slow");
		    },
		    success: function(data){
						var html = $("<div/>");
						html.append(data);

						var omraadeliste      = $("select#wm_omraadeliste8", html).html();
						var eiendomstypeliste = $("select#wm_eiendomstypeliste8", html).html();

						$("select#wm_omraadeliste8").empty().append(omraadeliste);
						$("select#wm_eiendomstypeliste8").empty().append(eiendomstypeliste);
		    }
		});
		$(this).ajaxStart(function(){
			$("img#loading-region").removeClass('hide').addClass('show');
		});
		
		$(this).ajaxComplete(function(){
			$("img#loading-region").removeClass('show').addClass('hide');
		});
	});

	// submit property search
	$("form#eiendomsok").submit(function(){
		$.ajax({
		    url: '/eiendom/sok',
		    type: 'POST',
		    data: $("form#eiendomsok").serialize(),
		    dataType: 'html',
		    timeout: 1000,
		    error: function(){
					$("div#results").html('<p class="error">Feil: kan ikke opptatt søkresultater.</p>').fadeIn("normal");
		    },
		    success: function(data){
					$("div#results").html(data);
		    }
			}
		);
		$(this).ajaxStart(function(){
			$("div#results").addClass('loading').empty();
		});

		$(this).ajaxComplete(function(){
			$("div#results").removeClass('loading');
		});
		$('html,body').animate({scrollTop: 0}, 1000);
		return false;
	});
	
	// update region fields
	$("#section-eiendom-sok .pagination a").click(function(){
		$.ajax({
		    url: '/eiendom/sok',
		    type: 'GET',
		    // data: $(this).attr('href').replace("/\/eiendom\/sok\//", ""),
		    // data: $(this).queryString.serialize(),
		    dataType: 'html',
		    timeout: 1000,
				start: function(){
					$("div#results").addClass('loading').empty();
		    },
		    error: function(){
					$("div#results").html('<p class="error">Feil: kan ikke opptatt søkresultater.</p>').fadeIn("normal");
		    },
		    success: function(data){
					$("div#results").html(data);
		    }
			}
		);
		
		$(this).ajaxStart(function(){
			$("div#results").addClass('loading').empty();
		});

		$(this).ajaxComplete(function(){
			$("div#results").removeClass('loading');
		});
		
		$('html,body').animate({scrollTop: 0}, 1000);
	});



});
