﻿
/*<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA9ZEHUHujL16Ag9xwE-88LhTJEnghvKzrS_D0CY4ZMn6EvVc8lxRcK7pv2d4gBpyxyZvlNA-__C5h6g"
type="text/javascript"></script>*/
function CampusMap() { }

CampusMap.CampusMap = function(container, copyrightString, tileURL, mode,
    maxZoom, minZoom, defaultZoom, tileAspectRatio,
    centerLat, centerLng, printViewURL) {
    this.markerList = new Array();
    this.mode = mode;
    this.defaultZoom = defaultZoom;
    this.minZoom = minZoom;
    this.maxZoom = maxZoom;
    this.centerLat = centerLat;
    this.centerLng = centerLng;
    this.tileAspectRatio = tileAspectRatio;
    CampusMap.CampusMap.TileURL = tileURL;
    this.container = container;
    this.copyrightString = copyrightString;
    if (printViewURL != null && printViewURL != "") {
        CampusMap.CampusMap.printViewURL = printViewURL;
    }
    else {
        CampusMap.CampusMap.printViewURL = "";
    }
    this.create();
}

//Static functions / variables
CampusMap.CampusMap.MODE_EDIT = 0;
CampusMap.CampusMap.MODE_VIEW = 1;
CampusMap.CampusMap.TileURL = "";
CampusMap.CampusMap.TileWidth = 0;
CampusMap.CampusMap.TileHeight = 0;
CampusMap.CampusMap.printViewURL = "";

CampusMap.CampusMap.CustomGetTileURL = function(tile, zoom) {
    /*var ymax = 1 << zoom;
    var y = ymax - tile.y - 1;
    return CampusMap.CampusMap.TileURL + zoom + "/" + tile.x + "/" + y + ".png";*/

    return "/Images/maps/blank.jpg";
}

//***public callback events***
//function in the form of editClickCallback(lat, lng)
CampusMap.CampusMap.prototype.editClickCallback = null;
//function in the form of editMarkerClickCallback(markerInfoObj, lat, lng)
CampusMap.CampusMap.prototype.editMarkerClickCallback = null;

//Instance variables
CampusMap.CampusMap.prototype.container;
CampusMap.CampusMap.prototype.gmap;
CampusMap.CampusMap.prototype.copyrightString;
CampusMap.CampusMap.prototype.mode;
CampusMap.CampusMap.prototype.tempMarker = null;
CampusMap.CampusMap.prototype.markerList;
CampusMap.CampusMap.prototype.defaultZoom;
CampusMap.CampusMap.prototype.maxZoom;
CampusMap.CampusMap.prototype.minZoom;
CampusMap.CampusMap.prototype.tileAspectRatio;
CampusMap.CampusMap.prototype.centerLat;
CampusMap.CampusMap.prototype.centerLng;


//Public functions
CampusMap.CampusMap.prototype.getMarkerList = function() {
    return this.markerList;
}

CampusMap.CampusMap.prototype.showAllMarkers = function() {
    for (var markerIndex in this.markerList) {
        var markerInfo = this.markerList[markerIndex];

        markerInfo.setVisible(true);
    }
}

CampusMap.CampusMap.prototype.hideAllMarkers = function() {
    for (var markerIndex in this.markerList) {
        var markerInfo = this.markerList[markerIndex];

        markerInfo.setVisible(false);
    }
}

CampusMap.CampusMap.prototype.searchTitle = function(searchString) {
    for (var markerIndex in this.markerList) {
        var markerInfo = this.markerList[markerIndex];

        if (markerInfo.titleContains(searchString)) {
            markerInfo.setVisible(true);
        }
        else {
            markerInfo.setVisible(false);
        }
    }
}

CampusMap.CampusMap.prototype.getMarkerFromId = function(markerId) {
    for(var markerIndex in this.markerList) {
        var markerInfo = this.markerList[markerIndex];
        
        if(markerInfo.markerId == markerId)
            return markerInfo;                      
    }
    
    return null;
}

CampusMap.CampusMap.prototype.searchDescription = function(searchString) {
    for (var markerIndex in this.markerList) {
        var markerInfo = this.markerList[markerIndex];

        if (markerInfo.descriptionContains(searchString)) {
            markerInfo.setVisible(true);
        }
        else {
            markerInfo.setVisible(false);
        }
    }
}

