//////////////////////////////////////////
////////////////////////////////////////
/*
// All rights reserved to Amadot(ZBN) Ltd. (c) 
// Google Maps handling version 2.0.0.1
// last update -  22/04/11
// MAIN JS FILE
- additional files: maps_event.js,maps_markers.js,maps_forms.js ,maps_search.js,maps_parse.js,maps_icons,maps_poly.js   
- this WHOLE namespace has CSS dependencies (maps.css)
- this WHOLE namespace has HTML dependencies 
- YOU NEED TO LOAD THE JS FILE "/omer/js/screen.js" in your HTML FILE (BEFORE THIS JS FILE)
*/
///////////////////////////////////////
//////////////////////////////////////////

//loading google's namespaces
google.load("maps", "2", {"language" : "en"}); 
google.load("search", "1");

/////////////////////////
/////////// MAPS namespace
/////////////////////////
var MAPS = {}; 
MAPS.mapObj = null; //holds the google.maps.map2 object
MAPS.ajaxUrl = "ajax.php";
////////////	defining browser	//////////////////////
MAPS.browser = '';
if (navigator.appVersion.indexOf("MSIE 6") !== -1)
	MAPS.browser = "ie6";
else if (navigator.appVersion.indexOf("MSIE 7") !== -1)
	MAPS.browser = "ie7";
else if (navigator.appVersion.indexOf("MSIE 8") !== -1)
	MAPS.browser = "ie8";
else if (navigator.appVersion.indexOf("Chrome") !== -1)
	MAPS.browser = "chrome";
else if (navigator.userAgent.indexOf("Firefox/3") !== -1)
	MAPS.browser = "ff3"; 
else if (navigator.userAgent.indexOf("Firefox/2") !== -1)
	MAPS.browser = "ff2"; 
else if (navigator.userAgent.indexOf("Opera/") !== -1)
	MAPS.browser = "opera"; 
else 
	MAPS.browser = "ie7"; 	
MAPS.screenX = 0;
MAPS.screenY = 0;
MAPS.bgImg = new Image();
MAPS.bgImg.src = "images/background.png";

////////////////////////////////////
////////////////////////////////////

MAPS.eventListeners = []; //holds all the map event listeners (set on MAPS.mapDefine())
MAPS.timeOutObj = null;

MAPS.DivNames = {};
MAPS.DivNames.sideBar = 'sideBar';
MAPS.DivNames.statusDiv = 'statusDiv';
MAPS.DivNames.blocker = 'blocker';
MAPS.DivNames.formDiv = 'inputFormDiv';
MAPS.DivNames.markerTypeDiv = 'markerTypeDiv';
MAPS.DivNames.ShowMarkersDiv = 'ShowMarkersDiv';
MAPS.DivNames.searchDiv = 'searchDiv';
MAPS.DivNames.ShowMarkersTable = 'markersTable';
MAPS.DivNames.polyControl = 'polyControl';
MAPS.DivNames.searchResults = 'searchResults';
MAPS.DivNames.helpDiv = 'helpDiv';
MAPS.DivNames.writeMeDiv = 'writeMeDiv';
MAPS.DivNames.pleaseWaitDiv = 'pleaseWaitDiv';
MAPS.DivNames.titleDiv = 'titleDiv';
MAPS.DivNames.searchMenuDiv = 'searchMenuDiv';
MAPS.DivNames.markersMenuDiv = 'markersMenuDiv';
MAPS.DivNames.daysFilterMenuDiv = 'daysFilterMenuDiv';
MAPS.DivNames.printTitleDiv = 'printTitleDiv';
MAPS.DivNames.importanceFilterMenuDiv = 'importanceFilterMenuDiv';

