var map={
	list:[],
	list_tag:new Object({all:[]}),
	range_x:[-11.52,+ 2.86],
	range_y:[+59.55,+49.63],
	map_info_original:'',
	init:function(){
		var map=$('map');
		var map_search=$('map_search');
		this.map_info_original=$('map_info').innerHTML;
		this.list.each(function(item,item_index){
			{
				var distribution=false;
				var warehousing=false;
				var logisticSupportServices=false;
				var international=false;
				
				if(
					item.tag.indexOf("<li>Standard</li>")>-1||
					item.tag.indexOf("<li>Specialised</li>")>-1||
					item.tag.indexOf("<li>Through the Night</li>")>-1||
					item.tag.indexOf("<li>Dairy</li>")>-1
				)
					distribution=true;
					
				if(
					item.tag.indexOf("<li>Picking &amp; packing</li>")>-1
				)
					warehousing=true;
					
				if(
					item.tag.indexOf("<li>Product assembly</li>")>-1||
					item.tag.indexOf("<li>Contract packing</li>")>-1||
					item.tag.indexOf("<li>Deferred production</li>")>-1||
					item.tag.indexOf("<li>Labour supply</li>")>-1||
					item.tag.indexOf("<li>Training services</li>")>-1
				)
					logisticSupportServices=true;
					
				if(
					item.tag.indexOf("<li>Air</li>")>-1||
					item.tag.indexOf("<li>Sea</li>")>-1||
					item.tag.indexOf("<li>Road</li>")>-1
				)
					international=true;
					
				if(!distribution)
					item.tag=item.tag.replace("<li>Distribution</li>","<li style=\\'display: none;\\'>Distribution</li>");
				if(!warehousing)
					item.tag=item.tag.replace("<li>Warehousing</li>","<li style=\\'display: none;\\'>Warehousing</li>");
				if(!logisticSupportServices)
					item.tag=item.tag.replace("<li>Logistics Support Services</li>","<li style=\\'display: none;\\'>Logistics Support Services</li>");
				if(!international)
					item.tag=item.tag.replace("<li>International</li>","<li style=\\'display: none;\\'>International</li>");
					
				this.list_tag.all.push(item);
				var tag_list=item.tag.match(/<li>[^<]+<\/li>/g);
				if(tag_list!=null)tag_list.each(function(tag){
					tag=tag.replace(/^<li>(.*)<\/li>$/,'$1').replace('&amp;','&');
					if(this.list_tag[tag]==undefined)
						this.list_tag[tag]=[];
					this.list_tag[tag].push(item);
				},this);
			}
			
			var description='';
			description+='<h2>'+item.name+'</h2>';
			description+=item.data;
			description+=
				'The following service/s available at this site are:<br />'+
				'<ul>'
			;
			description+=item.tag;
			description+=
				'</ul>'
			;
						
			description=description.replace(/<li>(Standard)<\/li>/g,"<li><a href=\\'/ourservices/distribution/standard.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Specialised)<\/li>/g,"<li><a href=\\'/ourservices/distribution/specialised.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Through the Night)<\/li>/g,"<li><a href=\\'/ourservices/distribution/throughthenightsamedayservice.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Dairy)<\/li>/g,"<li><a href=\\'/ourservices/distribution/dairy.ashx\\'>$1</a></li>");
			
			description=description.replace(/<li>(Picking &amp; packing)<\/li>/g,"<li><a href=\\'/ourservices/warehousing.ashx\\'>$1</a></li>");
			
			description=description.replace(/<li>(Product assembly)<\/li>/g,"<li><a href=\\'/ourservices/logisticsupportservices/valueadded.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Contract packing)<\/li>/g,"<li><a href=\\'/ourservices/logisticsupportservices/valueadded.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Deferred production)<\/li>/g,"<li><a href=\\'/ourservices/logisticsupportservices/valueadded.ashx\\'>$1</a></li>");
			
			description=description.replace(/<li>(Air)<\/li>/g,"<li><a href=\\'/ourservices/international/airfreight.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Sea)<\/li>/g,"<li><a href=\\'/ourservices/international/seafreight.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Road)<\/li>/g,"<li><a href=\\'/ourservices/international/roadfreight.ashx\\'>$1</a></li>");
			
			description=description.replace(/<li>(Labour supply)<\/li>/g,"<li><a href=\\'/ourservices/logisticsupportservices/recruitment.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Training services)<\/li>/g,"<li><a href=\\'/ourservices/logisticsupportservices/recruitment.ashx\\'>$1</a></li>");
			
			description=description.replace(/<li>(Distribution)<\/li>/g,"<li><a class=\\'maphead\\' href=\\'/ourservices/distribution.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Warehousing)<\/li>/g,"<li><a class=\\'maphead\\' href=\\'/ourservices/warehousing.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Logistics Support Services)<\/li>/g,"<li><a class=\\'maphead\\' href=\\'/ourservices/logisticsupportservices.ashx\\'>Logistics Support Services</a></li>");
			description=description.replace(/<li>(International)<\/li>/g,"<li><a class=\\'maphead\\' href=\\'/ourservices/international.ashx\\'>$1</a></li>");
			description=description.replace(/<li>(Additional)<\/li>/g,"<li>Additional Services</li>");
			
			map.insert('<a href="#" onclick="$(\'map_info\').innerHTML=\''+description+'\';map.point_select($(this).childElements()[0]);return false"><div class="point" id="map_point_'+item_index+'" style="top:'+item.y+'px;left:'+item.x+'px" title="'+item.name+'"></div></a>');
		},this);
		/*for(var tag in this.list_tag){
			map_search.options[map_search.options.length]=new Option(tag);
		};*/
	},
	point_select_last:null,
	point_select:function(point){
		if(this.point_select_last)
			$(this.point_select_last).removeClassName('point_selected');
		if(point)
			$(point).addClassName('point_selected');
		this.point_select_last=point;
	},
	add:function(name,tag,longitude,latitude,data){if(longitude.length&&latitude.length){
	    longitude=longitude[0];
	    latitude =latitude [0];
		var map=$('map');
		var index=0;
		while(index<this.list.length&&this.list[index].name!=name)index++;
		if(index>=this.list.length)
			this.list.push({
				name:name,
				tag:'',
				longitude:longitude,
				latitude:latitude,
				x:(longitude-this.range_x[0])/(this.range_x[1]-this.range_x[0])*map.getWidth(),
				y:(latitude -this.range_y[0])/(this.range_y[1]-this.range_y[0])*map.getHeight(),
				data:data,
				index:this.list.length
			})
			
		this.list[index].tag+=tag;
		
		function sign(value){return value?value>0?+1:-1:0;}
		
		var offset=[0,0];
		for(var i=0;i<index;i++){
			var distance=[
				this.list[i].x-this.list[index].x,
				this.list[i].y-this.list[index].y
			];
			var distance_min=4;
			if(Math.abs(distance[0])<distance_min&&Math.abs(distance[1])<distance_min){
				//console.log("moving "+this.list[index].name+" away from "+this.list[i].name);
				offset[0]-=sign(distance[0])*(distance_min-Math.abs(distance[0]));
				offset[1]-=sign(distance[1])*(distance_min-Math.abs(distance[1]));
			}
		}
		this.list[i].x+=offset[0];
		this.list[i].y+=offset[1];
	}},
	search:function(tag){
		$('map_info').innerHTML=this.map_info_original;
		this.point_select(null);
		this.list.each(function(location){
			$('map_point_'+location.index).hide();
		},this);
		if(this.list_tag[tag]!=undefined){
			this.list_tag[tag].each(function(location){
				$('map_point_'+location.index).show();
			},this);
		}/*else
			console.log("no results for "+tag);*/
	}
};
Event.observe(document,'dom:loaded',function(){map.init();});