var thisPage={};
thisPage.catID = false;
thisPage.areaID = false;
thisPage.areaList = false;
thisPage.catList = false;
thisPage.searchStatus = false;
thisPage.setCatID=function(catID){
	this.catID=catID;
	jQuery(document).trigger('close.facebox')
};
thisPage.setAreaID=function(areaID){
	this.areaID=areaID;
	jQuery(document).trigger('close.facebox')
};
thisPage.showDialogAreasNone=function(){
	this.showDialog('No Areas Found','Sorry, no areas were found that match your search.  Please try again');
};
thisPage.showDialogCatsNone=function(){
	this.showDialog('No Categories Found','Sorry, no categories were found that match your search.  Please try again');	
};
thisPage.showDialog=function(header,body){
	html="<div id='dialog_header'>" + header + "</div><div id='dialog_body'>" + body + "</div>";
	jQuery.facebox(body);

}
thisPage.showDialogAreas=function(items){
				listHTML = '';
				for(var index=0; index<items.length; index++) {
					listHTML = listHTML + '<li><a href="javascript:thisPage.setAreaID(' + items[index].id + '); thisPage.reCheck();">' + items[index].fqName + '</a></li>';
				}
				theHTML="<p>Your search returned more than one area.  Please select the area that you require from the list below:-</p>" +
						"<div id='popup_list'><ul>" + listHTML + "</ul></div>"
				this.showDialog('Please Clarify ...',theHTML);
	
};
thisPage.showDialogCats=function(items){
					
				// Instantiate the Dialog
				listHTML = '';
				for(var index=0; index<items.length; index++) {
					listHTML = listHTML + '<li><a href="javascript:thisPage.setCatID(' + items[index].id + '); thisPage.reCheck();">' + items[index].fqName + '</a></li>';
				}
				theHTML="<p>Your search returned more than one category.  Please select the category that you require from the list below:-</p>" +
						"<div id='popup_list'><ul>" + listHTML + "</ul></div>"
				this.showDialog('Please Clarify ...',theHTML);

	
};
thisPage.reCheck=function(){
	
	if(this.searchStatus['cats']=='blankInput'){
		this.catID=false;
		this.catList=false;
	}
	if(this.searchStatus['areas']=='blankInput'){
		this.areaID=false;
		this.areaList=false;
	}
	if (this.searchStatus['cats']=='tooManyResults'){
		this.showDialog('Too Many Results','Sorry, your category search produced too many results to display. Please try again');
	}else if (this.searchStatus['areas']=='tooManyResults') {
		this.showDialog('Too Many Results','Sorry, your area search produced too many results to display. Please try again');
	}else if(this.catID===false && (this.catList.toString().indexOf("Object") != -1) ){
		if(this.catList.length==0){
			this.showDialogCatsNone();	
		}else if(this.catList.length==1){
			this.catID=this.catList[0].id;
		}else{
			this.showDialogCats(this.catList);
		}
	}else if(this.areaID===false && (this.areaList.toString().indexOf("Object") != -1) ){
		if(this.areaList.length==0){
			this.showDialogAreasNone();	
		}else if(this.areaList.length==1){
			this.areaID=this.areaList[0].id;
		}else{
			this.showDialogAreas(this.areaList);
		}
	}
	if((this.areaID!==false || this.searchStatus['areas']=='blankInput') && (this.catID!==false || this.searchStatus['cats']=='blankInput')){
		// Build the URL to the classifieds board
		url = window.location.protocol + '//' + window.location.host;
		if(window.location.port!=''){
			url += ':' + window.location.port;
		}
		url += '/classifieds';
		if(this.catID !==false) url += '_cat' + this.catID;
		if(this.areaID !== false) url += '_area' + this.areaID;
		url += '.html';
		window.location.href=url;
		
	}
};

thisPage.doSearch=function(category,area,parent){
	var sUrl="/server.php?query=searchEngine&category=" + escape(category) + "&area=" + escape(area);

	jQuery.get('/server.php',{'query':'searchEngine','category':category,'area':area},function(response,statustext){
		
		oResults=eval("(" + response + ")");
		//Both oResults.cats and oResults.areas will contain an array of objects with the following properties :-
		// id,fqName, name
		//Simply store the results and use the reCheck method to decide on which dialog to display
		thisPage.areaList=oResults.areas;
		thisPage.catList=oResults.cats;
		thisPage.searchStatus=oResults.status;
		thisPage.reCheck();
	
	
	});
		
			
	
}
thisPage.responseFunction=function(oResults,resultstext){


}
/* Remove extra spacing if no banner */
jQuery('document').ready(function(){
	if(jQuery("#BannerPremium").size()==0 && jQuery("#maincolumn").size()>0){
				jQuery("#maincolumn").css('margin-top','0px');
		
	}
});