﻿function CountryItem(id, name, code, lat, lng, zoom, defaultContactID) {
    this.id = id;
    this.name = name;
    this.code = code;
    this.lat = lat;
    this.lng = lng;
    this.zoom = zoom;
    this.locations = [];
    this.defaultContactID = defaultContactID;
}

function LocationItem(id, countryid, name, lat, lng, zoom, defaultContactID) {
    this.id = id;
    this.countryid = countryid;
    this.name = name;
    this.lat = lat;
    this.lng = lng;
    this.zoom = zoom;
    this.defaultContactID = defaultContactID;
}

function AddressItem(id, countryid, locationid, title, lat, lng, displayAddress, phone, fax, email, regnum, type) {
    this.id = id;
    this.countryid = countryid;
    this.locationid = locationid;
    this.title = title;
    this.lat = lat;
    this.lng = lng;
    this.displayAddress = displayAddress;
    this.phone = phone;
    this.fax = fax;
    this.email = email;
    this.regnum = regnum;
    this.type = type;
    this.marker = null;
}

function ContactEmailItem(id, subject) {
    this.id = id;
    this.subject = subject;
}

var map;
var countriesArray = [];
var locationsArray = [];
var addressesArray = [];
var selectedTypes = [];
var contactsEmails = [];
var icons = [];
var defaultIconURL = "/App_Themes/EgmontCom/images/places.gif";
var currentInfoPanel;
var selectLocationText = "Select ...";
var defaultEmailSubject = "Select ...";
var defaultCountryID = 0;
var defaultContactID = 0;
var closeButtonText = "Close";

function initializeContactMap() {
    if (!GBrowserIsCompatible()) {
        return;
    }

    map = new GMap2(document.getElementById("divGoogleMapContact"));
    if (defaultCountryID == 0 || !countriesArray[defaultCountryID]) {
        map.setCenter(new GLatLng(30.751277776257812, -7.734375), 2);
    }
    else {
        $("#selectCountries").val(defaultCountryID);
        countryChangedByID(defaultCountryID);
    }

    addMarkers();
    map.addControl(new GLargeMapControl3D());
    map.addControl(new GScaleControl());
}

function addMarkers() {
    for (i in addressesArray) {
        addAddress(i, true);
    }
}

function addAddress(addressID, isInit) {
    var markerOptions;
    var marker;
    var addressType = addressesArray[addressID].type;

    if (icons[addressType] != null) {
        markerOptions = { icon: icons[addressesArray[addressID].type] };
    }
    else {
        markerOptions = { icon: icons[0] };
    }

    marker = new GMarker(new GLatLng(addressesArray[addressID].lat, addressesArray[addressID].lng), markerOptions)
    marker.id = addressID;
    addressesArray[addressID].marker = marker;

    GEvent.addListener(marker, "click", function() {
        addOverlay(this.id);
        showAddressEmails(this.id);
    });

    if (isInit && (selectedTypes[addressType] == null || !selectedTypes[addressType])) {
        return;
    }
    
    map.addOverlay(marker);
}

function countryChanged() 
{
    var countryID = $("#selectCountries").val();
    countryChangedByID(countryID);

    if (countryID != 0 && countriesArray[countryID] != null && countriesArray[countryID].defaultContactID != 0 && addressesArray[countriesArray[countryID].defaultContactID] != null) {
        showContactInfo(countriesArray[countryID].defaultContactID);
        showAddressEmails(countriesArray[countryID].defaultContactID);
    }
}

function countryChangedByID(countryID) {
    var selectLocations = $("#selectLocations");
    var locationID;

    if (countryID != null && countryID != "0") {
        var country = countriesArray[countryID];
        if (country != null) {
            map.setCenter(new GLatLng(country.lat, country.lng), country.zoom);

            var options = '<option value="0">' + selectLocationText + '</option>';

            for (i in country.locations) {
                locationID = country.locations[i];

                options += '<option value="' + locationsArray[locationID].id + '">' + locationsArray[locationID].name + '</option>';
            }

            selectLocations.html(options);
        }
    }
    else {
        var options = '<option value="0">' + selectLocationText + '</option>';
        selectLocations.html(options);
    }
}

function locationChanged() 
{
    var locationID = $("#selectLocations").val();

    if (locationID != null) {
        var location = locationsArray[locationID];
        if (location != null) {
            map.setCenter(new GLatLng(location.lat, location.lng), location.zoom);

            if (location.defaultContactID != 0 && addressesArray[location.defaultContactID] != null) {
                showContactInfo(location.defaultContactID);
                showAddressEmails(location.defaultContactID);
            }
        }
    }
}

