﻿// JScript File

var SideBarWidth = 180;
var t;
var MapWidth = 0;
var Popup, Popup2;

function OpenWindow(url) {
    window.open(url,'_blank','toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, titlebar=yes, fullscreen=no, copyhistory=yes, left=0, top=0, width=' + screen.availWidth.toString() + ', height=' + screen.availHeight.toString());
}
        
function FullScreen()
{
    var SideBarColumn = document.getElementById("SideBarColumn");
    var SideBarContent = document.getElementById("SideBarContent");
    var FullScreenLink = document.getElementById("MapFullScreen");
    var MapFooter = document.getElementById("MapFooter");
    
    //if (SideBarColumn.style.width == "200px")
    if (SideBarContent.style.display != "none")
    {
        /*MapFooter.style.position = "absolute";
        MapFooter.style.bottom = "1px";*/
        MapFooter.style.left = "5px";
        
        //SideBarColumn.style.width = "10px";
        SideBarColumn.style.borderRight = "1px none #708090";
        SideBarContent.style.display = "none";
        
        FullScreenLink.innerHTML = "Normal View";
        
        //FullScreenLink.title = "Normal View";
        IsFullScreen = true;
    }
    else
    {
        /*MapFooter.style.position = "";
        MapFooter.style.bottom = "";*/
        MapFooter.style.left = "205px";
        
        //SideBarColumn.style.width = "200px";
        SideBarColumn.style.borderRight = "1px solid #708090";
        SideBarContent.style.display = "block";
        
        FullScreenLink.innerHTML = "Full Screen View";
        
        //FullScreenLink.title = "Full Screen View";
        IsFullScreen = false;
    }
    
    delete SideBarColumn;
    delete SideBarContent;
    
    return false;
}

function Email()
{
    var IsPopupOpened = false;
    
    MapTitleEncoded = document.getElementById("ctl00_cphSideBar_hdnMapTitleEncoded").value;
    MapURLEncoded = document.getElementById("ctl00_cphSideBar_hdnMapURLEncoded").value + "%26bbox%3d" + document.getElementById("ctl00_cphSideBar_hdnBBox").value + "%26layers%3d" + document.getElementById("ctl00_cphSideBar_hdnLayers").value;

    ClosePopup();
    
    if (Popup2 != null)
    {
        if (!Popup2.closed)
        {
            IsPopupOpened = true;            
        }
    }

    if (IsPopupOpened) 
    {
        Popup2.location = "email.aspx?url=" + MapURLEncoded + "&title=" + MapTitleEncoded;
    }
    else
    {
        Popup2 = window.open("email.aspx?url=" + MapURLEncoded + "&title=" + MapTitleEncoded,'_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, titlebar=yes, fullscreen=no, copyhistory=yes, left=' + (((screen.availWidth - 500) / 2).toString())  + ', top=' + (((screen.availHeight - 500) / 3).toString()) + ', width=500, height=500');
    }
    
    window.focus();
    Popup2.focus();

    return false;
}

function Bookmark()
{
    MapTitle = document.getElementById("ctl00_cphSideBar_hdnMapTitle").value;
    MapURL = document.getElementById("ctl00_cphSideBar_hdnMapURL").value + "&bbox=" + document.getElementById("ctl00_cphSideBar_hdnBBox").value + "&layers=" + document.getElementById("ctl00_cphSideBar_hdnLayers").value;

    if (navigator.appName == "Microsoft Internet Explorer")
    {
        window.external.AddFavorite(MapURL,MapTitle);
    }
//    else if (navigator.appName == "Netscape")
//    {
//        
//        window.sidebar.addPanel(MapTitle,MapURL,"");
//    }
    else
    {
        window.prompt('For non Microsoft Internet Explorer browser, please copy the URL below and bookmark it manually.', MapURL);
    }
}

function Comments()
{
    MapURLEncoded = document.getElementById("ctl00_cphSideBar_hdnMapURLEncoded").value + "%26bbox%3d" + document.getElementById("ctl00_cphSideBar_hdnBBox").value + "%26layers%3d" + document.getElementById("ctl00_cphSideBar_hdnLayers").value;

    OpenWindow('http://www.reefbase.org/contribute/comments.aspx?url=' + MapURLEncoded);
}

function PopupSave(url) {
    var IsPopupOpened = false;

    MapURLEncoded = document.getElementById("ctl00_cphSideBar_hdnMapURLEncoded").value + "%26bbox%3d" + document.getElementById("ctl00_cphSideBar_hdnBBox").value + "%26layers%3d" + document.getElementById("ctl00_cphSideBar_hdnLayers").value;

    ClosePopup();
    
    if (Popup2 != null)
    {
        if (!Popup2.closed)
        {
            IsPopupOpened = true;            
        }
    }

    if (IsPopupOpened) 
    {
        Popup2.location = url + "&theme=" + document.getElementById("ctl00_cphSideBar_hdnTheme").value + '&mapurl=' + MapURLEncoded;
    }
    else
    {
        Popup2 = window.open(url + "&theme=" + document.getElementById("ctl00_cphSideBar_hdnTheme").value + '&mapurl=' + MapURLEncoded,'_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, titlebar=yes, fullscreen=no, copyhistory=yes, left=' + (((screen.availWidth - 500) / 2).toString())  + ', top=' + (((screen.availHeight - 500) / 3).toString()) + ', width=500, height=500');
    }
    
    window.focus();
    Popup2.focus();
    
    return false;
}

