﻿   
   
function Directions(el,fromid, toid) {
     var url ='/services/DirectionsAjax.ashx?fromid=' + fromid + '&toid=' + toid;
    $('#window').HTML = '';
        $('#window').show();
 
    $.get(url,
      null,
       ShowDirections
     );    
}

function ShowDirections(text) {
  var el = document.getElementById('windowContent');
    el.innerHTML = ('<p style="padding:10px;">'+ text + '</p>');
    //this.blur();
}

function createMarker(item, icon) {
    var name = item.Name;
    var address = item.Address;
    var cat = item.CategoryMask;
            
            
    var point = new GLatLng(item.Latitude, item.Longitude);
   
    var marker = null;
    if (icon == null)
        marker = new GxMarker(point);
    else
        {
            var ls_choppedName = item.Name.substring(0,22);
            
            //alert (item.Name.Length + ' ' + (ls_choppedName.Length));
            if (item.Name.length > ls_choppedName.length)
                ls_choppedName = ls_choppedName  + '...';
            marker = new GxMarker(point, icon, '<div id="MapInfoPreview" style="margin-left:10px;padding:4px; width:161px; width:153px !important; height:33px; height:25px !important; overflow:hidden;background-image:url(\'/s/i/maps/preview.png\');filter:alpha(opacity=70);-moz-opacity:.70;opacity:.70;" class="PNGFix"><strong>' + ls_choppedName + '</strong> ' + (item.WalkingTime != -1 ? '<br />' + item.WalkingTime + ' mins away' : '' ) + '</div>');
        }
        //marker = new GxMarker(point, icon, '<div id="MapInfoPreview" style="position:absolute;top:500px;left:500px;z-index:1; width:161px; height:33px;background-image:url(\'/s/i/maps/preview.png\');filter:alpha(opacity=45);-moz-opacity:.45;opacity:.45;" class="PNGFix">' + item.Name + '</div>');
    

    
    var url = '';
    var text = '';
    
    if (name != establishmentName) {
      url = "/" + item.StateCode.toLowerCase() + "/" + item.City.toLowerCase().replace(/\s/g, "_") + "/" + item.Id + "/" + item.Name.toLowerCase().replace(/\s/g, "_");
      text = '<b><a href="' + url + '">' + name + '</a></b><br />' +  address + '<br /><br />';
    }
    else 
        text = '<b>' + name + '</b><br />' +  address + '<br /><br />';
        
    if (name != establishmentName) 
        if (item.WalkingTime != -1) {
            text = text + '<em>' + item.WalkingTime + ' minute walk to <br />' + establishmentName;
            //text = text + ' (<a href="#" onclick="Directions(' + item.Id + ',' + establishmentId + '); return false;">Directions</a>)';
            text = text + ' (<a href="#" class="windowOpen" onclick="Directions(this,' + item.Id + ',' + establishmentId + '); return false;">Directions</a>)';
            }
    
    GEvent.addListener(marker, "click", 
        function() { marker.openInfoWindowHtml(text); });
    
    //GEvent.addListener(marker, "mouseover",
     //   function() { 
     //   var el=document.getElementById('MapInfoPreview');
     //   el.style.display='block';
    //    el.innerHTML = item.Name;// + this.style.left;
        
    //    });
        
    //GEvent.addListener(marker, "mouseout",
    //    function() { 
    //    var el=document.getElementById('MapInfoPreview');
    //    el.style.display='none';
    //    });
        
    
    return marker;              
}
   

    function plotArray(a) {
        var counter = 0;
        for (counter = 0; counter < a.length; counter++) {
            var icon = null;
            var cat = a[counter].PrimaryCategory;
            
            var item = a[counter];
            
                var name = item.Name;
                
            if (loadedE[item.Id] == undefined &&  (name != establishmentName)  )
            {
            
                loadedE[item.Id] = cat;//1;
                //loadedE[item.Id].Visible = true;
                
        //alert('cat: ' +cat + ' '+ (cat & 8));
        
                if (cat == 4){
                if (name.indexOf('(Orange Line)')>0)
                    map.addOverlay(createMarker(item, iconSOrange));
                else if (name.indexOf('(Red Line)')> 0)
                    map.addOverlay(createMarker(item, iconSRed));
                else if (name.indexOf('(Green Line)')> 0)
                    map.addOverlay(createMarker(item, iconSGreen));
                else if (name.indexOf('(Blue Line)')> 0)
                    map.addOverlay(createMarker(item, iconSBlue));
                else if (name.indexOf('(Silver Line)')> 0)
                    map.addOverlay(createMarker(item, iconSSilver));                                                
                } else if (cat == 1)
                    map.addOverlay(createMarker(item, iconBar));
                else if (cat == 8)
                    map.addOverlay(createMarker(item, iconRestaurant));               
                else if (cat == 2)
                    map.addOverlay(createMarker(item, iconParking));
                else if (cat == 16)
                    map.addOverlay(createMarker(item, iconHotel));
             
            }
        }
    }
    
    
    function Establishment(id,name, lat, lon, address, city, state, zip, primarycategory, categories, walkingTime, walkingDistance) {
        this.Id = id;
        this.Name = name;
        this.Latitude = lat;
        this.Longitude = lon;
        this.Address = address;
        this.PrimaryCategory = primarycategory;
        this.CategoryMask = categories;
        this.WalkingTime = walkingTime;
        this.WalkingDistance = walkingDistance;
        this.City = city;
        this.StateCode = state;
        this.ZipCode = zip;
}

   var iconBar = new GIcon();
