var banners, bnum, bmax, bprev, tmpbnum;
var rar;
var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
var hoverimage;

$(document).ready(function() {
	banners = Array();
	bmax = Array();
	bnum = Array();
	bprev = Array()
	tmpbnum = Array()
	
	checkBanner(1);
	checkBanner(2);
	
	checkForImage();
	$("#checkforimage input").click(function() {
		checkForImage();
	});

	$(document).pngFix(); 
	
	
	lightbox()
	
	if($("#wonningzoeker").length > 0) {
		//$("#wonningzoeker").hover(function() {
		//	$("#mapimages").stop();
		//	$("#mapimages").show("slow");
		//
		//}, function() {
		//	$("#mapimages").hide();
		//});
	}
	if($("#profile").length > 0) {
		
		$("input[name$=realname]").blur(function () {
			if(document.forms["profile"].elements["achternaam"].value == "") {
				document.forms["profile"].elements["achternaam"].value = this.value;
			}
		});
		
		$("input[name$=__belangstellende]").click(function() {
			if(this.checked) {
				showAll();
			} else {
				hideAll();
			}
		});
		if($("input[name$=__belangstellende]").attr("checked") == false) {
			hideAll();
		}
	}
	
	if($("#Map area").length > 0) {
		if(ie6 || ie55) {
			
		}  else {
			rr = new Array();
			var mapimage = $("#mapimages");
			//$(mapimage).hide();
			if(raster) {
				$.each(raster, function(key, val) {
					if(val) {
						rr[key] = new Image();
						rr[key].src = val;
						rr[key].id = "b" + key;
						$(mapimage).append(rr[key]);
					}
				});
				
				//$(mapimage).delay(1000).show();
			}
			
			if(document.forms["keuze"]) {
				setOption(document.forms["keuze"].elements["object"], 0)
			}
	
			//reCheck();
			var areamap = new Array();
			hoverimage = document.getElementById("hoverimage");
			
			$("#Map area").each(function(i) {
				this.num = $(this).attr("bnr");
				areamap[i] = $("#mapimages #b" + this.num);
				$(this).hover(function() {
					//$("#infowindow").text($(this).attr("title"));
					//$("#infowindow").show();
					if($(rr[this.num]).is(":visible")) {
				 		hoverimage.src = rr[this.num].src;
				 	}
				 	//$(rr[this.num]).addClass("active");
				}, function() {
					//$("#infowindow").hide();
					hoverimage.src = "/gfx/raster4/overlay.gif";
					//$("#hoverimage").attr("src", "/gfx/raster4/overlay.gif");
					//$(rr[this.num]).removeClass("active");
				});
				$(this).click(function() {
					$("#infobouwnummer div").hide();
					$("#bouwnummer" + this.num).fadeIn();
					return false;
				});
			});
			
		}
	}
	$("#closeflash").click(function() {
		$("#flashpop").hide();
		return false;
	});
	$("#openflash").click(function() {
		doFlash();
	});
	$("a.flowplayer").each(function() {
		 f = flowplayer(this.id, "/flash/flowplayer.commercial-3.1.5.swf", { 
    		clip: {
	       		autoPlay: false
			}
   		});
   		f.load();
	});	
});

function doFlash() {
	$("#flashpop").fadeIn("slow", function() {
		$("#flash").flashembed("/media/a.swf", {"bgColor":"#0f455c"});
	});
	
	
}

function hideAll() {
	$("#naw").hide();
	$("#nawaanvullende").hide();
	$("#partner_naw").hide();
	$("#inkomen").hide();
	$("#huidige").hide();
	$("#bouwnummer").hide();
	$("#redenandere").hide();
	$("#voorkeur").hide();
	$("#bekend").hide();
	$("#terugkeergar").hide();
	$("#Code").hide();
	
}

function showAll() {
	$("#naw").show();
	$("#nawaanvullende").show();
	$("#partner_naw").show();
	$("#inkomen").show();
	$("#huidige").show();
	$("#bouwnummer").show();
	$("#redenandere").show();
	$("#voorkeur").show();
	$("#bekend").show();
	$("#terugkeergar").show();
	$("#Code").show();
}
	
function checkBanner(num) {
	banners[num] = Array();
	nn = "#imagelist" + num + " div";
	bmax[num] = $(nn).length;
	if(bmax[num] > 1) {
		$(nn).each(function(i) {
			banners[num][i] = this;
			$(this).fadeTo(1, 0);
			$(this).hide();
			if(i == 0) {
				$(this).fadeTo(1, 1);
				$(this).show();
			}
		});
		bnum[num] = 0;
		t = setTimeout("dobBanner(" + num + ");", num * 2500);
	}
}
function dobBanner(num) {
	tmpbnum[num] = bnum[num];
	nn = "#imagelist" + num + " div";
	
	$(nn).eq(tmpbnum[num]).fadeTo(1000, 0, function() {
		$(this).hide();
	});
	bprev[num] = bnum[num];
	bnum[num]++;
	if(bnum[num] >= bmax[num]) {
		bnum[num] = 0;
	}
	$(nn).eq(bnum[num]).show();
	$(nn).eq(bnum[num]).fadeTo(1000, 1, function() {	
		setTimeout("dobBanner(" + num + ");", 5000);
	});
}
var checkA, activeImage;
function checkForImage() {
	if(typeof(arr) == "undefined") {
		return;
	}
	activeImage = new Array()
	checkA = new Array();
	$("#allimages img").hide();
	p = 0;
	$("#checkforimage input").each(function () {
		
		if(this.type == "checkbox") {
			v = $(this).attr("rel");
			checkA[v] = this.checked;
			if(this.checked) {
				p = p + parseFloat($("#p" + v).text());
			}
			
		}
	});
	$("#tot_sum").html("&euro; " + number_format(p, 2, ",", "."));
	tot = p + parseFloat($("#koopsom").text());
	$("#tot_tot").html("&euro; " + number_format(tot, 2, ",", "."));
	num = 0;
	for(i in arr) {
		show = true;
		for(j in checkA) {
			if(arr[i][j] == undefined) {
				arr[i][j] = false;
			}
			if(arr[i][j] == checkA[j]) {
				
			} else {
				
				show = false;
			}
		}
		if(show) {
			activeImage[num] = i;
			num++;
		}
	}
	an = 0;
	$("#img" + activeImage[an]).fadeIn("slow");
	$("#afblabel").text("afbeelding " + (an+1) + " van " + activeImage.length);
}
var an;
function loopImage(what) {
	$("#img" + activeImage[an]).fadeOut("slow", function() {
		if(what) {
			an = an + 1;
			if(an >= activeImage.length) {
				an = 0;
			}
		} else {
			an = an - 1;
			if(an < 0) {
				an = activeImage.length-1;
			}
		}
		$("#img" + activeImage[an]).fadeIn("slow");
		$("#afblabel").text("afbeelding " + (an+1) + " van " + activeImage.length);
	});
}



function number_format (number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;
 
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
 
    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}
