/*
  ZWF Basic Insert Partial Content Utility System
  (c) 2010 - Jelmer van der Meer - ZWF Ontwerp
*/
var zwfb_bipcus = null;

function ZWFB_BipcusClient(comp, url, blocking, filler){
  this.blocking  = blocking;
  this.component = comp;
  this.remoteUrl = url;
  this.request   = null;
  this.result    = '';
  this.filler    = filler;
  this.ctype     = 'GET';
  this.params    = null;

  this.execute = function(){
    this.loading();
    this.request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    this.request.bipcusClient = this;
    
    if(this.remoteUrl.substr(0, 6)=='POST::'){
      this.remoteUrl = this.remoteUrl.substr(6);
      var index = this.remoteUrl.indexOf('?');
      if(index!=-1){
        this.ctype     = 'POST';
        this.params    = this.remoteUrl.substr(index+1);
        this.remoteUrl = this.remoteUrl.substr(0, index);       
      }
    }
    
    this.request.open(this.ctype, this.remoteUrl, !this.blocking);
    if(!this.blocking){
      this.request.onreadystatechange = function(){
        if(this.readyState==4){
          if(this.status==200) zwfb_bipcus.removeClient(this);
        }
      }
    }
    if(this.params!=null){
      this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      this.request.setRequestHeader("Content-length", this.params.length);
      this.request.setRequestHeader("Connection", "close");       
    }
    this.request.send(this.params);
    if(this.blocking) zwfb_bipcus.removeClient(this.request);
  }
  this.loading = function(){
    if(this.component=='return') return;
    if(this.component=='reload') return;
    if(this.component=='')       return;

    var elem = document.getElementById(this.component);
    if(elem==null) return;

    elem.innerHTML = this.filler==null || this.filler==false || this.filler=='' ? '<div class="zwfb_loading">Pagina wordt geladen ... </div>' : this.filler;
  }
  this.finish = function(){
    if(this.component=='reload'){
      window.location.href = window.location.href;
      return;
    }
    else if(this.component=='return'){
      this.result = this.request.responseText;
      return;
    }
    else if(this.component=='') return;

    var elem = document.getElementById(this.component);
    if(elem==null) return;

    var content = this.request.responseText;
    var temp = this.clean(content);

    elem.innerHTML = '';

    if(temp[1]!=''){
      var ins = document.createElement('style');
      ins.text = temp[1];
      ins.type = 'text/css';
      document.getElementsByTagName('head')[0].appendChild(ins);
    }

    if(temp[3].length>0){
      for(var i=0; i<temp[3].length; ++i){
        if(zwfb_bipcus.checkCSS(temp[3][i])) continue;
        var ins = document.createElement("link");
        ins.setAttribute("rel", "stylesheet");
        ins.setAttribute("type", "text/css");
        ins.setAttribute("href", temp[3][i]);
        document.getElementsByTagName('head')[0].appendChild(ins);
      }
    }

    elem.innerHTML = temp[0];

    if(temp[2]!=''){
      var ins = document.createElement('script');
      ins.setAttribute('type', 'text/javascript');
      ins.text = temp[2];
      document.getElementsByTagName('head')[0].appendChild(ins);
    }
  }
  this.clean = function(contents){
    var temp = new Array();
    temp[0] = '';
    temp[1] = '';
    temp[2] = '';
    temp[3] = new Array();
    var index1 = 0, index2 = 0;
    while( (index1 = contents.indexOf('<style')) >= 0){
      index2 = contents.indexOf('</style', index1);
      if(index2>=0){
        temp[0] += contents.substring(0, index1);
        index1 = contents.indexOf('>', index1)+1;
        temp[1] += ' '+contents.substring(index1, index2);
        index2 = contents.indexOf('>', index2)+1;
        contents = contents.substring(index2);
      }
      else break;
    }

    contents = temp[0] + contents;
    temp[0] = '';

    var index1 = 0, index2 = 0;
    while( (index1 = contents.indexOf('<link')) >= 0){
      index2 = contents.indexOf('/>', index1);
      if(index2>=0){
        temp[0] += contents.substring(0, index1);
        index1 = contents.indexOf('href=', index1)+1;
        index1 = contents.indexOf('"', index1)+1;
        var index3 = contents.indexOf('"', index1);
        temp[3][temp[3].length] = contents.substring(index1, index3);
        contents = contents.substring(index2+2);
      }
      else break;
    }

    contents = temp[0] + contents;
    temp[0] = '';

    var index1 = 0, index2 = 0;
    while( (index1 = contents.indexOf('<script')) >= 0){
      index2 = contents.indexOf('</script', index1);
      if(index2>=0){
        temp[0] += contents.substring(0, index1);
        index1 = contents.indexOf('>', index1)+1;
        temp[2] += ' '+contents.substring(index1, index2);
        index2 = contents.indexOf('>', index2)+1;
        contents = contents.substring(index2);
      }
      else break;
    }

    temp[0] += contents;
    return temp;
  }
}

