var gbStaticMapTip;
var gszMapSize;
var gaszLayerStatus = new Array();

function setCookie(szName,szValue)
{
    call( '/common/save.php?action=set&name='+szName+'&value='+szValue,null, setCookieCallback,1);
}

function getCookie(szName)
{
    call( '/common/save.php?action=get&name='+szName,null, getCookieCallback,1);
}

function setCookieCallback(szResponse)
{
 //alert(szResponse);
}

function getCookieCallback(szResponse)
{
 //alert(szResponse);
}

function saveSettings(szAppName)
{
    setCookie(szAppName + '_mapsize',gszMapSize);
    setCookie(szAppName + '_layerStatus',gaszLayerStatus.join(','));
}

function mapSizeSmall()
{
    var mapsize = getMapSize();
    gszMapSize = 'small';
    if (mapsize[0] != 439)
    {
      document.forms[0].NAV_CMD.value = '';
      applyMapSize( 439, 349 );
    } 
}

function mapSizeMedium()
{
    var mapsize = getMapSize();
    gszMapSize = 'medium';
    if (mapsize[0] != 663)
    {
      document.forms[0].NAV_CMD.value = '';    
      applyMapSize( 663, 517 );
    }
}

function mapSizeLarge()
{
    var mapsize = getMapSize();
    gszMapSize = 'large';
    if (mapsize[0] != 919)
    {
      document.forms[0].NAV_CMD.value = '';    
      applyMapSize( 919, 773 );
    }
}

function getButtonByDivName( szName )
{
    var i;
    for (i=0; i< goCWCButtonManager.aButtons.length; i++)
    {
        var oButton = goCWCButtonManager.aButtons[i];
        if (oButton.szDivName == szName)
        {
            return oButton;
        }
    }
    return null;
}

function activateTab( szName )
{
    var oButton = getButtonByDivName( szName );
    if (oButton != null)
    {
        oButton.bActive = true;
        oButton.SetState( CWCBUTTONSTATE_SELECTED );
        oButton.oGroup.ActivateButton( oButton );
    }
}

function showMapTab()
{
    activateTab( 'divMaps' );
    showMapTabs( );

}

function showInfoTab()
{
    activateTab( 'divInfo' );
    showTab( null, 'divInfoTab' );
}

function showMapTabs()
{
    CWCDHTML_ShowLayer( 'divGeneralTab' );
    CWCDHTML_ShowLayer( 'divLayersTab' );
    CWCDHTML_ShowLayer( 'divInfoTab' );
    CWCDHTML_ShowLayer( 'divBlank' );
    CWCDHTML_ShowLayer( 'MapLayerBG' );
    CWCDHTML_ShowLayer( 'MapLayerDiv' );
    CWCDHTML_ShowLayer( 'momaptips_overlay');
    updateTab( 'divGeneralTab', 'divGeneral' );
    updateTab( 'divLayersTab', 'divLayers' );
    updateTab( 'divInfoTab', 'divInfo' );
    MapWPositionMapDHTML();
}

function hideMapTabs()
{
    //check if the function has been overloaded
    if ( arguments[1] == 'showPreview' )
    {
    	//show the map preview
    	previewMap();
    }
    
    CWCDHTML_HideLayer( 'divGeneralTab' );
    CWCDHTML_HideLayer( 'divLayersTab' );
    CWCDHTML_HideLayer( 'divInfoTab' );
    CWCDHTML_HideLayer( 'divBlank' );
    CWCDHTML_HideLayer( 'divGeneral' );
    CWCDHTML_HideLayer( 'divLayers' );
    CWCDHTML_HideLayer( 'divInfo' );
    CWCDHTML_HideLayer( 'MapLayerBG' );
    CWCDHTML_HideLayer( 'MapLayerDiv' );
    CWCDHTML_HideLayer( 'momaptips_overlay');
    DistTool.dtFinishDist();
    CWCDHTML_SetLayerPos( 'MapLayerDiv', -1000, -1000 );
}

function showTab( oButton, szTab )
{

     CWCDHTML_SetLayerZOrder( 'divGeneralTab' , (szTab == 'divGeneralTab') ? 3 : 1 );
     CWCDHTML_SetLayerZOrder( 'divLayersTab', (szTab == 'divLayersTab') ? 3 : 1 );
     CWCDHTML_SetLayerZOrder( 'divInfoTab', (szTab == 'divInfoTab') ? 3 : 1 );
}

function updateTab( szTab, szDiv )
{
    var oLayer = CWCDHTML_GetLayer( szTab );
    if (oLayer != null)
    {
        if (oLayer.zIndex == 3)
        {
            CWCDHTML_ShowLayer( szDiv );
        }
    }
}

function printPreviewMap()
{
    var format = document.forms[0].printMapFormat.options[document.forms[0].printMapFormat.selectedIndex].value;
    if (document.forms[0].PreviewClicked.value == 0)
    {
        alert("Click on Preview before printing") ;
    }
    else
    {
        frames['printPreviewFrame'].focus();
        window.frames['printPreviewFrame'].fullsizeImage();
        frames['printPreviewFrame'].print();
    }
    return true;
}

function quickMap(bNewWindow)
{        
    document.forms[0].PreviewClicked.value = 1;
    var url = "preview.php?sid=" + document.forms[0].sid.value;
    url += "&title="+escape( document.forms[0].printMapTitle.value );
    var format = "PDF";  //new line
    url += "&selImageFormat="+format;
    var mapscale = document.forms[0].setMapScale.options[document.forms[0].setMapScale.selectedIndex].value;
    url += "&mapscale=current";
    var pagesize = document.forms[0].printMapPageSize.options[document.forms[0].printMapPageSize.selectedIndex].value;
    url += "&pagesize="+pagesize;
    
    if (bNewWindow)
    {
        launchWindow( url );
    }
    else
    {
        window.frames['printPreviewFrame'].location.href = url;
    }

    return true;
}

