// =================================================================================================
// docsearch.js - AJAX live search of documents with multiple search conditions
//
// Author: Mark Stickley, Evolving Media ©2007
// =================================================================================================

Event.observe(window, 'load', initialise, false);

var conditioncounter; //this keeps a track of the number of conditions. If conditions are removed, they just disappear but the counter remains the same.

function initialise(){
	//$('searchconditions').innerHTML = "";
	conditioncounter = $('conditioncounter').value;
	//addSearchCondition();
	updateResults();
	sortButtons();
}

function addSearchCondition(skiptransition){
	var newdiv = document.createElement('div');//'<div class="searchcondition" id="condition'+conditioncounter+'"></div>';
	newdiv.setAttribute('class', 'searchcondition');
	newdiv.setAttribute('id', 'conditionholder'+conditioncounter);
	$('searchconditions').appendChild(newdiv);
	$('conditionholder'+conditioncounter).hide();
	var myajax = new Ajax.Updater('conditionholder'+conditioncounter, '/home/guidance-library/searchelement', {method: 'get', parameters: 'element=searchcondition&conditioncounter='+conditioncounter+"&_frameset=false", asynchronous: false});
	Effect.SlideDown('conditionholder'+conditioncounter, {duration: 0.5});
	
	conditioncounter++;
	$('conditioncounter').value = conditioncounter;
	
	sortButtons();
	
	return false;
}

function removeSearchCondition(scid){ //that's Search Condition ID
	scid = 'conditionholder'+scid;
	Effect.BlindUp(scid, { 
		duration: 0.5,
		afterFinish: function(){ 
		  	Element.remove(scid); 
		  	updateResults(); 
	  		sortButtons();
		}
	});
	return false;
}

function sortButtons(){
	// the add / remove buttons are hidden by default in case the user doesn't have JS. 
	// in the case of the + button it will hide all but the last one.
	adds = $$('.docsearchadd');
	adds.each(Element.hide);
	Element.show(adds[adds.length-1]);
	
	//show all the minus buttons, unless there's only 1 element left in which case hide it.
	if($$('.docsearchminus').length == 1){
		$$('.docsearchminus').each(Element.hide);
	}
	else{
		$$('.docsearchminus').each(Element.show);
	}
	
	//hide the ugly submit button!
//	Element.hide($('docsearch_submit'));
}

function changedElement(num){
	// This is basically to check if the element dropdown is on file type ror not. If it is, it has
	// to change the value field into a select object.
	myelement = $('element'+num);
	if(myelement[myelement.selectedIndex].innerHTML=="Type"){
		myrel = $('relationship'+num);
		myoptions = myrel.getElementsByTagName('option');
		for(var i=0;i<myoptions.length;i++){
			if(myoptions[i].innerHTML!='Is' && myoptions[i].innerHTML!='Is not'){
				Element.remove(myoptions[i]);
			}
		}
	
		var newselect = document.createElement('select');
		newselect.setAttribute('class', 'docsearchvalue');
		newselect.setAttribute('className', 'docsearchvalue'); //for IE who is being awkward.
		newselect.setAttribute('name', 'value'+num);
		newselect.setAttribute('id', 'value'+num);
		newselect.setAttribute('onchange', 'updateResults();');
		newselect.onchange = updateResults; //for IE who is being awkward.
		
		myajax = new Ajax.Request('/home/guidance-library/searchelement', {
			asynchronous: false,
			parameters: 'element=filetypes'+"&_frameset=false",
			onSuccess: function(transport){
				//we have to loop through and parse the options
				var result = transport.responseText;
				var lines = new Array();
				lines = result.split("\n");

				for(i=0;i<lines.length;i++){
					if(lines[i].length){
						var optionvalue = lines[i].match(/value=".*"/)[0];
						optionvalue = optionvalue.substr(7, optionvalue.length-8);
						var optiontext = lines[i].match(/>.*</)[0];
						optiontext = optiontext.substr(1, optiontext.length-2);
						newselect.options[i] = new Option(optiontext, optionvalue); 
					}
				}
				myelement.parentNode.replaceChild(newselect, $('value'+num));
			}
		});
	}
	else{
		//check if the value is a select or not and if it is, swap it back out.
		myvalue = $('value'+num);
		if(myvalue.nodeName=='SELECT'){
			//put those options back in the relationship dropdown!
			myrel = $('relationship'+num);
			myoptions = myrel.getElementsByTagName('option');
			for(var i=0;i<myoptions.length;i++){
				Element.remove(myoptions[i]); //remove them all so we can start over.
			}
			op1 = new Option('Contains', '0');
			op2 = new Option('Is', '1');
			op3 = new Option('Does not contain', '2');
			op4 = new Option('Is not', '3');
			myrel.options[0] = op1;
			myrel.options[1] = op2;
			myrel.options[2] = op3;
			myrel.options[3] = op4;
			
			newinput = document.createElement('input');
			newinput.setAttribute('class', 'docsearchvalue');
			newinput.setAttribute('className', 'docsearchvalue'); //for IE who is being awkward.
			newinput.setAttribute('id', 'value'+num);
			newinput.setAttribute('name', 'value'+num);
			newinput.setAttribute('onkeyup', 'updateResults();');
			newinput.onkeyup = updateResults; //for IE who is being awkward.
			myvalue.parentNode.replaceChild(newinput, $('value'+num));
		}
	}
}

function updateResults(){
	//first, compile the parameters. This is the difficult bit.
	var params = '';

	for(i=0;i<conditioncounter;i++){
		var condition = $('condition'+i);
		if(condition && $F('value'+i) != ""){
			if(params!="") params = params+"&";
			params = params+"element"+i+"="+$F('element'+i)+"&relationship"+i+"="+$F('relationship'+i)+"&value"+i+"="+$F('value'+i);
		}
	}
	
	params = 'conditioncounter='+(conditioncounter-1)+'&url='+$F('url')+'&orderby='+$F('orderby')+'&sort='+$F('sort')+'&'+params;

	updateNumResults(params);
	
	//now update the results table
	var myajax = new Ajax.Updater('filetable', '/home/guidance-library/searchelement', {
		method: 'get', 
		parameters: 'element=results&'+params+"&_frameset=false",
		asynchronous: true, 
		onSuccess: function(){ 
			sortables_init(); 
		} 
	});
}

function updateNumResults(params){
	var myajax = new Ajax.Updater('numresults', '/home/guidance-library/searchelement', {
		method: 'get', 
		parameters: 'element=numresults&'+params+"&_frameset=false", 
		asynchronous: true
	});
}