// JavaScript Document
henworx.MapCms.DB = function (){
	this.recentLocations = null;
	
	this.beforeGetRecentAsXml=function(){};
	this.afterGetRecentAsXml=function(){};
	
	this.getRecentAsXml = function(limit,offset,boolAsynchronous){
						
						_outputFormat = 'xml';
						_params = '';
						this.getFor='recent'							
						this._getFromDb(_params,boolAsynchronous,this.beforeGetRecentAsXml,this.afterGetRecentAsXml,_outputFormat,false)
					};
	
	
	
	_dbObj=this;
	
	//private method
	this._getFromDb=function(params,boolAsynchronous,__beforeCallback,__afterCallBack,outputFormat,returnOutput){
		
						__beforeCallback();
						if(boolAsynchronous){
						requestOutput = requestURL(gPluginUrl+'/read-xml.php',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+'/read-xml.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(xml);//debug
	if(xml == null ){ return false;}
	arrXml = xml2array(xml);
	
	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.city,oAdd.lat,oAdd.lng);
		
		myLoc= new henworx.MapCms.Location(mAdd);
		with(myLoc){
			setTitle(o.title);			
			arrLocations[arrLocations.length]=myLoc;
		}
	}
	//gMapCmsDebug.append(arrLocations.length);//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;
		default:
		
	}
	
}