
function HelpControl()
{
  this.mSource = null;
  this.helpUrl = null;
  this.popUp = null;
  this.helpContent = null;
  this.helpContainer = null;
  this.helpOpen = null;
  this.helpOpenButton = null;
  this.helpClose = null;
  this.url = null;
  this.helpHeaderText= null;
  this.helpHeader = null;
  this.isPopUpTip = null;
  var st;
  this.setAnimate = null;
  this.mDimensions = null;
  this.hidden = null;
  this.showTip = null;
  this.showTipParameter = null;
  
  this.init = function()
  {         
    st = this;
        
    //these hold the glow
    this.helpGlowR = $( document.createElement( 'div' ) );
    this.helpGlowR.id = 'HelpGlowR';
    this.helpGlowB = $( document.createElement( 'div' ) );
    this.helpGlowB.id = 'HelpGlowB';
    this.helpContent = $( document.createElement( 'iframe' ) );  
    
    if(this.popUp)
    {
        this.helpContent.id = 'HelpContentPopUp'; 
    }
    else
    {
        this.helpContent.id = 'HelpContent';
    }
    
    //this.helpImage =   $( document.createElement( 'a' ) ); 
    //this.helpImage.innerHTML='&nbsp';
    //this.helpImage.id = 'HelpImage'; 
    this.helpContainer = $( document.createElement( 'div' ) );
    this.helpContainer.id= 'HelpPopUp';
    this.helpHeader = $( document.createElement( 'div' ) );
    this.helpHeader.id = 'HelpHeader';
    
    if( this.popUp)
    {
        this.helpFooter = $( document.createElement( 'div' ) );
        this.helpFooter.id = 'HelpFooter';
    }

    this.helpContainer.appendChild(this.helpHeader);
    //this.helpContainer.appendChild(this.helpImage);
    this.helpContainer.appendChild(this.helpContent);
    
    if( this.popUp)
    {
        this.helpContainer.appendChild(this.helpFooter);
    }
    
    this.helpGlowR.appendChild(this.helpGlowB);
    this.helpGlowB.appendChild(this.helpContainer);
    
    this.helpHeaderText = $( document.createElement( 'a' ) ); 
    this.helpHeaderText.id = 'HelpHeaderText';
    this.helpHeaderText.innerHTML ='Help Tip';
    this.helpClose = $( document.createElement( 'a' ) );

    this.helpClose.id = 'HelpClose'; 
    
    this.helpClose.innerHTML = '&nbsp';
    this.helpClose.onclick = Staic_Close_Help;
    
    if(this.popUp)
    {
        this.helpFooterNeverShowAgain = $( document.createElement( 'a' ) ); 
        this.helpFooterNeverShowAgain.id = 'HelpFooterNeverShowAgain';
        this.helpFooterNeverShowAgain.innerHTML='Hide This Tip';
        this.helpFooterNeverShowAgain.onclick= Static_Never_Show_Again;
        
        this.helpNoMoreTips = $( document.createElement( 'a' ) ); 
        this.helpNoMoreTips.id = 'HelpFooterNoMoreTips';
        this.helpNoMoreTips.innerHTML='Hide All Tips';
        this.helpNoMoreTips.onclick= Static_No_More_Tips;
        
        this.helpFooterShowMore = $( document.createElement( 'a' ) ); 
        this.helpFooterShowMore.id = 'ShowMore';
        this.helpFooterShowMore.innerHTML='Show More';
        this.helpFooterShowMore.onclick= Static_Show_More;
       
        this.helpFooter.appendChild(this.helpFooterNeverShowAgain);
        this.helpFooter.appendChild(this.helpNoMoreTips);
        this.helpFooter.appendChild(this.helpFooterShowMore);
    }

    this.helpOpen = $( document.createElement( 'a' ) );
    this.helpOpen.innerHTML = 'Pop&nbsp;out';
    if(this.popUp)
    {
        this.helpOpen.id = 'HelpPopUpOpen';
    }
    else
    {
        this.helpOpen.id = 'HelpOpen';
    }
    this.helpOpen.onclick= Staic_Open_Help;    
        
    this.helpOpenButton = $( document.createElement( 'a' ) );
    this.helpOpenButton.innerHTML = '&nbsp';
 
    this.helpHeader.appendChild(this.helpHeaderText);
    this.helpHeader.appendChild(this.helpOpen);
    if(this.popUp)
    {
        this.helpFooter.appendChild(this.helpOpen);   
    }

    this.helpHeader.appendChild(this.helpClose);  
        
    this.helpOpen.appendChild(this.helpOpenButton);
    
    if(this.popUp)
    {
        this.loadContentAndShow();
    }

    document.body.appendChild( this.helpGlowR );
    
    if(! this.popUp)
    {
        this.helpGlowR.hide();
        this.hidden = true;
    }
        
    this.helpGlowR.style.position = 'absolute';
    
    /*if(! this.popUp)
    {
        this.updateSizeHelpContainer();
    }*/
    
    this.updatePosition();   
    
    if(this.popUp)
    {
        this.updateSizeHelpPopUp();
        
    }
    else
    {
        this.updateSizeHelp();
    }
    
    if(this.showTip=="false")
    {
        this.helpGlowR.hide();
    }

  };
  
  
  this.loadContentAndShow = function()
  {
    var headerText = null;       
    // load the page into the container
 
    if( st.popUp )
    {
      st.helpGlowR.show();
      
      if( st.showTip=="true" )
      {
        st.helpContent.src = st.helpUrl;
      }
    }
    else
    {
      st.toggle();
    }
    
  };
  
  this.toggle = function()
  {
      if( st.hidden == true )
      {
        st.show();
        st.hidden = false;
        
        st.helpContent.src = st.helpUrl;
      }
      else
      {
        st.hide();
        st.hidden = true;
      }
  };
  
  this.updatePosition = function()
  {
    var windowSize = jshGetWindowSize();
   
    // if it is the popUp then center it in the middle
    if(this.popUp)
    {
        this.helpGlowR.style.left = windowSize[0]/3 + 'px';     
        this.helpGlowR.style.top = windowSize[1]/3 + 'px';
    } 
    // else just below the header and left of the narrow search
    else
    {
        this.helpGlowR.style.left = '165px';    
        this.helpGlowR.style.top =  '105px';
    }
    
  };
  
  // if this not the popUp but instead a click from the help..
  // the the size should be according to the size of the window
  this.updateSizeHelpContainer = function()
  {
    var windowSize = jshGetWindowSize();
    this.helpContainer.style.width = windowSize[0] - 330 + 'px';
    this.helpContainer.style.height = '500px';   
        
  };
  
  this.updateSizeHelp = function()
  {
    this.updateSizeHelpContainer();
    
    this.helpGlowR.style.height = (this.helpContainer.style.height.replace(/px/,'') -0) + 6 + 'px';
    this.helpGlowR.style.width =(this.helpContainer.style.width.replace(/px/,'') -0)+ 10 + 'px';
    
    this.helpGlowB.style.height =(this.helpContainer.style.height.replace(/px/,'') -0) + 9 + 'px';
    this.helpGlowB.style.width =(this.helpContainer.style.width.replace(/px/,'') -0) + 3 + 'px';
        
    this.helpContent.style.width =(this.helpContainer.style.width.replace(/px/,'') -0) + 'px';
    this.helpContent.style.height = (this.helpContainer.style.height.replace(/px/,'') -0) -28 + 'px';
        
    this.mDimensions = $( [ this.helpGlowR.getWidth(), this.helpGlowR.getHeight() ] );
      
  };
  
  this.updateSizeHelpPopUp = function()
  {
    this.helpGlowR.style.height = this.helpContainer.offsetHeight + 5 + 'px';
    this.helpGlowR.style.width = this.helpContainer.offsetWidth + 7 + 'px';

    this.helpGlowB.style.height = this.helpContainer.offsetHeight + 8 + 'px';
    this.helpGlowB.style.width = this.helpContainer.offsetWidth + 'px';
    
    this.helpContent.style.width = this.helpContainer.offsetWidth - 2 + 'px';
    this.helpContent.style.height = this.helpContainer.offsetHeight + 5 + 'px';
    
    this.mDimensions = $( [ this.helpGlowR.getWidth(), this.helpGlowR.getHeight() ] );
  };
  
  this.render = function( url, helpUrl, popUp )
  {
    this.url = url;
    this.helpUrl = helpUrl;
    this.popUp = popUp;

    if(this.helpUrl != null && this.helpUrl !='')
    {    
        this.init();
    }
    
  };
  
  function Staic_Close_Help( e )
  {
    if(st.popUp)
    {
        if(st.helpGlowR)
        {
         st.helpGlowR.hide();
        }
    }
    else
    {
        st.hide();
    }
    
  };

  function Staic_Open_Help( e )
  {
    st.helpGlowR.hide();
    window.open(st.helpUrl,'mywindow','scrollbars=yes,resizable=yes'); 
  };
  function doNothing()
  {
      
  }
  function Static_Never_Show_Again ( e )
  {
    st.helpGlowR.hide();
    
    if (st.showTipParameter == '&advancedSearchShowTip=false')
    {
        st.setAdvancedSearchObject.advancedSearchShowTip = false;
    }
    
    var sUrl = st.url;
    sUrl += st.showTipParameter ;
    sUrl += '&rand=' + Math.random();
      
    //debug( 'sUrl: ' + sUrl );
      
    var callback =
    {
      success: doNothing,
      failure: doNothing,
      timeout: timeoutMilli // time out at 10 sec
    }
    
    // make call
    var transaction = YAHOO.util.Connect.asyncRequest( 'GET', sUrl, callback, null );
    
    // if you are on the user settings page refresh those checkboxes immdiately
    st.refreshCheckBox();
  };
  
  function Static_No_More_Tips( e )
  {
    st.helpGlowR.hide();
    
    if (st.showTipParameter == '&advancedSearchShowTip=false')
    {
        st.setAdvancedSearchObject.advancedSearchShowTip = false;
    }
    
    var sUrl = st.url;
    sUrl += '&hideAllTips=true';
    sUrl += '&rand=' + Math.random();
      
    //debug( 'sUrl: ' + sUrl );
      
    var callback =
    {
      success: doNothing,
      failure: doNothing,
      timeout: timeoutMilli // time out at 10 sec
    }
    
    // make call
    var transaction = YAHOO.util.Connect.asyncRequest( 'GET', sUrl, callback, null );
    
    st.refreshCheckBoxes();
  };
  
  function Static_Show_More( e )
  {
    st.helpGlowR.hide();  
    //var evt = document.createEvent('MouseEvents');
    //evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    //st.mSource.dispatchEvent(evt);
    if(st.mSource.fireEvent)
    {
        st.mSource.fireEvent('onclick');
    }
    else if(document.createEvent)
    {
        var evt = document.createEvent('MouseEvents');
        evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        st.mSource.dispatchEvent(evt);
    }
  };
  
  this.refreshCheckBox = function()
  {
    if(this.helpUrl=='/insight/help/userSettingsHelp.html')
    {
        var checkbox = $('userSettings.helpfulTip.user.settings');
        if(checkbox)
        {
            checkbox.checked = false;  
        }
    }
    if(this.helpUrl=='/insight/help/advancedSearchHelp.html')
    {
        var checkbox = $('userSettings.helpfulTip.advancedSearch');
        if(checkbox)
        {
            checkbox.checked = false;  
        }
    }
  };
  
  this.refreshCheckBoxes = function()
  {
      var checkboxes = document.getElementsByName('displayHelpfulTips');
      
      if( checkboxes )
      {
          var i;
          for (i  in checkboxes)
          {
              checkboxes[i].checked = false;
          }
      }
  };
  
  
  this.hide = function()
  {
    if( this.mAnimate == true )
    {
      if( ! this.popUp )
      {
        ElementEffects.animateElementGrowth( this.helpGlowR, [ 0, 0 ], 55, .45, doNothing );
      }
      else
      {
          this.helpGlowR.hide();
      }
    }
    else
    {
      this.helpGlowR.hide();
    }
    this.hidden = true;
  };
  
  this.show = function()
  {                     
    st.updateSizeHelp();
    st.helpGlowR.show();   
    
    if( !this.popUp)
    {
        if( st.mAnimate == true )
        {
          st.helpGlowR.style.width = 0 + 'px';
          st.helpGlowR.style.height = 0 + 'px';
          ElementEffects.animateElementGrowth( st.helpGlowR, st.mDimensions, 55, .45, doNothing );
        }
    }
  };
  
  this.setAnimate = function( animate )
  {
    this.mAnimate = animate;
  };
  
  this.setShowTip = function (showTip)
  {
    this.showTip = showTip;
  };
  
  this.setHelpUrl = function (helpUrl)
  {
      this.helpUrl = helpUrl;
  };
  
  this.setListnerSource = function (source)
  {
      this.mSource = source; 
      this.setListner();
  };
  
  this.setListner = function ()
  {
    // add this on click even for the click of the word help in the MainNav
    if(! this.popUp)
    {
        YAHOO.util.Event.addListener( this.mSource, 'click', this.loadContentAndShow );
    } 
  };
  
  this.setShowTipParameter = function ( parameter )
  {
      this.showTipParameter = parameter;
  };
  
  this.setHelpPopObject = function ( setHelpPopObject )
  {
      this.setHelpPopObject = setHelpPopObject;
  };
  
  this.setHelpObject = function ( setHelpObject )
  {
    this.setHelpObject = setHelpObject;
  };
  
  this.setAdvancedSearchObject = function (  setAdvancedSearchObject )
  {
    this.setAdvancedSearchObject = setAdvancedSearchObject;
  };
  
 }