// INIT
var map,
    count = 0,
    users = {},
    pics;

// Functions & Data
function googleInitialize(lat, lon, mapData, picURL) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(lat, lon), 10);

        pics = picURL;

        var customUI = map.getDefaultUI();
        customUI.controls.scalecontrol = false;
        customUI.maptypes.hybrid = false;
        customUI.maptypes.physical = false;
        map.setUI(customUI);

        var markers = mapData.split("|");
        for (var i=0, len=markers.length; i<len; ++i) {
            var userData = markers[i].split(",");
            users[userData[0]] = userData;
            markLocation(userData);
        }
    }
}

function panToCenter(lat, lon) {
    map.panTo(new GLatLng(lat, lon));
}

function markLocation(userData) {
    // set point and marker icon
    var point = new GLatLng(userData[2],userData[3]);
    var marker = new GMarker(point, { icon:getUserIcon(pics + userData[11] + 'gm.gif')});

    // display info window on click
    GInfoWindowOptions = { maxWidth:'500' }
    GEvent.addListener(marker, "mouseover", function() {
        marker.openInfoWindowHtml("<div class='float-left' style='margin-right:5px;height:100px;'><img src='" + pics + userData[11] + "75x100.jpg' border='0' width='75' height='100' alt='Register' /></div><div class='map_infowindow'>" + userData[1] + "<br />" + userData[4] + ", " + userData[5] + "<br />" + userData[6] + " seeking " + userData[7] + " aged " + userData[8] + "-" + userData[9] + "<br />Photos: 14" + "<br /><a class='profile_modal' href='javascript:openModal(" + userData[0] + ");'>Register now to contact</a></div>", GInfoWindowOptions);
    });

    // add icon to map
    map.addOverlay(marker);
}

function getUserIcon(image) {
    var userIcon = new GIcon();
    userIcon.image = image; 
    userIcon.iconSize = new GSize(48,38);
    userIcon.iconAnchor = new GPoint(48,38);
    userIcon.infoWindowAnchor = new GPoint(40,0);
    return userIcon;
}

function openModal(userID) {
    show_hide('#miniprofile_popup', false);

    jQuery('#miniprofile_pic').attr('src', pics + users[userID][11] + "245x184.jpg");
    jQuery('#miniprofile_username').html(users[userID][1]);
    jQuery('#miniprofile_location').html("Location: " + users[userID][4] + ", " + users[userID][5]);
    jQuery('#miniprofile_info').html(users[userID][6] + " seeking " + users[userID][7] + " aged " + users[userID][8] + "-" + users[userID][9]);
    jQuery('#miniprofile_rating').html("Rating: " + users[userID][10]);
    jQuery('#miniprofile_register').html(users[userID][1]);

    invokeModal();
}

function invokeModal() {
    jQuery('#register_popup').show();
    jQuery.fancybox({
        href: '#register_popup',
        title: "FREE Instant Registration",
        width: 600, 
        hideOnOverlayClick: false,
        autoDimensions: false,
        onClosed: function() {
            jQuery('#register_popup').hide();
        }
    });
}

