﻿// JScript File
     var map = null;
     var gc = new GClientGeocoder();
	 var gmarkers = [];
     var i = 0;
     var lastlinkid;
	 var latitude = null;
	 var longitude = null;
	 var catname = null;
	 var catSViewname = null;
	 var hdnfld=null;
	 var cafeIcon = new GIcon();
	 var cafeIconVw = new GIcon();
	 var cafeIcon1 = new GIcon();
        // cafeIcon.image = getBaseURL() + img/map/map-pin.png";
        //cafeIcon.shadow = getBaseURL() + "img/map/shadow.png";
        //cafeIcon.image = "http://www.yadig.com/img/map/map-pin.png";
        cafeIcon.image = "http://www.yadig.com/img/map/ya_blue.png";
        cafeIcon.shadow = "http://www.yadig.com/img/map/shadow.png";
        cafeIcon.iconSize = new GSize(26, 34);
        cafeIcon.iconAnchor = new GPoint(6, 20);
        markerOptions = { icon:cafeIcon };
        
	 function loadView(id,lat,lang,options,catname) {
	     var catnameAnd=document.getElementById(catname).value;
	        if(catnameAnd=="Beauty & Spa")
            {
            cafeIconVw.image = "http://www.yadig.com/img/map/pins/Beauty.png";
            }
            else if(catnameAnd=="Sports & Leisure")
            {
            cafeIconVw.image = "http://www.yadig.com/img/map/pins/Sport.png";
            }
            else if(catnameAnd=="Medical & Health")
            {
            cafeIconVw.image = "http://www.yadig.com/img/map/pins/Medical.png";
            }
            else if(catnameAnd=="Events & Entertainment")
            {
            cafeIconVw.image = "http://www.yadig.com/img/map/pins/events.png";
            }
            else if(catnameAnd=="Tourism & Attractions")
            {
            cafeIconVw.image = "http://www.yadig.com/img/map/pins/sights.png";
            }
            else if(catnameAnd=="Arts & Culture")
            {
            cafeIconVw.image = "http://www.yadig.com/img/map/pins/culture.png";
            }
            else
            {
	        cafeIconVw.image = "http://www.yadig.com/img/map/pins/"+document.getElementById(catname).value+".png";
	        }
        cafeIconVw.shadow = "http://www.yadig.com/img/map/shadow.png";
        cafeIconVw.shadowSize =  new GSize(42, 38);
        cafeIconVw.iconSize = new GSize(26, 34);
        cafeIconVw.iconAnchor = new GPoint(6, 20);
        cafeIconVw.infoWindowAnchor = new GPoint(30,4)
        markerOptions = { icon:cafeIconVw };
     if (GBrowserIsCompatible()) {
     
         map = new GMap2(document.getElementById(id));
         
         if(options==1)
         {
            map.setUIToDefault();
           
            }
            
          else
          map.addControl(new GSmallZoomControl());
           // map.addControl(new GSmallMapControl());
           map.disableScrollWheelZoom();
         point = new GLatLng(document.getElementById(lat).value,document.getElementById(lang).value);
         
          map.setCenter(point,12);
          var marker = new GMarker(point,markerOptions);
          map.addOverlay(marker);
          }
    }
    
     function loadViewToolTipURL(id,lat,lang,options,tooltip,urlSent,catSViewname) {
        cafeIcon1.image = "http://www.yadig.com/img/map/pins/"+document.getElementById(catSViewname).value+".png";
        cafeIcon1.shadow = "http://www.yadig.com/img/map/shadow.png";
        cafeIcon1.shadowSize = new GSize(42, 38);
        cafeIcon1.iconSize = new GSize(26, 34);
        cafeIcon1.iconAnchor = new GPoint(6, 20);
        cafeIcon1.infoWindowAnchor = new GPoint(30,4)
     if (GBrowserIsCompatible()) {
         map = new GMap2(document.getElementById(id));
         markerOptions = { title:tooltip,icon:cafeIcon1 };
         if(options==1)
            map.setUIToDefault();
          else
          map.addControl(new GSmallZoomControl());
           // map.addControl(new GSmallMapControl());
           map.disableScrollWheelZoom();
         point = new GLatLng(document.getElementById(lat).value,document.getElementById(lang).value);
         
          map.setCenter(point,12);
          var marker = new GMarker(point,markerOptions);
          map.addOverlay(marker);
           GEvent.addListener(marker, "click", function() {

                    window.location = urlSent;
            });
       }
    }

    function load(id,lat,lang,hdn) {
      if (GBrowserIsCompatible()) {
        this.latitude=lat;
        this.longitude=lang;
        hdnfld=document.getElementById(hdn).value;
        map = new GMap2(document.getElementById(id));
         map.setUIToDefault();
         map.disableScrollWheelZoom();
        if(document.getElementById(latitude).value!='' && document.getElementById(longitude).value!='')
        {
        
            onChangeLatLag();
        }
        else
        {
            if(hdnfld!='')
                {
                    showplace(hdnfld) 
                }
            //map.setCenter(new GLatLng(25.271139,55.307485), 12);  
        //map.addControl(new GSmallZoomControl());
        //map.addControl(new GMapTypeControl());
        //map.enableDoubleClickZoom();
		//createContextMenu(map); 
		}
		GEvent.addListener(map, 'click', getAddress);
       
      }
    }