//////////////////////////////
MAPS.infoWindow = null; 
MAPS.infoWindow_containers = null; 
MAPS.infoWindowInit = function()
{
	MAPS.infoWindow = MAPS.mapObj.getInfoWindow();
	MAPS.infoWindow.enableMaximize();
	
	//adding listeners to when the infoWindow is maximized & restored
	MAPS.eventListeners[3] = google.maps.Event.addListener(MAPS.infoWindow,  "maximizeclick",  function(){
		document.getElementById(MAPS.DivNames.statusDiv).style.visibility = "hidden";
		MAPS.hidePageElements();
		
	});
	
	MAPS.eventListeners[4] = google.maps.Event.addListener(MAPS.infoWindow,  "restoreclick",  function(){
		document.getElementById(MAPS.DivNames.statusDiv).style.visibility = "visible";
		MAPS.showPageElements();
	});	
	
	MAPS.eventListeners[5] = google.maps.Event.addListener(MAPS.mapObj,  "infowindowopen",  function(){
		if (document.getElementById('infoTable'))
			document.getElementById('infoTable').style.width='100%';
	});
	
	MAPS.eventListeners[6] = google.maps.Event.addListener(MAPS.infoWindow,  "closeclick",  function(){
		document.getElementById(MAPS.DivNames.statusDiv).style.visibility = "visible";
		MAPS.showPageElements();
	});	
	
}

////////////////////////////////
MAPS.Controls = {
	lMap : null,
	scale : null,
	mapType : null
}


//////////////////////////////
//the map initiate function
MAPS.mapDefine = function()
{
	//MAPS.Log.add("MAPS.mapDefine start"); 
	
	//setting the size of the map
	var mapOptions = {
		size: new google.maps.Size(MAPS.screenX,MAPS.screenY),
		draggableCursor: "default",
		draggingCursor: "move"
	}
	
	//creating the map object
	var map = new google.maps.Map2(document.getElementById('MapDiv'),mapOptions);
	
	//centering the map
	map.setCenter(new google.maps.LatLng(City.lat, City.lng), 14); //Paris' location
	
	///////setting map's options//////
	//map.enableContinuousZoom();
	map.enableScrollWheelZoom();
	map.disableDoubleClickZoom();
	//map.enableGoogleBar();
		
	///controls//
	MAPS.Controls.lMap = new google.maps.LargeMapControl();
	MAPS.Controls.scale = new google.maps.ScaleControl();
	MAPS.Controls.mapType = new google.maps.MapTypeControl();
	map.addControl(MAPS.Controls.lMap);
	map.addControl(MAPS.Controls.scale);
	map.addControl(MAPS.Controls.mapType);
	
	//icons
	MAPS.Icons.registerIcons();
	
	//map "click" event handler//
	MAPS.eventListeners[0] = google.maps.Event.addListener(map,  "click",  function(overlay, point){
			if (overlay == null && MAPS.UserEvents.isCtrl) //if the click was on the map & ctrl key is pressed
			{
				MAPS.Overlays.Polygon.tempObj = null;
				MAPS.Forms.InputForm.reset();
				MAPS.Markers.tempPoint = point; 
				MAPS.Forms.InputForm.show();
			}
	});
	
	MAPS.eventListeners[1] = google.maps.Event.addListener(map,  "load",  function(){
		//setTimeout(function(){
			//City.setMarkers();MAPS.MarkersMNG.getBounds();//},1000);
	});
	
	//adding the marker manager event handlers
	MAPS.eventListeners[3] = google.maps.Event.addListener(map,  "dragend",  function(){MAPS.MarkersMNG.getBounds()});
	MAPS.eventListeners[4] = google.maps.Event.addListener(map,  "zoomend",  function(oldLevel, newLevel){MAPS.MarkersMNG.getBounds()});
	
	//GeoCoder
	MAPS.Search.searcher = new google.maps.ClientGeocoder();
	
	//referencing the map object to MAPS.mapObj
	MAPS.mapObj = map;
	
	//continuing the map load process
	setTimeout(function(){MAPS.onloadFunction();}, 100);

	//MAPS.Log.add("MAPS.mapDefine finish");

	//// registering resize event
	if (window.addEventListener)
	    window.addEventListener('resize', MAPS.resizeHandler, true);
	else if (window.attachEvent)
	    window.attachEvent('onresize', MAPS.resizeHandler);
}

