var topLichtkrant = new Array();
var topLichtkrantIterator = 0;
var bottomLichtkrant = new Array();
var bottomLichtkrantIterator = 0;
var scrollLeftLoop;
var scrollRightLoop;
var scrollAutoLoop;
var scrollAutoDirection = 1;
var scrollDownLoop;
var scrollUpLoop;
var rente;
var windowTimout = 0;
var max;
var leenbaar;
var awaitSubmit = false;
var belasting;

function urlencode( str ) {
     var hash_map = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The hash_map is identical to the one in urldecode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }

    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}

$(document).ready(function(){
	$("input[type='submit']").addClass("button");
	$("input[type='button']").addClass("button");
	$("input[type='image']").addClass("image");
	changeTopLichtkrant();
	changeBottomLichtkrant();

	$("#verzekeringswindow table").hide();
	$("#verzekeringswindow #stap_1").show();


	$("#hypotheekberekenen table").hide();
	$("#hypotheekberekenen #stap_1").show();

	$("#hypotheekberekenen #stap_1 .button_next").click(function(){
		leenbaarbedrag();
	});
	$("#hypotheekberekenen #stap_2 .button_next").click(function(){
		$('#hypotheekberekenen #stap_2').fadeOut(function(){
			$('#hypotheekberekenen h2:first').html('Stap 3: Extra informatie');
			$('#hypotheekberekenen #stap_3').fadeIn();
		})

	});
	$("#hypotheekberekenen #stap_3 .button_next").click(function(){
		$('#hypotheekberekenen #stap_3').fadeOut(function(){
			$('#hypotheekberekenen h2:first').html('Stap 4: Contact opnemen');
			$('#hypotheekberekenen #stap_4').fadeIn(function(){
				$("#hypotheekberekenen #hypname").focus();
			});
		})

	});

	if(windowTimout !== 0){
		setTimeout(function(){ $("#notification_window").fadeOut(); },windowTimout*1000);
	}

	$("#movie_title a.movie_title").click(function(){
		$("#movie_container").hide();
		$("#movie_naw").hide();
		$("#movie_window iframe").attr("src",$(this).attr("href")).show();

		return false;
	});

	$("#hypotheekberekenen #stap_4 .button_next").click(function(){
		$('#hypotheekberekenen #stap_4').fadeOut(function(){
			$('#hypotheekberekenen h2').html('Dank u!');
			$('#hypotheekberekenen #stap_5').fadeIn();
			var data = "";
			$('#hypotheekberekenen input').map(function(){
				if($(this).attr("name") !== "")
					data += $(this).attr("name")+"="+urlencode($(this).val())+"&";
			});
			$.post("/objecten/&/optie/hypotheek",data);
		})
	});

	/*$("#huur_prijs").hide();*/

	$("#verzekering").click(function(){
		$("#verzekeringswindow table").hide();
		$('#verzekeringswindow').fadeIn();
		$("#verzekeringswindow #verz_stap_1").show();
	});
	$("#verz_stap_1 .button_next").click(function(){
		$('#verz_stap_1').hide();
		$('#verz_stap_2').fadeIn();
	    $('#verz_auto_table').fadeIn();
	    $('#verz_bromfiets_table').fadeIn();
	    $('#verz_motor_table').fadeIn();
	});
	$("#verz_stap_2 .button_next").click(function(){
		$('#verz_stap_2').hide();
		$('#verz_stap_3').fadeIn();
	});
	$("#verz_stap_3 .button_next").click(function(){
		$('#verz_stap_3').hide();
		$('#verz_stap_4').fadeIn();
	});
	$("#verz_stap_4 .button_next").click(function(){
		$('#verz_stap_4').hide();
		$('#verz_stap_5').fadeIn();
		var data = "";
		$('#verz_blok_container input').map(function(){
			if($(this).attr("name") !== "") data += $(this).attr("name")+"="+urlencode($(this).val())+"&";
		});
		$('#verz_blok_container select').map(function(){
			if($(this).attr("name") !== "") data += $(this).attr("name")+"="+urlencode($(this).val())+"&";
		});
		$('#verz_blok_container textarea').map(function(){
			if($(this).attr("name") !== "") data += $(this).attr("name")+"="+urlencode($(this).val())+"&";
		});
		//alert(data);
		$.post("/diverse/&/verzekering",data+'echt=yes');
	});
	$("#verz_stap_5 .button_next").click(function(event){
		$('#verz_stap_5').hide();
		$('#verzekeringswindow').fadeOut();
		event.preventDefault();
	});
	$("#verz_stap_2 .button_prev").click(function(){
		$('#verz_stap_2').hide();
		$('#verz_stap_1').fadeIn();
	});
	$("#verz_stap_3 .button_prev").click(function(){
		$('#verz_stap_3').hide();
		$('#verz_stap_2').fadeIn();
	});
	$("#verz_stap_4 .button_prev").click(function(){
		$('#verz_stap_4').hide();
		$('#verz_stap_3').fadeIn();
	});
	$("#verz_stap_5 .button_prev").click(function(){
		$('#verz_stap_5').hide();
		$('#verz_stap_4').fadeIn();
	});


	$("#berekenen").click(function(){
		$("#hypotheekberekenen table").hide();
		$("#hypotheekberekenen #stap_1").show();
		$('#hypotheekberekenen #leeftijd').focus();
		$('#hypotheekberekenen').fadeIn();

	});
	$("#search_options a").click(function(){
		$("#search_options a").removeClass("selected");
		$(this).addClass("selected");

		if($(this).attr("href") !== "soort"){
			$("#soort").val("*");
		}
		else{
			$("#soort").val($(this).attr("soort"));
		}

		if($(this).attr("href") == "koop"){
			$("#is_koopwoning").val(1);
		}
		else if($(this).attr("href") == "huur"){
			$("#is_koopwoning").val(0);
		}
		else{
			$("#is_koopwoning").val("*");
		}

		if($(this).attr("href") == "nieuw"){
			$("#is_nieuwbouw").val(1);
		}
		else{
			$("#is_nieuwbouw").val("*");
		}


		if($(this).attr("href") == "buitenland"){
			$("#is_nederland").val(1);
			//$("#search_country").show();
		}
		else{
			$("#is_nederland").val("*");
			//$("#search_country").hide();
		}


		if(awaitSubmit == false) $("#zoektabel form").submit();
        $("#zoek_form").submit();
		return false;
	});
	
	//$("#search_country option[value="+$("#search_country").attr("selected")+"]").attr("selected","selected");

	resize();
});
$(window).resize(resize);