function showAddressEmails(addressID) {
    contactsEmails = [];
    var selectSubject = $("#ContactForm_SelectedSubjectId");
    var options = '<option value="0">' + defaultEmailSubject + '</option>';
    selectSubject.html(options);

    if (addressID != null && addressID != 0) {
        var request = GXmlHttp.create();
        var handlerUrl = "/Templates/Contacts/GetContactEmails.ashx?contactID=" + addressID;
        request.open("GET", handlerUrl, true);
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                loadContactEmails(request.responseText);
            }
        }

        request.send(null);  
    }
}

function loadContactEmails(xml) {
    var routeXml = GXml.parse(xml);
    var emails = routeXml.getElementsByTagName('email');

    if (emails.length == 0) {
        return;
    }

    var email;
    var id;
    var subject;
    for (var i = 0; i < emails.length; i++) {
        email = emails[i];
        id = email.getAttribute('id');
        subject = email.getAttribute('subject');
        contactsEmails.push(new ContactEmailItem(id, subject));
    }

    var selectSubject = $("#ContactForm_SelectedSubjectId");
    var options = '<option value="0">' + defaultEmailSubject + '</option>';
    for (i in contactsEmails) {
        options += '<option value="' + contactsEmails[i].id + '">' + contactsEmails[i].subject + '</option>';
    }

    selectSubject.html(options);
}

function addOverlay(addressID) {
    var address = addressesArray[addressID];
    
    if (currentInfoPanel != null) {
        currentInfoPanel.remove();
    }
    
    currentInfoPanel = new ContactInfo(address);
    map.addOverlay(currentInfoPanel);
    map.setCenter(new GLatLng(address.lat, address.lng));
}

function RemoveOverlay() {
    if (currentInfoPanel != null) {
        currentInfoPanel.remove();
    }
}

function GetInfoAddressDetails(address) {
    var html = '<p class="EIH">' + address.title + '</p>';

    if (address.displayAddress != "") {
        html += '<p>' + address.displayAddress + '</p>';
    }

    if (address.phone != "") {
        html += '<p>' + address.phone;
        if (address.fax != "")
        {
            html += '<br />' + address.fax + '</p>';
        }
        else
        {
            html += '</p>';
        }
    }
    else if (address.fax != "") {
        html += '<p>' + address.fax + '</p>';
    }

    if (address.email != "") {
        html += '<p><a href="#">' + address.email + '</a>';
        if (address.regnum != "") {
            html += address.regnum + '</p>';
        }
        else {
            html += '</p>';
        }
    }
    else if (address.regnum != "") {
        html += '<p>' + address.regnum + '</p>';
    }

    return html;
}

function showContactInfo(addressID) {
    if (addressID == null || addressID == 0) {
        return;
    }

    var address = addressesArray[addressID];
    if (address != null) {
        var addressDetailsHtml = GetInfoAddressDetails(address);
        $("#addressDetailsInfo").html(addressDetailsHtml);
    }
}

function filterTypes(selectedType) {
    var checkboxType = $("#labelType" + selectedType);
    if (checkboxType) 
    {
        var addOperation = !($(checkboxType).hasClass("checked"));
        for (i in addressesArray) {
            if (addressesArray[i].type == selectedType && addressesArray[i].marker) {
                if (addOperation) {
                    addAddress(i, false);
                }
                else {
                    map.removeOverlay(addressesArray[i].marker);
                }
            }
        }
    }
}

/* ContactInfo custom overlay */
function ContactInfo(address) {
    this.address = address;
    this.pointLatLng = new GLatLng(address.lat, address.lng);
    this.shiftLeft = 10;
    this.shiftTop = -30;
}
ContactInfo.prototype = new GOverlay();

// Creates the DIV representing the ContactInfo
ContactInfo.prototype.initialize = function(map) {
    var div = document.createElement("div");
    var addressDetailsHtml = GetInfoAddressDetails(this.address);
    $("#addressDetailsInfo").html(addressDetailsHtml);
    var html = '<div class="map_popup"><a class="float_r" href="javascript: {}" onclick="RemoveOverlay()">' + closeButtonText + '</a><div class="clear"><!-- --></div><address>' + addressDetailsHtml + '</address></div><div class="arrow_popup"><!-- --></div>';
    div.style.overflow = "hidden";
    div.style.position = "absolute";
    div.style.width = "236px";
    div.innerHTML = html;

    map.getPane(G_MAP_FLOAT_PANE).appendChild(div);

    this.map = map;
    this.mainDiv = div;
}

ContactInfo.prototype.remove = function() {
    if (this.mainDiv.parentNode) {
        this.mainDiv.parentNode.removeChild(this.mainDiv);
    }
}

ContactInfo.prototype.copy = function() {
    return new ContactInfo(this.address);
}

ContactInfo.prototype.redraw = function(force) {
    // We only need to redraw if the coordinate system has changed
    if (!force) return;

    var pointPixels = this.map.fromLatLngToDivPixel(this.pointLatLng);
    
    this.mainDiv.style.left = (pointPixels.x + this.shiftLeft) + "px";
    this.mainDiv.style.top = (pointPixels.y + this.shiftTop) + "px";
}