function ZWFB_BipcusClientManager(){
  this.csslinks = new Array();
  this.clients  = new Array();

  this.checkCSS = function(url){
    for(var i=0; i<this.csslinks.length; ++i) if(this.csslinks[i]==url) return true;
    this.csslinks[this.csslinks.length] = url;
    return false;
  }
  this.addResultClient = function(comp_id, url, filler){
    var newclient = new ZWFB_BipcusClient('return', url, true, filler);
    this.clients.push(newclient);
    newclient.execute();
    return newclient.result;
  }
  this.addBlockClient = function(comp_id, url, filler){
    var newclient = new ZWFB_BipcusClient(comp_id, url, true, filler);
    this.clients.push(newclient);
    newclient.execute();
    return true;
  }
  this.addClient = function(comp_id, url, filler){
    var newclient = new ZWFB_BipcusClient(comp_id, url, false, filler);
    this.clients.push(newclient);
    newclient.execute();
    return true;
  }
  this.removeClient = function(client_req){
    var new_array = new Array();
    for(var i in this.clients){
      if(this.clients[i].request==client_req) this.clients[i].finish();
      else new_array.push(this.clients[i]);
    }
    this.clients = new_array;
  }
}

zwfb_bipcus = new ZWFB_BipcusClientManager();

function ZWFB_ReloadItem(url, item){
  this.url  = url;
  this.item = item;

  this.equals = function(item, all){
    if(item.item!=this.item) return false;
    if(!all) return true;
    return item.url==this.url;
  }
  this.reloadSelf = function(){
    zwfb_bipcus.addClient(this.item, this.url);
  }
}
function ZWFB_ReloadPath(name, url, item){
  this.name  = name;
  this.paths = new Array();
  this.items = new Array();

  this.register = function(ipath, iurl, item){
    if(ipath.length<=0) this.items[this.items.length] = new ZWFB_ReloadItem(iurl, item);
    else{
      var next = ipath.pop();
      for(var i in this.paths){
        if(this.paths[i].name!=next) continue;
        this.paths[i].register(ipath, iurl, item);
        return;
      }
      var tmp = new ZWFB_ReloadPath(next, iurl, item);
      tmp.register(ipath, iurl, item);
      this.paths[this.paths.length] = tmp;
    }
  }
  this.unregister = function(ipath){
    if(ipath.length<=0) this.items = new Array();
    else{
      var next = ipath.pop();
      for(var i in this.paths) if(this.paths[i].name==next) this.paths[i].unregister(ipath.slice(0));
    }
  }
  this.reload     = function(ipath, event){
    if(ipath.length<=0) for(var i in this.items) event.register(this.items[i]);
    else{
      var next = ipath.pop();
      for(var i in this.paths) if(this.paths[i].name==next) this.paths[i].reload(ipath.slice(0), event);
    }
  }
}

