﻿// JScript File
        //var color = ''
        //var id = ''
        //var address = ''
        var map = ''
        var baseIcon = ''
        var map = ''
        var mResponse = null
        var mResponseCenter = null
        var mDoSearch = true
        var zoomLevel = 3
        var LongLati = ''
        var Long;
        var Lati;
        var SetCenter = false;
        var isCenterFirstItem = true;   
       // var apiKey = "ABQIAAAAPiF74qlcU4FL5-vAc4rsExTG90SqrlaEGIFZ7S_Y3kL3Q_BOWRROwZbQQRNXCYxlUvR3PTiBjbbHig"
       var apiKey = ""
             
        function GoogleMap(lang,curid)
        {
            loadMap();
            setMapCenter();
            baseIcon=createBaseIcon();  
            CruiseCenter.CruiseOfferDetail.getGoogleCoordinates(lang,curid,getGoogleCoordinates_CallBack);
        }
        
        function getGoogleCoordinates_CallBack(response)
        {
            mResponse = response;
            if (response != null && response.value != null)
            {
                var j = 0;
                var centerPointObtained = false;
                for(i=0;i<response.value.length;i++)
                {
                  var item = parseCoordinates(response.value[i])
                  if (isCenterFirstItem == true && centerPointObtained == false)
                  {
                    var thisRepsonseItem = response.value[i];
                    var coordinates=thisRepsonseItem.split("|");
                    if (coordinates[0] != '')
                    {
                        LongLati = coordinates[0];
                        centerPointObtained = true;
                    }
                  }
                }
                setMapCenter();
            }
        }
        
       // parse the response item to get the google coordinates for city or venue
       // with the color of ballon 
        function parseCoordinates(responseitem)
        {
            var coordinates=new Array();
            var points = new Array();
            coordinates=responseitem.split("|");
            points=coordinates[0].split(",");
            var color = coordinates[1];
            var id=coordinates[8];
            var city = coordinates[3];
            var PortName = coordinates[4];
            var address = city;
            if (isCenterFirstItem == true)
                LongLati = coordinates[0];
            var item = true;
            if(points[0] != null && points[0] != "" && points[1] != null && points[1] != "")
            {
                Long = points[0];
                Lati = points[1];
                markLocation(points[0],points[1],color,id,address,id)
                if(Long == '' || Lati == '')
                    item = false;
            }
            else
            {
                var street = coordinates[6]
                var postalcode = coordinates[7]
                var country = ""
                if (mDoSearch == true)
                    CoordinateSearch(apiKey,street,postalcode,city,country,color,id,address)//searches for the port
                if(!SetCenter)
                    item = false;              
            }
            return item
        }
        

        //Loads the map as zurich city as center
        function loadMap()
        {
            map = new GMap2(document.getElementById("map"));
            //map.addControl(new GSmallMapControl());

            return map;        
        }
        
        function setMapCenter()
        {
            if(LongLati != '')
            {
                var centerPoints = new Array();
                centerPoints=LongLati.split(",");
                if(centerPoints[0] != null && centerPoints[0] != "" && centerPoints[1] != null && centerPoints[1] != "")
                {
                    var lng = centerPoints[0];
                    var latt = centerPoints[1];
                    map.setCenter(new GLatLng(lng,latt), zoomLevel);
                }
            }
            else
                map.setCenter(new GLatLng(41.387917,2.169919), zoomLevel);            
        }
        
         // Create a base icon for all of our markers that specifies the
         // shadow, icon dimensions, etc.
        function createBaseIcon()
        {
            baseIcon = new GIcon();   
            baseIcon.shadow = "images/Map/mm_20_shadow.png";
            baseIcon.iconSize = new GSize(12, 20);
            baseIcon.shadowSize = new GSize(22, 20);
            baseIcon.iconAnchor = new GPoint(6, 20);
            baseIcon.infoWindowAnchor = new GPoint(5, 1);
            baseIcon.infoShadowAnchor = new GPoint(18, 25);
            return baseIcon; 
        }
    
        function markLocation(lng,lat,color,id,address, link)
        {              
            var point = new GLatLng(lng,lat);
            if(color=="red") {
            map.addOverlay(createMarker(point,address,color,link));                   
            }
         }
        

        //if no coordinates available google search 
        function CoordinateSearch(apiKey,street,postalcode,city,country,color,id,address)
        {
          var gRequest = null;
          if(apiKey != '' || street != '' || postalcode != '' || city != '' || country != '')
          {
            
      		//Searching in the geocoder for the coordinates
            var geocoder = new GClientGeocoder();  
			var tempSearchValue = street + ', ' + postalcode +" "+ city;
			geocoder.getLocations(tempSearchValue, getCoordinates);
          }
        }
        
        function getCoordinates(response) {
    	    if (!response || response.Status.code != 200)
	    	    callError("! Folgende Adresse konnte nicht gefunden werden: '" + response.Status.code + " / " + response.name + "'");
	        else{
		        callError("+ Folgende Adresse konnte gefunden werden: '" + response.name + "'");
    		    tempPlace = response.Placemark[0];
                
                // loop to find id of POS
                for(i=0;i<mResponse.value.length;i++) {
                
                      var coordinates=mResponse.value[i].split("|");
                      var city = coordinates[3];
                      var street = coordinates[6];
                      var plz = coordinates[7];
                      var id= coordinates[2];
                                      
                      if (response.name == street + ', ' + plz +" "+ city) {
                        var tempCoords = tempPlace.Point.coordinates[1] + ',' + tempPlace.Point.coordinates[0];
        			    CruiseCenter.CruiseOfferDetail.storeCoordinates(id, tempCoords);
                      }
                }            
		    }        
        }		

var mDefaultErrorOn = false;
function callError(pMessage){
	var tempError = document.getElementById("divError");
	if ((tempError) && ((document.URL.indexOf('&error=true') > 0) || (mDefaultErrorOn))) tempError.innerHTML = tempError.innerHTML + pMessage + '<br />';
}


function GoogleMapAllRegions(lang)
{
    mDoSearch = false;
    zoomLevel = 1;
    loadMap();
    setMapCenter();
    baseIcon=createBaseIcon();  
    CruiseCenter.ReisezieleMain.getGoogleMainRegions(lang,getGoogleCoordinates_CallBack);
}

function GoogleMapRegion(lang,mainRegion,center)
{
    mDoSearch = false;
    isCenterFirstItem = false;
    LongLati = center;
    loadMap();
    setMapCenter();
    baseIcon=createBaseIcon();
      
    CruiseCenter.ReisezieleMain.getGoogleRegions(lang,mainRegion,getGoogleCoordinates_CallBack);
}

function setGoogleMapKey(key)
{
    if(key!='') 
        apiKey = key;
}