
/*
  ZWF Basic Insert Partial Content Utility System
  (c) 2010 - Jelmer van der Meer - ZWF Ontwerp
*/
var zwfb_bipcus = null;

function ZWFB_BipcusClient(comp, url){
  this.component = comp;
  this.remoteUrl = url;
  this.status    = 0;
  
  this.setStatus    = function(state){this.status = state;}
  this.isWaiting    = function(){return this.status==0;}
  this.isProcessing = function(){return this.status==1;}
  this.isDone       = function(){return this.status==2;}
  this.setContent   = function(content){
  	if(this.component=='reload'){
  		window.location.href = window.location.href;
  		return;
  	}
  	
  	var elem = document.getElementById(this.component);
  	if(elem==null) return;
  	
  	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);
	    }
  	}
  	if(temp[2]!=''){
      var ins = document.createElement('script');
      ins.setAttribute('type', 'text/javascript');
      ins.text = temp[2];
      document.getElementsByTagName('head')[0].appendChild(ins);
  	}
  	elem.innerHTML = temp[0];
  }
  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;
  	    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_BipcusLoader(comp){
  this.counter        = 0;
  this.component  = comp;
  this.client           = null;
  this.target          = '';
  this.state           = false;
  
  this.isFree      = function(){return !this.state;}
  this.isWaiting  = function(){return this.state;}
  this.loadClient = function(cl){
  	if(!cl.isWaiting()) return;
  	this.state = true;
  	cl.setStatus(1);
  	this.target = cl.component;
  	this.client   = cl;
  	var elem = document.getElementById(this.component);
  	if(elem==null){
      var ins = document.createElement('iframe');
      ins.setAttribute('style', 'position: fixed; right: 5px; top: 5px; width: 200px; height: 200px; display: none;');
      ins.setAttribute('id', this.component);

      if (navigator.userAgent.indexOf("MSIE") > -1 && !window.opera){
      	ins.name = this.component;
        ins.onreadystatechange = function(){
          if(ins.readyState == "complete"){
          	//alert('hi 1');
          	var k = this;
          	zwfb_bipcus.loaded(this.name);
          }
        }
      }
      else{
        ins.setAttribute('onload', 'zwfb_bipcus.loaded("'+this.component+'");');
      }
      ins.setAttribute('src', cl.remoteUrl);      

      document.getElementsByTagName('body')[0].appendChild(ins);
  	}
  	else elem.src = cl.remoteUrl;
  }
  this.loaded      = function(){
  	if(this.isFree()) return;
    var elem2 = window.document.getElementById(this.component);
  	if(this.client!=null){
  	  this.client.setStatus(2);
  	  var text = '';
  	  if(elem2.contentDocument != undefined){
  	    if(elem2.contentDocument.body.innerText == undefined) text = elem2.contentDocument.body.textContent;
  	    else text = elem2.contentDocument.body.innerText;
  	  }
  	  else if(elem2.contentWindow != undefined){
  	    if(elem2.contentWindow.document.body.innerText == undefined) text = elem2.contentWindow.document.body.textContent;
  	    else text = elem2.contentWindow.document.body.innerText;
  	  }
  	  else{
  	    if(elem2.document.body.innerText == undefined) text = elem2.document.body.textContent;
  	    else text = elem2.document.body.innerText;
  	  }
      this.client.setContent(text);
    }
    else{
  	  var elem = document.getElementById(this.target);
  	  if(elem==null) return;    	
      elem.innerHTML = elem2.contentDocument.body.innerHTML;
    }
    this.state = false;
  }
}

function ZWFB_BipcusClientManager(){
	this.csslinks = new Array(); 
  this.loaders  = 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.addActionClient = function(url){
    var newclient = new ZWFB_BipcusClient('reload', url);
    for(var i=0; i<this.clients.length; ++i){
      if(this.clients[i]==null || this.clients[i].isDone()){
        this.clients[i] = newclient;
        return true;
      }
    }
    this.clients[this.clients.length] = newclient;
    return true;  	
  }
  this.addClient = function(comp_id, url){
    var newclient = new ZWFB_BipcusClient(comp_id, url);
    for(var i=0; i<this.clients.length; ++i){
      if(this.clients[i]==null || this.clients[i].isDone()){
        this.clients[i] = newclient;
        return true;
      }
    }
    this.clients[this.clients.length] = newclient;
    return true;
  }
  this.addLoader = function(comp_id){
    this.loaders[this.loaders.length] = new ZWFB_BipcusLoader(comp_id);
  }
  this.run = function(){
  	this.counter ++;
  	for(var i=0; i<this.loaders.length; ++i){
  	  if(this.loaders[i].isWaiting()) continue;
  	  for(var j=0; j<this.clients.length; ++j){
  	    if(this.clients[j].isWaiting()){
  	      this.loaders[i].loadClient(this.clients[j]);
  	      break;
  	    }
  	  }
  	}
  }
  this.loaded = function(comp_id){
    for(var i=0; i<this.loaders.length; ++i){
      if(comp_id!=this.loaders[i].component) continue;
      this.loaders[i].loaded();
    }
  }
}

function short_zwfb(comp){
  zwfb_bipcus.loaded(comp);
}

function zwfb_runloop(){
  zwfb_bipcus.run();
  setTimeout('zwfb_runloop()', 100);
}

zwfb_bipcus = new ZWFB_BipcusClientManager();
zwfb_bipcus.addLoader('loader1');

setTimeout('zwfb_runloop()', 1000);