MAPS.mapInit = function()
{
	//MAPS.Log.add("MAPS.mapInit start");
	MAPS.screenX = General.screenX;
	//MAPS.screenX = Screen.getInnerX(); //fullscreen version
	MAPS.screenY = General.screenY;
	google.setOnLoadCallback(MAPS.mapDefine);	
	//MAPS.Log.add("MAPS.mapInit end");
}


/**********
LOG 
***********/
MAPS.isLog = debug;
MAPS.Log = {}
MAPS.Log.add = function(txt)
{
	if (MAPS.isLog !== "1")
		return;
	
	try
	{ 
		
		var time = new Date();
		var h = time.getHours();
		var M = time.getMinutes();
		var s = time.getSeconds();
		var m = (time.getTime() % 1000);
		document.getElementById('logDiv').innerHTML += h + ":" + M + ":" + s + ":" + m + "-> " + txt + "<br/>";
	}
	catch(e) {}
}


////////////////////////////////////////
/*
	The MAP LOADING PROCESS 
	the process is different between IE and FF
	THIS PART MUST BE ON THE END OF THE FILE
*/
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
MAPS.pleaseWait = null;
MAPS.showPleaseWaitDiv = function()
{
	///// creating popups /////
	MAPS.pleaseWait = new Popup.Popup(MAPS.DivNames.pleaseWaitDiv, null , {type: Popup.PositionType.SCREEN_CENTER_MIDDLE});
	
	///// showing welcome div popup /////
	MAPS.pleaseWait.show();
	
	//document.getElementById(MAPS.DivNames.pleaseWaitDiv).style.display = "block";
}

MAPS.hidePleaseWaitDiv = function()
{
	if (!MAPS.pleaseWait) return;
	MAPS.pleaseWait.hide();
	//document.getElementById(MAPS.DivNames.pleaseWaitDiv).style.display = "none";
}

MAPS.onloadFunction = function()
{
	MAPS.Forms.ShowMarkersForm.Init();
	MAPS.Forms.InputForm.generateForm();
	
	MAPS.setCenterPosition();
	MAPS.Forms.InputForm.setBlockerHeight();
	
	setTimeout(function(){
		
		///////// creating the markers //////////
		MAPS.getMarkers();
		
		///////// showing page elements //////////
		MAPS.showPageElements();
		MAPS.createFloatingMenus();
		if (username !== '')
			document.getElementById(MAPS.DivNames.statusDiv).style.display = "block";
		
		///////// hiding blocker and intro //////////
		//MAPS.hidePleaseWaitDiv();
		
		///////// starting markers counter //////////
		MAPS.MarkersMNG.getBounds();
	}, 1000);
	
	MAPS.infoWindowInit();

	MAPS.Forms.writeMeDiv.reset();
}

MAPS.resizeHandler = function ()
{
    General.calcScreenSize();
    MAPS.screenX = General.screenX;
    MAPS.screenY = General.screenY;
    document.getElementById('MapDiv').style.height = MAPS.screenY + "px";
    document.getElementById('MapDiv').style.width = MAPS.screenX + "px";
    MAPS.mapObj.checkResize();
}


MAPS.createFloatingMenus = function()
{
	new Float.element(MAPS.DivNames.titleDiv,'titleDivDragTd');
	
	new Float.element(MAPS.DivNames.markersMenuDiv,'markersDragTd');
	var params = {
		element : MAPS.DivNames.markersMenuDiv,
		controllerElement : 'markersControlImg',
		colapseImg : 'images/arrowDowncanvas22.png',
		expandImg : 'images/arrowUpcanvas22.png'
		}
	new Colapse.element(params);
	
	new Float.element(MAPS.DivNames.searchMenuDiv,'searchDragTd');
		var params = {
		element : MAPS.DivNames.searchMenuDiv,
		controllerElement : 'searchControlImg',
		colapseImg : 'images/arrowDowncanvas22.png',
		expandImg : 'images/arrowUpcanvas22.png'
		}
	MAPS.Search.searchMenuDivColapseObject = new Colapse.element(params);
	
	new Float.element(MAPS.DivNames.daysFilterMenuDiv,'daysDragTd');
		var params = {
		element : MAPS.DivNames.daysFilterMenuDiv,
		controllerElement : 'daysControlImg',
		colapseImg : 'images/arrowDowncanvas22.png',
		expandImg : 'images/arrowUpcanvas22.png'
		}
	new Colapse.element(params);
	
	new Float.element(MAPS.DivNames.importanceFilterMenuDiv,'importanceDragTd');
	var params = {
		element : MAPS.DivNames.importanceFilterMenuDiv,
		controllerElement : 'importanceControlImg',
		colapseImg : 'images/arrowDowncanvas22.png',
		expandImg : 'images/arrowUpcanvas22.png',
		state: "expand"
		}
	new Colapse.element(params);
}