function scrollDown()
{
	document.getElementById('nieuwsberichten').scrollTop+=5;
	scrollDownLoop=setTimeout("scrollDown()",20);
}
function scrollUp()
{
	document.getElementById('nieuwsberichten').scrollTop-=5;
	scrollUpLoop=setTimeout("scrollUp()",20);
}



function cansubmit(){
	if ($("#hypotheekberekenen #stap_3").css('display') == 'table')
		return true;
	else
		return false;
}
function resize(){
	var width = $(window).width();
	if($(window).width() < 1200){
		width = 1200;
		$("html").css("width",width);
		$("#zoektabel,#nieuws").css("width",width-400);
	}
	else{
		$("html").css("width",'');
		$("#zoektabel,#nieuws").css("width",$(window).width()-392);
	}

	$("#msn_window,#notification_window,#movie_window").css("width",width-20);
	/*$("#hypotheekberekenen .last").css("background-position",(width-671)+"px 70px");*/
}

function closeMovie(){
	$('#movie_container').html('<span id=movieReplacer></span>');
}

function changeTopLichtkrant(){
	$("#banners").fadeOut(function(){
		$("#banners").html(topLichtkrant[topLichtkrantIterator]);
		$("#banners").fadeIn();
	});

	if(topLichtkrantIterator >= topLichtkrant.length) topLichtkrantIterator = 0;
	topLichtkrantIterator++;

	setTimeout("changeTopLichtkrant()",6000);
}

function changeBottomLichtkrant(){
	$("#idealmolliebanner span").fadeOut(function(){
		$("#idealmolliebanner span").html(bottomLichtkrant[bottomLichtkrantIterator]);
		$("#idealmolliebanner span").fadeIn();
	});
	if(bottomLichtkrantIterator >= bottomLichtkrant.length) bottomLichtkrantIterator = 0;
	bottomLichtkrantIterator++;

	setTimeout("changeBottomLichtkrant()",6000);
}