function showplace(address) {
map.clearOverlays();
  gc.getLatLng(
    address,
    function(point) {
      if (!point) {
        map.setCenter(new GLatLng(25.271139,55.307485), 12);  
      } else {
        map.setCenter(point, 13);
        var marker = new GMarker(point,markerOptions);
              map.addOverlay(marker);
        map.addOverlay(marker);
        document.getElementById(latitude).value = point.lat();
			  document.getElementById(longitude).value = point.lng();
        //marker.openInfoWindowHtml(address);
      }
    }
  );
}
	function getAddress(overlay, latlng) {
	
		 document.getElementById(latitude).value = latlng.lat();
		 document.getElementById(longitude).value = latlng.lng();
		 map.clearOverlays();
		 point = new GLatLng(latlng.lat(),latlng.lng());
		 marker = new GMarker(point,markerOptions);
		 map.addOverlay(marker);
	}
    
    function onChangeLatLag()
    {
    	 point = new GLatLng(document.getElementById(latitude).value,document.getElementById(longitude).value);
	     map.clearOverlays();
         var marker = new GMarker(point,markerOptions);
         map.addOverlay(marker);
          map.setCenter(point, 12);
         //marker.openInfoWindowHtml(address);
    }
    
    
    function showAddress(address) {
	 map.clearOverlays();
      if (gc) {
        gc.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 12);
			  map.clearOverlays();
              var marker = new GMarker(point,markerOptions);
              map.addOverlay(marker);
                 map.setCenter(point, 12);
              marker.openInfoWindowHtml(address);
			  document.getElementById(latitude).value = point.lat();
			  document.getElementById(longitude).value = point.lng();
            }
          }
        );
      }
    }
	
	function createContextMenu(map)
   {
      contextmenu = document.createElement("div");
      contextmenu.style.visibility="hidden";
      contextmenu.style.background="#ffffff";
      contextmenu.style.border="1px solid #8888FF";
	  contextmenu.innerHTML = '<a href="javascript:zoomIn()"> <div class="context">  Zoom in  </div></a>' + '<a href="javascript:zoomOut()">  <div class="context">  Zoom out  </div></a>'+ '<a href="javascript:zoomInHere()">  <div class="context">  Zoom in here  </div></a>'+ '<a href="javascript:zoomOutHere()">  <div class="context">  Zoom out here  </div></a>'+ '<a href="javascript:centreMapHere()">  <div class="context">  Centre map here  </div></a>';

      map.getContainer().appendChild(contextmenu);
      GEvent.addListener(map,"singlerightclick",function(pixel,tile) 
      {
          clickedPixel = pixel;
          var x=pixel.x;
          var y=pixel.y;
          if (x > map.getSize().width - 120) 
          { 
            x = map.getSize().width - 120 
          }
          if (y > map.getSize().height - 100) 
          { 
            y = map.getSize().height - 100 
          }
          var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
          pos.apply(contextmenu);
          contextmenu.style.visibility = "visible";
     });
     GEvent.addListener(map, "click", function() 
     {
          contextmenu.style.visibility="hidden";
     });
 }
	 function zoomIn() 
	 {
				map.zoomIn();
				contextmenu.style.visibility="hidden";
	 }      
	 function zoomOut() 
	 {
			   map.zoomOut();
			   contextmenu.style.visibility="hidden";
	 }      
	 function zoomInHere() 
	 {
			   var point = map.fromContainerPixelToLatLng(clickedPixel)
			   map.zoomIn(point,true);
			   contextmenu.style.visibility="hidden";
	 }      
	 function zoomOutHere() 
	 {
			  var point = map.fromContainerPixelToLatLng(clickedPixel)
			  map.setCenter(point,map.getZoom()-1); 
			  contextmenu.style.visibility="hidden";
	 }      
	 function centreMapHere() 
	 {
			 var point = map.fromContainerPixelToLatLng(clickedPixel)
			 map.setCenter(point);
			 contextmenu.style.visibility="hidden";
	 }
 


function getBaseURL() {
        var url = location.href;  // entire url including querystring - also: window.location.href;
        var baseURL = url.substring(0, url.indexOf('/', 14));
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);
        return baseLocalUrl + "/";
}



