///////////////////////////////////
//////////////////////////////////
// All rights reserved to Amadot(ZBN) Ltd. (c) 
// Google Maps handling version 2.0
// print class
// last update -  22/02/09
/////////////////////////////////
/*
	MAPS ZOOMS/SCALE:
	14 = 500m 
	15 = 200m
	16 = 100m
	17 = 50m
	18 = 20m (1)
	19 = 20m (2)
	20 = 10m
*/


MAPS.Print = {
	
	///////////////////
	///// INITIATE ///
	///////////////////
	enabled : false,
	enable : function()
	{
		if (MAPS.Print.enabled)
		{
			MAPS.Print.disable();
			return;	
		}
		MAPS.Print.enabled = true;
		MAPS.Markers.disableDragging();
		///////setting map's options//////
		//MAPS.mapObj.disableContinuousZoom();
		//MAPS.mapObj.disableScrollWheelZoom();
		//MAPS.mapObj.disableGoogleBar();
			
		/// controls ///
		//MAPS.mapObj.removeControl(MAPS.Controls.lMap);
		MAPS.mapObj.removeControl(MAPS.Controls.mapType);
		
		/*
		document.getElementById(MAPS.DivNames.statusDiv).style.display = "none";
		document.getElementById(MAPS.DivNames.searchDiv).style.display = "none";
		document.getElementById(MAPS.DivNames.ShowMarkersTable).style.display = "none";
		document.getElementById(MAPS.DivNames.searchResults).innerHTML = "";
		document.getElementById(MAPS.DivNames.searchResults).style.display = "block";
		*/
		document.getElementById(MAPS.DivNames.printTitleDiv).style.display = "block";
		document.getElementById(MAPS.DivNames.searchMenuDiv).style.display = "none";
		document.getElementById(MAPS.DivNames.markersMenuDiv).style.display = "none";
		document.getElementById(MAPS.DivNames.daysFilterMenuDiv).style.display = "none";
		document.getElementById(MAPS.DivNames.statusDiv).style.display = "none";
		document.getElementById(MAPS.DivNames.importanceFilterMenuDiv).style.display = "none";
		
		//document.body.className = "body_print";
		
		/*
		if (MAPS.Print.rectangle == null)
			MAPS.Print.drawRect();
		else
		{
			var currCenter = MAPS.mapObj.getCenter();
			MAPS.Print.showMarker();
			MAPS.Print.centerMarker.setLatLng(currCenter); //positioning the marker in the center of the screen;
			MAPS.mapObj.setZoom(14); //setting the zoom to 500m
			MAPS.Print.moveRect(currCenter); //moving the rectangle to the center of the screen
		}
		*/
		
		//new version - added 14/04/08
		MAPS.Print.mapOnMove();
		
		var event1 = google.maps.Event.addListener(MAPS.mapObj,"dragend",MAPS.Print.mapOnMove);
		var event2 = google.maps.Event.addListener(MAPS.mapObj,"zoomend",MAPS.Print.mapOnMove);
		MAPS.Print.eventListenersArr.push(event1);
		MAPS.Print.eventListenersArr.push(event2);
		
		MAPS.Print.movedNumbersArr = new Array();
		
	},
	
	disable : function()
	{
		MAPS.Print.enabled = false;
		///////setting map's options//////
		//MAPS.mapObj.enableContinuousZoom();
		//MAPS.mapObj.enableScrollWheelZoom();
		//MAPS.mapObj.enableGoogleBar();
			
		///controls//
		//MAPS.mapObj.addControl(MAPS.Controls.lMap);
		MAPS.mapObj.addControl(MAPS.Controls.mapType);
		MAPS.Markers.enableDragging();
		
		/*
		document.getElementById(MAPS.DivNames.statusDiv).style.display = "block";
		document.getElementById(MAPS.DivNames.searchDiv).style.display = "block";
		document.getElementById(MAPS.DivNames.ShowMarkersTable).style.display = "block";
		document.getElementById(MAPS.DivNames.searchResults).style.display = "none";
		document.getElementById(MAPS.DivNames.searchResults).innerHTML = "";
		*/
		document.getElementById(MAPS.DivNames.printTitleDiv).style.display = "none";
		document.getElementById(MAPS.DivNames.searchMenuDiv).style.display = "block";
		document.getElementById(MAPS.DivNames.markersMenuDiv).style.display = "block";
		document.getElementById(MAPS.DivNames.daysFilterMenuDiv).style.display = "block";
		document.getElementById(MAPS.DivNames.statusDiv).style.display = "block";
		document.getElementById(MAPS.DivNames.importanceFilterMenuDiv).style.display = "block";
		
		//document.body.className = "";
		
		/*
			DISABLED 14/04/08
		MAPS.Print.hideRect();
		MAPS.Print.hideMarker();
		*/
		MAPS.Print.removeNumbers();
		
		//clears the event handlers
		google.maps.Event.removeListener(MAPS.Print.eventListenersArr[0]);
		google.maps.Event.removeListener(MAPS.Print.eventListenersArr[1]);
		MAPS.Print.eventListenersArr = new Array();
		
		MAPS.Markers.disableDragging();
		
		if (username === 'admin' && MAPS.Print.movedNumbersArr.length > 0)
		{
			MAPS.Print.saveLocation();	
		}
		
		
	},
	
	removeNumbers : function()
	{
		if (MAPS.Print.numbersMarkersArr != null)
		{
			//deleting the numbers markers
			var len = MAPS.Print.numbersMarkersArr.length;
			for (var i=0;i<len;i++)
			{
				MAPS.mapObj.removeOverlay(MAPS.Print.numbersMarkersArr[i]);
			}
			MAPS.Print.numbersMarkersArr = null;
			
			//deleting the icons
			MAPS.Print.numbersIconsArr = null;
		}
		MAPS.Print.isMarkerClicked = false;
	},
	/////////////////////////
	///// EVENT LISTENERS ///
	////////////////////////
	eventListenersArr : new Array(),
	
	mapOnMove : function()
	{
		MAPS.Print.removeNumbers();
		var currCenter = MAPS.mapObj.getCenter();
		MAPS.Print.markerClicked(currCenter);
	},
	
	///////////////////
	///// RECTANGLE ///
	///////////////////
	rectangle : null,
	rectHeight : 0,
	rectWidth : 0,
	centerMarker : null,
	centerMarkerOpt : {
		clickable : false,
		draggable : true,
		dragCrossMove : true,
		bouncy : false,
		title : "Drag the marker to move the print area",
		icon: MAPS.Icons.arrow.mapIcon
	},
	
	drawRect : function()
	{
		//getting current center position
		var currCenter = MAPS.mapObj.getCenter();
		var currLat = currCenter.lat();
		var currLng = currCenter.lng();
		
		//setting the zoom to 100m
		MAPS.mapObj.setCenter(new google.maps.LatLng(currLat, currLng), 16); 
		
		var bounds = MAPS.mapObj.getBounds();
		var sw = bounds.getSouthWest();
		var ne = bounds.getNorthEast();
		var mapN = ne.lat();
		var mapE = ne.lng();
		var mapS = sw.lat();
		var mapW = sw.lng();
		
		MAPS.Print.rectHeight = (mapN-mapS);
		MAPS.Print.rectWidth = (mapE-mapW);
		var rectHeight = MAPS.Print.rectHeight;
		var rectWidth = MAPS.Print.rectWidth;
		
		//var cityCenterLng = parseFloat(currLng);
		//var cityCenterLat = parseFloat(currLat);
		var cityCenterLng = parseFloat(currLng);
		var cityCenterLat = parseFloat(currLat);
		
		MAPS.mapObj.setCenter(new google.maps.LatLng(currLat, currLng), 14); 
		
		var polyArr = new Array();
		//NE
		var rectNElng = cityCenterLng + (rectWidth/2);
		var rectNElat = cityCenterLat + (rectHeight/2);
		var rectNEpoint = new google.maps.LatLng(rectNElat,rectNElng);
		
		//NW
		var rectNWlng = cityCenterLng - (rectWidth/2);
		var rectNWlat = cityCenterLat + (rectHeight/2);
		var rectNWpoint = new google.maps.LatLng(rectNWlat,rectNWlng);
		
		//SE
		var rectSElng = cityCenterLng + (rectWidth/2);
		var rectSElat = cityCenterLat - (rectHeight/2);
		var rectSEpoint = new google.maps.LatLng(rectSElat,rectSElng);
		
		//SW
		var rectSWlng = cityCenterLng - (rectWidth/2);
		var rectSWlat = cityCenterLat - (rectHeight/2);
		var rectSWpoint = new google.maps.LatLng(rectSWlat,rectSWlng);
		
		
		polyArr.push(rectNEpoint);
		polyArr.push(rectNWpoint);
		polyArr.push(rectSWpoint);
		polyArr.push(rectSEpoint);
		polyArr.push(rectNEpoint);
		
		//google center marker
		MAPS.Print.centerMarker = new google.maps.Marker(new google.maps.LatLng(cityCenterLat, cityCenterLng),MAPS.Print.centerMarkerOpt);
		google.maps.Event.addListener(MAPS.Print.centerMarker,"dragend",function(){
				var newPoint = MAPS.Print.centerMarker.getLatLng();
				MAPS.Print.moveRect(newPoint);
				MAPS.Print.removeNumbers();
			});
		google.maps.Event.addListener(MAPS.Print.centerMarker,"click",function(){
				var point = MAPS.Print.centerMarker.getLatLng();
				MAPS.Print.markerClicked(point);
			});	
		
		MAPS.Print.showMarker();
		
		//google polygon object
		MAPS.Print.rectangle = new google.maps.Polygon(polyArr,MAPS.Overlays.Polygon.lineColor, MAPS.Overlays.Polygon.lineWidth, MAPS.Overlays.Polygon.lineOpacity, MAPS.Overlays.Polygon.fillColor, 0);
		MAPS.Print.showRect();
	},
	
	showRect : function()
	{
		MAPS.mapObj.addOverlay(MAPS.Print.rectangle);
	},
	
	showMarker : function()
	{
		MAPS.mapObj.addOverlay(MAPS.Print.centerMarker);
	},
	
	hideRect : function()
	{
		MAPS.mapObj.removeOverlay(MAPS.Print.rectangle);
	},
	
	hideMarker : function()
	{
		MAPS.mapObj.removeOverlay(MAPS.Print.centerMarker);
	},
	
	moveRect : function(point)
	{
		MAPS.Print.hideRect();
		MAPS.Print.rectangle = null;
		
		var centerLat = point.lat();
		var centerLng = point.lng();
		var rectHeight = MAPS.Print.rectHeight;
		var rectWidth = MAPS.Print.rectWidth;
		
		var polyArr = new Array();
		//NE
		var rectNElng = centerLng + (rectWidth/2);
		var rectNElat = centerLat + (rectHeight/2);
		var rectNEpoint = new google.maps.LatLng(rectNElat,rectNElng);
		
		//NW
		var rectNWlng = centerLng - (rectWidth/2);
		var rectNWlat = centerLat + (rectHeight/2);
		var rectNWpoint = new google.maps.LatLng(rectNWlat,rectNWlng);
		
		//SE
		var rectSElng = centerLng + (rectWidth/2);
		var rectSElat = centerLat - (rectHeight/2);
		var rectSEpoint = new google.maps.LatLng(rectSElat,rectSElng);
		
		//SW
		var rectSWlng = centerLng - (rectWidth/2);
		var rectSWlat = centerLat - (rectHeight/2);
		var rectSWpoint = new google.maps.LatLng(rectSWlat,rectSWlng);
		
		
		polyArr.push(rectNEpoint);
		polyArr.push(rectNWpoint);
		polyArr.push(rectSWpoint);
		polyArr.push(rectSEpoint);
		polyArr.push(rectNEpoint);
		
		//google polygon object
		MAPS.Print.rectangle = new google.maps.Polygon(polyArr,MAPS.Overlays.Polygon.lineColor, MAPS.Overlays.Polygon.lineWidth, MAPS.Overlays.Polygon.lineOpacity, MAPS.Overlays.Polygon.fillColor, 0);
		MAPS.Print.showRect();
	},
	
	noIdMarkers : 0, //holds the number of how many markers have no id
	
	isMarkerClicked : false,
	
	markerClicked : function(mPoint)
	{
		/*
			DISABLED 14/04/08
		var center = MAPS.mapObj.getCenter();
		var zoom = MAPS.mapObj.getZoom();
		if (center != mPoint)
			MAPS.mapObj.setCenter(mPoint,16);
		else if (zoom != 16)
			MAPS.mapObj.setZoom(16);
		*/
		
		//writing down the visible markers
		setTimeout(function()
		{
			MAPS.MarkersMNG.getBounds();
			var markersArr = MAPS.MarkersMNG.visibleMarkers;
			var len = markersArr.length;
			var markerObj = null;
			var noIdMarkersStr = '';
			var noIdMarkersNumstr = '';
			
			
			for (var i=0;i<len;i++)
				markerObj = markersArr[i];
			
			if (!MAPS.Print.isMarkerClicked)
			{
				MAPS.Print.isMarkerClicked = true;
				//does nothing until we get all the ids from DB	
				if (MAPS.Print.noIdMarkers != 0)
					MAPS.Print.timeOutFunc(markersArr);
				else
					MAPS.Print.drawNumbers(markersArr);
			}
			
		},20);
		
	},
	
	timeOutFunc : function(markersArr)
	{
		if (MAPS.Print.noIdMarkers != 0)
			setTimeout(function(){MAPS.Print.timeOutFunc(markersArr);},500);
		else
			MAPS.Print.drawNumbers(markersArr);			
		
	},
	
	numbersMarkersArr : null,
	numbersIconsArr : null,
	movedNumbersArr : null,
	
	drawNumbers : function(markersArr)
	{
		MAPS.Print.numbersMarkersArr = new Array();
		MAPS.Print.numbersIconsArr = new Array();
		
		var len = markersArr.length;
		var markerObj = null;
		var markerCenter = null;
		var markerOptions = null;
		var iconObj = null;
		var iconSrc = '';
		var markersNumStr = '';
		
		for (var i=0;i<len;i++)
		{
			markerObj = markersArr[i];
			//////////// defining the num-mark position
			var _point;
			if (markerObj.numLat !== -1000 && markerObj.numLng !== -1000)
				_point = new google.maps.LatLng(markerObj.numLat,markerObj.numLng);
			else
				_point = markerObj.marker.getLatLng();
				
			/////////// creating the icon
			iconSrc = "images/numbers/" + markerObj.dbId + ".png";
			MAPS.Print.numbersIconsArr.push(new google.maps.Icon(G_DEFAULT_ICON,iconSrc));
			iconObj = MAPS.Print.numbersIconsArr[MAPS.Print.numbersIconsArr.length-1];
			if (markerObj.dbId < 1000)
				iconObj.iconSize = new google.maps.Size(21,14);
			else					 
				iconObj.iconSize = new google.maps.Size(28,14);	
			iconObj.shadow = "";
			iconObj.iconAnchor = new google.maps.Point(12,0);
			iconObj.printImage = "http://www.amadot.co.il/maps/" + iconSrc;	
			iconObj.mozPrintImage = "http://www.amadot.co.il/maps/" + iconSrc;
			iconObj.printShadow = "";
			
			/////////// defining marker's options
			markerOptions = {
				draggable : true,
				dragCrossMove : true,
				bouncy : false,
				title : "drag to move",
				icon: iconObj	
			}
			
			////////// creating the marker
			var numMarker = new google.maps.Marker(_point,markerOptions);
			numMarker.markerObjRef = markerObj;
			
			////////// creating marker's dragend event listener
			google.maps.Event.addListener(numMarker,"dragend",function(){
				var newPoint = this.getLatLng();
				try
				{
					this.markerObjRef.numLat = newPoint.lat();
					this.markerObjRef.numLng = newPoint.lng();
					MAPS.Print.movedNumbersArr.push(this.markerObjRef.num);
				}
				catch(e)
				{
					alert("error update location for number-marker:\n" + e);	
				}	
			});
			
			////////// inserting the marker into array
			MAPS.Print.numbersMarkersArr.push(numMarker);
			
			////////// adding the marker to the map
			MAPS.mapObj.addOverlay(MAPS.Print.numbersMarkersArr[MAPS.Print.numbersMarkersArr.length-1]);
			
						
			////////// building the string for the markers list
			if (markersNumStr != '') markersNumStr += ",";
			markersNumStr += markerObj.dbId;
		}
		
		var html = "<h1 id='userTitle'></h1>\n\
			<table width='100%' cellspacing='0' cellpadding='0' border='0' class='printInDisplay'>\n\
			<tr>\n\
				<td\n\
					<h5 class='resultsLinks printInvisible'>You can drag the numbers-markers as you like</h5>\n\
					<form action='markers.php' method='get' name='markers4print' target='_blank'>\n\
						<input type='hidden' name='city' id='city' value='"+City.name+"'>\n\
						<input type='hidden' name='markers' id='markers' value='"+markersNumStr+"'>\n\
						<input type='hidden' name='title' id='title'>\n\
					</form>\n\
				</td>\n\
			</tr>\n\
			<tr>\n\
				<td><a class='resultsLinks printInvisible' href='javascript:MAPS.Print.giveTitle();'>Name the map</a></td>\n\
			</tr>\n\
			<tr>\n\
				<td><a class='resultsLinks printInvisible' href='javascript:MAPS.Print.showMarkerList();'>Open marker list</a></td>\n\
			</tr>\n\
		</table>";
		
		/*
		var html = "<h5 class='resultsLinks printInvisible'>You can drag the numbers-markers as you like</h5>";
		html += "<form action='markers4print.asp' method='post' name='markers4print' target='_blank'>";
		html += "<input type='hidden' name='city' id='city' value='"+City.name+"'>";
		html += "<input type='hidden' name='markers' id='markers' value='"+markersNumStr+"'>";
		html += "<input type='hidden' name='title' id='title'>";
		html += "</form>";
		
		html += "<a class='resultsLinks printInvisible' href='javascript:MAPS.Print.giveTitle();'>Name the map:</a><br/>";
		html += "<center><h1 id='userTitle'></h1></center>";
		html += "<a class='resultsLinks printInvisible' href='javascript:MAPS.Print.showMarkerList();'>Open marker list</a>";
		*/
		
		document.getElementById(MAPS.DivNames.printTitleDiv).innerHTML = html;
		
	},
	
	saveLocation : function()
	{
		var answer = confirm("Save number-markers position?");
		if (answer)
		{
			// showing the blocker //
			document.getElementById(MAPS.DivNames.blocker).style.display = "block";	
			
			// creating a new array with num-markers objects //
			var numMarkersObjArr = new Array();
			var markerObj = null;
			var l = MAPS.Print.movedNumbersArr.length;
			for (var i=0;i<l;i++)
			{
				markerObj = MAPS.Markers.arr[MAPS.Print.movedNumbersArr[i]];
				var numMarkerObj = new Object();
				numMarkerObj.dbId = markerObj.dbId;
				numMarkerObj.numLat = markerObj.numLat;
				numMarkerObj.numLng = markerObj.numLng;
				numMarkersObjArr.push(numMarkerObj);
			}
			
			// sorting and removing double num-markers //			
			numMarkersObjArr.sort(function(a,b){return a.dbId - b.dbId});
			for (var i=0;i<l;i++)
			{
				if (i<l-1)
				{
					if (numMarkersObjArr[i].dbId === numMarkersObjArr[i+1].dbId)
					{
						numMarkersObjArr.splice(i,1);
						i--;
						l--;	
					}
				}
			}
			
			// creating strings for sending db //		
			var numStrA = new Array();
			var latStrA = new Array();
			var lngStrA = new Array();
			for (var i=0;i<l;i++)
			{
				if (numStrA.length > 0)
					numStrA.push(",");
				if (latStrA.length > 0)
					latStrA.push(",");
				if (lngStrA.length > 0)
					lngStrA.push(",");
					
				numStrA.push(numMarkersObjArr[i].dbId);	
				latStrA.push(numMarkersObjArr[i].numLat);
				lngStrA.push(numMarkersObjArr[i].numLng);
			}
			
			MAPS.Parse.updateNumberMarkers(numStrA.join(""),latStrA.join(""),lngStrA.join(""));
			
		}
		
		MAPS.Print.movedNumbersArr = null;
		
		// removing the blocker //
		document.getElementById(MAPS.DivNames.blocker).style.display = "none";	
	},
	
	
	showMarkerList : function()
	{
		document.forms.markers4print.submit();
	},
	
	giveTitle : function()
	{
		var title = prompt("Name the map","");
		title = MAPS.Parse.trim(title);
		if (title != '')
		{
			document.getElementById('userTitle').innerHTML = title;	
			document.forms.markers4print['title'].value = title;	
		}
	}
	
	
};



