/*dependency 
	b. mapcmsAddress.js
	
	Short Location Format:
		#title# = title of the post
		#image# = default image
		#excertp# = short desc of the posts
		#openlink# = popup the detail description of the post
*/
//whenever possible send the parameter address as MAPCMS address
//this will improve the performance

henworx.MapCms.Location=function (_address){
	//if address is not of type mapcms.henworx.Address make it
	if(typeof _address.latlng =='undefined'){
		_address=new henworx.MapCms.Address(_address);
	}
	this.shortLocationFormat = '<h1>#title#</h1> #image# #excerpt# #more#';
	this.id = null;
	this.title = null;
	this.image = null;
	this.description = null;
	this.address=_address;
	this.lat=_address.lat;
	this.lng=_address.lng;
	this.latlng=_address.latlng;
	this.categories=null;
	this.customField= new Array();
	this.emblem=null;
	this.iconText=null
	this.icon=null;
	this.uniqueID=makeUniqueID(); //constructor
	//events you can override these events 
	this.beforeSetExcerpt=function(){};
	this.afterSetExcerpt=function(){};
	this.beforeSetDescription=function(){};
	this.afterSetDescription=function(){};
	this.beforeSetCustomField=function(){};
	this.afterSetCustomField=function(){};
	this.beforeAddToMap=function(){};
	this.afterAddToMap=function(){};
	this.beforeSetCategory=function(){};
	this.afterSetCategory=function(){};
	this.beforeSetEmblem=function(){};
	this.afterSetEmblem=function(){};
	
	//creates icon for the map
	// all parameters are optional if not set values are taken from globalk settings
	this.createIcon= function (iconUrl,sizeX,sizeY,anchorX,anchorY,infoWindowAnchorX,infoWindowAnchorY){
			
			//get defaults if paremeter is not set
			if(!iconUrl)
				iconUrl=gIconConfig['iconUrl'];
			if(!sizeX)
				sizeX=gIconConfig['sizeX'];
			if(!sizeY)
				sizeY=gIconConfig['sizeY'];
			if(!anchorX)
				anchorX=gIconConfig['anchorX'];
			if(!anchorY)
				anchorY=gIconConfig['anchorY'];
			if(!infoWindowAnchorX)
				infoWindowAnchorX=gIconConfig['infoWindowAnchorX'];
			if(!infoWindowAnchorY)
				infoWindowAnchorY=gIconConfig['infoWindowAnchorY'];
				
			oIcon=new GIcon();
			// pointer.php is a php file that generates image using GD with emblem and text.
			// what is emblem?
			// you may want to show one icon with red color and other as blue.
			// so to so the iocn red you use the emblem =red
			// and save the pointer_red.png in theme folder
			// the pointer.php automatically reads the emblem file and shows it
			oIcon.image = gPluginUrl+"/images/pointer.small.png";
			//gMapCmsDebug.append(oIcon.image);
			oIcon.iconSize = new GSize(sizeX, sizeY); //image size;
			oIcon.iconAnchor = new GPoint(anchorX, anchorY); //the pointer is at x,y from top left.
			oIcon.infoWindowAnchor = new GPoint(infoWindowAnchorX, infoWindowAnchorY);//the center of blue circle on top is at x,y from topleft.
			this.icon = new GIcon(oIcon);
	}
	//auto create icon
	if(!this.icon) {this.createIcon();}
	
	//Id
	this.setID = function(_id){
		this.id = _id;
	}
	//title
	this.setTitle = function(_title){
		if(typeof(_title) != 'undefined')
			this.title = _title;
		else
			this.title = ''; 
	}
	//Image
	this.setImage = function(_image){
		if(typeof(_image) != 'undefined')
			this.image = _image;
		else
			this.image = '';
	}
	//Excerpt
	this.setExcerpt=function(_excerpt){
		//raise event beforeSetExcerpt
		this.beforeSetExcerpt(_excerpt)
		
		if(typeof(_excerpt) != 'undefined')
			this.excerpt=_excerpt;
		else
			this.excerpt = '';
		
		//raise event afterSetExcerpt
		this.afterSetExcerpt(_excerpt)
	}
	//description
	this.setDescription=function(_desc){
		//raise event 
		this.beforeSetDescription(_desc)
		if(typeof(_desc) != 'undefined')
			this.description=_desc;
		else
			this.description = '';
		
		//raise event 
		this.afterSetDescription(_desc)
	}
	
	
	this.setCategory=function(_categories){
		//raise event 
		this.beforeSetCategory(_categories)
		this.catefories=_categories
		//raise event 
		this.afterSetCategory(_categories)
	}
	
	this.setEmblem=function(_emblem){
		//raise event 
		this.beforeSetEmblem(_emblem)
		this.emblem=_emblem
		this.createIcon(); //emblem changed so create icons
		//raise event 
		this.afterSetEmblem(_emblem)
	}
	
	//TODO set other values here
	this.setCustomField=function(fld_name,fld_value){
		//raise event 
		this.beforeSetCustomField(fld_name,fld_value)
		
		this.customField[fld_name]=fld_value
		
		//raise event 
		this.afterSetCustomField(fld_name,fld_value)
	}
	
	this.getCustomField=function(fld_name){
		return this.customField[fld_name];
	}
	//add to map
	this.addToMap=function (_map) {
		//raise event
		this.beforeAddToMap(_map)
		
		_map.addLocation(this)
		
		//raise event
		this.afterAddToMap(_map)
	}
}


