// ******************************************************
// *** MENU FUNCTIONS ***********************************
// ******************************************************


//show menu
function showMenu(menuName){
    mnu = $get(menuName);
	if(mnu != null){
		mnu.style.visibility = "visible";
	}
}

//hide menu
function hideMenu(menuName){
	mnu = $get(menuName);
	if(mnu != null){
		mnu.style.visibility = "hidden";
	}
}

//hide menu all
function hideMenuAll(){
    var list = "Menu_Tools_On,Menu_Nav_On,Menu_Options_On,Menu_Schools_On".split(",");
    for (i=0;i<4;i++){
	    mnu = $get(list[i]);
	    if(mnu != null){
		    mnu.style.visibility = "hidden";
	    }
	}
}

// Disable the mouse down event
// for adding text when the
// Draw Text panel isn't there
function disableMapMouseDownEvent(e) {
  if (null == map) {
    map = $find('Map1');
  }
  if (MapIdentifySchoolsClick) {
    map.remove_mouseDown(MapIdentifySchoolsClick);
    hideFloatingPanel('Panel_SchoolsIdentify');
  }
}//disableMapMouseDownEvent


// ******************************************************
// *** PANEL FUNCTIONS **********************************
// ******************************************************

//set panel
function setPanel(newpanel){
  oldpanel = document.frmMain.panel.value;
  if(oldpanel != ""){
    hideFloatingPanel(oldpanel);
  }
  theFP = $get(newpanel);
  if(theFP != null){
    document.frmMain.panel.value = newpanel;
    winW = getWinWidth() - 348; //280;
    theFP.style.left = winW + "px";
    theFP.style.top = "10px";
    theFP.style.width = "348px";
    showFloatingPanel(newpanel);
  }else{
    alert("This functionality is under construction!");
  }
}

//position panel
function positionPanel(panel){
  theFP = $get(panel);
  if(theFP != null){
      theFP.style.left = (getWinWidth() - 260) + "px";
      theFP.style.top = "10px";
  }
}


// ******************************************************
// *** MENUBAR FUNCTIONS ********************************
// ******************************************************

//menu home
function menuHome(){
    window.location = "http://www.palmbeach.k12.fl.us";
}

//menu contact
function menuContact(){
    window.location.href = "mailto:goldstein@palmbeach.k12.fl.us";
}

//menu help
function menuHelp(){
    theURL = "help.htm";
    helpWin = window.open(theURL, "Help", "width=500,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=1,left=300,top=0");
}


// ******************************************************
// *** MAP TOOLS FUNCTIONS ******************************
// ******************************************************

//menu zoomin
function menuZoomIn(){
  MapDragRectangle('Map1','MapZoomIn',false,'');
  setActiveTool('zoomin', 'Zoom In');
  hideMenu('Menu_Tools_On');  
  closeMeasureToolbarTool();
  disableMapMouseDownEvent();
}

//menu zoomout
function menuZoomOut(){
  MapDragRectangle('Map1','MapZoomOut',false,'');
  setActiveTool('zoomout', 'Zoom Out');
  hideMenu('Menu_Tools_On');  
  closeMeasureToolbarTool();
  disableMapMouseDownEvent();    
}

//menu zoomfull
function menuZoomFull(){
  var message = "ZoomFullExtent";
  var context = "ZoomFullExtent";
  WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
  hideMenu('Menu_Tools_On');
}

//menu pan
function menuPan(){
  MapDragImage('Map1','MapPan',false,'');
  setActiveTool('pan', 'Pan Map');
  hideMenu('Menu_Tools_On');  
  closeMeasureToolbarTool();
  disableMapMouseDownEvent();    
}

//menu measure
function menuMeasure(){
  startMeasure();
  setActiveTool('measure', 'Measure');
  hideMenu('Menu_Tools_On');
  disableMapMouseDownEvent();  
}

//menu identify
function menuIdentify(){
  MapIdentifySchools();
  setActiveTool('identify', 'Identify');
  hideMenu('Menu_Tools_On');  
  closeMeasureToolbarTool();   
  MapDragRectangle('Map1','',false,'');
  MapPoint('Map1','',false,''); 
}

//set activeTool
function setActiveTool(imgname,tag){
	img = $get("MB_ACTIVETOOL");
	if(img != null){
		img.src = "images/s_"+imgname+".gif";
		img.alt = "Active Tool: " + tag;		
	}
}

// ******************************************************
// *** MAP OPTIONS FUNCTIONS ****************************
// ******************************************************

