
(function($){
	$.fn.mapstraction_marker = function(options) {
		// plugin defaults
	    var defaults = {
			hcardclass	: '',
	        uniqueiddiv	: 'locationmap',
	        type		: 'google',
	        zoom		: 'large',
	        maptype		: false,
	        lat			: 41.3862666, //barcelona
	        lon			: 2.1340734,
	        setzoom		: 15,
			markers		: [],
			mode		:'display',
			img_url		:'',
			icons_url	:'',
			update		:false,
			title_label			: '',
			description_label	: '',
			start_label			: '',
			end_label			: ''
	    };

		// update from users options
		var options = $.extend(defaults, options);

    // process each main item
		return this.each(function() 
		{
        
					
			// Now initialise the map
			var mapstraction = new Mapstraction(options.uniqueiddiv, options.type);
		    mapstraction.addControls({
		            zoom: options.zoom,
		            map_type: options.maptype
		    });

		    if (options.lat != '' && options.lon != '') {
		           mapstraction.setCenterAndZoom(new LatLonPoint(options.lat, options.lon), options.setzoom);
		    }
				// create all market 
			markers = options.markers;			
			addPoint(markers);
			
			function addPoint(markers) {
				if(markers.length>0){
					$(markers).each(function(i,marker){		
			            var latitude 	= marker.latitude;
						var longitude 	= marker.longitude;
						var image 		= marker.image;
						var description = marker.description;
						var title 		= marker.title;
						var start 		= marker.start;
						var end 		= marker.end;
						var icons_image = marker.imageIcons;
						var icons_id 	= marker.icons;
						
						var markerHtml 	= '<div class = "map_bubble">';
		
						if(title!='' && title !=undefined){
							markerHtml+= '<p class = "map_bubble_title">'+options.title_label+ title+'</p>';
						}
						if(description!='' && description !=undefined){
							markerHtml+= '<p class = "map_bubble_description">'+options.description_label+ description+'</p>';
						}
						if( start!='' && start != undefined){
							markerHtml+= '<p class = "map_bubble_start>'+options.start_label+ start+'</p>';
						}
						if(start == undefined){start='';}
						
						if( end!=''  && end != undefined){
							markerHtml+= '<p class = "map_bubble_end">'+options.end_label + end+'</p>';
						}
						if(end == undefined){end='';}
						if( image!='' && image != undefined){
							markerHtml+= '<div class = "map_bubble_image"><img src="'+options.img_url+image+'&w=90&h=90&zc=1"   /></div>';
						}
						markerHtml+= '</div>';
						
						
						
						if (latitude != '' && longitude != '') {
			                var myPoint = new LatLonPoint(latitude, longitude);
			                if (options.lat == '' || options.lon == '') {
			                    mapstraction.setCenterAndZoom(myPoint, options.setzoom);
			                }
							
			                var marker = new Marker(myPoint);
							marker.setLabel(title);
							//mapstraction.removeMarker(marker);
												
			                marker.setInfoBubble(markerHtml);
							if(icons_image!= 'default'){
								marker.setIcon(options.icons_url+icons_image,[20,34]);
								//iconSize : [20,34] });*/
							}
			                mapstraction.addMarker(marker);	
							
							
						}
					});	
				}
			};	
        })
	}
	

})(jQuery);        
