﻿function getNextSibling(startBrother) {
    endBrother = startBrother.nextSibling;
    while (endBrother.nodeType != 1) {
        endBrother = endBrother.nextSibling;
        if (!endBrother)
            return null;
    }
    return endBrother;
}

function enlargeWindow() {
    $get("wrapper").style.width = "auto";
    $get("content").style.width = "auto";
    $("#mainDiv").css("max-width", "10000px");
    $("#mainTable").css("max-width", "10000px");
    $("#divContentBordered").css("max-width", "10000px");
}

function showNextRow(el) {
    if (el.parentNode.parentNode.nextSibling.nodeType != 1) {
        var row = el.parentNode.parentNode.nextSibling.nextSibling;
    }
    else {
        var row = el.parentNode.parentNode.nextSibling;
    }

    row.style.display = "block";
    el.style.display = "none";
}

// this function determines whether the event is the equivalent of the microsoft mouseleave or mouseenter events.
function isMouseLeaveOrEnter(e, handler) {
    if (e.type != 'mouseout' && e.type != 'mouseover') return false;
    var reltg = e.relatedTarget ? e.relatedTarget :
	        e.type == 'mouseout' ? e.toElement : e.fromElement;
    while (reltg && reltg != handler) reltg = reltg.parentNode;
    return (reltg != handler);
}

function changeCollapsibleState(divID, imageID) {
    if (document.getElementById(divID).style.display == "none") {
        document.getElementById(divID).style.display = "block";
        document.getElementById(imageID).src = './Resources/Images/ns-collapse.gif';
    }
    else {
        document.getElementById(divID).style.display = "none";
        document.getElementById(imageID).src = './Resources/Images/ns-expand.gif';
    }

}

var map;
var geocoder;


function initializeGMap(lat, longit, address) {

    //@namespace url(http://www.w3.org/1999/xhtml);@-moz-document domain(maps.google.com),url-prefix(http://www.google.com/maps){#copyright{display:none !important;}}
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        if (address == "") {
            var bounds = map.getBounds();
            var latlng = new GLatLng(parseFloat(longit), parseFloat(lat), 13);
            map.addOverlay(new GMarker(latlng));
            map.setCenter(new GLatLng(parseFloat(longit), parseFloat(lat), 13), 16);
        }
        else {
            geocoder = new GClientGeocoder();
            showLocation(address);
        }
    }
}


function showLocation(address) {
    geocoder.getLocations(address, addAddressToMap);
}

function addAddressToMap(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } else {
        initTooltips();
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        map.setCenter(point);
        map.setZoom(17);
        marker = new GMarker(point);

        var tooltip = new Tooltip(marker, place.address, 5);
        marker.tooltip = tooltip;
        map.addOverlay(marker);

        map.addOverlay(tooltip);
        GEvent.addListener(marker, 'mouseover', function() {
            this.tooltip.show();
        });
        GEvent.addListener(marker, 'mouseout', function() {
            this.tooltip.hide();
        });


        // map.addOverlay(marker);
        // marker.openInfoWindowHtml(place.address);
    }
}


function showAddress(address) {
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
              if (!point) {
                  alert(address + " not found");
              } else {
                  map.setCenter(point, 13);
                  var marker = new GMarker(point);
                  map.addOverlay(marker);
                  marker.openInfoWindowHtml(address);
              }
          }
        );
    }
}
function setDefImage(element) {
    element.src = "Resources/Images/nopicture.jpg";
}

function Tooltip(marker, text, padding) {
    this.marker_ = marker;
    this.text_ = text;
    this.padding_ = padding;
}

function initTooltips() {
    /* Tooptip util Functions */

    Tooltip.prototype = new GOverlay();

    Tooltip.prototype.initialize = function(map) {
        var div = document.createElement("div");
        div.appendChild(document.createTextNode(this.text_));
        div.className = 'tooltip';
        div.style.position = 'absolute';
        div.style.visibility = 'hidden';
        map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
        this.map_ = map;
        this.div_ = div;
    }

    Tooltip.prototype.remove = function() {
        this.div_.parentNode.removeChild(this.div_);
    }

    Tooltip.prototype.copy = function() {
        return new Tooltip(this.marker_, this.text_, this.padding_);
    }

    Tooltip.prototype.redraw = function(force) {
        if (!force) return;
        var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
        var iconAnchor = this.marker_.getIcon().iconAnchor;
        var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
        var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
        this.div_.style.top = yPos + 'px';
        this.div_.style.left = xPos + 'px';
    }

    Tooltip.prototype.show = function() {
        this.div_.style.visibility = 'visible';
    }

    Tooltip.prototype.hide = function() {
        this.div_.style.visibility = 'hidden';
    }

    /* End of Tooptip util Functions */
}
