﻿var map = null;
var groupLayer = null;
var selectLayer = null;
var defaultCenter = null;
var defaultZoom = 7;
var selStyle = "a";
var fixed = 0;
var showSwitch = 0;

function pageLoad()
{
    
    //Init VE
    if (map==null)
    {
        map = new VEMap('userGroupMap'); //userGroupMap is ths DIV element id that contains the map.
        map.SetDashboardSize(VEDashboardSize.Small);

        map.LoadMap();        
        groupLayer = new VEShapeLayer();
        selectLayer = new VEShapeLayer();
        map.AddShapeLayer(groupLayer);   
        map.AddShapeLayer(selectLayer); 
    }
    
    //Hide Show all groups.
    //$get("showAllLink").style.display = "none";
}


function showGrid(value)
{
    if (value)
    {
        $get("userGroupGrid").style.display = "block";
    }
    else
    {
        $get("userGroupGrid").style.display = "none";
    }
    

}


function stateChanged()
{
    //VEMap.Find(what, where, findType, shapeLayer, startIndex, numberOfResults, showResults, createResults, useDefaultDisambiguation, setBestMapView, callback);
    //results = map.Find(txtWhat.value,txtWhere.value,null,null,index,numResults,true,true,true,true,MoreResults);

    showGrid(false);
    var state = getSelectedValue("ctl00_MainContent_ddlState") + ", US";
    map.Find("",state,null,null,1,1,true,false,true,true);
}





//AJAX Calls
function getUserGroups()
{
    var city = getSelectedValue("ctl00_MainContent_ddlCity");
    var radius = getSelectedValue("ctl00_MainContent_ddlRadius");
    
    if (city != "")
    {
        //alert(city);
        var latitude = city.split(',')[0].trim();
        var longitude = city.split(',')[1].trim();
       
        PageMethods.UserGroupSearch(latitude,longitude,radius,mapUserGroups,errorCallback);
        
        //Center and Zoom
        if (map != null)
        {
            defaultCenter = new VELatLong(latitude,longitude);
            map.SetCenterAndZoom(defaultCenter,getZoomByRadius());
        }
        showGrid(true);
    }
    else
    {
        showGrid(false)
    }
    
    
}

function mapUserGroups(userGroups)
{
    var description;
    //Clear last results.
    groupLayer.DeleteAllShapes();    
    map.ClearInfoBoxStyles();
    for(var i = 0;i < userGroups.length;i++)
    {
        var point = new VELatLong(userGroups[i].Latitude,userGroups[i].Longitude);
        
        var pin = new VEShape(VEShapeType.Pushpin,point);
        pin.SetTitle(userGroups[i].GroupName);
        description = '<div style="width:400px">';
        description += '<img src="'+userGroups[i].LogoUrl+'" onload="setLogoWidth(this)" onerror="setDefaultLogo(this)" /><br/>' + userGroups[i].Description;
        if (userGroups[i].HomePageUrl != "")
        {
            description += '<br/><a href="'+ userGroups[i].HomePageUrl + '" target="_blank">' + userGroups[i].HomePageUrl + '</a>';
        }
        description += '</div>';
        //description = userGroups[i].LogoUrl+'<br/>' + userGroups[i].Description;
        pin.SetDescription(description);
        pin.SetCustomIcon(getGroupIcon(userGroups[i].GroupNumber));
        
        groupLayer.AddShape(pin);     
    }
    
    map.SetZoomLevel(getZoomByRadius());
    
}

function getZoomByRadius()
{
    var radius = getSelectedValue("ctl00_MainContent_ddlRadius");
    var zoomLevel;
    switch(radius)
    {
        case "1":
            zoomLevel = 14;
            break;
        case "5":
            zoomLevel = 12;
            break;
        case "10":
            zoomLevel = 10;
            break;
        case "20":
            zoomLevel = 9;
            break;
        case "25":
            zoomLevel = 9;
            break;
        case "50":
            zoomLevel = 8;
            break;
        case "100":
            zoomLevel = 7;
            break;
        case "250":
            zoomLevel = 5;
            break;
        default:
            zoomLevel = defaultZoom;
            break;
    }
    
    return zoomLevel;
    
    
}

function centerOnUserGroup(index)
{
    var pin = groupLayer.GetShapeByIndex(index);
    if (pin != null)
    {
        var points = pin.GetPoints();
        map.SetCenterAndZoom(points[0],14);
        selectLayer.DeleteAllShapes();
        var pin2 = copyPushPin(pin);
        selectLayer.AddShape(pin2);
        groupLayer.Hide();
        selectLayer.Show();
        $get("showAllLink").style.display = "block";
    }
}

function showAllGroups()
{
    groupLayer.Show();
    selectLayer.Hide();
    map.SetCenterAndZoom(defaultCenter,getZoomByRadius());
    $get("showAllLink").style.display = "none";
}

function copyPushPin(pushPin)
{
    var points = pushPin.GetPoints();
    var twinPin = new VEShape(VEShapeType.Pushpin,points[0]);
    twinPin.SetTitle(pushPin.GetTitle());
    twinPin.SetDescription(pushPin.GetDescription());
    twinPin.SetCustomIcon(pushPin.GetCustomIcon());
    
    return twinPin;
}

function setLogoWidth(img)
{

    if (img.width > 400)
    {
        img.width = 400;
    }
}

function setDefaultLogo(img)
{
    //Hide for now.
    img.style.display='none';
}

//Returns the selected value from a drop down list.
function getSelectedValue(id)
{
    var selectObject = $get(id);
    var value = "";
    if (selectObject != null)
    {
        for(var i = 0;i < selectObject.options.length;i++)
        {
            if (selectObject.options[i].selected)
            {
                value = selectObject.options[i].value;
                break;
            }
        }
    }    
    return value;
    
    
}

function errorCallback(error)
{
    alert(error.get_message());
}

function getGroupIcon(IconNumber)
{
    icon = "http://maps.live.com/i/bin/1.3.1204222815.33/pins/RedCircle" + IconNumber + ".gif";
    icon = "../images/MapNumbers/RedCircle" + IconNumber + ".gif";
    //alert(icon);
    return icon;
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

