﻿/***** Global vars ***********/
  // This var is to make sure messages sent to display BEFORE
  // the light box is loaded, are sent via alert() rather than 
  // accessing the null/undefined silverlight control
    var util_lightBoxLoaded = false;  
  // block event redirect to silverlight control until the overlay is loaded  
    var util_overlayLoaded = false;
  // block event redirect to silverlight control until it is loaded
    var util_titleBarLoaded = false;
    
    var lightboxCtl = null;
    
  // global store of currently logged in user -- for reference only, do not trust to do unchecked commits/edits!!
    var loggedInUserID = "";
    var loggedInUserName = "";
    
    
//-------------------------------------------------------------------//
// OSName -- Operating system name of the executing instance         //
//-------------------------------------------------------------------//
var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
    
//
// setCookie:      
//  Utility function to set a cookie value in javascript.
//  inputs - 
//      string c_name - the name of the cookie key, use this name to retrieve the value later
//      value - the value to be stored in a cookie
//      int expiredays - the number of days for the cookie to remain active
//  remarks - 
//      setCookie won't store the value if the value==null
//
function setCookie(c_name,value,expiredays)
{
    if(value != null){
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }else{
      util_Error("Setting a null value for cookie: " + c_name);
    }
}
//
// getCookie:
//  Utility function to retrieve a cookie value in javascript
//  inputs - 
//      string c_name - the key used to store the value originally
//  outputs - 
//      the value stored with the given key, or "" if no value is stored with that key
//
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        { 
	        c_start=c_start + c_name.length+1;
	        c_end=document.cookie.indexOf(";",c_start);
	        if (c_end==-1) c_end=document.cookie.length;
	        return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}

//-----------------------------------------//
// Bookmark helper script                  //
//-----------------------------------------//
//function bookmark()
//  {
//  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) 
//    {
//    window.external.AddFavorite("http://www.reportstorms.com","Report Storms");
//    } 
//  else if (navigator.appName == "Netscape") 
//    {
//    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
//    //window.sidebar.addPanel("Report Storms","http://www.reportstorms.com","");
//    } 
//  else 
//    {
//    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
//    }
//  }


