﻿
var maxWidth = 250;
var shareInfoWindow = {
    map: null,
    infowindow: null    
};

shareInfoWindow.openInfoWindow = function(marker) {
    shareInfoWindow.infowindow.setContent(marker.content);
    shareInfoWindow.infowindow.open(shareInfoWindow.map, marker);
}

function clearOverlays() {    
    jQuery("#smallMapInfoWindow").empty().css({ "display": "none" });
    var count = 0;
    if (gOffices) {
        /* Deletes the reference to the mark of every mark */
        jQuery(gOffices).each(function() {
            this.setMap(null);
        });        
    }
    gOffices = [];
    if (shareInfoWindow.infowindow != null) {
        shareInfoWindow.infowindow.close();
    }
}

function addNavigationLinks(gOfficesToShow) {
    var content = "";
    var count = 0;
    var next = 0;
    var prev = 0;
    jQuery.each(gOfficesToShow, function() {        
        var tempInfoWindowContent = this.content;
        /* Appends to the clicked mark the previous boton when there are more than 1 panel*/
        if (count != 0) {
            prev = count - 1;
            tempInfoWindowContent += "<a href='#" + prev + "' class='prev-tab'>&#171;Previous</a>";
        }
        /* Appends to the clicked mark the next boton when there are more than 1 panel*/
        if (count != (gOfficesToShow.length - 1)) {
            next = count + 1;
            tempInfoWindowContent += "<a href='#" + next + "' class='next-tab'>Next&#187;</a>";
        }        
        content += "<a id=" + count + "'></a><div id='panel" + count + "' style='display:none'>" + tempInfoWindowContent + "</div>";
        count++;
    });
    return content;
}

function createMultiInfoWindow(content, mapType) {
    if (mapType == "smallMapInfoWindow") { jQuery("#" + mapType).css({ "display": "block" }).append("<a id='close'>Close</a>" + content); }
        
    /* Shows the first panel when the mark is clicked*/
    jQuery("#panel0").css({ "display": "block" });
    /* Hides and shows the panels whenever the next or previous links are clicked */
    jQuery(".next-tab, .prev-tab").click(function() {        
        var href = jQuery(this).attr("href").split("#");
        jQuery("#panel" + href[1]).siblings().css({ "display": "none" });
        jQuery("#panel" + href[1]).css({ "display": "block" });
        jQuery("#close").css({ "display": "block" });
        return false;
    });
    jQuery("#close").click(function() {
        var html = jQuery("#" + mapType).html();
        jQuery("#" + mapType).empty().css({ "display": "none" });
    });    
}

/* This function creates markers and infoWindows for the small maps */
function createMarker(point, data, title_in, iconType, mapType) {        
    var infoWindowType;
    var infoTitle;
    if(title_in == "N/A" || title_in == "" || title_in == null){ infoTitle= iconType }
    else { infoTitle = title_in }
    
    var mark = new google.maps.Marker({
        position: point,
        map: shareInfoWindow.map,
        icon: gicons[iconType],
        content: data,
        title: infoTitle
    });
    
    /* Opens the infowindow for a mark when it is clicked */
    google.maps.event.addListener(mark, "click", function() {        
        var content = "";
        var gOfficesToShow = [];
        var size = gOffices.length - 1;
        /* Search for all the marks with the same position, to know how many are they */
        jQuery.each(gOffices, function() {
            if (mark.position.equals(this.position)) {
                gOfficesToShow.push(this);
            }
        });

        if (gOfficesToShow.length > 1) {
            content = addNavigationLinks(gOfficesToShow);
        }
        /* Opens the infowindow for Small Map */
        if (mapType == "smallMapInfoWindow") {
            if (jQuery("#smallMapInfoWindow").html()) {
                jQuery("#smallMapInfoWindow").empty();
            }
            if (content != "") {
                createMultiInfoWindow(content, mapType);
            }
            /* For when there is just 1 mark in a certain position */
            else {
                jQuery("#" + mapType).css({ "display": "block" }).append("<a id='close'>Close</a><div style='display:block'>" + mark.content + "</div>");
                jQuery("#close").click(function() {
                    var html = jQuery("#" + mapType).html();
                    jQuery("#" + mapType).empty().css({ "display": "none" });
                });
            }
        }
        /* Opens the infowindow for Full Map */
        else {
            if (content != "") {                
                $("#" + mapType).remove();
                var tempContent = mark.content;
                var newContent = "<div id='" + mapType + "'>" + content + "</div>";
                mark.content = newContent;

                shareInfoWindow.openInfoWindow(mark);
                /* Assigned the new css to the Panel0 element now that it is loaded on the page */
                createMultiInfoWindow(newContent, "fullMapInfoWindow");
                var div = jQuery("#" + mapType);
                shareInfoWindow.infowindow.setContent("<div id='" + mapType + "'>" + $(div).html() + "</div>");
                /* Assigned the .click() method to the Next and Previous links now that they are loaded on the page */
                createMultiInfoWindow(mark.content, "fullMapInfoWindow");
                mark.content = tempContent;
            }
            else { shareInfoWindow.openInfoWindow(mark); }
        }
    });    
    gOffices.push(mark);
}

