/**
 * loads the description from an item into its DOM Node. this Module optionally since HTML is often defined within the description
 * of the KML element an items description is injected into the node using innerHTML rather than the mootools setText method which
 * will print any HTML as plain text (No conversion to DOM) **this allows the possibility of Malicious code injections ie javascript**
 * which should be prevented this is for now left as a TODO:
 */
var ContentModule=MapItemModule.extend({
	process:function()
	{
	var me=this;
	var div=new Element('span',{'class':"content"});
	var text=(me.item.getDescription?me.item.getDescription():false)|| MapFactory.GetItemDescription(me.item)||false;
		if(text){
		div.innerHTML=text;
		div.injectInside(me.node);
	}
	me.fireEvent('onLoad');
	}
});

/**
 * Stats Module is a simple module that prints some information about a map Item
 * Since often Polygons do not have any description is is nice to diplay some information. 
 */
var StatsModule=MapItemModule.extend({
	process:function()
	{
	var me=this;
	var div=new Element('div',{'class':"content"});
	var mapItem=MapFactory.ResolveItem(me.item);
	if(mapItem instanceof google.maps.Polygon){
		var text="Polygon Area: ~"+parseInt(google.maps.geometry.spherical.computeArea(mapItem.getPaths()[0]))+" square meters.";
		div.setText(text);
		div.injectInside(me.node);
	}
	if(mapItem instanceof google.maps.Polyline){
		var text="Line Length: ~"+parseInt(google.maps.geometry.spherical.computeLength(mapItem.getPath()))+" meters.";
		div.setText(text);
		div.injectInside(me.node);
	}
	me.fireEvent('onLoad');
	}
});

/**
 * Injects a Title into the Node depending on the type of item passed as second argument to the constructor.
 * This Module can be extended to difien other simple modules that simple print text into a DOM Node.
 */
var TitleModule=MapItemModule.extend({
	process:function()
	{
	var me=this;
	var span=new Element('span');
	me.node.appendChild(span);
	me.setText(span);
	me.fireEvent('onLoad',[]);
	},
	/**Gets the title for an item, markers polygons and layers provide different methods to access this resource*/
	setText:function(title)
	{
		var me=this;
			var text=(me.item.getTitle?me.item.getTitle():false)|| MapFactory.GetItemName(me.item)||MapFactory.ItemDisplayType(me.item);
			if(text.length>50){
				title.setText(text.substring(0,45)+" ...");
				text.title=text;
			}else{
				title.setText(text);			
			}


		return title;
	}

});



var AvatarModule=MapItemModule.extend({
	process:function()
	{
	var me=this;
	var d=new Element('div', {style:" float: left; height:20px; width:40px; overflow:visible;"});
	var a=new Element('a',{
		style:"display:block;"
			});
	
	me.node.appendChild(d);
	d.appendChild(a);
	
	
	var details=new MapItemOwnerDetailsRequest(me.item);
	details.addEvent('onSuccess',function(results){

		mm_debug(results);
		if(results&&results.details){
			//alert('avatar');
			var profile=false;
			var avatar=false;
			
			if(results.details.avatar&&results.details.avatar!=""){
				a.appendChild(new Element('img',{src:results.details.avatar, style:"height:20px; width:auto; margin-right:5px; margin-left:3px;"}));
				if(results.details.profile&&results.details.profile!=""){
					a.href=results.details.profile;			
				}
				me.fireEvent('onLoad',[]);
			}else{
				me.node.removeChild(d);
				me.fireEvent('onLoad',[]);
			}
			

			
			
		}else{
			me.node.removeChild(d);
			me.fireEvent('onLoad',[]);
		}
		
	});
	details.addEvent('onFailure',function(){
		me.node.removeChild(d);
		me.fireEvent('onLoad',[]);
	});
	details.execute();
	}

});
	
	
	