function ZWFB_ReloadEvent(){
  this.items = new Array();

  this.register = function(item){
    for(var i in this.items) if(this.items[i].equals(item, false)) return;
    this.items[this.items.length] = item;
  }
  this.reload   = function(){
    for(var i in this.items) this.items[i].reloadSelf();
  }
}

function ZWFB_ReloadManager(){
  this.item = new ZWFB_ReloadPath('ROOT', '');

  this.register   = function(ipath, iurl, item){
    var patharray = ipath.split('/');
    patharray = patharray.reverse();
    this.item.register(patharray, iurl, item);
  }
  this.unregister = function(ipath){
    var patharray = ipath.split('/');
    patharray = patharray.reverse();
    this.item.unregister(patharray);
  }
  this.reload     = function(ipath){
    var patharray = ipath.split('/');
    patharray = patharray.reverse();
    var result    = new ZWFB_ReloadEvent();
    this.item.reload(patharray, result);
    result.reload();
  }
}

var zwfb_reloadmanager = new ZWFB_ReloadManager();

function zwfb_popuppresetclass(url, con){
  this.url     = url;
  this.content = con;
}

function zwfb_popupwindowclass(){
  this.presets  = new Array();
  this.popupbox = false;
  this.url      = false;
  this.x        = 0;
  this.y        = 0;
  this.count    = 0;
  this.nohide   = false;

  this.registerPreset = function(url, content){
    for(var i in this.presets){
      if(url!=this.presets[i].url) continue;
      else{
        this.presets[i].content = content;
        return;
      }
    }
    this.presets[this.presets.length] = new zwfb_popuppresetclass(url, content);
  }
  this.registerBox = function(item){
    this.popupbox = item;
    var elem = document.getElementById(item);
    if(elem!=null) return;
    var nelem = document.createElement('div');
    //nelem.setAttribute('class', 'popupwindow');
    nelem.setAttribute('id'   , item);
    nelem.setAttribute('style', 'position: fixed;');
    nelem.className   = 'popupwindow';
    nelem.onmouseover = function(){this.nohide = true; return false;};
    //nelem.onmouseout  = function(){this.nohide = false; zwfb_popup_box.hide(null); return false;};
    nelem.onclick     = function(){zwfb_popup_box.hide(null); return false;};
    nelem.style.left  = '2px';
    nelem.style.top   = '2px';

    var _body = document.getElementsByTagName('body')[0];
    if(_body==null) return;
    _body.appendChild(nelem);
    nelem.style.display = 'none';
  }
  this.show        = function(evt, url, show){
    if(!evt) evt = window.event;
    if(this.url==url && !show) return;
    this.url     = url;
    if(show==true){
      this.move(evt);
      this.delay(this.count);
      this.nohide = true;
    }
  }
  this.hide        = function(evt){
    if(this.nohide==true) return;
    var elem = document.getElementById(this.popupbox);
    if(elem==null) return;
    this.count++;
    elem.style.display = 'none';
  }
  this.delay       = function(inval){
    if(inval < this.count) return;
    if(this.url==false) return;
    var elem = document.getElementById(this.popupbox);
    if(elem==null){
      zwfb_popup_box.registerBox('mypopupbox');
      elem = document.getElementById(this.popupbox);      
      if(elem==null) return;
    }
    
    if(elem.style.display=='block') return;

    var found = false;
    for(var i in this.presets){
      if(this.presets[i].url==this.url){
        if(this.presets[i].content=='') elem.innerHTML = 'Geen extra informatie';
        else elem.innerHTML = this.presets[i].content;
        found = true;
      }
    }
    if(!found) zwfb_bipcus.addBlockClient(this.popupbox, this.url, ' ');
    elem.style.display = 'block';

    var window_w = 0;
    var window_h = 0;
    if(typeof(window.innerWidth)=='number'){
      window_w = window.innerWidth;
      window_h = window.innerHeight;
    }
    else{
      window_w = document.documentElement.clientWidth;
      window_h = document.documentElement.clientHeight;
    }

    if((this.x+elem.offsetWidth+20)>window_w) elem.style.left = (this.x - 5 - elem.offsetWidth)+'px';
    else elem.style.left    = (this.x + 5)+'px';

    if((this.y+elem.offsetHeight+5)>window_h) elem.style.top = (this.y - 5 - elem.offsetHeight)+'px';
    else elem.style.top     = (this.y + 5)+'px';
  }
  this.move       = function(evt){
    if(evt==null) evt = window.event;
    var nx = Math.abs(evt.clientX-this.x);
    var ny = Math.abs(evt.clientY-this.y);

    this.count++;
    this.x       = evt.clientX;
    this.y       = evt.clientY;

    if(nx > 5 || ny > 5) this.hide();
    if(nx < 4  || ny < 4 ) setTimeout('zwfb_popup_box.delay('+this.count+')', 250);
  }
  this.out       = function(url){
    if(this.url==url) this.url = false;
  }
}

