﻿var MaxZIndex = 99999;
var ShowPointTimeout;

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 SetOpacity(sender, value) {
    sender.style.opacity = value / 10;
    sender.style.filter = 'alpha(opacity=' + value * 10 + ')';
}

function BringToFront(sender)
{
    MaxZIndex += 1;
    sender.style.zIndex = MaxZIndex;
}

function PopupInfo(url) {
    var CRWYearMonth = "";
    var IsPopupOpened = false;
   
   if ($get("hdnWMS").value == "RGWSST" || 
       $get("hdnWMS").value == "RGWSSTANOMALY" || 
       $get("hdnWMS").value == "RGWHOTSPOT" || 
       $get("hdnWMS").value == "RGWDHW")
    {
        CRWYearMonth = "&CRWYearMonth=" + $get("hdnCRWYearMonth").value;
    }
    
    
    ClosePopup2();

    
    if (Popup != null)
    {
        if (!Popup.closed)
        {
            IsPopupOpened = true;            
        }
    }

    if (IsPopupOpened) 
    {
        Popup.document.getElementById("InfoMainFrame").style.display = "none";
        Popup.location = url + "&bbox=" + $get("hdnBBox").value + "&wms=" + $get("hdnWMS").value + "&mapguid=" + $get("hdnMapGUID").value + CRWYearMonth;
    }
    else
    {
        Popup = window.open(url + "&bbox=" + $get("hdnBBox").value + "&wms=" + $get("hdnWMS").value + "&mapguid=" + $get("hdnMapGUID").value + CRWYearMonth,'_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();
    
    //ShowInfo(url + "&bbox=" + $get("hdnBBox").value + "&wms=" + $get("hdnWMS").value + noaayear);

    return false;
}

function SetBBox(value) {
    var BBox = value.substr(0,value.lastIndexOf(","));
    $get("hdnBBox").value = BBox;
}

function SetShowPoint()
{
    if ($get("hdnShowPoint").value != "")
    {
        var BBox = $get("hdnBBox").value;
        var xmin = parseFloat(BBox.split(",")[0]);
        var xmax = parseFloat(BBox.split(",")[2]);
        var ShowPoint = $get("hdnShowPoint").value;
        var x = parseFloat(ShowPoint.split(",")[0]);
        var y = parseFloat(ShowPoint.split(",")[1]);
        
        if (x < xmin || x > xmax)
        {
            if (x < xmin)
            {
                x += 360;
            }
            else if (x > xmax)
            {
                x -= 360;
            }
        }
        
        try
        {
            flashMovie('GISClient').PointFeature(x, y);
        }
        catch(err)
        {

        }
        
        clearTimeout(ShowPointTimeout);
        ShowPointTimeout = setTimeout("SetShowPoint()",3000);
    }
}

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 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) {
    SetLayerList("Topography", value == "true");
    SetLayer2List("Topography", value == "true");
    
    SetLayerList("Bathymetry", value == "true");
    SetLayer2List("Bathymetry", value == "true");
}

function Placenames(value) {
    SetLayerList("Place Names", value == "true");
    SetLayer2List("Place Names", value == "true");
}

function SetLayerList(Layer,Visible)
{
    var Layers = "";
    var LayerList = $get("hdnLayers").value.split(",");
    
    for (ChildLayer in LayerList)
    {
        if (LayerList[ChildLayer] != Layer)
        {
            Layers += "," + LayerList[ChildLayer];
        }
    }
    
    if (Visible)
    {
        Layers += "," + Layer;
    }
    
    if (Layers != "")
    {
        Layers = Layers.substr(1);
    }
    
    $get("hdnLayers").value = Layers;
}

function SetLayer2List(Layer,Visible)
{
    var Layers = "";
    var LayerList = $get("hdnLayers2").value.split(",");
    
    for (ChildLayer in LayerList)
    {
        if (LayerList[ChildLayer] != Layer)
        {
            Layers += "," + LayerList[ChildLayer];
        }
    }
    
    if (Visible)
    {
        Layers += "," + Layer;
    }
    
    if (Layers != "")
    {
        Layers = Layers.substr(1);
    }
    
    $get("hdnLayers2").value = Layers;
}

function SetOptionList(Options)
{
    $get("hdnOptions").value = Options;
}

function SetOption2List(Options)
{
    $get("hdnOptions2").value = Options;
}

function RefreshMap()
{
    url = GetURL();
    window.location = url;
    return false;
}

function GetURL()
{
    return "default.aspx?wms=" + $get("hdnWMS").value + "&wmsbbox=" + $get("hdnWMSBBox").value + "&bbox=" + $get("hdnBBox").value + "&layers=" + $get("hdnLayers").value + "&layers2=" + $get("hdnLayers2").value + "&timestep=" + $get("hdnTimeStep").value + "&theme=" + $get("hdnTheme").value + "&" + $get("hdnOptions").value + "&" + $get("hdnOptions2").value + "&MapGUID=" + $get("hdnMapGUID").value;
}















var xmlActive, t;
        
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
        
function SetSessionActive()
{
    xmlActive=GetXmlHttpObject();
    
    if (xmlActive==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    
    var url="getdata.aspx?type=";
    xmlActive.onreadystatechange=stateSessionActiveChanged;
    xmlActive.open("GET",url,true);
    xmlActive.send(null);
    
    t = setTimeout("SetSessionActive()",180000);
}

function stateSessionActiveChanged() 
{ 

}










var delta = 0; 
var ZoomIndex = 0;
var ZoomLevels = new Array(1, 0.5, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.002, 0.001);
var ZoomT = null;

function Zoom()
{
    var WMSBBox = $get("hdnWMSBBox").value;
    var xmin = parseFloat(WMSBBox.split(",")[0]);
    var ymin = parseFloat(WMSBBox.split(",")[1]);
    var xmax = parseFloat(WMSBBox.split(",")[2]);
    var ymax = parseFloat(WMSBBox.split(",")[3]);
    var xdistance = (xmax - xmin) / 2;
    var ydistance = (ymax - ymin) / 2;
    
    if (delta < 0)
    {
        ZoomIndex -= 1;
        
        if (ZoomIndex < 0)
        {
            ZoomIndex = 0;
        }
    }	
	else
    {
        ZoomIndex += 1;
        
        if (ZoomIndex > 9)
        {
            ZoomIndex = 9;
        }
    }
    
    xmin = CurrentLon - (xdistance * ZoomLevels[ZoomIndex]);
    ymin = CurrentLat - (ydistance * ZoomLevels[ZoomIndex]);
    xmax = CurrentLon + (xdistance * ZoomLevels[ZoomIndex]);
    ymax = CurrentLat + (ydistance * ZoomLevels[ZoomIndex]);
    
    NewBBox = xmin.toString() + "," + ymin.toString() + "," + xmax.toString() + "," + ymax.toString();
    //alert(NewBBox);
    
    flashMovie('GISClient').ZoomTo(NewBBox);
}

function SetZoomIndex()
{
    var WMSBBox = $get("hdnWMSBBox").value;
    var xminWMS = parseFloat(WMSBBox.split(",")[0]);
    var yminWMS = parseFloat(WMSBBox.split(",")[1]);
    var xmaxWMS = parseFloat(WMSBBox.split(",")[2]);
    var ymaxWMS = parseFloat(WMSBBox.split(",")[3]);
    var xdistanceWMS = (xmaxWMS - xminWMS) / 2;
    var ydistanceWMS = (ymaxWMS - yminWMS) / 2;
    
    
    var BBox = $get("hdnBBox").value;
    var xmin = parseFloat(BBox.split(",")[0]);
    var ymin = parseFloat(BBox.split(",")[1]);
    var xmax = parseFloat(BBox.split(",")[2]);
    var ymax = parseFloat(BBox.split(",")[3]);
    var xdistance = (xmax - xmin) / 2;
    var ydistance = (ymax - ymin) / 2;
    
    var Rate = xdistance / xdistanceWMS;
    
    if (Rate >= 0.75)
    {
        ZoomIndex = 0
    }
    else if (Rate >= 0.35)
    {
        ZoomIndex = 1
    }
    else if (Rate >= 0.15)
    {
        ZoomIndex = 2
    }
    else if (Rate >= 0.075)
    {
        ZoomIndex = 3
    }
    else if (Rate >= 0.04)
    {
        ZoomIndex = 4
    }
    else if (Rate >= 0.02)
    {
        ZoomIndex = 5
    }
    else if (Rate >= 0.0075)
    {
        ZoomIndex = 6
    }
    else if (Rate >= 0.0035)
    {
        ZoomIndex = 7
    }
    else if (Rate >= 0.0015)
    {
        ZoomIndex = 8
    }
    else if (Rate < 0.0015)
    {
        ZoomIndex = 9
    }
}
 
function handle() {
    clearTimeout(ZoomT);
    ZoomT = setTimeout("Zoom()",100);
}

function wheel(event){
//	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta) {
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	if (delta)
		handle();
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}

/* Initialization code. */
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;