
/**
* TODO: rewrite this POS
*/
if(TSCM.cfg.RMportal === false) {
//POPUPS
var servicesPopup = {

      init : function(){
        var dom = YAHOO.util.Dom;
     //SERVICES
         //Create a new link and append it to the Body. The stylesheet sets it to position:absolute and initially visibility:hidden.
         var servicesHoverLink = document.createElement('span');
         servicesHoverLink.id = 'capsHoverTabServices';
         document.body.appendChild(servicesHoverLink);

         //The stylesheet sets it to position:absolute and initially visibility:hidden.
         var servicesHoverBox = dom.get("servicesPopup");

         //Create a new Img that will be behind the visible hover box and onmouseover will trigger the hiding of hoverLink and hoverBox.
         var servicesHoverBoxSurround = document.createElement('img');
         servicesHoverBoxSurround.id = 'capsHoverSurround';
         servicesHoverBoxSurround.src = "http://i.thestreet.com/files/tsc/v2008/css/images/spacer.gif";
         document.body.appendChild(servicesHoverBoxSurround);

          var elm;
        if (premiumPage)  {         //adds iframe behind recent quotes (only on premium pages) popup so it will float over pdf
            elm ='iframe';
        } else  {
             elm ='em';
        }
          var servicesIframe = document.createElement(elm);
          servicesIframe.id = 'servicesiframeShim';
          document.body.appendChild(servicesIframe);

         //object to change arrow image in
         var servicesArrow = dom.get("servicesArrow");

         var el = dom.get('servicesLink');
         enhanceServicesLink(el, servicesHoverLink, servicesHoverBox, servicesHoverBoxSurround, servicesArrow, servicesIframe);

      }
};



/*SERVICES POPUP*/
 //-----------------------------------------------------------------------------------------------
 //When user moves mouse over servicesEl link, get data and display in popup
 function enhanceServicesLink(servicesEl, hoverLink, hoverBox, hoverBoxSurround, arrow, shim)  {
    var dom = YAHOO.util.Dom;
     //Define the mouseover function for the LINK
     servicesEl.onmouseover = function()  {

         //Get position of link
         servicesEl.pos = dom.getXY(servicesEl);

         //Hack to deal with IE bug (including IE7!) not getting correct vertical position.
         //If link is at the beginning of a line that is not the first line of a paragraph,
         //the position will be one line too high.
         var agent = navigator.userAgent.toLowerCase();
         var indexMSIE = agent.indexOf("msie")
         if (indexMSIE != -1)  {
             //var version = navigator.appVersion; //Perhaps IE8 will fix bug so be prepared to check version
             if(servicesEl.offsetLeft < 10)  {
                 //Since position is reported one line too high, can't use comparison of offsetTop against
                 //offsetHeight to distinguish between 1st and 2nd lines though it does work for 3rd, 4th.
                 //Instead, check to see if servicesEl is the first child of link's parent.
                 var para = servicesEl.parentNode;
                 var children = para.childNodes;
                 if(servicesEl != children.item(0))  {
                     servicesEl.pos[1] = servicesEl.pos[1] + servicesEl.offsetHeight;
                 }
            }
        }

         //Update the hoverLink text and href, make and position over the existing link.
         arrow.src = "http://i.thestreet.com/files/tsc/v2008/css/images/greenArrowTrans.gif"; //change white arrow to green
         hoverLink.innerHTML = servicesEl.innerHTML;
         hoverLink.style.visibility = 'visible';
         var hoverLinkLeft = servicesEl.pos[0];
         var hoverLinkTop = servicesEl.pos[1];
         hoverLink.style.left = hoverLinkLeft - 3 + 'px';
         hoverLink.style.top = hoverLinkTop + 'px';

         //Make the popup visible and centered just over the link
         hoverBox.style.visibility = 'visible';
         hoverBox.style.top  = (hoverLinkTop + hoverLink.offsetHeight + 1) + 'px';
         hoverBox.style.left  = (hoverLinkLeft - (hoverBox.offsetWidth - hoverLink.offsetWidth) + 1) + 'px';

         //Make the capsHoverSurround Div 200px larger than popup and position 100px above and to the left of popup
         hoverBoxSurround.style.visibility = 'visible';
         hoverBoxSurround.style.width  = (hoverBox.offsetWidth + 200) + 'px';
         hoverBoxSurround.style.height = (hoverBox.offsetHeight + 200) + 'px';
         hoverBoxSurround.style.top  = (hoverLinkTop - 100) + 'px';
         hoverBoxSurround.style.left  = (hoverLinkLeft - 200) + 'px';

         //Put the iframe shim right behind it (z-index set in css)
         shim.style.top  = (hoverLinkTop + 23) + 'px';
         shim.style.left  = (hoverLinkLeft + 27 ) + 'px';
         shim.style.width = hoverBox.offsetWidth + 'px';
         shim.style.height = hoverBox.offsetHeight + 'px';
         shim.style.visibility = 'visible';
    };

     //Define function to turn everything off
     hideServicesHover = function()  {
         arrow.src = "http://i.thestreet.com/files/tsc/v2008/css/images/transArrow.gif"; //change green arrow to white
         hoverLink.style.visibility = 'hidden';
         hoverBox.style.visibility = 'hidden';
         hoverBoxSurround.style.visibility = 'hidden';
         shim.style.visibility = 'hidden';
         shim.style.height = 0 + 'px';
    }

     //Define the mouseover function for the SURROUND DIV to turn everything off when user moves mouse away
     hoverBoxSurround.onmouseover = hideServicesHover;

     //Also hide the hover box when the page unloads to make sure it is gone if user hits Back button
     YAHOO.util.Event.addListener( window, "unload", hideServicesHover );
}

var tscNavRoll = new function() {
   var DOM = YAHOO.util.Dom;
   var Event = YAHOO.util.Event;
   var subnavs=[];
   var defaultSubnav= null;
   var defaultId = null;
   var helperTop = null;
   var helperBottom = null;
   var navbtns = [];

   return { 
      init : function(){
         //log("tscNavRoll init");
         var navRegion = DOM.getRegion('nav');
         var subRegion = DOM.getRegion('taxonomy_subnav');

         //create helper divs for mouseovers that simulate mouseout
         this.helperTop = tscNavRoll.createHelper("helperTop", DOM.getDocumentWidth(), 3);
         this.helperBottom = tscNavRoll.createHelper("helperBottom", DOM.getDocumentWidth(), 6);

         var navbtns = YAHOO.util.Dom.getChildren(YAHOO.util.Dom.getFirstChild("nav"));
         var bHasActive = false;
         for (var i in navbtns) {
            var btn = navbtns[i];
            if(YAHOO.util.Dom.hasClass(btn,"active") && (btn.id != '') && (typeof(btn.id) != 'undefined')) {
               this.defaultId = btn.id;
               bHasActive = true;
            }
            YAHOO.util.Event.on(btn, 'mouseover', function(e) {
               //log("this.id: " + this.id);
               tscNavRoll.over(this);
            });
         }

         //if there's no active nav, then use the first child as the default subnav
         if ((!bHasActive) || typeof(this.defaultId) == "undefined") {
            //log("no active button.");
            this.defaultId = navbtns[0].id;
         }

         subnavs = DOM.getChildren("taxonomy_subnav");
         for (var i in subnavs) {
            var subnav = subnavs[i];
            if (subnav.id === "subnav_" + this.defaultId) {
               this.defaultSubnav = subnav;
               //log("defaultSubnav: " + this.defaultSubnav.id);
            }
         }

         this.showDefault();

         //reposition the helpers on resize.
         YAHOO.util.Event.addListener(window,"resize",function() {
            tscNavRoll.positionAllHelpers();
         });

         //timeout needed for chrome to place helper correctly.
         window.setTimeout(function() {
                tscNavRoll.positionAllHelpers();
           }, 100);

         navbtns = DOM.getChildren(DOM.getFirstChild("nav"));

      },
      over:function(el) {
         var bFound = false;
         for (var i in subnavs) {
            var subnav = subnavs[i];
            if (subnav.id === "subnav_"+el.id) {
               bFound = true;
               DOM.setStyle(subnav, 'display', 'block');
            } else {
               DOM.setStyle(subnav, 'display', 'none');
            }
         }
         //display default subnav if the id doesn't exist (ie. premium products)
         if (!bFound) {
            //log("element not found. show default subnav");
            DOM.setStyle(this.defaultSubnav, 'display', 'block');
         }

         for (var i in navbtns) {
            var btn = navbtns[i];
            if (btn.id === this.defaultId) {
               //log("active button never changes: "+ btn.id);
            } else if (btn.id == el.id) {
               DOM.addClass(btn,"active");
            } else {
               DOM.removeClass(btn,"active");
            }
         }
      },
      createHelper: function(elid,w,h) {
         var DOM = YAHOO.util.Dom;
         var helper = document.createElement('div');
         helper.id = elid;
         document.body.appendChild(helper);
         DOM.setStyle(helper, 'position', 'absolute');
         DOM.setStyle(helper, 'height', h + 'px');
         DOM.setStyle(helper, 'font-nize', '0px'); //for ie6. Otherwise, the height will be too large.
         DOM.setStyle(helper, 'padding', '0px');
         DOM.setStyle(helper, 'margin', '0px');
         DOM.setStyle(helper, 'background', 'transparent url(http://www.thestreet.com/files/tsc/v2008/css/images/1x1.gif) repeat');
         this.positionHelper(helper);

         YAHOO.util.Event.on(helper, 'mouseover', function(e) {
            this.showDefault();
         },this,true);

         return helper;

      },
      positionHelper : function(el) {
         var navRegion = DOM.getRegion('nav');
         var subRegion = DOM.getRegion('taxonomy_subnav');
         switch (el.id) {
            case "helperTop":
               var helperPosition = [0, (navRegion.top - 3)];
               break;
            case "helperBottom":
               var helperPosition = [0, (navRegion.top + navRegion.height + subRegion.height)];
               break;
            default:
               log ("unrecognized helper: " + el.id);
         }

         DOM.setXY(el.id, helperPosition);
         DOM.setStyle(el, 'width', DOM.getViewportWidth() + 'px');

      },
      positionAllHelpers : function() {
         try{
            this.positionHelper(this.helperTop);
            this.positionHelper(this.helperBottom);
         } catch (e) {
            log(e)
         };
      },
      showDefault : function() {
         var DOM = YAHOO.util.Dom;

         for (var i in subnavs) {
            var subnav = subnavs[i];
            DOM.setStyle(subnavs[i], 'display', 'none');
         }
         DOM.setStyle(this.defaultSubnav, 'display', 'block');

         for (var i in navbtns) {
            var btn = navbtns[i];
            if (btn.id == this.defaultId) {
               //log("active button never changes: "+ btn.id);
            }  else {
               DOM.removeClass(btn,"active");
            }
         }
      }
   } // ret
} // navpop

} // rm

