// JavaScript Document

sf_FileLoader.count=0;
sf_FileLoader.arrInst= new Array();
function sf_FileLoader()
{
	this.bustcachevar=0 //bust potential caching of external pages after initial request? (1=yes, 0=no)
//	this.rootdomain="http://"+window.location.hostname;
	this.bustcacheparameter="";
	this.page_request = false;
	
	this.xtimer = null;
	
	this.iIndex = sf_FileLoader.count;
	this.cName = 'sf_FileLoader';
	this.cachObj = null;
	sf_FileLoader.arrInst[sf_FileLoader.count] = this;
	sf_FileLoader.count++;
	this.loadDone = false;
}

sf_FileLoader.prototype.ajaxpage = function (url, containerid, pageRelativePath, cachObj)
{
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		this.page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			this.page_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				this.page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	else
		return false;
	var _index = this.iIndex;
	this.loadDone = false;
	this.page_request.onreadystatechange=function()
	{
		_me = sf_FileLoader.arrInst[_index];
		if (_me.page_request.readyState == 4 && (_me.page_request.status==200 || window.location.href.indexOf("http")==-1))
		{
			_me.loadpage(containerid,pageRelativePath,cachObj);
		}
		else if (_me.page_request.readyState == 4 && (_me.page_request.status==404 || window.location.href.indexOf("http")==-1))
		{
			_me.loadnopage(containerid,cachObj);
		}
	}
	
	if (this.bustcachevar) //if bust caching of external page
		this.bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	
	this.page_request.open('GET', url+this.bustcacheparameter, true)
	this.page_request.send(null)
}

sf_FileLoader.prototype.loadpage = function (containerid,pageRelativePath,cachObj)
{
	if (this.page_request.readyState == 4 && (this.page_request.status==200 || window.location.href.indexOf("http")==-1))
	{
		this.cachObj = cachObj;
		this.xtimer = setInterval("sf_FileLoader.arrInst["+this.iIndex+"].setPage('"+containerid+"','"+pageRelativePath+"')" , 30);
	}
}

sf_FileLoader.prototype.setPage = function(containerid,value)
{
	obj = document.getElementById(containerid);
	if (obj)
	{
		 clearInterval(this.xtimer);
		 val = this.page_request.responseText;
		 val = sf_FileLoader.arrInst[this.iIndex].cleanContent(val,value);
		 obj.innerHTML=val;
		 if (this.cachObj)
		 	this.cachObj.setVal (val);
		this.loadDone = true;
	}
}
sf_FileLoader.prototype.loadnopage = function (containerid)
{
	//this.cachObj = cachObj;
	this.xtimer = setInterval("sf_FileLoader.arrInst["+this.iIndex+"].setNoPage('"+containerid+"')" , 30);
}

sf_FileLoader.prototype.setNoPage = function(containerid)
{
	obj = document.getElementById(containerid);
	if (obj)
	{
		 clearInterval(this.xtimer);
		 obj.innerHTML="<p>content not available</p>";
		this.loadDone = true;
	}
}

sf_FileLoader.prototype.cleanContent = function(content,value)
{
	var rExp = new RegExp('\.\.\/currentSite\/','gim');
	content = content.replace(rExp,value);
	return(content);
}

sf_FileLoader.prototype.getRelPath = function(arrPath)
{
	// Later if we want each SR to inherit from different master projects, the array 'arrPath' will be a 3-dimensions array having the master project's id as index.
	// We will need to test first the master project id used by this SR and then the pattern.
	var currentPath = window.location.href;
	var defaultIndex = 0;
	var i = 0;
	while(i<arrPath.length)
	{
		if(currentPath.indexOf(arrPath[i]['pattern'])>=0 && arrPath[i]['pattern']!='default')
			break;
		else if(arrPath[i]['pattern']=='default')
			defaultIndex = i; 
		i++;
	}
	if(i==arrPath.length)
		i=defaultIndex;

	return(arrPath[i]['relPath']);
}