CampusMap.CampusMap.OpenPrintable = function(markerId, url) {
    window.open(CampusMap.CampusMap.printViewURL + "?markerId=" + markerId);
}

CampusMap.CampusMap.prototype.addMarker = function(markerId, lat, lng, title, description, thumbnailImage) {
    var gmap = this.gmap;
    var cmap = this;
    var point = new GLatLng(lat, lng);
    var marker = new GMarker(point, { "title": title });
    var fullContent = "";

    //Define marker content
    var titleA = "";

    //Image thumbnail is defined
    if (thumbnailImage != null && typeof (thumbnailImage) == "string") {
        //print url is set
        if (CampusMap.CampusMap.printViewURL != "") {
            titleA = "<table><tr><td rowspan='2'><img style='border:0px;' src='" + thumbnailImage + "' alt='Thumbnail' /></td><td style='text-align:right;'><a href='javascript:CampusMap.CampusMap.OpenPrintable(" + markerId + ");'>Print</a></td></tr><tr><td style='vertical-align:bottom;'><span style='font-weight:bold;'>" + title + "</span></td></tr></table>";
            //"<div><div style='float:left;'>" + titleA + "</div><div style='float:right;margin-right:15px;'><a href='javascript:CampusMap.CampusMap.OpenPrintable(" + markerId + ");'>Print</a></div><div style='clear:both;'></div></div>";
        }
        //Print url is not set
        else {
            titleA = "<table><tr><td><img style='border:0px;' src='" + thumbnailImage + "' alt='Thumbnail' /></td><td style='vertical-align:bottom;'><span style='font-weight:bold;'>" + title + "</span></td></tr></table>";
        }
    }
    //Thumbnail not defined
    else {
        //print url is set
        if (CampusMap.CampusMap.printViewURL != "") {
            titleA = "<b>" + title + "</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:CampusMap.CampusMap.OpenPrintable(" + markerId + ");'>Print</a>"
        }
        //print url not set
        else {
            titleA = "<b>" + title + "</b>";
        }
    }
    
    fullContent = titleA + "<br />" + description;
    
    var markerInfo = new CampusMap.MarkerInfo(markerId, title, description, marker, point, fullContent);
    this.markerList.push(markerInfo);
    this.gmap.addOverlay(marker);

    switch (this.mode) {
        case CampusMap.CampusMap.MODE_VIEW:
            GEvent.addListener(marker, "click", function() {
                gmap.setCenter(point, gmap.getZoom());

                
                gmap.openInfoWindowHtml(point, fullContent);
            });

            break;
        case CampusMap.CampusMap.MODE_EDIT:
            GEvent.addListener(marker, "click", function() {
                cmap.editMarkerClickCallback(markerInfo, lat, lng);
            });
            break;
    }
}

CampusMap.CampusMap.prototype.setTemporaryMarker = function(lat, lng) {
    if (this.tempMarker != null)
        this.gmap.removeOverlay(this.tempMarker);

    this.tempMarker = new GMarker(new GLatLng(lat, lng));
    this.gmap.addOverlay(this.tempMarker);
}

//Private functions
CampusMap.CampusMap.prototype.getCopyrightCollection = function() {
    var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180),
        new GLatLng(90, 180)), 0, this.copyrightString);

    var copyrightCollection = new GCopyrightCollection("");
    copyrightCollection.addCopyright(copyright);

    return copyrightCollection;
}

CampusMap.CampusMap.prototype.getMapType = function() {
    var tilelayers = [new GTileLayer(this.getCopyrightCollection(), this.minZoom, this.maxZoom)];
    tilelayers[0].getTileUrl = CampusMap.CampusMap.CustomGetTileURL;
    var maptype = new GMapType(tilelayers, new GMercatorProjection(12), "", { errorMessage: "" });

    return maptype;
}

CampusMap.CampusMap.prototype.initEvents = function() {
    GEvent.bind(this.gmap, "click", this, this.onMapClick);
}

