﻿var MAP_CONTAINER_ID = "ifMap";
var PRIMARY_CONTENT_ID = "primaryMapContent";
var SECONDARY_CONTENT_ID = "secondaryMapContent";
var LEGEND_WIDTH = 275;
var _clientWidth;
var _clientHeight;	
var totalLeftNavWidth;
var totalHeaderHeight;
var mapContainer;
var primaryContent;
var secondaryContent;
var panHeader;
var mnmHeader;
var bottomPanel;
var tabID;

//RETURNS THE VALUE OF A NAMED PARAMETER IN A QUERY STRING
function gup(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";  
  var regex = new RegExp( regexS );  
  var results = regex.exec( window.location.href );  
  if( results == null )
  {
    return "";
  }
  else
  {
    return results[1];
  }
}

// attach functions to window onload event

window.onload = function(){ 
  tabID = gup("tabId");
  mapContainer = $(MAP_CONTAINER_ID);
  primaryContent = $(PRIMARY_CONTENT_ID);
  secondaryContent = $(SECONDARY_CONTENT_ID);
  panHeader = $("panHeader");
  mnmHeader = $("mnmHeader");
  bottomPanel = $("bottomPanel");
  bottomPanel.show();
  panFoot = $("panFooter");
  HandleSizing(false);
  moveBotNav2($("botPanelMinMax"));
  if(tabID == "2")
  {     
    //$('ifMap').src = ("maps/impact.html");            
    ReLoadImpactDetail();
  }
  // attach functions to window onresize event
  window.onresize = function() {
    bottomPanel.show();
    HandleSizing(true);
  };
};

//Resizes the map for onload and onresize
function HandleSizing(resizeMap)
{
  _clientWidth = document.viewport.getWidth();
  _clientHeight = document.viewport.getHeight();
  panFoot = $("panFooter");
  totalLeftNavWidth = secondaryContent.getWidth();
  totalHeaderHeight = panHeader.getHeight()+mnmHeader.getHeight();
  bottomDimensions = bottomPanel.getDimensions();
  bottomHeight = bottomDimensions.height;
  bottomWidth = bottomDimensions.width;
//  position footer element
  if(_clientHeight < 650){
    panFoot.style.top = "650px";    
  }else{
    panFoot.style.top = _clientHeight+"px";
  }
  panFoot.style.display = "block";
  var mapContainerWidth = _clientWidth - totalLeftNavWidth;
  var mapContainerHeight = _clientHeight - bottomHeight - totalHeaderHeight;
  $('leftNav').style.width = LEGEND_WIDTH+"px";
  $('leftNav').style.height = _clientHeight-totalHeaderHeight+"px"; 
  if($('serviceTab1')!= null && $('serviceTab2') != null)
  {   
    $('serviceTab1').style.height = (_clientHeight-totalHeaderHeight-120)*.65+"px";
    $('serviceTab2').style.height = (_clientHeight-totalHeaderHeight-120)*.65+"px";
}
    if ($('statContent') != null ) {
        $('statContent').style.height = (_clientHeight - totalHeaderHeight - 80) * .65 + "px";        
    }
  leftHandle = $('leftExpando').getWidth();  
  secondaryContent.style.height = _clientHeight-totalHeaderHeight+"px";
  secondaryContent.style.top = totalHeaderHeight+"px";

  primaryContent.style.width = mapContainerWidth+"px";
  mapContainer.style.width = mapContainerWidth+"px";
  mapContainer.style.height = mapContainerHeight+"px";
  mapContainer.style.top = totalHeaderHeight+"px";

  if(resizeMap)
  {
    if ($("imgMinMaxLeft").hasClassName('panelClosed'))
    { 
        $("leftNavContent").hide();
        $("leftNav").style.width = leftHandle+"px";
        mapContainerWidth = _clientWidth - leftHandle;
        mapContainer.style.width = mapContainerWidth+"px";
        primaryContent.style.width = mapContainerWidth+"px";
        primaryContent.style.left = leftHandle+"px";
    }
//position footer element onresize
  if(_clientHeight < 650){
    panFoot.style.top = "650px";    
  }else{
    panFoot.style.top = _clientHeight+"px";
  }

  }
    bottomPanel.style.top = _clientHeight - bottomHeight+"px";
    bottomPanel.style.left = $('leftNav').getWidth()+"px";
    bottomPanel.style.width = mapContainerWidth+"px";
    var winMap = window.frames['ifMap'].map;
    if(winMap != null)
    {
        winMap.Resize(mapContainerWidth, mapContainerHeight);
    }
}

//changes the image state for panel toggle onmouseover
function hoverState(obj,panel){
  obj = $(obj);
  if(panel=="bottom")
  {
    if (obj.hasClassName('panelClosed'))
    {
      obj.src="images/scaleDown.gif";
    }
    else
    {
      obj.src="images/scaleDown_hover.gif";
    }
  }
  else if(panel=="left")
  {
    if (obj.hasClassName('panelClosed'))
    {
      obj.src="images/scaleLeft.gif";
    }
    else
    {
	    obj.src="images/scaleLeft_hover.gif";
    }	
  }
}

