﻿// JScript File for Garage Sales

function ShowAdd()
{
    if(MemberLogged)
    {
      $get('browseDiv').style.display = "none";
      $get('addDiv').style.display = "block";
    }
    
}


function ShowMap()
{
    $get('browseDiv').style.display = "block";
    $get('addDiv').style.display = "none";
    
    ShowGarage("");
}

  function DoHideCal(sender,args)
    {
    //alert("Sheet");
        sender.hide();
    }

function garageInfo(uid)
{
    var url = "garageinfo.aspx?uid="+uid;
    GDownloadUrl(url, function(data, responseCode) 
        {
            var container = null;
             if($get("garageinfo")==null )
             {
              container = document.createElement("div");
              container.id = "garageinfo";
             }else
             {
                 container = $get("garageinfo");
             }
             
             container.style.position = "absolute";
             container.style.top=0;
             container.style.left = 0;
             var c1 = map.fromLatLngToDivPixel(this.lastpoint);
             // Container size //
             var map_cnt = map.getContainer();
             var map_cnt_width = map_cnt.offsetWidth;
             var map_cnt_height = map_cnt.offsetHeight;
             if(map_cnt_width == 0)
             {
                map_cnt_width=810;
                map_cnt_height=365;
             }
             // Visible bounds
             var bnds =map.getBounds(); // LatLanBounds
             var p1 = map.fromLatLngToDivPixel(bnds.getNorthEast());// Point
             var p2 = map.fromLatLngToDivPixel(bnds.getSouthWest());// Point
             // Let's decide sides
             var cnt_x = map_cnt_width +( c1.x- p1.x);
             var cnt_y = c1.y - p1.y;
  
             if((cnt_x < 240)||((cnt_x>405)&&(cnt_x<560)))
                
                {
                    container.style.left = "";
                    container.style.right = "0px";
                }
                
             map.getContainer().appendChild(container);
             container.innerHTML = data;
             
        });
     
  
}

function CloseGarageInfo()
{
    map.getContainer().removeChild($get("garageinfo"));
}

function GarageSearch()
{
    ShowGarage("?from="+$get(gar_search_from).value+"&to="+$get(gar_search_to).value+"&q="+$get(gar_search_q).value);
}

// Maps //
function CreateGarageIcon(  zoom )
{
    if(zoom>16) zoom=16;
    var p_icon = G_DEFAULT_ICON;
    
    return p_icon;
}



function ShowGarage(aParams)
{
    map.clearOverlays();

    if(mgr!=null) 
    {
       for(a in mgr) // This is HARD way to delete object //
        {
            mgr[a] = function() {};
        }
        delete mgr;
    }
    mgr = new GMarkerManager(map);  

    var icon = new GIcon();
    if(icon_url!="")
    {
    icon.image = icon_url;
    icon.iconSize = new GSize(26, 26);
    icon.iconAnchor = new GPoint(13, 13);
    icon.infoWindowAnchor = new GPoint(12, 0);
    }else icon = G_DEFAULT_ICON;
    
    GDownloadUrl(items_url+aParams, function(data, responseCode) 
            {
                var bounds = new GLatLngBounds();
                bounds.extend( map.getCenter());
                var xml = GXml.parse(data);
                var markers = xml.documentElement.getElementsByTagName("GarageSales");
                  for (var i = 0; i < markers.length; i++) 
                                {
                                    var point = new GLatLng(parseFloat(markers[i].getAttribute("Lat")),
                                                 parseFloat(markers[i].getAttribute("Long")));
                                    bounds.extend(point);
                                    var text = markers[i].getAttribute("Description");
                                    var title = markers[i].getAttribute("Address");
                                    for(z=11;z<=17;z++)
                                    {
                                        mgr.addMarker( createMarkerBalloon(point,title,text,CreateGarageIcon(z)), z , z );
                                    }
                                    

                                  }
                                
                                if(markers.length > 0) 
                                {
                                    var zoom = map.getBoundsZoomLevel(bounds);
                                    if((zoom>12)||(zoom<11)) zoom=12;
                                    map.setZoom(zoom); // set zoom level to fit all
                                    map.setCenter(bounds.getCenter()); // set Center
                                }
                                
                                mgr.refresh();

          });
   
}



if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 