// Ifcn2 theme specific scripting.

ifcn2 = {activeView:0, 
         timeoutPeriod : 20000, 
         pagetimeout:null, 
         menutimeout:null, 
         menudelay: 200, 
         viewArray : new Array(), 
         nextArrowArray: new Array(), 
         navLinks: new Array(),
         collapsedItems : new Array()
         }; //theme specific wrapper object, to avoid namespace problems

ifcn2.clearViews = function() //clears all the center of page views
  {
     for (var i = 0, viewlen = ifcn2.viewArray.length; i < viewlen; i++)
      {
        ifcn2.viewArray[i].style.display = 'none'
      }
  }
  
  
ifcn2.hideSidebarItems = function()
  {
    var thisH4;
    if (this.collapsedItems.length)
      {
        for(var i = 0; i < this.collapsedItems.length; i++) 
          {
            thisH4 = this.collapsedItems[i];
            thisH4.className = 'opener'
            thisH4.onclick = ifcn2.showSidebarItem
            if(thisH4.targElem) {thisH4.targElem.style.display = 'none'}            
           }
      }
  }
  
ifcn2.closeSidebarItem = function()
  {
    if(this.targElem)
      {
        this.targElem.style.display = 'none';
        this.className = 'opener'
      }
    this.onclick = ifcn2.showSidebarItem
  }

ifcn2.showSidebarItem = function()
  {
    ifcn2.hideSidebarItems();
    if(this.targElem)
      {
        this.targElem.style.display = 'block';
        this.className = 'opened'
      }
    this.onclick = ifcn2.closeSidebarItem
  }
  
ifcn2.initSidebar = function()  
  {
    var sidebar = document.getElementById('sidebar')
    if(sidebar == null) return false;
    
    var h4NodeList = sidebar.getElementsByTagName('h4');
    var thisH4, targElem
    if (h4NodeList.length)
      {
        for(var h = 0, hlen = h4NodeList.length; h < hlen; h++)
          {
            thisH4 = h4NodeList[h]
            if(thisH4.id && thisH4.id.match(/for_/))
              {
                targElem = document.getElementById(thisH4.id.replace(/for/,'targ'))
                if(targElem != null)
                  {
                    thisH4.targElem = targElem
                    thisH4.className = 'opener'
                    thisH4.onclick = this.showSidebarItem;
                    this.collapsedItems.push(thisH4)
                  } //end, target exists
              }//end, h4 has an id beginning with 'for_'
          }//end, looping over h4s
      }//end, there are h4s
    this.hideSidebarItems()
  }//end method

  
ifcn2.getPosition = function(HTMLelem) //function to figure out the position of an element. Returns an object with x and y properties
  {
    var position = {x:0, y:0}
    if (HTMLelem.offsetParent) 
      {while (HTMLelem.offsetParent) 
        {
          position.x += HTMLelem.offsetLeft;
          position.y += HTMLelem.offsetTop; 
          HTMLelem = HTMLelem.offsetParent;
         }
       }
    else if (HTMLelem.x && HTMLelem.y) 
      { 
        position.x += HTMLelem.x; 
        position.y += HTMLelem.y; 
      }
    return position;
  }
  
  
ifcn2.initPosition = function() //initializes the placement of the active area. We always want it centered-- unless there's not enough room
  {
    if (document.getElementById('innerbodydiv') != null) return false //don't run on subpages
    this.positionActiveArea()
    window.onresize = this.positionActiveArea
  }
  

