var mapScriptTagId = null;
var map = null;
var geocoder = null;

/**
 * Initialize map
 */
function initMap() {
    // get all javascripts to initialize google maps
    map = new GMap2(document.getElementById("COMPANY_MAP"));
    // mark center
    //map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    // initialize geocoder to point address
    geocoder = new GClientGeocoder();
}

/**
 * Displays map for the address passed
 */
function showMapPanel(name, address1, address2) {
    if(address2 != null && trim(address2) != '') {
        // set window title
        var el = document.getElementById('COMPANY_MAP_TITLE');
        if (el) {
            // check if address is already being displayed
            if(el.innerHTML.indexOf("close.gif") > -1) {
                // no need to render the address again
                return;
            }
            // get existing title
            var previousTitle = el.innerHTML;
            // set an attribute
            el.setAttribute("previousTitle", previousTitle);
            // prepare new title
            var mapTitle = '<span class="boldtext" style="float:left;">';
            //if(trim(name) != '')
            //    mapTitle += '<div style="font-size:11px">'+name+'</div>';
            if(trim(address1) != '')
                mapTitle += address1;
            if(trim(address1) != '' && trim(address2) != '')
                mapTitle += ", ";
            if(trim(address2) != '')
                mapTitle += address2;
            mapTitle += '</span>';
            mapTitle += '<span style="float:right"><img style="cursor:pointer" src="chameleon/purpleskin/icons/close.gif" width="16px" height="16px" alt="Close" title="Close" style="cursor:pointer" onclick="hideMapPanel(); return false;" /></span>';

            el.innerHTML = mapTitle;
        }

        if(mapScriptTagId != null) {
            if (GBrowserIsCompatible()) {
                // in case, maps are uninitialized
                if(!geocoder) initMap();
                if (geocoder) {
                    // prepare address to be mapped
                    var mapaddress = trim(address1);
                    if(mapaddress != ""){
                        mapaddress += " ";
                    }
                    mapaddress += trim(address2)
                    geocoder.getLatLng (
                        // full address
                        mapaddress,
                        function (point) {
                            if (!point) {
                                if(window.console)console.log('Address not found. Check city and zip.')
                                // address not found
                                // call API again for ciy state and zip
                                geocoder.getLatLng (
                                    // city, state, zip
                                    trim(address2),
                                    function (point2) {
                                        if(!point2) {
                                            // error, handle gracefully
                                            alert(mapaddress + " not found");
                                        }
                                        else {
                                            // found city, state and zip at least
                                            plotPoint(point2, name, address1, address2);
                                        }
                                    }
                                );
                            }
                            else {
                                // found full address, plot the point
                                plotPoint(point, name, address1, address2);
                            }
                        }
                    );
                }
            }
        }
        else {
            var mapPanelBody = document.getElementById("COMPANY_MAP");
            mapPanelBody.innerHTML = "<br/><br/><center>Map not available</center>";
        }
    }
}

/**
 * Plot point on the map
 */
function plotPoint(point, name, address1, address2) {
    // Create our marker icon
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.image = "chameleon/purpleskin/icons/map.gif";
    baseIcon.iconSize = GSize(16, 16);
    baseIcon.shadow = "";
    // head quarter image
    //baseIcon.image = "http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/examples/images/headquarters.png";
    //baseIcon.iconSize = GSize(32, 32);
    // Set up our GMarkerOptions object
    markerOptions = {icon:baseIcon};
    // set center
    // 1st parameter is the point
    // 2nd is the zoom level (higher the better)
    map.setCenter(point, 13);
    // map control for zoom in / out
    map.addControl(new GSmallMapControl());
    var marker = new GMarker(point, markerOptions);
    map.addOverlay(marker);
    // for popup window
    var address = '';
    address += "<span style='float:left'><img src='./imagesEx/favicon.png' />&nbsp;&nbsp;</span>";
    address += "<table cellspacing='0' cellpadding='0' border='0' width='200px' class='smallnormaltext'>";
    if(trim(name) != '')
        address += "<tr><td><b>"+name+"</b></td></tr>";;
    if(trim(address1) != '')
        address += "<tr><td>"+address1+"</td></tr>";;
    if(trim(address2) != '')
        address += "<tr><td>"+address2+"</td></tr>";;
    address += "</table>";
    marker.openInfoWindowHtml(address);
}

/**
 * Hides map panel
 */
function hideMapPanel() {
    var el = document.getElementById('COMPANY_MAP');
    if(el) {
        window[el.getAttribute('defaultFunctionName')]();
    }
    // set title
    $('COMPANY_MAP_TITLE').innerHTML = $('COMPANY_MAP_TITLE').getAttribute('previousTitle');
    // nullify objects
    map = null;
    geocoder = null;
}

