﻿var loadedPoints = new Array();
var gmap;
var geocoder;

$(function() {
    jQuery.fn.outerHTML = function(s) {
        return (s)
    ? this.before(s).remove()
    : jQuery("<p>").append(this.eq(0).clone()).html();
    }
    initialize();
});

function initialize() {
    if (GBrowserIsCompatible()) {
        gmap = new google.maps.Map(document.getElementById("map"));
        gmap.setCenter(new google.maps.LatLng(-33, 150), 5);
        var mapOpts = new GMapUIOptions();
        gmap.setMapType(G_HYBRID_MAP);
        gmap.setUIToDefault();

        GEvent.addListener(gmap, "infowindowbeforeclose", function() { $(".selectedTweet").attr("class", "tweet"); selectedID = null; });
        InitDraggableFire();
        InitMyHouse();

        LoadOfficialTweets();
    }
}

function InitDraggableFire() {
    
}


function AddPoint(id, location, isOfficial) {
    if ($.inArray(id, loadedPoints) > -1) return;

    var matchedLocation = location.match(/-?\d+\.\d+[, ]+-?\d+\.\d+/i);
    
    if (matchedLocation) GeocodeLocation(id, matchedLocation + "", isOfficial);
    else $.getJSON("/Locate?" + location, function(data) { GeocodeLocation(id, data.longitude + "," + data.latitude, isOfficial); });
}

var selectedID = null;
function MarkerClicked(marker, id) {
    var divID = "tweet" + id;
    var html = $("#" + divID).outerHTML();
    marker.openInfoWindowHtml(html);

    selectedID = id;

    $("#" + divID).attr("class", "selectedTweet");

    if ($("#officialNotices #" + divID).get().length > 0) $("#officialNotices .content").scrollTo($("#" + divID), 800);
    else $("#communityNotices .content").scrollTo($("#" + divID), 800);
}

function MarkerMouseOver(marker, id) {
    $("#tweet" + id).attr("class", "selectedTweet");
}

function MarkerMouseOut(marker, id) {
    if(selectedID != id) $("#tweet" + id).attr("class", "tweet");
}

var allMarkers = new Array();
function GeocodeLocation(id, location, isOfficial) {
    var imageURL = isOfficial ? "/Content/Images/fire.png" : "/Content/Images/user_fire.png";

    var lat = location.split(',')[0];
    var lng = location.split(',')[1];

    if (isNaN(lat) || isNaN(lng)) return false;

    var pos = new google.maps.LatLng(lng, lat);

    var fireIcon = new GIcon(G_DEFAULT_ICON);
    fireIcon.image = imageURL;
    fireIcon.iconSize = new GSize(24, 37);
    fireIcon.infoWindowAnchor = new GPoint(9, 2);
    fireIcon.shadow = "";

    var markerOptions = { icon: fireIcon };

    var marker = new GMarker(pos, markerOptions);
    marker.id = id;
    gmap.addOverlay(marker);
    allMarkers.push(marker);

    GEvent.addListener(marker, "click", function() {
        MarkerClicked(marker, id);
    });

    GEvent.addListener(marker, "mouseover", function() {
        MarkerMouseOver(marker, id);
    });

    GEvent.addListener(marker, "mouseout", function() {
        MarkerMouseOut(marker, id);
    });
    
    //incidentMarker.id = id;
    loadedPoints.push(id);

    return true;
}