ifcn2.getWindowSize = function() //gets the current size of the browser window
  {
    var windowSize = {width:0,height:0}
  
    if (window.innerHeight) 
      {
        windowSize.height = window.innerHeight;
        windowSize.width = window.innerWidth;
      }
    else if (document.documentElement.clientHeight > 0) 
      {
        windowSize.height = document.documentElement.clientHeight;
        windowSize.width = document.documentElement.clientWidth;
      }
    else 
      {
        windowSize.height = document.body.clientHeight;
        windowSize.width = document.body.clientclientWidth;
      }
    return windowSize;
  }
  
 ifcn2.positionActiveArea = function() // actually does the repositioning
  {
    var targetElem = document.getElementById('bodydiv')
    var windowSize = ifcn2.getWindowSize()
    var diffX = windowSize.width - targetElem.offsetWidth
    var diffY = windowSize.height - targetElem.offsetHeight
    var offsetX = (diffX > 0) ? Math.floor(diffX/2) : 0
    var offsetY = (diffY > 0) ? Math.floor(diffY/2) : 0
    
    targetElem.style.margin = '0 0 0 0'
    targetElem.style.top = offsetY + 'px'
    targetElem.style.left = offsetX + 'px'    
  }
  


  
  
ifcn2.hideMenus = function() //method to actually hide all menus
  {
    var storageDiv = document.getElementById('menustorage')
    var contentArea = document.getElementById('innerbodydiv')
    for(var mn = 0; mn < ifcn2.navLinks.length; mn++) 
      {
        if (ifcn2.navLinks[mn].menu != null) 
          {
            ifcn2.navLinks[mn].menu.style.display = 'none'
            storageDiv.appendChild(ifcn2.navLinks[mn].menu)
            ifcn2.navLinks[mn].parentNode.className = ''
          }
      }
  }
  
ifcn2.showMenu = function() //method to show an individual menu
  {
    if (ifcn2.menutimeout) {clearTimeout(ifcn2.menutimeout)}; 
    ifcn2.hideMenus();     
    var bodyElem = document.getElementsByTagName('BODY')[0]   
    if (this.menu) 
    {
      bodyElem.appendChild(this.menu);
      var position = ifcn2.getPosition(this.parentNode)
     
      this.menu.style.left = (position.x + 1)+ 'px'; 
      this.menu.style.top = (58 + position.y) + 'px'; 
      this.menu.style.display = 'block'  
      this.parentNode.className = 'over'
      
    }    
  }
  
  
ifcn2.setHidemenu = function() //sets a timeout to hide all menuse
  {
    ifcn2.menutimeout = setTimeout(ifcn2.hideMenus,ifcn2.menudelay)  
  }
  
ifcn2.initMenus = function() //initialize the menus
  {
    var mainnav = document.getElementById('mainnav');
    
    
    if (mainnav == null) {return false}
    var allNavLinks = mainnav.getElementsByTagName('A');
    var thisNavLink, submenu
    if(!allNavLinks.length){return false}
    for (var i = 0, navlen = allNavLinks.length; i < navlen; i++)
      {
        thisNavLink = allNavLinks[i]
        submenu = document.getElementById(thisNavLink.id.replace(/ml/,'menu'))
          {
            
            if(submenu != null)
              {
                if(thisNavLink.parentNode.offsetWidth) {submenu.style.width = parseInt(thisNavLink.parentNode.offsetWidth) + 'px'}
                thisNavLink.menu = submenu;
                thisNavLink.className = 'more'
                thisNavLink.onmouseover = this.showMenu; 
                thisNavLink.onmouseout = this.setHidemenu
                submenu.main = thisNavLink;
                submenu.onmouseover = function(){clearTimeout(ifcn2.menutimeout)} ;  
                submenu.onmouseout = this.setHidemenu ;  
                
                ifcn2.navLinks.push(thisNavLink)
              }
          }
      } 
  }

ifcn2.nextView = function() //go tothe next view
  {
    clearTimeout(ifcn2.pagetimeout)
    ifcn2.clearViews()
    var newIdx = this.idx + 1;
    if(newIdx >= ifcn2.viewArray.length){newIdx = 0}
    ifcn2.viewArray[newIdx].style.display = 'block';
    ifcn2.activeView = newIdx;
    ifcn2.incrementView()
    
  }
  
ifcn2.prevView = function() //go to the previous view
  {
    clearTimeout(ifcn2.pagetimeout)
    ifcn2.clearViews()
    var newIdx = this.idx - 1;
    if(newIdx < 0){newIdx = ifcn2.viewArray.length - 1}
    ifcn2.viewArray[newIdx].style.display = 'block';
    ifcn2.activeView = newIdx;
    ifcn2.incrementView()
  }
  
