/*


	User.js
	
	Loaded in the top frameset only.
	
	Javascript synchronisation between frames regarding the session
	happens here. Session is a global singleton. It defines a library
	that subframes call for all userrelated stuff, like
	
		<a onclick="top.User.clearCollections()">clear</a>
		
*/

User = {
	tron				: (document.location.href.indexOf("tron")!=-1),
	collections			: "",
	collectionstimer	: null,
	search				: new Array(),
	searchtimer			: null,
	
	trace				: USTrace,
	setCollections		: USSetCollections,
	throttleCollections	: USThrottleCollections,
	setSearch			: USSetSearch,
	throttleSearch		: USThrottleSearch
}


function USTrace(msg) {
	if (this.tron) alert(msg);
}
// note setcollections does not store the users phpsession - 
// it should be called after the users phpsession was 
// stored or retrieved, eg from the hidden frame 

function USSetCollections(collectionstr) {
	this.trace("USSetCollections "+collectionstr);
	clearTimeout(this.collectionstimer);
	this.collectionstimer=setTimeout("top.User.throttleCollections('"+collectionstr+"')",2000);
}

function USThrottleCollections(collectionstr) {
	this.trace("USThrottleCollections ");
	
	clearTimeout(this.collectionstimer);
	if (this.collections != collectionstr) {
		this.trace("USThrottleCollections - changed.");
		this.collections = collectionstr;
		for (idx=0; idx<top.frames.length; idx++) {
			if (top.frames[idx].onChangeCollections) {
				top.frames[idx].onChangeCollections(collectionstr);
			}
		} 
	} else this.trace("USThrottleCollections - unchanged.");
}

function USSetSearch(form) {
	this.trace("USSetSearch "+form.name);
	clearTimeout(this.searchtimer);
	this.searchtimer=setTimeout("top.User.throttleSearch('"+form.name+"')",2000);
}

function USThrottleSearch(formname) {
	this.trace("USThrottleSearch ");
	
	clearTimeout(this.collectionstimer);
	var changed=false;
	var form=jQuery('form[name='+formname+']',top.nav.document);
	var checkboxes = form.find('input:checkbox');
	checkboxes.each(function () {
		if (top.User.search[this.name]=this.checked) {
			top.User.search[this.name]=this.checked;
			changed=true;
		}
	});
	var textfields=form.find('input[type=text]');
	textfields.each(function () {
		if (top.User.search[this.name]=this.value) {
			top.User.search[this.name]=this.value;
			changed=true;
		}
	});
	if (changed) {
		this.trace("USThrottleSearch - changed.");
		for (idx=0; idx<top.frames.length; idx++) {
			if (top.frames[idx].onChangeSearch) {
				top.frames[idx].onChangeSearch();
			}
		} 
	} else this.trace("USThrottleSearch - unchanged.");
}