var zwfb_popup_box = new zwfb_popupwindowclass();

/* dynamic class add/remove tools */
function hasClass(elem,clss){
  if(!elem.className) return false;
  else return elem.className.match(new RegExp('(\\s|^)'+clss+'(\\s|$)'));
}
function addClass(elem,clss){
  if(!hasClass(elem,clss)) elem.className += " "+clss;
}
function removeClass(elem,clss){
  if(hasClass(elem,clss)){
    var reg = new RegExp('(\\s|^)'+clss+'(\\s|$)');
    elem.className=elem.className.replace(reg,' ');
  }
}

function addLoadEvent(func) { 
  var oldonload = window.onload; 
  if(typeof window.onload != 'function') window.onload = func;
	else{ 
	  window.onload = function(){ 
	    if(oldonload) oldonload(); 
	    if(func) func();
	  }
  }
}

function getElementsByClass(clss, elem){
	if(!elem) elem = document;
	var elems = elem.getElementsByTagName('*');
	for(var i=0; i<elems.length; ++i) if(hasClass(elems[i], clss)) return elems[i];
	return null;
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
  var output = "";
  var chr1, chr2, chr3 = "";
  var enc1, enc2, enc3, enc4 = "";
  var i = 0;

  do{
    chr1 = input.charCodeAt(i++);
    chr2 = input.charCodeAt(i++);
    chr3 = input.charCodeAt(i++);

    enc1 = chr1 >> 2;
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    enc4 = chr3 & 63;

    if(isNaN(chr2)) enc3 = enc4 = 64;
    else if(isNaN(chr3)) enc4 = 64;

    output = output+keyStr.charAt(enc1)+keyStr.charAt(enc2)+keyStr.charAt(enc3)+keyStr.charAt(enc4);
    chr1 = chr2 = chr3 = "";
    enc1 = enc2 = enc3 = enc4 = "";
  } while(i < input.length);

  return output;
}

function decode64(input){
  if(input.substring(0, 8)!='|BASE64|') return input;
  else input = input.substring(8);
  
  var output = "";
  var chr1, chr2, chr3 = "";
  var enc1, enc2, enc3, enc4 = "";
  var i = 0;

  // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  // var base64test = /[^A-Za-z0-9\+\/\=]/g;
  // if (base64test.exec(input)) {
  //    alert("There were invalid base64 characters in the input text.\n" +
  //          "Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
  //         "Expect errors in decoding.");
  // }
  // input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

  do{
    enc1 = keyStr.indexOf(input.charAt(i++));
    enc2 = keyStr.indexOf(input.charAt(i++));
    enc3 = keyStr.indexOf(input.charAt(i++));
    enc4 = keyStr.indexOf(input.charAt(i++));

    chr1 = (enc1 << 2) | (enc2 >> 4);
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    chr3 = ((enc3 & 3) << 6) | enc4;

    output = output + String.fromCharCode(chr1);

    if(enc3 != 64) output = output + String.fromCharCode(chr2);
    if(enc4 != 64) output = output + String.fromCharCode(chr3);

    chr1 = chr2 = chr3 = "";
    enc1 = enc2 = enc3 = enc4 = "";
  } while(i < input.length);
  return output;
}