ifcn2.incrementView = function() //sets a timeout to auto-advance views
  {    
    function incView()
      {
        ifcn2.nextArrowArray[ifcn2.activeView].onclick()
      }
    ifcn2.pagetimeout = setTimeout(incView,ifcn2.timeoutPeriod)
  }
  
ifcn2.initViews = function() //initializes view changes
  {
    this.viewport = document.getElementById('viewport')
    if(this.viewport == null) {return false} //if we don't find the viewport, bail.
    var childCollection = this.viewport.childNodes;
    var thisChild,thisArrow
    var arrowArray = ['RA','LA']
    var idx = 0;
    for (var c = 0, childlen = childCollection.length; c < childlen; c++)
      {
         thisChild = childCollection[c]
         if(thisChild.nodeName == 'DIV' && thisChild.className.match(/view/))
          {
            this.viewArray.push(thisChild)
            idx = parseInt(thisChild.id.replace(/v/,''))
            for (var i = 0; i < arrowArray.length; i++)
              {                
                thisArrow = document.getElementById(arrowArray[i] + idx);
                thisArrow.idx = idx;
                thisArrow.onclick =  (arrowArray[i] == 'LA') ? this.prevView : this.nextView
                
                thisArrow.onmouseover = this.highlightImg
                thisArrow.onmouseout = this.unhighlightImg
                if(arrowArray[i] == 'RA'){this.nextArrowArray.push(thisArrow)}
              } //end, for i           
          }//end, if(thisChild.nodeName == 'DIV' && thisChild.className.match(/view/))
      }//end, for c
  }//end, method
  
  
ifcn2.highlightImg = function() //image rollover
  {
    var srcString = this.src
    srcString = srcString.replace(/.(gif|png|jpg|jpeg)/,'_over.$1')    
    this.src = srcString
  }
  
ifcn2.unhighlightImg = function() //image rolloff
  {
    var srcString = this.src
    srcString = srcString.replace(/_over.(gif|png|jpg|jpeg)/,'.$1')
    this.src = srcString
  }
  
ifcn2.setStart = function() //picks a view randomly to start with
  {
    var nextLen = this.nextArrowArray.length
    if (nextLen == 0) return false;
    var randView = Math.floor(nextLen  * Math.random())
    this.nextArrowArray[randView].onclick();
  }
  
ifcn2.doPopup = function()
  {
     var targetURL = this.href;
     if (targetURL.match(/sp.cfm\?pageid/))
      {
        targetURL = targetURL.replace(/sp.cfm/,'popup.cfm')
      }
     popWindow(targetURL,720,540);
     return false
  }
  
ifcn2.setHandlers = function()
  {
    var allLinks = document.getElementsByTagName('A')
    var thisLk 
    for(var a = 0, alLen = allLinks.length; a < alLen; a++)
      {
        thisLk = allLinks[a]
        if(thisLk.className.match(/popup/)) {thisLk.onclick = this.doPopup;}
      }
  }
  
ifcn2.initialize = function() //Start Me Up
  {
    this.initPosition();
    if(!document.getElementById) {return false}
    this.initMenus()
    this.initViews()
    this.setHandlers()
    this.initSidebar()
    this.setStart()
  }
  
ifcn2.initialize()


//HUB SPOT TRACKING CODE.
//----------------------------------------------
//Start of HubSpot Logging Code
var hs_portalid=88888; 
var hs_salog_version = "2.00";
var hs_ppa = "preventure.app8.hubspot.com";
var hubscriptsrc=unescape("%3Cscript src='" + document.location.protocol + "//" + hs_ppa + "/salog.js.aspx' type='text/javascript'%3E%3C/script%3E");
//alert(hubscriptsrc);
document.write(hubscriptsrc);
//alert(1)
//End of HubSpot Logging Code

