/*
This library allows you to refresh content through ajax calls
in various ways.
It depends on prototypejs library, and should be portable to all major browsers (FF,IE,SF).
Usage instructions:
pass an associative array to the function freshcontent() as the page loads. The following are mandatory fields in the array
 obj: The target object id which will be updated, the content of the DOM object will be erased, and updated content will be pushed into it instead.
Now either
 url: URL to fetch the new content for the new div from
Or
 content: Actual string of HTML to go into the obj object. You may add scripts to the string.
The following are optional
 waitdom: true or false. Whether or not we should wait the entire DOM to load before commencing with update.
 timeout: wait X miliseconds before doing anything.
 condition: will do nothing if condition:false. This is used to be able to use more styled code. Instead of if (X) freshcontent we'll write freshcontent({condition:X})

usage example 

<div style="border: 1px solid blue" id="fc1">
  Hello World - changed by Freshcontent({obj:'fc',content='blaa')
</div>
<script type="text/javascript">
  FreshContent({obj:'fc1',condition:true,url:'try.php',timeout:3000,refresh:1000,waitdom:true,content:'blaa'});
</script>
*/

function setAjaxContent(optn) {
  	/*This functions sets the actual url output to the object*/
	window.setTimeout(function() {
		var ajax = new Ajax.Updater({success:optn.obj},optn.url,{evalScripts:true,method: 'get'});
	},optn.timeout);
}

function setAjaxPeriodicContent(optn) {
  	/*This functions sets the actual url output to the object*/
	if (optn.refresh >= 1000) optn.refresh = (optn.refresh/1000).toFixed(0);
	//var ajax = new Ajax.PeriodicalUpdater({success:optn.obj},optn.url,{method: 'get',frequency:optn.refresh});
	window.period_ar.push({freq:optn.refresh,obj:optn.obj,url:optn.url});
}

function periodCheck() {
	window.secs += 1;
	window.secs = window.secs % (60*100);
	window.period_ar.each(function (a,i) {
		if (window.secs % a.freq == 0) {
			if (a.content && a.content!='')
				$(a.obj).update(a.content);
			else
				var ajax = new Ajax.Updater({success:a.obj},a.url,{evalScripts:true,method: 'get'});
		}
	});
}

function setStaticContent(optn) {
	if (optn.refresh >= 1000) optn.refresh = (optn.refresh/1000).toFixed(0);
	if (optn.refresh != 0) {
		window.period_ar.push({obj:optn.obj,freq:optn.refresh,content:optn.content});
		return;
	}
	window.setTimeout(function() {
		$(optn.obj).update(optn.content);
	},optn.timeout);
}

function setContent(optn) {
	if (!optn.refresh) optn.refresh = 0;
	if (!optn.timeout) optn.timeout = 0;
	if (!optn.url || optn.url=='') return setStaticContent(optn);
	if (optn.refresh != 0) return setAjaxPeriodicContent(optn);
	return setAjaxContent(optn);
}

function setAtDomLoad(optn) {
	if (!optn.timeout) optn.timeout = 0;
	Event.observe(window,'load',function() {window.setTimeout(setContent.bind(this,optn),optn.timeout)} );
}

function FreshContent(optn) {
  if (!optn || (optn.condition && optn.condition==false) || !optn.obj) return;
	if (optn.waitdom) return setAtDomLoad(optn);
	setContent(optn);
}

var pexi = new PeriodicalExecuter(periodCheck,1);
window.period_ar = [];
window.secs = 0;