iconBar.image = "/s/i/maps/bar.png";
iconBar.shadow = "/s/i/maps/mm_20_shadow.png";
iconBar.iconSize = new GSize(12, 20);
iconBar.shadowSize = new GSize(22, 20);
iconBar.iconAnchor = new GPoint(6, 20);
iconBar.infoWindowAnchor = new GPoint(5, 1);


   var iconBarLarge = new GIcon();
iconBarLarge.image = "/s/i/maps/bar_large.gif";
iconBarLarge.shadow = "/s/i/maps/mm_20_shadow.png";
iconBarLarge.iconSize = new GSize(28, 28);
iconBarLarge.shadowSize = new GSize(22, 20);
iconBarLarge.iconAnchor = new GPoint(14, 28);
iconBarLarge.infoWindowAnchor = new GPoint(5, 1);


   var iconRestaurant = new GIcon();
iconRestaurant.image = "/s/i/maps/restaurant.png";
iconRestaurant.shadow = "/s/i/maps/mm_20_shadow.png";
iconRestaurant.iconSize = new GSize(20, 20);
iconRestaurant.shadowSize = new GSize(22, 20);
iconRestaurant.iconAnchor = new GPoint(10, 20);
iconRestaurant.infoWindowAnchor = new GPoint(10, 1);



   var iconDistance = new GIcon();
iconDistance.image = "/s/i/maps/distance.png";
iconDistance.shadow = "";//"/s/i/maps/mm_20_shadow.png";
iconDistance.iconSize = new GSize(476, 402);
iconDistance.shadowSize = null;//new GSize(22, 20);
iconDistance.iconAnchor = new GPoint(242, 200);
iconDistance.infoWindowAnchor = new GPoint(5, 1);


    var iconHotel = new GIcon();
iconHotel.image = "/s/i/maps/hotel.png";
iconHotel.shadow = "/s/i/maps/mm_20_shadow.png";
iconHotel.iconSize = new GSize(12, 20);
iconHotel.shadowSize = new GSize(22, 20);
iconHotel.iconAnchor = new GPoint(6, 20);
iconHotel.infoWindowAnchor = new GPoint(5, 1);

    var iconParking = new GIcon();
iconParking.image = "/s/i/maps/parking.png";
iconParking.shadow = "/s/i/maps/mm_20_shadow.png";
iconParking.iconSize = new GSize(12, 20);
iconParking.shadowSize = new GSize(22, 20);
iconParking.iconAnchor = new GPoint(6, 20);
iconParking.infoWindowAnchor = new GPoint(5, 1);

    var iconSRed = new GIcon();
iconSRed.image = "/s/i/maps/subway_red.png";
iconSRed.shadow = "/s/i/maps/mm_20_shadow.png";
iconSRed.iconSize = new GSize(12, 20);
iconSRed.shadowSize = new GSize(22, 20);
iconSRed.iconAnchor = new GPoint(6, 20);
iconSRed.infoWindowAnchor = new GPoint(5, 1);

    var iconSOrange = new GIcon();
iconSOrange.image = "/s/i/maps/subway_orange.png";
iconSOrange.shadow = "/s/i/maps/mm_20_shadow.png";
iconSOrange.iconSize = new GSize(12, 20);
iconSOrange.shadowSize = new GSize(22, 20);
iconSOrange.iconAnchor = new GPoint(6, 20);
iconSOrange.infoWindowAnchor = new GPoint(5, 1);



    var iconSBlue = new GIcon();
iconSBlue.image = "/s/i/maps/subway_blue.png";
iconSBlue.shadow = "/s/i/maps/mm_20_shadow.png";
iconSBlue.iconSize = new GSize(12, 20);
iconSBlue.shadowSize = new GSize(22, 20);
iconSBlue.iconAnchor = new GPoint(6, 20);
iconSBlue.infoWindowAnchor = new GPoint(5, 1);


    var iconSGreen = new GIcon();
iconSGreen.image = "/s/i/maps/subway_green.png";
iconSGreen.shadow = "/s/i/maps/mm_20_shadow.png";
iconSGreen.iconSize = new GSize(12, 20);
iconSGreen.shadowSize = new GSize(22, 20);
iconSGreen.iconAnchor = new GPoint(6, 20);
iconSGreen.infoWindowAnchor = new GPoint(5, 1);



    var iconSSilver = new GIcon();
iconSSilver.image = "/s/i/maps/subway_silver.png";
iconSSilver.shadow = "/s/i/maps/mm_20_shadow.png";
iconSSilver.iconSize = new GSize(12, 20);
iconSSilver.shadowSize = new GSize(22, 20);
iconSSilver.iconAnchor = new GPoint(6, 20);
iconSSilver.infoWindowAnchor = new GPoint(5, 1);