function setMapCenter(country) {
    var mapOptions = [];
    switch (country) {
        case 'Houston Corporate':
            mapOptions[0] = new google.maps.LatLng(31.970804, -99.898682);
            mapOptions[1] = 3;
            break;
        case 'Administrative Office':
            mapOptions[0] = new google.maps.LatLng(35.50019, -119.181931);
            mapOptions[1] = 3;
            break;            
        case 'Alabama':
            mapOptions[0] = new google.maps.LatLng(32.318231,-86.902298);
            mapOptions[1] = 5;
            break;
        case 'Arizona':
            mapOptions[0] = new google.maps.LatLng(34.048928,-111.093731);
            mapOptions[1] = 5;
            break;
        case 'California':
            mapOptions[0] = new google.maps.LatLng(36.778261,-119.417932);
            mapOptions[1] = 4;
            break;
        case 'Colorado':
            mapOptions[0] = new google.maps.LatLng(39.550051,-105.782067);
            mapOptions[1] = 5;
            break;
        case 'Delaware':
            mapOptions[0] = new google.maps.LatLng(38.910832,-75.52767);
            mapOptions[1] = 7;
            break;
        case 'Florida':
            mapOptions[0] = new google.maps.LatLng(27.5375,-81.518555);
            mapOptions[1] = 5;
            break;
        case 'Georgia':
            mapOptions[0] = new google.maps.LatLng(32.157012,-82.902832);
            mapOptions[1] = 5;
            break;
        case 'Illinois':
            mapOptions[0] = new google.maps.LatLng(40.522151,-89.395752);
            mapOptions[1] = 5;
            break;
        case 'Indiana':
            mapOptions[0] = new google.maps.LatLng(40.5472,-85.605469);
            mapOptions[1] = 5;
            break;
        case 'Iowa':
            mapOptions[0] = new google.maps.LatLng(41.877741,-93.098145);
            mapOptions[1] = 5;
            break;
        case 'Kentucky':
            mapOptions[0] = new google.maps.LatLng(37.840157,-84.265137);
            mapOptions[1] = 5;
            break;
        case 'Louisiana':
            mapOptions[0] = new google.maps.LatLng(31.240985,-92.142334);
            mapOptions[1] = 6;
            break;
        case 'Maryland':
            mapOptions[0] = new google.maps.LatLng(39.044786,-76.640625);
            mapOptions[1] = 6;
            break;
        case 'Massachusetts':
            mapOptions[0] = new google.maps.LatLng(42.407211,-71.382437);
            mapOptions[1] = 6;
            break;
        case 'Missouri':
            mapOptions[0] = new google.maps.LatLng(37.964253,-91.831833);
            mapOptions[1] = 5;
            break;
        case 'Nebraska':
            mapOptions[0] = new google.maps.LatLng(41.492121,-99.909668);
            mapOptions[1] = 5;
            break;
        case 'Nevada':
            mapOptions[0] = new google.maps.LatLng(38.80261,-116.419389);
            mapOptions[1] = 5;
            break;
       case 'New Jersey':
            mapOptions[0] = new google.maps.LatLng(40.058324,-74.405661);
            mapOptions[1] = 6;
            break;
        case 'New Mexico':
            mapOptions[0] = new google.maps.LatLng(34.97273,-105.032363);
            mapOptions[1] = 5;
            break;
        case 'North Carolina':
            mapOptions[0] = new google.maps.LatLng(35.759573,-79.0193);
            mapOptions[1] = 5;
            break;
        case 'Ohio':
            mapOptions[0] = new google.maps.LatLng(40.417287,-82.907123);
            mapOptions[1] = 5;
            break;
        case 'Oklahoma':
            mapOptions[0] = new google.maps.LatLng(35.007752,-97.092877);
            mapOptions[1] = 5;
            break;
        case 'Oregon':
            mapOptions[0] = new google.maps.LatLng(43.802819,-120.563965);
            mapOptions[1] = 5;
            break;
        case 'Pennsylvania':
            mapOptions[0] = new google.maps.LatLng(41.203322,-77.194525);
            mapOptions[1] = 5;
            break;            
        case 'South Carolina':
            mapOptions[0] = new google.maps.LatLng(33.836081,-81.163724);
            mapOptions[1] = 6;
            break;
        case 'Tennessee':
            mapOptions[0] = new google.maps.LatLng(35.514343,-86.583252);
            mapOptions[1] = 5;
            break;
        case 'Texas':
            mapOptions[0] = new google.maps.LatLng(31.970804,-99.898682);
            mapOptions[1] = 4;
            break;
        case 'Utah':
            mapOptions[0] = new google.maps.LatLng(39.3173,-111.09375);
            mapOptions[1] = 5;
            break;
        case 'Virginia':
            mapOptions[0] = new google.maps.LatLng(37.431251,-78.662109);
            mapOptions[1] = 5;
            break;
        case 'Washington':
            mapOptions[0] = new google.maps.LatLng(47.754098,-120.717773);
            mapOptions[1] = 5;
            break;
        case 'West Virginia':
            mapOptions[0] = new google.maps.LatLng(38.597626,-80.454903);
            mapOptions[1] = 5;
            break;
        case 'Wyoming':
            mapOptions[0] = new google.maps.LatLng(43.068888,-107.2924);
            mapOptions[1] = 5;
            break;   
        default:
            mapOptions[0] = new google.maps.LatLng(39.232253, -96.328125);
            mapOptions[1] = 3;

    }
    return mapOptions;
}

