﻿var map;
var contentString;
var boxText;
var ib;

function HomeControl(controlDiv, map) {

    controlDiv.style.padding = '5px';

    // Set CSS for the control border
    var controlUI = document.createElement('DIV');
    controlUI.style.backgroundColor = '#678AC7';
    controlUI.style.borderStyle = 'solid';
    controlUI.style.borderWidth = '1px';
    controlUI.style.borderColor = '#92A1FF';
    controlUI.style.shadow = true;
    controlUI.style.cursor = 'pointer';
    controlUI.style.textAlign = 'center';
    controlUI.title = 'Click to set the map to Default View';

    controlDiv.appendChild(controlUI);

    // Set CSS for the control interior
    var controlText = document.createElement('DIV');
    controlText.style.fontFamily = 'Arial,sans-serif';
    controlText.style.fontSize = '12px';
    controlText.style.fontWeight = 'Bold';
    controlText.style.paddingLeft = '5px';
    controlText.style.paddingRight = '5px';
    controlText.style.paddingTop = '1.3px';
    controlText.style.paddingBottom = '1.3px';
    controlText.style.color = 'white';
    controlText.innerHTML = 'Default View';
    

    controlUI.appendChild(controlText);

    var latlng = new google.maps.LatLng(25.911005, 9.875);

    // Setup the click event listeners
    google.maps.event.addDomListener(controlUI, 'click', function () {
        map.setCenter(latlng),
          map.setZoom(2)
    });
}


function init() {
    geocoder = new google.maps.Geocoder();

    var latlng = new google.maps.LatLng(25.911005, 9.875);
    var myOptions = {
        zoom: 2,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        , streetViewControl: false
    }
    map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

    //Setup DefaultView button div on map
    var homeControlDiv = document.createElement('DIV');
    var homeControl = new HomeControl(homeControlDiv, map);
    homeControlDiv.index = 1;
    map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);
    }

function addMarker(title, CountryProfileLink, lat, lan, OfficeTitle, address1, address2, address3, city, state, zip, country, phone, fax, contactName, contactjobtitle, ReadbioLink, ContactImagePath, MapMarkerImage, showMarker) {

    address = "";

    if (address1)
        address = address + address1 + ", ";
    if (address2)
        address = address + address2 + ", ";
    if (address3)
        address = address + address3 + ", ";

    if (city)
        address = address + city + ", ";

    if (state)
        address = address + state + ", ";

    if (zip)
        address = address + zip + ", ";

    if (country)
        address = address + country;


    geocoder = new google.maps.Geocoder(lat, lan);
    var location = new google.maps.LatLng(lat, lan);
    var image = new google.maps.MarkerImage(MapMarkerImage,
            new google.maps.Size(24, 39),
            new google.maps.Point(0, 0));
    var marker = new google.maps.Marker({
        map: map,
        draggable: false,
        icon: image,
        position: location
    });
    marker.setTitle(title);
    contentString = "";
    contentString = contentString + " <div style=\"top: 150px; left: 194px; display: block;\" id=\"" + title + "\" class=\"contact-info-wrapper\">";
    contentString = contentString + "<div class=\"contact-info-content clearfix\">";
    contentString = contentString + "<div class=\"contact-info-left\"> ";
    contentString = contentString + "<h3>" + title + "</h3><p>";

    if (OfficeTitle)
        contentString = contentString + OfficeTitle + "<br />";

    if (address1)
        contentString = contentString + address1 + "<br />";

    if (address2)
        contentString = contentString + address2 + "<br />";

    if (address3)
        contentString = contentString + address3 + "<br />";

    if (city)
        contentString = contentString + city + ", ";

    if (state)
        contentString = contentString + state + " ";

    if (zip)
        contentString = contentString + zip + " ";

    if (country)
        contentString = contentString + "<br />" + country + "<br />";
    else
        contentString = contentString + "<br />";

    if (CountryProfileLink) {
        contentString = contentString + "<a href=\"" + CountryProfileLink + "\"> [COUNTRY PROFILE]</a>";
    }
    contentString = contentString + "</p><p><a href=\'http://www.google.com\/maps?q=to:" + address + "\' target='_blank'>&rsaquo; Get Directions</a></p>";
    contentString = contentString + "<p class=\"last-child\"><b>Phone:</b> " + phone + "<br /><b>Fax:</b> " + fax + "</p>";
    contentString = contentString + "</div>";
    contentString = contentString + "<div class=\"contact-info-right\">";
    contentString = contentString + "<div class=\"img-frame\">";
    contentString = contentString + "<img src=\"" + ContactImagePath + "\" width=\"54\" height=\"54\" alt=\"Employee\" />";
    contentString = contentString + "</div>";
    contentString = contentString + "<p><b>" + contactName + "</b><br />" + contactjobtitle + "</p>";

    if (ReadbioLink)
        contentString = contentString + "<a href=\"" + ReadbioLink + "\" class=\"open-dialog\" onclick=\"javascript:opendialog('" + ReadbioLink + "');return false;\">&rsaquo; Read Bio</a>";

    contentString = contentString + "</div>";
    contentString = contentString + "</div>";
    contentString = contentString + "</div>";

    boxText = document.createElement("div");

    boxText.innerHTML = contentString;

    var myOptions = {
        content: boxText
        , disableAutoPan: false
    	, pixelOffset: new google.maps.Size(-547, -153)
        , closeBoxURL: ""
    };
    google.maps.event.addListener(marker, "click", function (e) {
        if (ib) {
            ib.close();
        }
        ib = new InfoBox(myOptions);
        ib.open(map, marker);
        if ($.browser.msie && $.browser.version == "7.0") {
            $(ib.getContent()).find(".contact-info-right").hide().fadeIn('fast');
        }

    });
    google.maps.event.addListener(map, "click", function (e) {
        if (ib) {
            ib.close();
        }
    });

    if (showMarker == "Yes" && lat != "" && lan != "") {
        if (ib) {
            ib.close();
        }
        ib = new InfoBox(myOptions);
        ib.open(map, marker);
        if ($.browser.msie && $.browser.version == "7.0") {
            $(ib.getContent()).find(".contact-info-right").hide().fadeIn('fast');
        }

    }
}