function naarRechts()
{
	document.getElementById('spotlight_objecten_container').scrollLeft+=10;
	scrollRightLoop=setTimeout("naarRechts()",20);
	//scrollAutoLoop=setTimeout("scrollSpotlight_auto()",20);
}
function naarLinks()
{
	document.getElementById('spotlight_objecten_container').scrollLeft-=10;
	scrollLeftLoop=setTimeout("naarLinks()",20);
}
function release()
{
	clearTimeout(scrollDownLoop)
	clearTimeout(scrollUpLoop)
	clearTimeout(scrollLeftLoop)
	clearTimeout(scrollRightLoop)
}
function scrollSpotlight_auto() {
    var blok = document.getElementById('spotlight_objecten_container');
    if (blok.scrollLeft==200 || blok.scrollLeft==0) {
	    scrollAutoDirection = scrollAutoDirection * -1;
	}
	blok.scrollLeft += scrollAutoDirection;
	scrollAutoLoop=setTimeout("scrollSpotlight_auto()",20);
}
function loadSpotlight(id){
	$.get("/objecten/&/spotlight_view/"+id,'',function(){
		document.location.href="/objecten/&/detail/"+id;
	});
	return false;
}
function loadUrl(id,url){
	closeMovie();
	$.get("/objecten/banners/&/view/"+id,'',function(){
		$("#movie_title a.movie_title").html("");
		$("#movie_window iframe").show().attr("src",url);
		$("#movie_container").hide();
		$("#movie_naw").hide();
		$("#movie_window").fadeIn();
	});
}
function loadBanner(id,url,title,href,naw){
    if (id==63) {
       alert('hier');
    }
	$("#movie_window").fadeIn("medium",function(){
		closeMovie();
		swfobject.embedSWF("/js/mediaplayer/player.swf", "movieReplacer", "800", "400", "9.0.0", "/js/expressInstall.swf", new Array(), {'flashvars':'file='+url+'&autostart=true'}, new Array() );
	}).show();
	$("#movie_title a.movie_title").attr("href",href).html("Klik hier voor de website van deze adverteeerder: "+title);

	$.get("/objecten/banners/&/view/"+id);
	$("#movie_window iframe").hide();
	$("#movie_container").show();
	$("#movie_naw").html(naw).show();
	$("#movie_window").fadeIn();
}

function leenbaarbedrag(){
	var leeftijd = $("#leeftijd").val();
	var inkomen = $("#inkomen").val();
	if (leeftijd == "0" || leeftijd == ""){
		alert("U heeft geen leeftijd ingevuld");
		return null;
	}
	if(inkomen == "0" || inkomen == ""){
		alert("U heeft geen inkomen ingevuld");
		return null;
	}

	leenbaar = (parseInt($("#inkomen").val()) + parseInt($("#inkomen_partner").val()) - parseInt($("#vaste_lasten").val())) * max;
	$("#leenbaar_bedrag").html("&euro; "+leenbaar+",-");
	$("#hypotheek_bedrag").val(leenbaar);


	if(parseInt($("#inkomen").val()) > parseInt($("#inkomen_partner").val()))
	{
		belasting = belastingschijf(parseInt($("#leeftijd").val()),parseInt($("#inkomen").val()));
	}
	else{
		belasting = belastingschijf(parseInt($("#leeftijd_partner").val()),parseInt($("#inkomen_partner").val()));
	}

	$("#belastingschijf").html(belasting+"%");
	$("#hypotheekberekenen #stap_1").fadeOut("medium",function(){
		$('#hypotheekberekenen h2:first').html('Stap 2: Berekening / maximale verkrijging hypotheek');
		$("#hypotheekberekenen #stap_2").fadeIn("medium",function(){
			$("#hypotheekberekenen #hypotheek_bedrag").focus();
		});
	});

	maandlasten();
}

function maandlasten()
{
	var jaarrente = Math.round(parseInt($("#hypotheek_bedrag").val())*(rente/100));
	var maandlast = Math.round((jaarrente*1-(belasting/100))/12);
	$("#bruto_jaarrente").html("&euro; "+jaarrente+" ,-");
	$("#maandelijkse_hypoheeklast h2:last").html("&euro; "+maandlast+" ,-");
}

function belastingschijf(leeftijd, inkomen){
	if(inkomen < 17878 && leeftijd < 64)
		return 33.5;
	if(inkomen < 17878 && leeftijd >= 64)
		return 15.6;
	if(inkomen < 32128 && leeftijd >= 64)
		return 24.10;
	if(inkomen < 54776)
		return 42;
	if(inkomen >= 54776)
		return 52;
}