function PopupInfo(url) {
    //ClosePopup();
    //window.open(url + "&bbox=" + document.getElementById("ctl00_cphSideBar_hdnBBox").value + "&query=" + document.getElementById("ctl00_cphSideBar_hdnQuery").value,'_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, titlebar=yes, fullscreen=no, copyhistory=yes, left=' + (((screen.availWidth - 640) / 2).toString())  + ', top=' + (((screen.availHeight - 480) / 3).toString()) + ', width=640, height=480');
    
    var noaayear = "";
    var IsPopupOpened = false;

    if (document.getElementById("ctl00_cphSideBar_cboDHWYear") != null)
    {
        noaayear = "&noaayearmonth=" + document.getElementById("ctl00_cphSideBar_hdnNOAAYearMonth").value;
    }
    
    ClosePopup2();

    
    if (Popup != null)
    {
        if (!Popup.closed)
        {
            IsPopupOpened = true;            
        }
    }

    if (IsPopupOpened) 
    {
        Popup.document.getElementById("InfoMainFrame").style.display = "none";
        Popup.location = url + "&bbox=" + document.getElementById("ctl00_cphSideBar_hdnBBox").value + "&wms=" + document.getElementById("ctl00_cphSideBar_hdnWMS").value + noaayear;
    }
    else
    {
        Popup = window.open(url + "&bbox=" + document.getElementById("ctl00_cphSideBar_hdnBBox").value + "&wms=" + document.getElementById("ctl00_cphSideBar_hdnWMS").value + noaayear,'_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, titlebar=yes, fullscreen=no, copyhistory=yes, left=10, top=10, width=500, height=' + (screen.availHeight - 80).toString());
    }
    
    window.focus();
    Popup.focus();

    return false;
}

function SetBBox(value) {
    var BBox = value.substr(0,value.lastIndexOf(","));
    document.getElementById("ctl00_cphSideBar_hdnBBox").value = BBox;
}

function AddCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function SetMapLatLon(Lat, Lon) {
    /*var DecimalLength = Lat.substr(Lat.indexOf(".") + 1).length;
    if (DecimalLength < 3) {
        for(i=1;i<=(3-DecimalLength);i++) {
            Lat += "0";
        }
    }
    
    DecimalLength = Lon.substr(Lon.indexOf(".") + 1).length;
    if (DecimalLength < 3) {
        for(i=1;i<=(3-DecimalLength);i++) {
            Lon += "0";
        }
    }*/
    
    if (Lon > 180)
    {
        Lon -= 360.0;
    }
    
    document.getElementById("MapLat").innerHTML = 'Lat: ' + Lat.toString().substr(0,8);
    document.getElementById("MapLon").innerHTML = 'Lon: ' + Lon.toString().substr(0,8);
}

function SetMapScale(value) {
    var MapScale = AddCommas(value.substr(value.lastIndexOf(",") + 1));
    /*var GoogleScale = (255000000 - parseInt(value.substr(value.lastIndexOf(",") + 1))) / 21007043;
    
    if (GoogleScale < 1)
    {
        GoogleScale = 1
    }
    
    document.getElementById("ctl00_cphSideBar_hdnGoogleScale").value = GoogleScale;*/
    
    //var FormattedMapScale = "";
    //var Comma = "";
    //var j = 4;
    
    /*alert(MapScale);
    
    for (i=MapScale.length-1;i>=0;i--)
    {
        if ((MapScale.length - i) == 4) {
            Comma = ",";
        }
        else
        {
            Comma = "";
        }
        
        FormattedMapScale = MapScale.charAt(i) + Comma + FormattedMapScale;
        
        j += 3;
    }
    alert(FormattedMapScale);
    document.getElementById("MapScale").innerText = "Scale = 1 : " + FormattedMapScale + " ";*/
    document.getElementById("MapScale").innerHTML = "Scale = 1 : " + MapScale + " ";
}

function ClosePopup()
{
    if (Popup != null)
    {
        if (!Popup.closed)
        {
            Popup.close();
        }
        Popup = null;
    }
}

function ClosePopup2()
{
    if (Popup2 != null)
    {
        if (!Popup2.closed)
        {
            Popup2.close();
        }
        Popup2 = null;
    }
}

function Topography(value) {
    var obj = document.getElementById("ctl00_cphSideBar_hdnLayers")
    var layers = obj.value;
    var LayerList = obj.value.split(",");
    
    if (value == "true")
    {
        if (layers.indexOf("Topography") == -1)
        {
            if (layers.length = 0)
            {
                layers = "Bathymetry,Topography";
            }
            else
            {
                layers += ",Bathymetry,Topography";
            }
        }
    }
    else
    {
        if ((layers.indexOf("Topography") >= 0) || (layers.indexOf("Bathymetry") >= 0))
        {
            layers = "";
            
            for (ChildLayer in LayerList)
            {
                if ((LayerList[ChildLayer] != "Bathymetry") && ((LayerList[ChildLayer] != "Topography")))
                {
                   layers += "," + LayerList[ChildLayer];
                }
            }
            
            if (layers != "")
            {
                layers = layers.substr(1);
            }
        }
    }

    obj.value = layers;
}

function Placenames(value) {
    var obj = document.getElementById("ctl00_cphSideBar_hdnLayers")
    var layers = obj.value;
    var LayerList = obj.value.split(",");
    
    if (value == "true")
    {
        if (layers.indexOf("Place Names") == -1)
        {
            if (layers.length = 0)
            {
                layers = "Place Names";
            }
            else
            {
                layers += ",Place Names";
            }
        }
    }
    else
    {
        if (layers.indexOf("Place Names") >= 0)
        {
            layers = "";
            
            for (ChildLayer in LayerList)
            {
                if (LayerList[ChildLayer] != "Place Names")
                {
                   layers += "," + LayerList[ChildLayer];
                }
            }
            
            if (layers != "")
            {
                layers = layers.substr(1);
            }
        }
    }
    
    obj.value = layers;
}