function checkInitBanner(){
	
	var date = new Date();
	//if(date!=good) return;
	var banner = getElementsByClass('logo-banner');
	if(!banner) return;
	
	
	var id = 1;
	
	var arr = new Array();
	arr.push('<div class="spc-banner spc-dots" style="text-align: right; background: url(/fileadmin/templates/img/banners/stippen/stip1.png) no-repeat center top;"></div>');
	
	arr.push('<div class="spc-banner" style="text-align: right; background: url(/fileadmin/templates/img/banners/winter/photogifts_winter_begin.png) no-repeat center top;"></div>');
	arr.push('<div class="spc-banner" style="text-align: right; background: url(/fileadmin/templates/img/banners/winter/photogifts_winter_gebalu40x60.png) no-repeat center top;"></div>');
	arr.push('<div class="spc-banner" style="text-align: right; background: url(/fileadmin/templates/img/banners/winter/photogifts_winter_gebalu50x70.png) no-repeat center top;"></div>');
	arr.push('<div class="spc-banner" style="text-align: right; background: url(/fileadmin/templates/img/banners/winter/photogifts_winter_gebalu90x60.png) no-repeat center top;"></div>');
	arr.push('<div class="spc-banner" style="text-align: right; background: url(/fileadmin/templates/img/banners/winter/photogifts_winter_alu80x80.png) no-repeat center top;"></div>');
	arr.push('<div class="spc-banner" style="text-align: right; background: url(/fileadmin/templates/img/banners/winter/photogifts_winter_alu100x80.png) no-repeat center top;"></div>');
	
	banner.innerHTML = arr.join('');
	banner.style.height = '175px';
	banner.style.overflow = 'hidden';  
	banner.style.background = 'url(/fileadmin/templates/img/banners/winter/photogifts_winter_achtergrond.png) no-repeat center top';
	
	bannerModule = new BannerModule(banner);
	bannerModule.init();
	setInterval('bannerModule.run();', 30);
	
}

function BannerModuleItem(indx, elem){
	this.item     = elem;
	this.index    = indx;
	
	this.run = function(time, indx){
		this.item.style.left = '0px';
		
		
		if(indx<0)         this.item.style.top  = '-175px';
		else if(indx>1)	   this.item.style.top  = '175px';
		else if(indx<0.125){
			indx = 1.0-Math.pow(indx*8.0, 0.5);
			this.item.style.top = '-'+(indx*175)+'px';
		}
		else if(indx>0.875){
			indx = Math.pow((indx-0.875)*8.0, 2);
			this.item.style.top = ''+(indx*175)+'px';
		}
	}
}

function BannerModule(ban){
	this.runner   = 0;
	this.module   = ban;
	this.list     = new Array();
	this.lasttime = (new Date()).getTime();
	this.dots     = null;
	
	this.init = function(){
		var tlist = this.module.getElementsByTagName('*');
		
		for(var i=0; i<tlist.length; ++i){
			if(tlist[i].parentNode!=this.module) continue;
			if(hasClass(tlist[i], 'spc-dots')) this.dots = tlist[i];
		  else this.list.push(new BannerModuleItem(this.list.length, tlist[i])); 
		}
		
	}
	this.run = function(){
		var now  = (new Date()).getTime();
		var past = now-this.lasttime;
		this.lasttime = now;
		
		past = past/1000.0;
		
		this.runner += past/5.0;
		if(this.runner > this.list.length) this.runner -= this.list.length;
		
		for(var i=0; i<this.list.length; ++i){
			var item = this.list[i];
			var offs = this.runner - i;
			item.run(past, offs);
		}
		
		if(this.dots) this.dots.style.background = 'url(/fileadmin/templates/img/banners/stippen/stip'+Math.floor(this.runner+1)+'.png) no-repeat center top';
	}
}

var bannerModule = null;

addLoadEvent(checkInitBanner);
