/*
url-loading object and a request queue built on top of it
*/

/* Constructor */
var debug=false;
function netObject(debug) {
	//alert("Constructing net Object");
	if (debug == null || debug == undefined) debug = false;
	this.debug = debug;
	//alert("debug = "+debug);
}

/* namespacing object */
var net=new netObject();	//netObject(true) if you want to debug

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;

var ignoreList=['getMessage', 'keepAlive', 'view record&component=PlatonAction', 'countUnreadMessages', 'getRecentActions'];

/*--- content loader object for cross-browser requests ---*/

net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.isBusy=false;
  if(ignoreReq(params)){
	makeBusy();
	this.isBusy=true;
  }
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  contentType='application/x-www-form-urlencoded;charset=UTF-8';
  this.loadXMLDoc(url,method,params,contentType);
}
net.send=function(url,onload,onerror,method,params,contentType){
  this.isBusy=false;
  if(ignoreReq(params)){
	makeBusy();
	this.isBusy=true;
  }
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  contentType='text/xml;charset=UTF-8';
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded;charset=UTF-8';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      //alert(contentType);
      this.req.send(params);
      //setTimeout(killGetMessage,300000);
    }catch (err){
		if(this.isBusy){
			makeLazy();
		}
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
		if(this.isBusy){
			makeLazy();
		}
      this.onload.call(this);
    }else{
		if(this.isBusy){
			makeLazy();
		}
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){
  if (debug == true) {
	  alert(getMessage("error fetching data")+"!"
		+"\n\n"+getMessage("readyState")+":"+this.req.readyState
		+"\n"+getMessage("status")+": "+this.req.status
		+"\n"+getMessage("headers")+": "+this.req.getAllResponseHeaders());
  }
  if(this.isBusy){
		makeLazy();
	}
}

net.send.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded;charset=UTF-8';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
      //setTimeout(killGetMessage,300000);
    }catch (err){
		if(this.isBusy){
			makeLazy();
		}
      this.onerror.call(this);
    }
  }
}

net.send.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
		if(this.isBusy){
			makeLazy();
		}
	  this.onload.call(this);
    }else{
		if(this.isBusy){
			makeLazy();
		}
      this.onerror.call(this);
    }
  }
}

net.send.prototype.defaultError=function(){
  if (debug == true) {
	  alert(getMessage("error fetching data")+"!"
		+"\n\n"+getMessage("readyState")+":"+this.req.readyState
		+"\n"+getMessage("status")+": "+this.req.status
		+"\n"+getMessage("headers")+": "+this.req.getAllResponseHeaders());
	}
	if(this.isBusy){
			makeLazy();
		}
}

net.ContentLoaderThis=function(url,onload,onerror,obj_reference,obj_reference2,method,params,contentType){
  this.isBusy=false;
  if(ignoreReq(params)){
	makeBusy();
	this.isBusy=true;
  }
  this.obj_reference=obj_reference;
  this.obj_reference2=obj_reference2;
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  contentType='application/x-www-form-urlencoded;charset=UTF-8';
  this.loadXMLDoc(url,method,params,contentType);
}
net.sendThis=function(url,onload,onerror,obj_reference,obj_reference2,method,params,contentType){
  this.isBusy=false;
  if(ignoreReq(params)){
	makeBusy();
	this.isBusy=true;
  }
  this.obj_reference=obj_reference;
  this.obj_reference2=obj_reference2;
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  contentType='text/xml;charset=UTF-8';
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoaderThis.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded;charset=UTF-8';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoaderThis.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      //alert(contentType);
      this.req.send(params);
      //setTimeout(killGetMessage,300000);
    }catch (err){
		if(this.isBusy){
			makeLazy();
		}
      this.onerror.call(this);
    }
  }
}

net.ContentLoaderThis.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
		if(this.isBusy){
			makeLazy();
		}
      this.onload.call(this);
    }else{
		if(this.isBusy){
			makeLazy();
		}
      this.onerror.call(this);
    }
  }
}

net.ContentLoaderThis.prototype.defaultError=function(){
  if (debug == true) {
	  alert(getMessage("error fetching data")+"!"
		+"\n\n"+getMessage("readyState")+":"+this.req.readyState
		+"\n"+getMessage("status")+": "+this.req.status
		+"\n"+getMessage("headers")+": "+this.req.getAllResponseHeaders());
  }
  if(this.isBusy){
			makeLazy();
		}
}

net.sendThis.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded;charset=UTF-8';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoaderThis.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
      //setTimeout(killGetMessage,300000);
    }catch (err){
		if(this.isBusy){
			makeLazy();
		}
      this.onerror.call(this);
    }
  }
}

net.sendThis.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
		if(this.isBusy){
			makeLazy();
		}
	  this.onload.call(this);
    }else{
		if(this.isBusy){
			makeLazy();
		}
      this.onerror.call(this);
    }
  }
}

net.sendThis.prototype.defaultError=function(){
  if (debug == true) {
	  alert(getMessage("error fetching data")+"!"
		+"\n\n"+getMessage("readyState")+":"+this.req.readyState
		+"\n"+getMessage("status")+": "+this.req.status
		+"\n"+getMessage("headers")+": "+this.req.getAllResponseHeaders());
  }
  if(this.isBusy){
			makeLazy();
		}
}

function makeBusy(){
	if(jQuery('#noclickLayer').length==0){
		jQuery('body').append("<div id='noclickLayer' style='filter: alpha(opacity=0); zoom:1; opacity: 0; -moz-opacity: 0; display:block; cursor:wait; position:absolute; top:"+jQuery(document).scrollTop()+"px; left:0; width:100%; height:100%; z-index:100000;'></div>");
		jQuery('#noclickLayer').css('background-color', '#FFF');
		jQuery(document).bind('scroll', function(){
			jQuery('#noclickLayer').css('top', jQuery(document).scrollTop());
		});
	}
}

function makeLazy(){
	jQuery('#noclickLayer').remove();
}

//dokimastiko bazei to loading se kapoio id
function makeBusyElement(elementId){
	var elem=jQuery('#'+elementId);
	elem.css('position', 'relative');
	var height=elem.height();
	if(jQuery('#'+elementId+'_noclickLayer').length==0){
		elem.append("<div id="+elementId+"_noclickLayer' style='filter: alpha(opacity=0); zoom:1; display:block; cursor:wait; position:absolute; top:"+jQuery(document).scrollTop()+"px; left:0; width:100%; height:"+height+"px; z-index:100000;'></div>");
		jQuery('#'+elementId+'_noclickLayer').css('background-color', '#000');
	}
}

function makeLazyElement(elemetId){
	jQuery('#'+elementId+'_noclickLayer').remove();
}

function ignoreReq(params){
	var l=ignoreList.length;
	for(var i=0; i<l; i++){
		if(params.indexOf(ignoreList[i])>=0){
			return false;
		}
	}
	return true;
}
scriptLoaded();

