//------cpMap---------------------------
cpMap = function(name,map){
	this.name=name;
	this.map=map;
	this.xml=new cpXML(this);
	this.show=false;

	var w=Math.min(document.documentElement.clientWidth-200,800);
	var h=Math.min(document.documentElement.clientHeight-200,800);
	drawBGDiv(this.name+'bgdiv');
	drawContentDiv(this.name+'cdiv',w,h);
	var content='<div onMouseover="this.style.background=\'#ffffff\';" onMouseout="this.style.background=\'#eeeeee\';" style="padding:5px;text-align:center; font-weight:bold;cursor:pointer;" onClick="cpMapItem[\''+this.name+'\'].hide();">close map</div>';
	content+='<div style="display:block; width:'+w+'px; height:'+(h-65)+'px;" id="'+this.name+'map"></div>';
	content+='<div style="padding:10px 5px 5px 5px;" id="'+this.name+'days"></div>';
	setContentDiv(this.name+'cdiv',content);

	this.srcpoints=Array();
	this.markers=Array();
	this.marker=Array();
	this.points=Array();
	this.polyline=Array();
	this.map = new GMap2(document.getElementById(this.name+'map'));
	this.newLatLng;
	this.map.addControl(new GMapTypeControl(1));
	this.map.addControl(new GLargeMapControl());

	this.hide=function(){
		document.getElementById(this.name+'cdivContentDiv').style.display='none';
		document.getElementById(this.name+'bgdivBGDiv').style.display='none';
	}

	this.unhide=function(){
		document.getElementById(this.name+'cdivContentDiv').style.display='block';
		document.getElementById(this.name+'bgdivBGDiv').style.display='block';
	}

	this.mapLoaded=function(val){
		eval(this.xmlResult);
		var mina=1000;
		var minb=1000;
		var maxa=0;
		var maxb=0;

		for(var i=0; i<this.srcpoints.length;i++) {
			this.points[i]=Array();
			for(var j=0; j<this.srcpoints[i].length/2;j++) {
				mina=Math.min(parseFloat(this.srcpoints[i][j*2+1]),mina);
				minb=Math.min(parseFloat(this.srcpoints[i][j*2]),minb);
				maxa=Math.max(parseFloat(this.srcpoints[i][j*2+1]),maxa);
				maxb=Math.max(parseFloat(this.srcpoints[i][j*2]),maxb);
				this.newLatLng = new GLatLng(parseFloat(this.srcpoints[i][j*2+1]),parseFloat(this.srcpoints[i][j*2]));
				this.points[i].push(this.newLatLng);
			}
		}
		var bounds = new GLatLngBounds(new GLatLng(mina, minb), new GLatLng(maxa, maxb)); 
		this.map.setCenter(bounds.getCenter(), this.map.getBoundsZoomLevel(bounds)); 

		var content='<span id="'+this.name+'button_f" onClick="cpMapItem[\''+this.name+'\'].paintMapFull();" style="font-weight:bold;color:#ffffff;background:#ff9600;border:1px solid; border-color: #c36e04;padding:5px 8px; cursor:pointer;">Full Tour</span>&nbsp;';
		content+='<span style="font-weight:bold;padding:6px 6px 6px 20px; cursor:pointer;">Single day:</span>&nbsp;';
		for(var i=0;i<this.points.length;i++)
			content+='<span id="'+this.name+'button_'+i+'" onClick="cpMapItem[\''+this.name+'\'].paintMapDay('+i+');" style="font-weight:bold;color:#ffffff;background:#ff9600;border:1px solid; border-color: #c36e04;padding:5px 8px; cursor:pointer;">'+(i+1)+'</span>&nbsp;';
		document.getElementById(this.name+'days').innerHTML=content;
		this.paintMapFull();
	}

	this.paintMapDay=function(day){
		if(this.show!==false) document.getElementById(this.name+'button_'+this.show).style.background='#ff9600';
		this.show=day;
		document.getElementById(this.name+'button_'+this.show).style.background='#b46a00';
		this.map.clearOverlays();
		var i=day;
		this.polyline[i] = new GPolyline(this.points[i], "#000088");
		this.polyline[i].setStrokeStyle({'color':'#0000aa','weight':3,'opacity':0.8});
		this.map.addOverlay(this.polyline[i]);
		for(var j=0; j<this.markers[i].length; j++) {
			var point = new GLatLng(this.markers[i][j][2],this.markers[i][j][1]);
			this.marker[j] = new GMarker(point, {title:this.markers[i][j][0]});
			GEvent.addListener(this.marker[j], 'click', function() {
				this.openInfoWindowHtml(this.getTitle());
			});
			this.map.addOverlay(this.marker[j]);
		}
	}

	this.paintMapFull=function(){
		if(this.show!==false) document.getElementById(this.name+'button_'+this.show).style.background='#ff9600';
		this.show='f';
		document.getElementById(this.name+'button_'+this.show).style.background='#b46a00';
		this.map.clearOverlays();
		for(var i=0;i<this.points.length;i++) {
			this.polyline[i] = new GPolyline(this.points[i], "#000088");
			this.polyline[i].setStrokeStyle({'color':'#0000aa','weight':3,'opacity':0.8});
			this.map.addOverlay(this.polyline[i]);
			for(var j=0; j<this.markers[i].length; j++) {
				var point = new GLatLng(this.markers[i][j][2],this.markers[i][j][1]);
				this.marker[j] = new GMarker(point, {title:this.markers[i][j][0]});
				GEvent.addListener(this.marker[j], 'click', function() {
					this.openInfoWindowHtml(this.getTitle());
				});
				this.map.addOverlay(this.marker[j]);
			}
		}
	}

	this.xmlNext=this.mapLoaded;
	this.xmlNextParams=0;
	this.xmlAction('xmlr/getMap.php?map='+map);
}

var cpMapItem=Array();

function addcpMap(id,map){
	if(cpMapItem[id]==null) {
		cpMapItem[id]=new cpMap(id,map);
	} else cpMapItem[id].unhide();
}

