// NEW - setup for jQuery plugins


(function($) {

  /* tabs plugin */

  $.fn.tabs = function(selector, noSelect){
    this.each(function(){
       var p = $(this);
       var tabs = $(selector || 'a', p);
       var panels = tabs.tabPanels();
       var loc = location.href;
       var p_tab1 = '#additionalreading';
       var p_tab2 = '#grants';
       var p_tab3 = '#contacts';
       var p_tab4 = '#disqus_thread';
       var gr_tab1 = '#description';
       var gr_tab2 = '#products';
       var gr_tab3 = '#grants';

       tabs.click(function(event){
         panels.hide();
//         $('.current', p).removeClass('current');
           // above line is replaced with following to fix 3rd party library conflict
           for(var i=0;i<tabs.length;i++){
               document.getElementById(tabs[i].id).className = '';
           }
           if(this.href.indexOf('grant.jsp') >= 0 || this.href.indexOf('npo.jsp') >= 0) {
              if(this.href.indexOf(gr_tab1) >= 0 && (location.href.indexOf(gr_tab2) >= 0 || location.href.indexOf(gr_tab3) >= 0) )  window.location.href = this.href; // for the 1st tab - show in href only if any of the other tabs were accessed last
              else if(this.href.indexOf(gr_tab2) >= 0 || this.href.indexOf(gr_tab3) >= 0) window.location.href = this.href;  // for remaining tabs - update the href
           }
           if(this.href.indexOf('product.jsp') >=0 || this.href.indexOf('productpreview.jsp') >=0) {
               if(this.href.indexOf(p_tab1) >= 0 && (location.href.indexOf(p_tab2) >= 0 || location.href.indexOf(p_tab3) >= 0 || location.href.indexOf(p_tab4) >= 0) )  window.location.href = this.href; // for the 1st tab - show in href only if any of the other tabs were accessed last
               else if(this.href.indexOf(p_tab2) >= 0 || this.href.indexOf(p_tab3) >= 0 || this.href.indexOf(p_tab4) >= 0) window.location.href = this.href;  // for remaining tabs - update the href
           }
           var panel = $(this).tabPanels();
         if (panel) {
             panel.show();
         }
         $(this).addClass('current');
         event.preventDefault();
       });

        // setting to a default tab based on URL value
        for(var i=0; i<tabs.length; i++) {
            if(loc == tabs[i]) {
                var t = ':eq('+i+')';
                tabs.filter(t).click();
                noSelect = true;
            }
        }
        
       if (!noSelect) {
         tabs.filter(":first").click();
       }
    });

    return this;      
  };
 
  /* tab panels helper plugin for tabs */
  
  $.fn.tabPanels = function() {
    return $(this).map( function(i, v) {

      var p = $($(this).attr("href")); 

      if (p.length > 0) {
        return p[0];
      }
    });
  }; 
 

    $(function() {
        if(document.getElementById("interactivedescdiv"))
        {
            document.getElementById("hiddenInteractiveDesc").innerHTML =  document.getElementById("interactivedescdiv").innerHTML;
            if(document.getElementById("interactivedescdiv").innerHTML.length > 300)
            {
                var xDesc = document.getElementById("interactivedescdiv").innerHTML.substring(0,300);
                
                if(xDesc.substring(298,299) == '<')
                {
                   xDesc = xDesc.substring(0,298) ;
                }
                else
                if(xDesc.substring(298,299) == '/')
                {
                   xDesc = xDesc.substring(0,297) ;
                }
                else if(xDesc.substring(298,299) == 'P' || xDesc.substring(298,299) == 'p')
                {
                   xDesc = xDesc.substring(0,296) ;
                }
                else if(xDesc.substring(298,299) == '>')
                {
                   xDesc = xDesc.substring(0,295) ;
                }
                
                if($.browser.msie)
                {
                    xDesc +=  "...<A HREF='#' onClick='showMoreInteractiveDesc(); return false;'><font size='1'>Show More</font></A>"
                }
                else
                {
                    xDesc +=   "...<A HREF='' onClick='showMoreInteractiveDesc(); return false;'><font size='1'>Show More</font></A>"
                }

                document.getElementById("interactivedescdiv").innerHTML = "<p>" + xDesc + "</p>";
            }
        }
    });
   
    
  $(document).ready( function() {
    // set up collapsed lists
    $(".collapsorz").collapsorz({
    	wrapLink: '<div class="full-text-toggle"><strong></strong></div>' // specify HTML code to wrap around the link
		});
 
  
    // setup toc reveal
    $('#toc-trigger').reveal({
      target: "#toc-menu",
      event: "mouseover",
      preventDefault: false,
      hideOnMouseLeave: true, 
      hideOnTargetClick: false, 
      hideOnTargetMouseLeave: true, 
      classShown: "menu-shown",
      affix: { to: 'sw', from: 'nw', glueInsideWindow: false },
      selectorHide: "a.toc-pop-close"
    });

    $('.tabs').tabs();
    
    // setup toggle
    
    $('.toggle-list .head').click(function() {
        $(this).toggleClass("open").next().slideToggle("fast");
        return false;
    }).next().hide();

    $('#toc-collapse-all').click( function() { $('#toc-toggle-list .head').removeClass("open").next().hide() } );

      $("a.toolemail,a.email").fancybox({
                'width'				: '50%',
				'height'			: '60%',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
        });

  });

})(jQuery);
