// JavaScript Document
henworx.MapCms.DB = function (){
	this.recentLocations = null;
	this.highlightLocations = null;
	this.categoryLocations = null;
	this.searchLocations = null;
	this.rangeLocations = null;
	this.recentHtml = null; //used privately
	
	this.beforeGetRecent=function(){};
	this.afterGetRecent=function(){};
	
	this.beforeGetCategoryAsXML = function(){};
	this.afterGetCategoryAsXML = function(){};
	
	this.beforeGetSearchAsXML = function(){};
	this.afterGetSearchAsXML = function(){};
	
	this.beforeGetHighlight=function(){};
	this.afterGetHighlight=function(){};
	
	this.beforeGetRecentAsXml=function(){};
	this.afterGetRecentAsXml=function(){};
	
	this.beforeGetRecentAsHtml=function(){};
	this.afterGetRecentAsHtml=function(){};
	
	this.getRecentAsXml = function (limit,offset,boolAsynchronous){
							this.getFor='recent'							
							return this._getFromDb(limit,offset,boolAsynchronous,this.beforeGetRecentAsXml,this.afterGetRecent,'xml',true);
	};
	
	this.getRecentAsHtml = function (limit,offset,boolAsynchronous){
							this.getFor='recent'							
							return this._getFromDb(limit,offset,boolAsynchronous,this.beforeGetRecentAsHtml,this.afterGetRecent,'html',true);
	};
	
	this.getHighlightAsXml = function (limit,offset,boolAsynchronous){
							this.getFor='highlight'							
							return this._getFromDb(limit,offset,boolAsynchronous,this.beforeGetRecentAsXml,this.afterGetRecent,'xml',true);
	};
	
	this.getHighlightAsHtml = function (limit,offset,boolAsynchronous){
							this.getFor='highlight'							
							return this._getFromDb(limit,offset,boolAsynchronous,this.beforeGetRecentAsHtml,this.afterGetRecent,'html',true);
	};
	
	_dbObj=this;
	this.getRecent = function(limit,offset,boolAsynchronous){
		if(!limit){
			limit=gPageSize;
		}
		if(!offset){
			offset=0;
		}
		_outputFormat = 'xml';
		_params = '?limit='+limit+'&offset='+offset+'&output='+_outputFormat;
		this.getFor='recent'							
		//this._getFromDb(_params,boolAsynchronous,this.beforeGetRecent,this.afterGetRecent,_outputFormat,false);
		this.getRecentXmlFile('recent.xml',_params,boolAsynchronous);
	};
	
	this.getRecentXmlFile = function(_xml_file_name,_params,boolAsynchronous){
			
			jQuery.ajax({
				type: "GET",
				url: gHost+'/'+gUploadPath+'/'+_xml_file_name,
				dataType: "xml",
				async: false,
				success: function(xml) {		
					
		 			henworx.MapCms.DB.generateLocationFromXml(xml,_dbObj);
					//alert('success');
				},
				error: function(){
					
					_dbObj._getFromDb(_params,boolAsynchronous,_dbObj.beforeGetRecent,_dbObj.afterGetRecent,'xml',false);
					//alert('error');
				}
		});
		
			

			
	}
	
	this.getHighlight = function(limit,offset,boolAsynchronous){
		if(!limit){
			limit=gPageSize;
		}
		if(!offset){
			offset=0;
		}
		_outputFormat = 'xml';
		_params = '?limit='+limit+'&offset='+offset+'&output='+_outputFormat;
		this._getFromDb(_params,boolAsynchronous,this.beforeGetHighlight,this.afterGetHighlight,_outputFormat,false)
	};
	
	this.getSearchAsXML = function(search_text,limit,offset,boolAsynchronous){						
		if(!limit){
			limit=gPageSize;
		}
		if(!offset){
			offset=0;
		}
		if(!boolAsynchronous){
			boolAsynchronous=false;
		}
		_outputFormat = 'xml';
		_params = '?limit='+limit+'&offset='+offset+'&output='+_outputFormat+'&s='+search_text;
		this.getFor='search'							
		this._getFromDb(_params,boolAsynchronous,this.beforeGetSearchAsXML,this.afterGetSearchAsXML,_outputFormat,false)
	};
	
	this.getCategoryAsXML = function(catid,limit,offset,boolAsynchronous){						
		if(!limit){
			limit=gPageSize;
		}
		if(!offset){
			offset=0;
		}
		if(!boolAsynchronous){
			boolAsynchronous=false;
		}
		_outputFormat = 'xml';
		_params = '?limit='+limit+'&offset='+offset+'&output='+_outputFormat+'&cat='+catid;
		this.getFor='category'							
		this._getFromDb(_params,boolAsynchronous,this.beforeGetCategoryAsXML,this.afterGetCategoryAsXML,_outputFormat,false)
	};
	
	this.getRangeAsXML = function(range,limit,offset,boolAsynchronous){						
		if(!limit){
			limit=gPageSize;
		}
		if(!offset){
			offset=0;
		}
		if(!boolAsynchronous){
			boolAsynchronous=false;
		}
		_outputFormat = 'xml';
		_params = '?limit='+limit+'&offset='+offset+'&output='+_outputFormat+'&range='+range;
		this.getFor='range'							
		this._getFromDb(_params,boolAsynchronous,this.beforeGetCategoryAsXML,this.afterGetCategoryAsXML,_outputFormat,false)
	};
	
	//private method
	this._getFromDb=function(params,boolAsynchronous,__beforeCallback,__afterCallBack,outputFormat,returnOutput){		
		__beforeCallback();
		if(boolAsynchronous){
			requestOutput = requestURL(gPluginUrl+'/includes/get-entries.php'+params,true,true,function (){
				   if (gHttpRequest.readyState == 4) { 
						if (gHttpRequest.status == 200) { 
							if(outputFormat == 'xml' && !returnOutput){			
								henworx.MapCms.DB.generateLocationFromXml(gHttpRequest.responseXML,_dbObj)
							}else if(outputFormat == 'xml'){
										requestOutput = gHttpRequest.responseXML
							}else if(outputFormat == 'html'){																															
									requestOutput = gHttpRequest.responseText
							}
						}
				   }
			});
		
		}else{
			requestOutput = requestURL(gPluginUrl+'/includes/get-entries.php'+params,false,true)
			if(!returnOutput && outputFormat == 'xml')
				henworx.MapCms.DB.generateLocationFromXml(requestOutput,_dbObj)
		}
		__afterCallBack;
		if(returnOutput)
			return requestOutput;
	}
}

