PublicationsBrowser = function()
{
	
	var tA = $('filterset').getElementsByClassName('toggleitem');
	for(var i=0;i<tA.length;i++){
		var elem = tA[i];
		elem.onclick = function()
		{
			PubsBrowser.toggle(this);
			return false;
		}
	}
	
	//$('viewall').onclick = function()
	//{
	//	PubsBrowser.viewAll();
	//}
	$('search').onkeydown = function()
	{
		PubsBrowser.liveSearch();
	}
	
	$('reset').onclick = function()
	{
		PubsBrowser.reset();
	}
	
	$('toggle_descriptions').onclick = function()
	{
		PubsBrowser.toggleDescriptions();
	}
	
	this.showDescriptions = true;
	
	//loop through stuff
	this.dataA = new Array();
	
	this.transferInterval = undefined;
	
	var myGlobalHandlers = {
		onCreate: function(){
			$('loading').show();
		},
		onComplete: function() {
			if(Ajax.activeRequestCount == 0){
				$('loading').hide();
			}
		}
	};
	
	Ajax.Responders.register(myGlobalHandlers);
		
}

PublicationsBrowser.prototype.liveSearch = function()
{
	clearInterval(this.transferInterval);
	this.transferInterval = setInterval(this.transfer.bind(this),500);	
}

PublicationsBrowser.prototype.reset = function()
{
	
	$('search').value = '';
	
	var tA = $('filterset').getElementsByClassName('toggleitem');
	for(var i=0;i<tA.length;i++){
		var elem = tA[i];
		elem.removeClassName('checked');
	}
	
	this.showDescriptions = true;
	$('toggle_descriptions').update('Hide Descriptions');
	
	this.dataA = new Array();
	this.transfer();
}

PublicationsBrowser.prototype.viewAll = function()
{
	this.dataA = new Array();
	this.transfer();
}


PublicationsBrowser.prototype.toggleDescriptions = function()
{
	this.showDescriptions = !this.showDescriptions;
	if(this.showDescriptions == true){
		$('toggle_descriptions').update('Hide Descriptions');
	}else{
		$('toggle_descriptions').update('Show Descriptions');
	}
	this.pumpDescriptions();
}

PublicationsBrowser.prototype.pumpDescriptions = function()
{
	var tA = $('results').getElementsByClassName('toggle');
	for(var i=0;i<tA.length;i++){
		var elem = tA[i];
		if(this.showDescriptions == true){
			elem.setStyle(
			{
				'display':'block'				
			});
		}else{
			elem.setStyle(
			{
				'display':'none'				
			});
		
		}
	}
}

PublicationsBrowser.prototype.toggle = function(elem)
{
		
	//if it exists inside the list turn it off, else add it
	var inset = false;
	for(var i=0;i<this.dataA.length;i++){
		if(this.dataA[i] == elem.id){
			
			this.dataA.splice(i,1);
			
			
			elem.removeClassName('checked');
			inset = true;
			break;
		}			
	}
	if(inset == false){
		
		elem.addClassName('checked');
		this.dataA.push(elem.id);
		
	}
	
	//
	clearInterval(this.transferInterval);
	this.transferInterval = setInterval(this.transfer.bind(this),50);
	
}

PublicationsBrowser.prototype.viewDetail = function(item)
{
	alert(item.id);
}

PublicationsBrowser.prototype.transfer = function()
{
	clearInterval(this.transferInterval);
	
	//create ajax transfer love
	var sendVars = new Object();
	sendVars.dataA = this.dataA.join(',');
	sendVars.search = $('search').value;
	sendVars.toggle = this.showDescriptions;
	sendVars.action = 'PublicationsBrowser';
	
	var handler = this.update;
	
	//var tA = $('content').getElementsByClassName('results');
	//var obj = $(tA[0]);
	var obj = $('results');

	var _scope = this;
	
	$('results').hide();
	
	var myAjax = new Ajax.Updater(
		obj,
		'/gateway/ajax', 
		{
			method			: 'post', 
			parameters		: formatPost(sendVars),
			onComplete		: handler.bind(this),
			evalScripts 	: true
		}
	);	
}

PublicationsBrowser.prototype.update = function(obj)
{
	
	//alert(obj.responseText);
	$('results').show();
	
	/*
	//loop through and attach onclicks
	var tA = $('results').getElementsByClassName('publication');
	for(var i=0;i<tA.length;i++){
		var elem = tA[i];
		elem.onclick = function()
		{
			PubsBrowser.viewDetail(this);
			return false;
		}
	}
	*/
	
	
	this.pumpDescriptions();

}

//on load parse and hook up items