function previewMap(bNewWindow)
{        
    document.forms[0].PreviewClicked.value = 1;
    var url = "preview.php?sid=" + document.forms[0].sid.value;
    url += "&title="+escape( document.forms[0].printMapTitle.value );
    var format = "PDF";  //new line
    url += "&selImageFormat="+format;
    var mapscale = document.forms[0].setMapScale.options[document.forms[0].setMapScale.selectedIndex].value;
    url += "&mapscale="+mapscale;
    var pagesize = document.forms[0].printMapPageSize.options[document.forms[0].printMapPageSize.selectedIndex].value;
    url += "&pagesize="+pagesize;
    
    if (bNewWindow)
    {
        launchWindow( url );
    }
    else
    {
        window.frames['printPreviewFrame'].location.href = url;
    }

    return true;
}

function launchWindow(url)
{
    var myWindow = window.open('blank.html', 'quickMapWin', 'resizable=yes, scrollbars=no, toolbar=no, menubar=no, status=no');
    myWindow.focus();
    myWindow.location.href = url;
    return;
}
      
function myMaptipsOut(e, content)
      {
          e = (e)?window.event:e;
          var d = document.getElementById('myMaptipsLayer');
          if (d)
          {
              d.style.visibility = "hidden";
          }
}

function myMaptipsOver(e, content)
{
    if(gbStaticMapTip==1)
    {
	return;
    }

          e = (e)?e:((event)?event:null);
          var d = document.getElementById('myMaptipsLayer');

          // create the layer if necessary
          if (d == null)
          {
              d = document.createElement('div');
              d.id = 'myMaptipsLayer';
              document.body.appendChild(d);
          }

          // style the layer
          d.style.position = 'absolute';
          d.style.backgroundColor = "#ffffcf";
          d.style.border = "1px solid #000000";
          d.style.fontFamily = "arial";
          d.style.fontSize = "12px";
          d.style.zIndex = 100;
          d.style.top = (e.clientY + 10) + "px";
          d.style.left = (e.clientX + 10) + "px";
          d.style.visibility = "visible";
          d.style.padding="2px";
          d.style.zIndex = 100;

          var sep = "";
          var result = "";

 
          // process content
          for (var i=0;i<content.length; i++)
          {
              var szItem = content[i].toLowerCase();
              gbStaticMapTip=0;
              switch ( szItem.substr( szItem.length - 3 ) )
              {
                  case 'jpg':
                  case 'png':
                  case 'gif':
                       result += sep +  '<img src="' + content[i]  + '" style="border: 1px solid #000000;"/>';
                       sep = "<br>";
                       break;
                  case 'ipx':
                       gbStaticMapTip=1;
                       szPhotoName = "spherical_photos/" + szItem;

                       szPlugin = '<OBJECT ID="IpixX1" WIDTH=280 HEIGHT=280 SPINSPEED=9 SPINSTYLE=FLAT INITFOV=90 CLASSID="CLSID:11260943-421B-11D0-8EAC-0000C07D88CF" CODEBASE="http://www.ipix.com/download/ipixx.cab#version=6,2,0,5"><!-- For MSIE 3,4+ --><PARAM NAME="IPXFILENAME" VALUE="'+szPhotoName+'"><!-- For Netscape 3,4+ --><embed src="'+szPhotoName+'" border=0 width=280 height=280 palette="FOREGROUND" type="application/x-ipix" pluginsPage="http://www.ipix.com/cgi-bin/download.cgi"></embed></OBJECT>';
                       result = szPlugin + '<br><a href="javascript:closeStaticMapTip()">Close</a>';
                  
                       break;
                  default:        
                       result = result + sep + content[i];
                       sep = "<br>";
               }
           }
           if(result=="")
           {
              result="No Data"
           }
          d.innerHTML = result;

}
      
function myMaptipsOut(e, content)
{
if(gbStaticMapTip == 1)
{
	return;
}
        closeStaticMapTip();
}

function closeStaticMapTip()
{
          gbStaticMapTip = 0;
          var d = document.getElementById('myMaptipsLayer');
          if (d)
          {
              d.style.visibility = "hidden";
          }
}

function buildGroup(groupName)
{
    var d = document.createElement('div');
    d.id = 'group_'+groupName;
    
    var cb = document.createElement('input');
    cb.type = 'checkbox';
    cb.name = 'legendgroupname[]';
    cb.id = 'cbg_'+groupName;
    cb.value = groupName;
    
    var s = document.createElement('span');
    s.className = 'label';
    s.innerHTML = groupName;

    d.appendChild(cb);
    d.appendChild(s);
    return d;
}

function buildLayer(layerRef, iconWidth, iconHeight)
{
    //layerRef has url and class name split by a |
    var n = layerRef.lastIndexOf("|");
    var layerURL = layerRef.substring(0, n);
    layerName = layerRef.substring(n+1);
    
    var d = document.createElement('div');
    d.className = 'legendLayer';
    d.style.display = 'none';
    var img = document.createElement('img');
    img.width = iconWidth;
    img.height = iconHeight;
    img.src = layerURL;
    var s = document.createElement('span');
    s.className = 'label';
    s.style.paddingLeft = '2px';
    s.innerHTML = layerName;
    d.appendChild(img);
    d.appendChild(s);
    return d;
}

function setGroupOn(groupName)
{
    document.getElementById('cbg_'+groupName).checked = true;
}