CampusMap.CampusMap.prototype.create = function() {
    this.gmap = new GMap2(this.container, { mapTypes: new Array(), backgroundColor: "#FFFFFF" });
    this.gmap.addControl(new GLargeMapControl());
    this.gmap.addMapType(this.getMapType());
    this.gmap.setCenter(new GLatLng(this.centerLat, this.centerLng), this.defaultZoom);

    //Calculate the initial longitude position of the left side (it will span to -1)
    //range of -30 lat to 30 lat = 60 height
    var max_lng = 60 * this.tileAspectRatio * -1;

    var boundaries = new GLatLngBounds(new GLatLng(-30, max_lng), new GLatLng(30, -1));
    overlay = new GGroundOverlay(CampusMap.CampusMap.TileURL, boundaries);
    this.gmap.addOverlay(overlay);
    this.initEvents();
}

//This is just a test function which programatically places a single marker
//It isnt really related to the final product, and is just here for reference
CampusMap.CampusMap.prototype.setMarkers = function() {
    var gmap = this.gmap;
    var point;
    var marker;
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "http://maps.google.com/mapfiles/ms/icons/blue-pushpin.png";

    markerOptions = { icon: blueIcon };

    //1. Pawley Hall, lat: -52.16045455774704, lng: 27.509765625
    point = new GLatLng(-52.16045455774704, 27.509765625);
    marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        var myHtml = "<b>1. Pawley Hall</b><br /><hr />Adult Career Counseling<br />Charter Schools<br />Food Service<br />Oakland University Center for Autism Research, Education and Support (OUCARES)<br />Lowry Early Childhood Center<br />Practicum Counseling<br />Reading<br />School of Education and Human Services";
        gmap.setCenter(point, gmap.getZoom());
        gmap.openInfoWindowHtml(point, myHtml);
    });

    this.gmap.addOverlay(marker);
}

CampusMap.CampusMap.prototype.onMapClick = function(overlay, latlng) {
    if (this.mode == CampusMap.CampusMap.MODE_EDIT) {
        if (this.editClickCallback != null && latlng != null)
            this.editClickCallback(latlng.lat(), latlng.lng());
    }
    //Do something in view mode?
    else {
        /*var myHtml = "The GPoint value is: " + this.gmap.fromLatLngToDivPixel(latlng) + " at zoom level " + this.gmap.getZoom();
        this.gmap.openInfoWindow(latlng, "lat: " + latlng.lat() + ", lng: " + latlng.lng());*/
    }
}

CampusMap.CampusMap.prototype.SetCenter = function(latitude, longitude) {
    if (!this.initialized)
        return;

    this.gmap.panTo(new GLatLng(latitude, longitude));
}

CampusMap.CampusMap.prototype.zoomMarker = function(markerId) {
    var marker = this.getMarkerFromId(markerId);
    
//    var bounds = new GLatLngBounds();
//    bounds.extend(marker.location);
//    this.gmap.setZoom(this.gmap.getBoundsZoomLevel(bounds));
    this.SetCenter(marker.location.lat(), marker.location.lng());
    this.gmap.openInfoWindowHtml(marker.location, marker.fullContent);
}

//Marker info storage
CampusMap.MarkerInfo = function(markerId, title, description, marker, location, fullContent) {
    this.title = title;
    this.fullContent = fullContent;
    this.description = description;
    this.markerObj = marker;
    this.markerId = markerId;
    this.location = location;
}

CampusMap.MarkerInfo.prototype.markerId;
CampusMap.MarkerInfo.prototype.title;
CampusMap.MarkerInfo.prototype.description;
CampusMap.MarkerInfo.prototype.markerObj;
CampusMap.MarkerInfo.prototype.location;
CampusMap.MarkerInfo.prototype.fullContent;

CampusMap.MarkerInfo.prototype.titleContains = function(searchString) {
    return (this.title.toLowerCase().indexOf(searchString.toLowerCase(), 0) > -1);
}

CampusMap.MarkerInfo.prototype.descriptionContains = function(searchString) {
    return (this.description.toLowerCase().indexOf(searchString.toLowerCase(), 0) > -1);
}

CampusMap.MarkerInfo.prototype.setVisible = function(visible) {
    if (visible)
        this.markerObj.show();
    else
        this.markerObj.hide();
}

CampusMap.IsInternetExplorer = function() {
    return navigator.appName.substr(0, 27) == "Microsoft Internet Explorer";
}