henworx.MapCms.DB.generateLocationFromXml = function (xml, dbObj){
	//gMapCmsDebug.append(dbObj);//debug
	if(xml == null ){ return false;}
	arrXml = xml2array(xml);
	if(typeof(arrXml) == 'undefined'){ return false; };
	var myLoc
	var arrLocations=new Array();
	for(i in arrXml.locations.location){
		o=arrXml.locations.location[i]
		oAdd=o.address
		mAdd= new henworx.MapCms.Address(oAdd.street+" "+oAdd.city,oAdd.lat,oAdd.lng);
		
		myLoc= new henworx.MapCms.Location(mAdd);
		with(myLoc){
			setID(o.id);
			setTitle(o.title);			
			setImage(o.image);
			setLogo(o.logo);
			setHeadshot(o.headshot);
			setExcerpt(o.excerpt);
			setDescription(o.desc);	
			setEmblem(o.emblem);
			//set custom fields
			for(_j=0;_j<henworx.customFields.length;_j++){
			 	_key=henworx.customFields[_j];
				if(o.customfield){
					if(o.customfield[_key])
						setCustomField(_key,o.customfield[_key])
				}
			}
			//icon=null;
			arrLocations[arrLocations.length]=myLoc;
		}
	}
	//gMapCmsDebug.append(arrLocations);//debug	
	//gMapCmsDebug.append("Switch:"+dbObj.getFor);//debug	
	switch(dbObj.getFor){
		case "recent":
			dbObj.recentLocations=arrLocations;
			break;
		case "highlight":
			dbObj.highlightLocations=arrLocations;
			break;
		case "category":
			dbObj.categoryLocations=arrLocations;
			break;
		case "search":
			dbObj.searchLocations=arrLocations;
			break;
		case "range":
			dbObj.rangeLocations=arrLocations;
			break;
		default:
	}
}