function setFullMapCenter(country) {
    var mapOptions = [];
    switch (country) {
        case 'Houston Corporate':
            mapOptions[0] = new google.maps.LatLng(31.970804, -99.898682);
            mapOptions[1] = 4;
            break;
        case 'Administrative Office':
            mapOptions[0] = new google.maps.LatLng(35.50019, -119.181931);
            mapOptions[1] = 4;
            break;               
        case 'Alabama':
            mapOptions[0] = new google.maps.LatLng(32.318231,-86.902298);
            mapOptions[1] = 6;
            break;
        case 'Arizona':
            mapOptions[0] = new google.maps.LatLng(34.048928,-111.093731);
            mapOptions[1] = 6;
            break;
        case 'California':
            mapOptions[0] = new google.maps.LatLng(36.778261,-119.417932);
            mapOptions[1] = 5;
            break;
        case 'Colorado':
            mapOptions[0] = new google.maps.LatLng(39.550051,-105.782067);
            mapOptions[1] = 6;
            break;
        case 'Delaware':
            mapOptions[0] = new google.maps.LatLng(38.910832,-75.52767);
            mapOptions[1] = 8;
            break;
        case 'Florida':
            mapOptions[0] = new google.maps.LatLng(27.5375,-81.518555);
            mapOptions[1] = 6;
            break;
        case 'Georgia':
            mapOptions[0] = new google.maps.LatLng(32.157012,-82.902832);
            mapOptions[1] = 6;
            break;
        case 'Illinois':
            mapOptions[0] = new google.maps.LatLng(40.522151,-89.395752);
            mapOptions[1] = 6;
            break;
        case 'Indiana':
            mapOptions[0] = new google.maps.LatLng(40.5472,-85.605469);
            mapOptions[1] = 6;
            break;
        case 'Iowa':
            mapOptions[0] = new google.maps.LatLng(41.877741,-93.098145);
            mapOptions[1] = 7;
            break;
        case 'Kentucky':
            mapOptions[0] = new google.maps.LatLng(37.840157,-84.265137);
            mapOptions[1] = 6;
            break;
        case 'Louisiana':
            mapOptions[0] = new google.maps.LatLng(31.240985,-92.142334);
            mapOptions[1] = 7;
            break;
        case 'Maryland':
            mapOptions[0] = new google.maps.LatLng(39.044786,-76.640625);
            mapOptions[1] = 7;
            break;
        case 'Massachusetts':
            mapOptions[0] = new google.maps.LatLng(42.407211,-71.382437);
            mapOptions[1] = 7;
            break;
        case 'Missouri':
            mapOptions[0] = new google.maps.LatLng(37.964253,-91.831833);
            mapOptions[1] = 6;
            break;
        case 'Nebraska':
            mapOptions[0] = new google.maps.LatLng(41.492121,-99.909668);
            mapOptions[1] = 6;
            break;
        case 'Nevada':
            mapOptions[0] = new google.maps.LatLng(38.80261,-116.419389);
            mapOptions[1] = 6;
            break;
       case 'New Jersey':
            mapOptions[0] = new google.maps.LatLng(40.058324,-74.405661);
            mapOptions[1] = 7;
            break;
        case 'New Mexico':
            mapOptions[0] = new google.maps.LatLng(34.97273,-105.032363);
            mapOptions[1] = 6;
            break;
        case 'North Carolina':
            mapOptions[0] = new google.maps.LatLng(35.759573,-79.0193);
            mapOptions[1] = 6;
            break;
        case 'Ohio':
            mapOptions[0] = new google.maps.LatLng(40.417287,-82.907123);
            mapOptions[1] = 6;
            break;
        case 'Oklahoma':
            mapOptions[0] = new google.maps.LatLng(35.007752,-97.092877);
            mapOptions[1] = 6;
            break;
        case 'Oregon':
            mapOptions[0] = new google.maps.LatLng(43.802819,-120.563965);
            mapOptions[1] = 6;
            break;
        case 'Pennsylvania':
            mapOptions[0] = new google.maps.LatLng(41.203322,-77.194525);
            mapOptions[1] = 7;
            break;            
        case 'South Carolina':
            mapOptions[0] = new google.maps.LatLng(33.836081,-81.163724);
            mapOptions[1] = 7;
            break;
        case 'Tennessee':
            mapOptions[0] = new google.maps.LatLng(35.514343,-86.583252);
            mapOptions[1] = 6;
            break;
        case 'Texas':
            mapOptions[0] = new google.maps.LatLng(31.970804,-99.898682);
            mapOptions[1] = 5;
            break;
        case 'Utah':
            mapOptions[0] = new google.maps.LatLng(39.3173,-111.09375);
            mapOptions[1] = 6;
            break;
        case 'Virginia':
            mapOptions[0] = new google.maps.LatLng(37.431251,-78.662109);
            mapOptions[1] = 6;
            break;
        case 'Washington':
            mapOptions[0] = new google.maps.LatLng(47.754098,-120.717773);
            mapOptions[1] = 6;
            break;
        case 'West Virginia':
            mapOptions[0] = new google.maps.LatLng(38.597626,-80.454903);
            mapOptions[1] = 6;
            break;
        case 'Wyoming':
            mapOptions[0] = new google.maps.LatLng(43.068888,-107.2924);
            mapOptions[1] = 6;
            break;   
        default:
            mapOptions[0] = new google.maps.LatLng(39.095963, -94.570312);
            mapOptions[1] = 3;
    }
    return mapOptions;
}

shareInfoWindow.init = function() {    
    var country = null;
    var latlng = new google.maps.LatLng(3.513421, 21.796875);
    var myOptions = {
        zoom: 2,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        navigationControl: true,
        mapTypeControl: false,
        scaleControl: true
    };
    shareInfoWindow.map = new google.maps.Map(document.getElementById("map"), myOptions);
    shareInfoWindow.infowindow = new google.maps.InfoWindow();
    readMap(country);
}