//menu overview
function menuOverview(){
  setPanel('Panel_Overview');
  hideMenu('Menu_Options_On');
}

//menu layers
function menuLayers(){
  setPanel('Panel_Layers');
  hideMenu('Menu_Options_On');
}

//menu clear
function menuClear(){
    findZone("","");
    hideMenu('Menu_Options_On');
}

//menu print
function menuPrint(){
    setPanel('Panel_Print');
    hideMenu('Menu_Options_On');
}

// ******************************************************
// *** SCHOOLS FUNCTIONS ********************************
// ******************************************************

//menu schools elementary
function menuSchoolsElementary(){
    setPanel('Panel_SchoolsElementary');
    hideMenu('Menu_Schools_On');
}

//menu schools middle
function menuSchoolsMiddle(){
    setPanel('Panel_SchoolsMiddle');
    hideMenu('Menu_Schools_On');
}

//menu schools high
function menuSchoolsHigh(){
    setPanel('Panel_SchoolsHigh');
    hideMenu('Menu_Schools_On');
}

//menu schools alternative
function menuSchoolsAlternative(){
    setPanel('Panel_SchoolsAlternative');
    hideMenu('Menu_Schools_On');
}

//menu schools by address
function menuSchoolsByAddress(){
    setPanel('Panel_SchoolsByAddress');
    hideMenu('Schools');
}

//menu schools by developement
function menuSchoolsByDevelopment(){
    setPanel('Panel_SchoolsByDevelopment');
    hideMenu('Menu_Schools_On');
}

//menu schools officials
function menuSchoolsOfficials(){
    //theURL = "http://cms.palmbeach.k12.fl.us/cms/userfiles/File/Legislative_Info_by_School.pdf";
    //offWin = window.open(theURL, "Officials", "width=500,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=1,left=300,top=0");
    setPanel('Panel_SchoolsByAddress');
    hideMenu('Menu_Schools_On');
}

//*************************************************************
//********************* GLOW FUNCTIONS ************************
//*************************************************************

var mapminx = 0;
var mapminy = 0;
var mapmaxx = 0;
var mapmaxy = 0;

//get map extent
function getMapExtent(){
    var message = "GetMapExtent";
    var context = "MapExtent";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
}

//update map extent
function updateMapExtent(extentParams){
    var ext = extentParams.split(":");
    mapminx = parseFloat(ext[0]);
    mapminy = parseFloat(ext[1]);
    mapmaxx = parseFloat(ext[2]);
    mapmaxy = parseFloat(ext[3]);
}

var glowTimer = null;

//show glow
function showGlow(x, y){
    window.clearTimeout(glowTimer);
    var coords = getPixelCoords(x,y).split(":");
    pX = parseInt(coords[0]);
    pY = parseInt(coords[1]);
    var glowLayer = "Panel_Glow";
    hideLayer(glowLayer);
    if((pX > 0) && (pX < getWinWidth()) && (pY > 0) && (pY < getWinHeight())){
      moveLayer(glowLayer, pX - 40, pY - 40);
      showLayer(glowLayer); 
    } else {
      glowTimer = window.setTimeout("panToFeature("+x+","+y+")", 600);
    } 
}//showGlow

//hide glow
function hideGlow(){
  //window.clearTimeout(glowTimer);
  var glowLayer = "Panel_Glow";
  hideLayer(glowLayer);
}

//row highlight
function rowHighlight(thisRow,rowclass) {
  thisRow.className = rowclass;
}

//get pixel coords
function getPixelCoords(x,y){
    if((mapminx != 0) && (mapminy != 0) && (mapmaxx != 0) && (mapmaxy != 0)){
        mapW = getWinWidth();
        mapH = getWinHeight();
        var pixelX = mapW / Math.abs(mapmaxx-mapminx);
        var deltaX = x - mapminx;
        pX = parseInt((pixelX * deltaX));
        var pixelY = mapH / Math.abs(mapmaxy-mapminy);
        var deltaY = mapmaxy - y;
        pY = parseInt((pixelY * deltaY));
        return pX + ":" + pY;	    
	}
}

//pan to feature
function panToFeature(x, y){
    var message = "PanToFeature:"+x+":"+y;
    var context = "PanToFeature";
    WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true); 
    cX = getWinWidth() / 2;
    cY = getWinHeight() / 2;
    moveLayer("Panel_Glow", cX - 40, cY - 40);
    showLayer("Panel_Glow")
}

