﻿function InitHomeScript() {

    InitPortal();

}


//
function InitPortal() {

    //if ($(".portalColumn").length == 0) {
    //    CreateMap("large");
    //    return;
    //}

    if ($("#LatestActivitiesNearMe").length == 0) {
        CreateMap("large");
        return;
    }

    CreateMap("small");
    
       

//    $(".portalColumn").sortable({
//        opacity: 0.5,
//        revert: true,
//        scroll: true,
//        handle: $(".portalColumn dl dt"),
//        connectWith: $(".portalColumn"),
//        placeholder: "dragDropPlaceholder",
//        forcePlaceholderSize: true,
//        stop: function(event, ui) { OnPortalPositionChanged(event, ui); }
//    });

//    // Hide the portal items
//    $("#NewsBlast").hide();
//    $("#UpcomingActivities").hide();
//    $("#LatestActivitiesNearMe").hide();
//    $("#PastActivities").hide();

//    // Call portal service and set up initial item locations
//    var jsonUrl = "PortalLayoutServicePage.aspx?m=GetPortalLayout";

//    $.getJSON(jsonUrl,
//        function(data, textStatus) {
//            InitPortalLayout(data, textStatus);
//        });

}

function InitPortalLayout(data, textStatus) {

    $.each(data, function(i, portalLayoutItem) { // Activities

        var selector = "#" + portalLayoutItem.PortalItemType;
        $(selector).appendTo($("#portalColumn" + portalLayoutItem.Column)).fadeIn(1000);

    });

    CreateMap("small");
}

function OnPortalPositionChanged(event, ui) {

    var portalLayoutItems = new Array();
    var index = 0;

    $("#portalColumn1 dl").each(function(i) {
        portalLayoutItems[index++] = new PortalLayoutItem(1, i, this.id);
    });

    $("#portalColumn2 dl").each(function(i) {
        portalLayoutItems[index++] = new PortalLayoutItem(2, i, this.id);
    });

    var jsonString = JSON.stringify(portalLayoutItems);

    var jsonUrl = "PortalLayoutServicePage.aspx?m=SavePortalLayout";
    $.post(jsonUrl, { json: jsonString });

}

function PortalLayoutItem(column, position, portalItemType) {

    this.Column = column;
    this.Position = position;
    this.PortalItemType = portalItemType;

}

function CreateMap(mapSize) {

    var mapElement = $("#mapElement");

    if (mapElement.length == 0)
        return;

    localSearch = new GlocalSearch();

    if (!GBrowserIsCompatible())
        return;

    map = new GMap2(mapElement[0]);    

    //if logged in then set the center of the map to users post code    
    var Latitude = $("[id$=userLatitude]").val();
    var Longitude = $("[id$=userLongitude]").val();

    if (Latitude.length > 0 && Longitude.length > 0) {

        map.setCenter(new GLatLng(Latitude, Longitude), 11);
    }
    else {
        map.setCenter(new GLatLng(53.412420, -2.902035), 11);
    }    
    
    map.setZoom(12);
    map.addControl(new GSmallMapControl());

    displayUpcomingActivitiesOnMap(mapSize);
    displayActivityOnMap(mapSize);

    var myLatitude = $("[id$=userLatitude]").val();
    var myLongitude = $("[id$=userLongitude]").val();
    
    //if we have a user post code then set it with a different icon
    if (Latitude.length > 0 && Longitude.length > 0) {
        var markerString = "Me!";
        var postCodeIcon = new GIcon(G_DEFAULT_ICON);
        var opts = new Object();
        opts.pixelOffset = new GSize(32, 5);
        opts.maxWidth = 10;
        opts.maxWidth = 10;
        postCodeIcon.image = "images/house.png";
        var point = new GLatLng(myLatitude, myLongitude);
        markerOptions = { clickable: false, draggable: false, icon: postCodeIcon };
        marker = new GMarker(point, markerOptions);
        marker.openInfoWindowHtml(markerString, opts);
        map.addOverlay(marker)
    }
}


function displayUpcomingActivitiesOnMap(mapSize) {

    $("#UpcomingActivities dd").each(function(i) {    
             
        var dd = $(this);
        var activity = new Object();
        activity.Longitude = dd.find("div[id^='longitude']").text();
        activity.Latitude = dd.find("div[id^='latitude']").text();
        activity.Name = dd.find("div.textItem a").text();
        activity.Description = dd.find("div.textItem a").attr("title");
        activity.ActivityLink = dd.find("div[id^='ActivityLink']").text();
        addActivityToMap(activity, mapSize);

        var mapLinkDiv = dd.find("div.mapLink");


        if (activity.Longitude == 0 || activity.Latitude == 0) {

            mapLinkDiv.hide();

        }
        else {

            // hook up the click event for the map link
            mapLinkDiv.click(function() {

                var dd = $(this).parent();
                var latitude = dd.find("div[id^='latitude']").text();
                var longitude = dd.find("div[id^='longitude']").text();

                map.panTo(new GLatLng(latitude, longitude));

            });
        }
    });
}



function displayActivityOnMap(mapSize) {

    $("#ActivityOnMap dd").each(function(i) {

        var dd = $(this);
        var activity = new Object();
        activity.Longitude = dd.find("div[id^='longitude']").text();
        activity.Latitude = dd.find("div[id^='latitude']").text();
        activity.Name = dd.find("div.textItem a").text();
        activity.Description = dd.find("div.textItem a").attr("title");
        activity.ActivityLink = dd.find("div[id^='ActivityLink']").text();
        addActivityToMap(activity, mapSize);

        var mapLinkDiv = dd.find("div.mapLink");


        if (activity.Longitude == 0 || activity.Latitude == 0) {

            mapLinkDiv.hide();

        }
        else {

            // hook up the click event for the map link
            mapLinkDiv.click(function() {

                var dd = $(this).parent();
                var latitude = dd.find("div[id^='latitude']").text();
                var longitude = dd.find("div[id^='longitude']").text();

                map.panTo(new GLatLng(latitude, longitude));

            });
        }
    });
}


function addActivityToMap(activity,mapSize) {

    if (activity.Latitude == 0 || activity.Longitude == 0)
        return;

    var point = new GLatLng(activity.Latitude, activity.Longitude);
    markerOptions = { clickable: true, draggable: false };
    marker = new GMarker(point, markerOptions);
    map.addOverlay(marker)
    //var locationHref = location.href;
    //var pos = locationHref.indexOf("LargeMap");    

    if (mapSize == "large") {

        marker.info_window_content = "<p><a href='ActivityDetails.aspx?activityId=" + activity.ActivityLink + "'>" + activity.Name + "</a></p>" + "<p>" + activity.Description + "</p>";
        marker.bindInfoWindowHtml(marker.info_window_content, { pixelOffset: new GSize(2, 10), maxWidth: 250 });
               
    }
    else
    {
        GEvent.addListener(marker, "click", function() {
            window.location = "LargeMap.aspx";
        }
        );
    }
}