MAPS.showPageElements = function()
{
	document.getElementById(MAPS.DivNames.titleDiv).style.display = "block";
	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.importanceFilterMenuDiv).style.display = "block";
}

MAPS.hidePageElements = function()
{
	document.getElementById(MAPS.DivNames.titleDiv).style.display = "none";
	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.importanceFilterMenuDiv).style.display = "none";
}

MAPS.hideMenus = function()
{
	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.importanceFilterMenuDiv).style.display = "none";
}

MAPS.setCenterPosition = function()
{
	// positioning menus //
	document.getElementById(MAPS.DivNames.searchMenuDiv).style.left = (MAPS.screenX - 200) + "px";
	document.getElementById(MAPS.DivNames.markersMenuDiv).style.left = (MAPS.screenX - 200) + "px";
	document.getElementById(MAPS.DivNames.daysFilterMenuDiv).style.left = (MAPS.screenX - 200) + "px";
	document.getElementById(MAPS.DivNames.importanceFilterMenuDiv).style.left = (MAPS.screenX - 200) + "px";
	
	///// positioning the POLY CONTROL DIV
	document.getElementById(MAPS.DivNames.polyControl).style.left = (MAPS.screenX/2) + "px";
	///// positioning the STATUS DIV
	document.getElementById(MAPS.DivNames.statusDiv).style.left = (MAPS.screenX-100) + "px";
	
	/////// input form
	var newLeft = (MAPS.screenX/2) - 340 ;
	var newTop = 5; //distance from top of screen
	document.getElementById(MAPS.DivNames.formDiv).style.left = newLeft + "px";
	document.getElementById(MAPS.DivNames.formDiv).style.top = newTop + "px";
	
	////// help div
	MAPS.Forms.helpDiv.set();
	
	///// write me
	MAPS.Forms.writeMeDiv.set();
}


MAPS.init = function()
{
	setTimeout(function(){
		MAPS.mapInit();
	},100);
}


MAPS.getMarkersHandler = function (response)
{
    if (!response.json) return;
    var markerData = response.json.amadot.data;
    var numOfMarkers = markerData.length;

    for (var i = 0; i < markerData.length; ++i)
        MAPS.Parse.loadMarker(markerData[i]);

    // sending next request
    if (MAPS.Markers.arr.length !== City.totalMarkers) MAPS.getMarkers();

    document.getElementById('progress').innerHTML = "(" + MAPS.Markers.arr.length + "/" + City.totalMarkers + ") Markers were loaded.";
    if (MAPS.Markers.arr.length === City.totalMarkers)
    {
        document.getElementById('toTheMap').style.display = 'block';
    }

    MAPS.Markers.setMarkersOnMapByImportance(4, true);
    MAPS.Markers.setMarkersOnMapByImportance(5, true);
}

MAPS.getMarkers = function()
{
	var data = MAPS.startJsonArray("getMarkers");
	data["data"] = new Array();
	data["data"]["startRow"] = MAPS.Markers.arr.length;
	data["data"]["maxResults"] = 200;
	MAPS.sendAjax(MAPS.toJson(data), new EVENTS.Handler(MAPS.getMarkersHandler));
	return;
}
