var outnabout_smallblock_maplink = new Class({
	
	//options
	options: {
		click_groups: $$('div.outandabout_smallblock'),
		mapLinkClass: 'right',
		map_identifier: 'mapcalmap'
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//set map links
		this.doMapLinks();
	},
	
	doMapLinks: function() {
		
		//for all of the elements
		this.options.click_groups.each(function(el) {
			
			//get the href
			var anchor = el.getElements('a' + (this.options.mapLinkClass ? '.' + this.options.mapLinkClass : ''))[0];
			
			//if we found the map link
			if($defined(anchor)) {
				var markers = new Array();
				var i = 1;
				
				el.getElements('li').each(function(li) {
					
					var link = li.getElements('a')[0].getProperty('href');
					
					var lat = null;
					var lon = null;

                    var full_id_prop = li.getProperty('id');

                    if (typeof(full_id_prop) == 'string')
                    {
                        var props = full_id_prop.split('_');

                        for (var subindex in props)
                        {
                            if (subindex & 1)
                            {
                                switch (props[subindex - 1])
                                {
                                    case 'lat':
                                        lat = props[subindex];
                                        break;

                                    case 'lon':
                                        lon = props[subindex];
                                        break;
                                }
                            }
                        }
                    }
					
					if(lat && lon){
						var details = {"marker_id": i ,"latitude": lat, "longitude": lon, "url": link};			
						markers.push(details);
					}
					
					i++;
				},this);
				
				if(markers.length){
					//add a click event to the item so it goes there when clicked. 
					this.setClick(anchor,this.options.map_identifier,markers);
				}else{
				    //Remove the link
					anchor.remove();
				}
			}
			
		},this);
	},
	
	//ads the click event
	setClick: function(element,identifier,markers) {
		
		element.addEvent('click', function() {
            window[identifier]["replaceMarkers"](markers);
            $('oaa_map_header_text').setText("MAP OF "+ element.rel.toUpperCase());
		});
	}
	
});
outnabout_smallblock_maplink.implement(new Options);
