﻿//er gaat iets mismet escape() en utf-8 ?! daarom workaround
var unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~";
var reserved = "!*'();:@&=+$,/?%#[]";
var hexchars = "0123456789ABCDEFabcdef";

setupBlader();

function setupBlader(){

	$("#nivo1 a").click(function() {
		$('#nivo1 a').removeClass('sel'); $(this).addClass('sel'); 
		laadBlader(this.href.substring(this.href.indexOf('=')+1)); return false; } );
	
	$("#nivo2").height( $("body").height() - 267);
	
	if (location.hash.indexOf('=') > 1) {
		h = location.hash.replace('#','').split('=');
		laadBlader(h[0]); resBlader(h[0],h[1]);
	}
}

function preplijst(t){
	$('#' + t + 'lijst li').click(function(){
		$('#' + t + 'lijst li').removeClass('sel'); $(this).addClass('sel'); 
		if (this.innerHTML.length == 1)
			{$('#nivo2').load('/zoeken/vervaardiger.aspx?l=' + this.innerHTML);}
		else
			{resBlader(t, this.title != '' ? this.title : this.innerHTML);}
		return false;
		});
}

function laadBlader(htm){
	var url = '/zoeken/' + htm + '.aspx';
	$('#nivo2').load(url);
}

function resBlader(w,s){
	location.hash = '#' + w + '=' + s;
	$('#bladerres').load('/zoeken/gsa_kaal.aspx?' + w + '=' + encode(s), bladerKlaar);
}

function bladerKlaar(){

	$("#bladerres a").each(function(){
		this.t = this.title; this.title = "";
	});
	$("#bladerres a").hover(function(e){							  
		$("body").append("<" + "div id='tooltip'>"+ this.t +"<" + "/div>");
		$("#tooltip").css("top",(e.pageY) + "px").css("left",(e.pageX) + "px").show();		
    },
	function(){	
		$("#tooltip").remove();
    });	
	
	$(".#bladerres a").mousemove(function(e){
		$("#tooltip").css("top",(e.pageY) + "px").css("left",(e.pageX) + "px");
	 });

}



function gethex(decimal) {
  return "%" + hexchars.charAt(decimal >> 4) + hexchars.charAt(decimal & 0xF);
  }

function encode(decoded) {
  var encoded = "";
    for (var i = 0; i < decoded.length; i++ ) {
      var ch = decoded.charAt(i);
      if (unreserved.indexOf(ch) != -1) {
        encoded = encoded + ch;
      } else {
		var charcode = decoded.charCodeAt(i);
        if (charcode < 128) {
          encoded = encoded + gethex(charcode);
        }
        if (charcode > 127 && charcode < 2048) {
          encoded = encoded + gethex((charcode >> 6) | 0xC0);
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }
        if (charcode > 2047 && charcode < 65536) {
          encoded = encoded + gethex((charcode >> 12) | 0xE0);
          encoded = encoded + gethex(((charcode >> 6) & 0x3F) | 0x80);
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }
        if (charcode > 65535) {
          encoded = encoded + gethex((charcode >> 18) | 0xF0);
          encoded = encoded + gethex(((charcode >> 12) & 0x3F) | 0x80);
          encoded = encoded + gethex(((charcode >> 6) & 0x3F) | 0x80);
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }
      }
    } 
   return encoded;
}