//------cpMap---------------------------
cpMap = function(name,map){
	this.name=name;
	this.map=map;
	this.xml=new cpXML(this);
	this.show=false;

	this.moveItB=null;
	this.endMoveB=null;

	this.lastxpos=-1;
	this.lastypos=-1;
	this.move=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,false);
	var content='<div style="border:1px solid; border-color:#d4d4d4 #404040 #404040 #d4d4d4; position:relative;">';
	content+='<div style="border:1px solid; border-color:#ffffff #808080 #808080 #ffffff; background:#d4d4d4; padding:3px; position:relative;">';
	content+='<div onMousedown="cpMapItem[\''+this.name+'\'].startMove(event);" style="background:#ff8000; color:#ffffff; margin-bottom:3px; font-weight:bold; font-size:13px; position:relative;">';
	content+='<div style="position:relative;padding:2px 8px 2px 8px;">Map</div>'
	content+='<div onClick="cpMapItem[\''+this.name+'\'].hide();" style="cursor:pointer; border:1px solid; border-color:#ffffff #404040 #404040 #ffffff; position:absolute; right:4px; top:3px; color:ffffff; width:17px; height:16px; background:#d4d4d4;font-size:12px;line-height:1; text-align:center;"><div style="border:1px solid; border-color:#d4d4d4 #808080 #808080 #d4d4d4; width:15px; height:14px; font-size:12px;line-height:1; text-align:center;color:#000000;">X</div></div>'
	content+='</div>';
	content+='<div style="display:block; width:'+(w-10)+'px; height:'+(h-79)+'px;" id="'+this.name+'map"></div>';
	content+='<div style="padding:10px 5px 5px 5px;" id="'+this.name+'days"></div>';
	content+='</div>';
	content+='</div>';
	setContentDiv(this.name+'cdiv',content);

	this.srcpoints=Array();
	this.markers=Array();
	this.marker=Array();
	this.points=Array();
	this.polyline=Array();
	this.infoWindow = new google.maps.InfoWindow();

	//this.map = new GMap2(document.getElementById(this.name+'map'));
	var myOptions = {
	    zoom: 4,
	    center: new google.maps.LatLng(-33, 151),
	    panControl: false,
	    zoomControl: true,
	    scaleControl: true,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	this.map = new google.maps.Map(document.getElementById(this.name+'map'),myOptions);
	this.newLatLng;
//	this.map.addControl(new GMapTypeControl(1));
//	this.map.addControl(new GLargeMapControl());

	this.startMove=function(e){
		this.endMove(null);
		this.lastxpos=e.clientX;
		this.lastypos=e.clientY;
		var self=this;
		if (document.addEventListener) document.getElementsByTagName('body')[0].addEventListener("mouseup", self.endMoveB=function(e) { self.endMove(e); }, true);
		else document.getElementsByTagName('body')[0].attachEvent("onmouseup", self.endMoveB=function(e) { self.endMove(e); }, true);
		if (document.addEventListener) document.getElementsByTagName('body')[0].addEventListener("mousemove", self.moveItB=function(e) { self.moveIt(e); }, true);
		else document.getElementsByTagName('body')[0].attachEvent("onmousemove", self.moveItB=function(e) { self.moveIt(e); }, true);
	}

	this.endMove=function(e){
		if(this.endMoveB!=null) {
			obj=document.getElementById(this.name+'cdivContentDiv');
			obj.style.zIndex='110';
			var self=this;
			if (document.addEventListener) document.getElementsByTagName('body')[0].removeEventListener("mouseup", self.endMoveB, true);
			else document.getElementsByTagName('body')[0].detachEvent("onmouseup", self.endMoveB, true);
			if (document.addEventListener) document.getElementsByTagName('body')[0].removeEventListener("mousemove", self.moveItB, true);
			else document.getElementsByTagName('body')[0].detachEvent("onmousemove", self.moveItB, true);
			this.moveItB=null;
			this.endMoveB=null;
		}
	}

	this.moveIt=function(e){
		obj=document.getElementById(this.name+'cdivContentDiv');
		obj.style.left=parseInt(obj.style.left)+(e.clientX-this.lastxpos)+'px';
		obj.style.top=parseInt(obj.style.top)+(e.clientY-this.lastypos)+'px';
		obj.style.zIndex='111';
		this.lastxpos=e.clientX;
		this.lastypos=e.clientY;
	}

	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';

		document.getElementById(this.name+'cdivContentDiv').style.top=(Math.max(document.documentElement.scrollTop,document.body.scrollTop))+(document.documentElement.clientHeight-h)/2+'px';
		document.getElementById(this.name+'bgdivBGDiv').style.top=(Math.max(document.documentElement.scrollTop,document.body.scrollTop))+'px';
	
	}

	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 google.maps.LatLng(parseFloat(this.srcpoints[i][j*2+1]),parseFloat(this.srcpoints[i][j*2]));
				this.points[i].push(this.newLatLng);
			}
		}
		var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(mina, minb), new google.maps.LatLng(maxa, maxb)); 
		this.map.setCenter(bounds.getCenter());
		this.map.fitBounds(bounds); 
		//this.map.fitBounds(bounds);
		//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.clearMap=function(){
		for(var i=0;i<this.points.length;i++) {
			if(this.polyline[i]!=null) {
				this.polyline[i].setMap(null);
				this.polyline[i]=null;
			}
		}
		this.polyline=Array();
		for(var i=0; i<this.marker.length; i++) {
			if(this.marker[i]!=null) {
				this.marker[i].setMap(null);
				this.marker[i]=null;
			}
		}
		this.marker=Array();
	}

	this.paintMapDay=function(day){
		this.clearMap();
		var self=this;
		var markercounter=0;
		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 google.maps.Polyline({path: this.points[i], strokeColor: "#0000aa", strokeOpacity: 0.8, strokeWeight: 3});
		this.polyline[i].setMap(this.map);
		for(var j=0; j<this.markers[i].length; j++) {
			var point = new google.maps.LatLng(this.markers[i][j][2],this.markers[i][j][1]);
			this.marker[markercounter] = new google.maps.Marker({position:point, title:this.markers[i][j][0]});
			google.maps.event.addListener(this.marker[markercounter], 'click', function(event) {
				self.infoWindow.setContent(this.getTitle());
				self.infoWindow.setPosition(event.latLng);
				self.infoWindow.open(self.map);
			});
			this.marker[markercounter].setMap(this.map);
			markercounter++;
		}
	}

	this.paintMapFull=function(){
		this.clearMap();
		var self=this;
		var markercounter=0;
		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 google.maps.Polyline({path: this.points[i], strokeColor: "#0000aa", strokeOpacity: 0.8, strokeWeight: 3});
			this.polyline[i].setMap(this.map);
			for(var j=0; j<this.markers[i].length; j++) {
				var point = new google.maps.LatLng(this.markers[i][j][2],this.markers[i][j][1]);
				this.marker[markercounter] = new google.maps.Marker({position:point, title:this.markers[i][j][0]});
				google.maps.event.addListener(this.marker[markercounter], 'click', function(event) {
					self.infoWindow.setContent(this.getTitle());
					self.infoWindow.setPosition(event.latLng);
					self.infoWindow.open(self.map);
				});
				this.marker[markercounter].setMap(this.map);
				markercounter++;
			}
		}
		this.infoWindow.setContent('TEST');
		this.infoWindow.open(this.map);
	}

	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();
}