//open and closes the left nav panel
function moveLeftNav2(obj)
{
  var winMap = window.frames['ifMap'].map;
  if (obj.hasClassName('panelOpen'))
  {
    obj.src = "images/scaleLeft.gif";
    obj.removeClassName('panelOpen');
	obj.addClassName('panelClosed');
    $("leftNavContent").hide();
	$("leftNav").style.width = 9+"px";
    var mapWidth = _clientWidth-9+"px";
 	mapContainer.style.width = mapWidth;	
    primaryContent.style.left = 9+"px";
	$("bottomPanel").style.width = _clientWidth-9+"px";
	$("bottomPanel").style.left = 9+"px";
    mapHeight = _clientHeight -  $("bottomPanel").getHeight() - totalHeaderHeight;
    winMap.Resize(mapWidth, mapHeight);
  }
  else
  {
    obj.src = "images/scaleRight.gif";
    obj.removeClassName('panelClosed');
    obj.addClassName('panelOpen');
    $("leftNavContent").show();
    $("leftNav").style.width = LEGEND_WIDTH+"px";
	mapWidth = _clientWidth-LEGEND_WIDTH+"px";
	mapContainer.style.width = mapWidth;	
	$("primaryMapContent").style.left = LEGEND_WIDTH+"px";
    $("bottomPanel").style.width = _clientWidth-LEGEND_WIDTH+"px";
    $("bottomPanel").style.left = LEGEND_WIDTH+"px";
    mapHeight = _clientHeight -  $("bottomPanel").getHeight() - totalHeaderHeight;
    winMap.Resize(mapWidth, mapHeight);
  }
}

//Opens Bottom Panel
function openBotNav2(obj) {
    var winMap = window.frames['ifMap'].map;
    defaultHeight = 60;
    if (tabID == 3) {
        boxHeight = 270;
    }
    else {
        boxHeight = 200;
    }
    obj.removeClassName('panelClosed');
    obj.addClassName('panelOpen');
    $("bottomPanel").style.height = boxHeight + "px";
    $("bottomPanel").style.top = _clientHeight - boxHeight + "px";
    mapContainer.style.height = _clientHeight - totalHeaderHeight - boxHeight + "px";
    if ($("ResultContainer")) {
        $("ResultContainer").show();
    }
    else if ($("tr_reportNearbyServices11")) {
        $("tr_reportNearbyServices11").show();
    }    
}

//Open and closes the bottom panel
function moveBotNav2(obj)
{
    defaultHeight = 60;
    if (tabID == 3) {
        boxHeight = 270;
    }
    else {
        boxHeight = 200;
    }
  var winMap = window.frames['ifMap'].map;
  var newMapWidth = $("bottomPanel").getWidth();
  if(obj.hasClassName('panelOpen'))
  {
    obj.removeClassName('panelOpen');
    obj.addClassName('panelClosed');
    if (tabID == 3) {
        var less = 50;
        $("bottomPanel").style.height = defaultHeight - less + "px";
        $("bottomPanel").style.top = _clientHeight - defaultHeight + less + "px";
        mapContainer.style.height = _clientHeight - totalHeaderHeight - defaultHeight + less + "px";
    }
    else {
        $("bottomPanel").style.height = defaultHeight + "px";
        $("bottomPanel").style.top = _clientHeight - defaultHeight + "px";
        mapContainer.style.height = _clientHeight - totalHeaderHeight - defaultHeight + "px";
    }
    if ($("ResultContainer")) {
        $("ResultContainer").hide();
    }
    else if ($("tr_reportNearbyServices11")) {
        $("tr_reportNearbyServices11").hide();
        $("tr_reportElectedOfficials12").hide();
    }
    else if ($("StatGraph11_Image1")) {
        $("StatGraph11_Image1").style.display = "none";
        $("StatGraph12_Image1").style.display = "none";
    }
    winMap.Resize(newMapWidth,_clientHeight-totalHeaderHeight-defaultHeight);
  }
  else
  {
    obj.removeClassName('panelClosed');
    obj.addClassName('panelOpen');
    $("bottomPanel").style.height = boxHeight+"px";
    $("bottomPanel").style.top = _clientHeight-boxHeight+"px";
    mapContainer.style.height = _clientHeight-totalHeaderHeight-boxHeight+"px";
    if($("ResultContainer"))
    {
      $("ResultContainer").show();
    }
    else if($("tr_reportNearbyServices11"))
    {
      $("tr_reportNearbyServices11").show();
  }
  else if ($("StatGraph11_Image1")) {
    $("StatGraph11_Image1").style.display = "";
    $("StatGraph12_Image1").style.display = "";
  }
    winMap.Resize(newMapWidth,_clientHeight-totalHeaderHeight-boxHeight);
  }
}

//Blind Effect for toggling left nav elements
function BUDEffect(element)
{
  new Effect.toggle(element,'Blind', {duration:1,afterFinish: OnFinish});
}

//Changes the toggle arrow based on state of left nav element
function OnFinish(object)
{
labelControl = object.element.parentNode.firstChild.nextSibling;	

  if(labelControl.src.include('MenuOpen'))
  {
    labelControl.src="images/smallMenuClose.gif";
    labelControl.alt="Click to close Panel";
  }
  else
  {
    labelControl.src="images/smallMenuOpen.gif";
    labelControl.alt="Click to open Panel";
  }
}