/**
 * Class contructor
 * @author Michael'd0dge'Fiedler
 * @param String source
 * @param String formId
 * @param int objId
 */
function QSearchAction(source, formId, objId)
{
	// major variables
	this.source		= source;
	this.formId		= formId;
	this.divId		= null;
	this.fObj		= document.getElementById(formId);
	this.t			= this.fObj.elements["topic"];
	this.domain		= null;
	this.lang		= null;
	this.sTerm		= "";
	this.sTopic		= 0;
	this.focusOn	= 1;
	this.sLimit		= null;
	this.objId		= objId;
	this.objRef		= null;
	this.isAutoComplete = true;
	
	switch(formId){
		case "qSearchForm"+objId:
		{
			this.qField	= document.getElementById("qsearch"+objId);
			this.currUri= null;
			// method declarations
			this.sendRequest 		= _sendRequest;
			//this.commitByEnter		= _enterPressedQ;
			this.getTopic			= _getTopic;
			this.bufferSearchTerm	= _buffTerm;
			this.catchSelection		= _parseSelectionEvent;
			// setup key listener
			this.qField.onKeyUp		= _enterPressedQ;
		}
		break;
	}
}
/**
 * @return void
 */
function _parseSelectionEvent()
{
	var tmp = this.qField.value;
	if(this.sTopic > 0){
		tmp = tmp.replace(/(<.+?>)/gi, "");
		tmp = tmp.split(") ");
		this.focusOn = parseInt(tmp[0]);
		this.qField.value = this.sTerm;
	}
	else{
		var catName = "";
		tmp = tmp.split("(ID");
		catName	= tmp[0];
		catName = catName.replace(/(<.+?>)/gi, "");
		tmp 	= tmp[1].split(":");
		catId 	= parseInt(tmp[0].substring(1,tmp[0].length));
		this.qField.value = catId;
		this.sTerm = catId;
	}
	this.sendRequest();
		
}
/**
 * @return void
 */
function _getTopic()
{
	//$("#qsearch").unautocomplete();
	if(this.t.length > 1){
		for(i=0;i<this.t.length;i++){
			if(this.t[i].checked == true){
				if(this.isAutoComplete){
					$("#qsearch"+this.objId).setOptions(
						{
							width:300,
							max:30,
							selectFirst:true,
							extraParams:{topic:this.t[i].value,qsearch_obj:this.objId}
						}
					);
				}
				this.sTopic = parseInt(this.t[i].value);
			}
		}
	}
	else{
		if(this.isAutoComplete){
			$("#qsearch"+this.objId).setOptions(
				{
					width:300,
					max:30,
					selectFirst:true,
					extraParams:{topic:this.t.value,qsearch_obj:this.objId}
				}
			);
		}
		this.sTopic = this.t.value;
	}
	
}
/**
 * @return void
 */
function _buffTerm()
{
	if(this.isAutoComplete)
		$("#qsearch"+this.objId).flushCache();
	
	this.sTerm = this.qField.value;
	//tmp = parseInt(this.sTerm);
	var tmp = this.sTerm;
	if(!isNaN(tmp) && tmp.length > 0){
		//console.log("TERM: "+tmp+", length: "+tmp.length);
		// if it is a numeric value
		if(this.t.length > 1 && this.t[1].checked==true)
			this.t[0].checked = true;
		else if(this.t.value > 0)
			this.t.value = 0;
		this.sTopic = 0;
	}
	else{
		// if it is a string
		/*
		if(this.t.length > 1 && this.t[0].checked==true){
			this.t[1].checked = true;
			this.sTopic = 1;
		}
		else if(this.t.value == 0){ 
			this.t.value = 1;
			this.sTopic = 1;
		}
		else{
			

		}
		*/
		//console.log("TOPIC: "+this.t.value);
		this.sTopic = this.t.value;
	}
	if(this.isAutoComplete)
		$("#qsearch"+this.objId).setOptions({extraParams:{topic:this.sTopic,qsearch_obj:this.objId}});
}

//******************************************************************************
//*                                                                            *
//*     QUICK CATEGORY SEARCH METHODS                                          *
//*                                                                            *
//******************************************************************************
/**
 * @return void
 */
function _sendRequest()
{
	if(isNaN(this.sTerm)){
		// it's a bloody string ... DARN!!
		var tmp = this.qField.value;
		this.sTopic = parseInt(this.sTopic);
		switch(this.sTopic){
			// category
			case 0:{
				if(tmp.length >= 2)
					document.location.href="index.php?qsearch="+
					catName+"&topic="+this.sTopic;
				else
					document.location.href="index.php?qsearch="+
					this.qField.value+"&topic="+this.sTopic;
			}
			break;
			// content
			case 5:
			case 1:{
				if(isNaN(this.focusOn)) this.focusOn = 0;
				var urlRewrite = 
					this.domain+this.lang+"/search/"+this.sTerm+"/"+
					this.sTopic+"/"+this.focusOn+"/0/"+this.sLimit+"/";
				document.location.href= urlRewrite;
				/*
					"index.php?qsearch="+this.sTerm+"&"+
					"topic="+this.sTopic+"&"+
					"focusOn="+this.focusOn;
				*/
			}
			break;
		}
	}
	else{
		// if a number is detected automatically
		// the category procedure jumps in effect
		// easy...got an ID
		if(this.sTerm != '')
			document.location.href=this.domain+this.lang+"/site__"+this.sTerm+"/";
	}
}