// util_Error:
//   Utitlity function to report errors to the best active mechanism,
//   and cary out the appropriate follow up action depending upon the
//   severity of the error.
//   inputs -
//      error message - the message displayed to the user
//   outputs - none
//      
function util_Error(msg)
  {
  // if Silverlight light box div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox'); 
    if(divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
  // generic alert() fall back
    if(lightboxCtl == null)
      alert(msg);
    else  
      {
      lightboxCtl.Show();
      divId.focus();
      lightboxCtl.ShowErrorMessage(msg);
      }
  }
// util_Message:
//   Utitlity function to report errors to the best active mechanism,
//   and cary out the appropriate follow up action depending upon the
//   severity of the error.
//   inputs -
//      error message - the message displayed to the user
//   outputs - none
//      
function util_Message(msg)
  {
  // if Silverlight light box div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox'); 
    if(divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
  // generic alert() fall back
    if(lightboxCtl == null)
      alert(msg);
    else  
      {
      lightboxCtl.Show();
      divId.focus();
      lightboxCtl.ShowMessage(msg);
      }
  }
// util_Message:
//   Utitlity function to dispaly a brief (2.5 sec) message that fades outreport errors to the best active mechanism,
//      
function util_FadeMessage(msg)
  {
  // if Silverlight light box div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox'); 
    if(divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if(lightboxCtl == null) return;
      {  
      lightboxCtl.Show();
      lightboxCtl.StartFadingMessage(msg);
      }
  }

//
// Output a debug message to the debug div
//
function util_DebugMessage(msg)
  { 
    var err = document.getElementById('div_Debug')
    if(err == null) return;
    var debugMsg = new Date().toUTCString() + ":  " + msg + "<br />" + err.innerHTML
    err.innerHTML = debugMsg;
  }

//
// Invoke the light box progress indicator
//
function util_ShowProgress(msg)
  {
  // if Silverlight light box div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox'); 
    if(divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if(lightboxCtl == null)
      return;
  // show the light box    
    lightboxCtl.Show();
    lightboxCtl.StartProgressIndicator(msg);
 }
  
//
// Hide/Stop the light box progress indicator
//
function util_HideProgress()
  {
  // if Silverlight light box div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox'); 
    if(divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if(lightboxCtl == null)
      return;
  // show the light box    
    lightboxCtl.StopProgressIndicator();
    lightboxCtl.Hide();
  }
  
function util_ShowLightBoxImage(eventID)
  {
  // if Silverlight light box div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox'); 
    if(divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if(lightboxCtl == null)
      return;
  // show image in the light box    
    lightboxCtl.Show();
    lightboxCtl.GetImageByEventId(eventID);
  }

function util_ShowLightBoxMovie(eventID)
  {
  // if Silverlight light box div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox'); 
    if(divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if(lightboxCtl == null)
      return;
  // show image in the light box    
    lightboxCtl.Show();
    lightboxCtl.GetMovieByEventId(eventID);
  }
  
function util_ShowReportStormsAboutBox()
  {
  // if Silverlight light box div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox'); 
    if(divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
  // generic alert() fall back
    if(lightboxCtl == null)
      return;
  // show image in the light box    
    lightboxCtl.Show();
    lightboxCtl.ShowReportStormsAboutBox();
  }
  
function util_CreateAccountBox()
  {
  // if Silverlight create account div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox');
    if (divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if (lightboxCtl == null)
      return;
  // show the create account box
    lightboxCtl.Show();
    lightboxCtl.ShowCreateAccountPage();
  }

  
function util_RecoverPasswordBox()
  {
    // if Silverlight create account div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox');
    if (divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if (lightboxCtl == null)
      return;
    // show the create account box
    lightboxCtl.Show();
    lightboxCtl.ShowRecoverPasswordPage();
  }
  
function util_ChangePasswordBox()
  {
  // if Silverlight change password div is active -- report there
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox');
    if (divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if (lightboxCtl == null)
      return;
  // show the change password box
    lightboxCtl.Show();
    lightboxCtl.ShowChangePasswordPage();
  }
  
  
function util_EditUserProfile()
  {
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox');
    if (divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if (lightboxCtl == null)
      return;
    // show the change password box
      lightboxCtl.Show();
      lightboxCtl.ShowEditUserProfilePage();
  }
  
  function util_PublicUserProfile(username)
  {
    var divId = document.getElementById(controlPrefix + '_LightBoxContent_idSilverlightLightBox');
    if (divId != null && util_lightBoxLoaded)
      lightboxCtl = divId.Content.lightBoxControls;
    if (lightboxCtl == null)
      return;
    // show the change password box
    lightboxCtl.Show();
    lightboxCtl.ShowPublicUserProfilePage(username);
  }
  
  
function util_hideBalloons()
  {
  // Access the overlay pages and hide all active balloons
    if(!util_overlayLoaded) return;
    
    var slDivID = document.getElementById('idVEMapOverlay');
    if(slDivID == null) return;
    var overlayCtl = slDivID.content.div_VEMapOverlay_SLScriptReference;
    if(overlayCtl == null) return;
  // hide overlay balloons    
    overlayCtl.HideBalloons();
    
  // hide the lightbox    
    if(lightboxCtl)
        lightboxCtl.Hide();
  }

function util_SetCurrentViewMessage(str)
  {
  // Access the overlay pages and hide all active balloons
    if(!util_overlayLoaded) return;
  
  // set the "Current View" message into the side bar
    var slDivID = document.getElementById('idVEMapOverlay');
    if(slDivID == null) return;
    var overlayCtl = slDivID.content.div_VEMapOverlay_SLScriptReference;
    if(overlayCtl == null) return;
  // set the message text
    overlayCtl.SetCurrentViewMessage(str);
  }

function util_SetControlsToDate(dateStr)
  {
  // Access the overlay pages and hide all active balloons
    if(!util_overlayLoaded) return;
  
  // set the "Current View" message into the side bar
    var slDivID = document.getElementById('idVEMapOverlay');
    if(slDivID == null) return;
    var overlayCtl = slDivID.content.div_VEMapOverlay_SLScriptReference;
    if(overlayCtl == null) return;
  // set the message text
    overlayCtl.SetControlsToDate(dateStr);
  }

function util_GetCurrentViewMessage()
  {
  // Access the overlay pages and hide all active balloons
    if(!util_overlayLoaded) return "";
  
  // set the "Current View" message into the side bar
    var slDivID = document.getElementById('idVEMapOverlay');
    if(slDivID == null) return "";
    var overlayCtl = slDivID.content.div_VEMapOverlay_SLScriptReference;
    if(overlayCtl == null) return "";
  // get the message text
    var str = overlayCtl.GetCurrentViewMessage();
    return str;
  }
  
  function util_RefreshLoginControl()
  {
    var divId = document.getElementById(controlPrefix + '_HeaderContent_idSilverlightTitleBar'); 
    var titleCtl = null;
    if(divId != null && util_titleBarLoaded == true)
      titleCtl = divId.Content.titleBar;
    if(titleCtl == null)
      return;      
    titleCtl.RefreshLogin();
  }
  
  function util_AutoLoginControl(username, password)
  {
    var divId = document.getElementById(controlPrefix + '_HeaderContent_idSilverlightTitleBar'); 
    var titleCtl = null;
    if(divId != null && util_titleBarLoaded == true)
      titleCtl = divId.Content.titleBar;
    if(titleCtl == null)
      return;      
    titleCtl.AutoLogin(username, password);
  }
  
  function util_RefreshLogoutControl()
  {
    var divId = document.getElementById(controlPrefix + '_HeaderContent_idSilverlightTitleBar'); 
    var titleCtl = null;
    if(divId != null && util_titleBarLoaded == true)
      titleCtl = divId.Content.titleBar;
    if(titleCtl == null)
      return;      
    titleCtl.RefreshLogoutControl();
  }
  
function util_IsEditProfile()
  {
  // set the "Current View" message into the title bar
    var divId = document.getElementById(controlPrefix + '_HeaderContent_idSilverlightTitleBar'); 
    var titleCtl = null;
    if(divId != null && util_titleBarLoaded == true)
      titleCtl = divId.Content.titleBar;
    if(titleCtl == null)
      return "";
  // get the message text
    var str = titleCtl.IsEditProfile();
    return str;
  }  

// Access to logged in GUID of user
  function util_SetLoggedInUserID(guid)
  {
  loggedInUserID = guid;
  }

  function util_GetLoggedInUserID()
  {
  return loggedInUserID;
  }

// Access to logged in UserName of user
  function util_SetLoggedInUserName(userName)
  {
  loggedInUserName = userName;
  }

  function util_GetLoggedInUserName()
  {
  return loggedInUserName;
  }


function util_SetMapDivFocus()
  {
  // Access the overlay pages and hide all active balloons
    if(!util_overlayLoaded) return;
  
    var slDivID = document.getElementById('idVEMapOverlay');
    if(slDivID == null) return;
    slDivID.focus();
  }



//      _trackEvent(category, action, opt_label, opt_value) 
//
//      Constructs and sends the event tracking call to the Google Analytics Tracking Code. Use this to track visitor behavior on your website that is not related to a web page visit, such as interaction with a Flash video movie control or any user event that does not trigger a page request. 
//
//      parameters
//            String   category The general event category (e.g. "Videos").
//            String   action The action for the event (e.g. "Play").
//            String   opt_label An optional descriptor for the event.
//            Int      opt_value An optional value to be aggregated with the event. 
//            returns
//                  Boolean whether the event was successfully sent. 
 function util_GoogleTrackEvent(category, action, label, label_int)
   {
   if(pageTracker != null)
     {
     var ilabel_int = parseInt(label_int, 10);
     var ret = pageTracker._trackEvent(category, action, label, ilabel_int);
     /*if(ret == false)
       {
       alert("PageTracker failed to track event " + ret);
       alert(category + " " + action + " " + label + " " + label_int);
       }*/
     }
   //else
   //  alert("PageTracker not defined!");
   }


//
// Utility object for the SL code to call javascript functions
//
function SLToJSUtility()
  {
    this.overlayLoaded  = function()        { util_overlayLoaded = true;  }
    this.lightBoxLoaded = function()        { util_lightBoxLoaded = true; }
    this.titleBarLoaded = function() { util_titleBarLoaded = true; }
    this.hideBalloons   = function()        { util_hideBalloons(); }
    this.mapDivResize   = function()        { VE_MapDivResize();   }
    this.searchMap      = function(address) { VE_SearchMap(address); }
    this.panMap         = function(dx, dy)  { VE_PanMap(dx, dy); }
    this.panMapLatLon   = function(lat, lon){ VE_PanMapLatLon(lat, lon); }
    this.setCenter      = function(lat, lon){ VE_SetCenter(lat, lon); }
    this.getCenter      = function()        { return VE_GetCenter(); }
    this.pixelToLatLon  = function(x, y)    { return VE_PixelToLatLon(x, y); }
    this.latLonToPixel  = function(lat, lon){ return VE_LatLonToPixel(lat, lon); }
    this.showLightBoxImage = function(eventID) { util_ShowLightBoxImage(eventID); }
    this.showLightBoxMovie = function(movieID) { util_ShowLightBoxMovie(movieID); }
    this.errorMessage   = function(msg)     { util_Error(msg);   }
    this.simpleMessage  = function(msg)     { util_Message(msg); }
    this.fadeMessage    = function(msg)     { util_FadeMessage(msg); }
    this.debugMessage   = function(msg)     { util_DebugMessage(msg); }
    this.showProgress   = function(msg)     { util_ShowProgress(msg); }
    this.hideProgress   = function()        { util_HideProgress(); }
    this.feedEvents     = function(clusters){ VE_FeedEvents(clusters);}
    this.setLocatorIcon = function(show, lat, lon) { VE_SetLocatorIcon(show, lat, lon); }
    this.getZoomLevel   = function()        { return VE_GetZoomLevel(); }
    this.setZoomLevel   = function(zoom)    { VE_SetZoomLevel(zoom); }
    this.setCenterAndZoomLevel = function(zoom, lat, lon)    { VE_SetCenterAndZoomLevel(zoom, lat, lon); }
    this.createAccountBox = function()      { util_CreateAccountBox(); }
    this.recoverPasswordBox = function()    { util_RecoverPasswordBox(); }
    this.changePasswordBox = function()     { util_ChangePasswordBox(); }
    this.editUserProfile = function()       { util_EditUserProfile(); }
    this.publicUserProfile = function(username) { util_PublicUserProfile(username); }
    this.setCurrentViewMessage = function(str)  { util_SetCurrentViewMessage(str); }
    this.getCurrentViewMessage = function()     { return util_GetCurrentViewMessage(); }
    this.setControlsToDate = function(dateStr)  { util_SetControlsToDate(dateStr); }
    this.refreshLoginControl = function()       { util_RefreshLoginControl(); }
    this.setLoggedInUserID = function(guid)     { util_SetLoggedInUserID(guid); }
    this.getLoggedInUserID = function()         { return util_GetLoggedInUserID(); }
    this.setLoggedInUserName = function(userName) { util_SetLoggedInUserName(userName); }
    this.getLoggedInUserName = function()         { return util_GetLoggedInUserName(); }
    this.autoLoginControl = function(username, password)       { util_AutoLoginControl(username, password); }
    this.refreshLogoutControl = function()       { util_RefreshLogoutControl();}
    this.isEditProfile = function()       { return util_IsEditProfile();}
    this.getOSName = function()         { return OSName; }
    this.setMapDivFocus = function()    { util_SetMapDivFocus(); }
    this.googleTrackEvent = function(category, action, label, label_int) { util_GoogleTrackEvent(category, action, label, label_int); }
  }
  
  
	
