/**** TRUNCATE ****/
(function (h) {
  function j(c, a, d, b, e, f, h) { var i, g; h ? (i = d === 0 ? "" : a.slice(-d), g = a.slice(-b)) : (i = a.slice(0, d), g = a.slice(0, b)); if (e.html(g + f).width() < e.html(i + f).width()) return b; g = parseInt((d + b) / 2, 10); i = h ? a.slice(-g) : a.slice(0, g); e.html(i + f); if (e.width() === c) return g; e.width() > c ? b = g - 1 : d = g + 1; return j(c, a, d, b, e, f, h) } h.fn.truncate = function (c) {
    c = h.extend({ width: "auto", token: "&hellip;", center: !1, addclass: !1, addtitle: !1 }, c); return this.each(function () {
      var a = h(this), d = { fontFamily: a.css("fontFamily"), fontSize: a.css("fontSize"),
        fontStyle: a.css("fontStyle"), fontWeight: a.css("fontWeight"), "font-variant": a.css("font-variant"), "text-indent": a.css("text-indent"), "text-transform": a.css("text-transform"), "letter-spacing": a.css("letter-spacing"), "word-spacing": a.css("word-spacing"), display: "none"
      }, b = a.text(); d = h("<span/>").css(d).html(b).appendTo("body"); var e = d.width(), f = !isNaN(parseFloat(c.width)) && isFinite(c.width) ? c.width : a.width(); e > f && (d.text(""), c.center ? (f = parseInt(f / 2, 10) + 1, e = b.slice(0, j(f, b, 0, b.length, d, c.token, !1)) + c.token +
b.slice(-1 * j(f, b, 0, b.length, d, "", !0))) : e = b.slice(0, j(f, b, 0, b.length, d, c.token, !1)) + c.token, c.addclass && a.addClass(c.addclass), c.addtitle && a.attr("title", b), a.html(e)); d.remove()
    })
  } 
})(jQuery);

jQuery(document).ready(function ($) {
  // Initialize Contact form modals.
  $("a.contact").fancybox({ 'type': 'iframe', 'width': 760, 'height': 606 });
  $(".IE9 a.contact").fancybox({ 'type': 'iframe', 'width': 760, 'height': 626 });
  $(".IE7 a.contact").fancybox({ 'type': 'iframe', 'width': 760, 'height': 636 });
	
  // Initialize Contact form modals.
  $("a.intranet").fancybox({ 'type': 'iframe', 'width': 760, 'height': 680 });
  $(".IE9 a.intranet, .IE8 a.intranet").fancybox({ 'type': 'iframe', 'width': 760, 'height': 680 });
  $(".IE7 a.intranet, .IE6 a.intranet").fancybox({ 'type': 'iframe', 'width': 760, 'height': 700 });
		
  $(".zoom").fancybox();
  //IE6 Fixes

  //Used to fix submenu displaying behind the theater slider in IE6 & 7
  $("body.IE6 .nav, body.IE7 .nav").hover(
    function () {
      $("body.IE6 .featured-stage, body.IE7 .featured-stage").css("z-index", "-1");
      $("body.IE6 .featured-stage, body.IE7 .featured-stage").css("position", "relative");
    },
          function () {
            $("body.IE6 .featured-stage, body.IE7 .featured-stage").css("z-index", "0");
            $("body.IE6 .featured-stage, body.IE7 .featured-stage").css("position", "relative");
          });

  //IE6 fixes - Only inline styles seemed to take
  $("body.IE6 footer .site-search input.infield").css('width', '120px');
  $("body.IE6 .tabs .tabnav").css('zoom', '1');

  var $ie6_main_nav = $('.IE6 #site-nav > li');
  $ie6_main_nav.hover(function () { $('.main-subnav', this).toggle(); });

  //Fix to add underlines to hover on hyperlinks in submenu in IE 6	
  $(".main-subnav a", $ie6_main_nav).hover(function () {
    $(this).css("text-decoration", "underline");
  }, function () {
    $('body.IE6 #site-nav .main-subnav a').css("text-decoration", "none")
  });


  if ($('html').hasClass('touch')) {
    $('#site-nav > li').click(function () { $('.main-subnav', this).toggle(); });
  }


  // ew dirty.
  $('.panel article:last').css('border-bottom', 'none');

  jQuery('.subscribe-newsletter div.infield  ').append('<label style="display:none;">Enter Email</label>');
  /* ========== Placing labels inside of fields. ========== */
  // on page load
  jQuery('input.infield').each(
    function () {
      var input = jQuery(this);
      var lbl = input.siblings('label');
      input.val(lbl.text()).css('color', '#c0c1c4');
      lbl.css('display', 'none');
    });
  // field gets the focus
  jQuery('input.infield').focusin(
        function () {
          var input = jQuery(this);
          var lbl = input.siblings('label');
          if (input.val() == lbl.text()) {
            input.val("").css('color', '#000');
          }
        }
    );
  // field loses the focus
  jQuery('input.infield').focusout(
        function () {
          var lbl = jQuery(this).siblings('label');
          var input = jQuery(this);
          if (input.val() == "") {
            if (input.attr('type') == 'password') {
              input.css('display', 'none');
              input.siblings('input.txtpwd').css('display', 'block');
            }
            else {
              input.val(lbl.text()).css('color', '#c0c1c4');
            }
          }
        }
    );


  /**** SLIDESHOW *****/
  if ($('.slideshow').length) {
    $('.slideshow')
    .after('<li id="slide-nav">')
    .cycle({
      fx: 'scrollLeft',
      speed: '300',
      timeout: 10000,
      pager: '#slide-nav'
    });

    /* GA tracking for slideshow links */
    $('.slideshow a').click(function () {
      _gaq.push(['_trackEvent', 'Banner Links', $('img', this).attr('alt')]);
    });
  }
  if ($('.news-feed').length) {
    $('.news-feed')
    .cycle({
      fx: 'scrollUp',
      speed: '300',
      timeout: 5000,
      width: 830,
      height: 30,
      fit: 1
    });
  }

  $('.news-feed-text').truncate({
    width: '615',
    token: '&hellip;',
    center: false
  });

  /**** PANELS/TABS *****/

  /***** INTERIOR PAGES HIDE EMPTY TABS *****/
  $('.tabnav li a').each(function () {
    var input = jQuery(this);
    if ($.trim(input.text()).length == 0) input.hide();
  });


  // setup ul.tabs to work as tabs for each div directly under div.panes
  $('ul.tabnav').tabs('div.panel > div');

  var TabApi = $('ul.tabnav').data('tabs');
  if (TabApi) {
    TabApi.onClick(function (e, index) {
      _gaq.push(['_trackEvent', 'Tab Interactions', document.title, 'tab ' + index]);
    });
  }

  /***** EQUAL HEIGHTS *****/
  if ($('.featured-articles').length === 1) {
    $('.featured-articles').equalHeights();
  }

  //first put an anchor around the selected
  $('.CMSListMenuUL li.CMSListMenuHighlightedLI').html('<a href=\"#\" class=\"current\">' + $('.CMSListMenuUL li.CMSListMenuHighlightedLI span.CMSListMenuLinkHighlighted').html() + "</a>");
  //now prepend the right arrow span to all li's in the navs
  $('.CMSListMenuUL a').prepend('<span>&rsaquo;</span>');


  /**** 100% Width of Content without interior secondary nav ***/
  if ($('.interior-secondary-nav, #secondaryNavMenu').length === 0) {
    $('.tabs').css('width', '100%');
  }

  // remove benner image container if stage is empty 
  if ($('.featured-stage li img').length === 0) {
    $('.featured-stage').remove();
  }

  /*** REMOVE NASTY BR FROM KINTECO FORM ***/
  if ($('.FormPanel').length === 1) {
    $('.FormPanel br').remove();
  }

  /** adding left margin to lead thumbnails if floated right **/
  if ($('img.lead-thumb').css('float') == 'right') {
    $('img.lead-thumb').css('margin', '0 0 8px 8px');
  }

  /** makes hover work for top nav in IE6 **/
  jQuery("#nav li").hover(
    function () {
      jQuery(this).addClass("sfhover");
    },
    function () {
      jQuery(this).removeClass("sfhover");
    });

  if (jQuery("div.resource-center-wrapper").length > 0) {
    jQuery("div#ctl00_plcMain_pnlResourceDataPanel ul").prependTo("div.resource-center-content");
    jQuery("div#ctl00_plcMain_pnlResourceDataPanel h1").prependTo("div.resource-center-content");
  }

  // Hides the select drop box for the contact forms
  jQuery("select#ctl00_plcMain_frmContactRegInput_ctl00_Industry").closest("li").css("display", "none");

  // remove the speech bubble
  if (jQuery("div.modal-contact-form-headline h1").html() == "") {
    jQuery("div.modal-contact-form-headline").hide();
  }

  // hides the select industry list ono contact forms
  jQuery("select#ctl00_plcMain_frmContactRegInput_ctl00_Industry").closest("li").css("display", "none");

  // gives the current menu item on events, news, 
  // and resource pages the correct url
  if (window.location.pathname.search("news-and-events/events") == 1) {
    jQuery("ul#secondaryNavMenu a.current").attr("href", "/news-and-events/events");
  }
  else if (window.location.pathname.search("news-and-events/news") == 1) {
    jQuery("ul#secondaryNavMenu a.current").attr("href", "/news-and-events/news");
  }

  //Change spans into proper labels for trainging form
  $('.training-form .FormPanel .EditingFormLabelCell span').each(function () {
    var html = $(this).html();
    $(this).replaceWith('<label>' + html + '</label>');
  });

  // hide upcoming events if no items
  if (jQuery("div.upcoming-events li").size() === 0) {
    jQuery("div.upcoming-events").hide();
  }

  // hide upcoming events if no items
  if (jQuery("div.upcoming-events li").size() === 0) {
    jQuery("div.upcoming-events").hide();
  }

  // hide upcoming events if no items
  if (jQuery("div.resource-center li").size() === 0) {
    jQuery("div.resource-center").hide();
  }

  // javascript to compensate for the state label
  // in the control
  if (jQuery("#ctl00_plcMain_frmContactRegInput_ctl00_Country_uniSelectorCountry_drpSingleSelect").val() === "USA") {
     jQuery("label#countryLabel").after("<label id='stateLabel'>State*</label>");
  }
  
  jQuery("#ctl00_plcMain_frmContactRegInput_ctl00_Country_uniSelectorCountry_drpSingleSelect")
  .change(function () {
	  setInterval("addStateField()", 250);
  })
  .focus(function(){
    setInterval("addStateField()", 250);
  });
  
  // hide now on calendar control
  jQuery("#ctl00_plcMain_frmTrainingReg_ctl00_TrainingDate_btnNow").hide();
  
  // make sure all links to customer login open in another window
  jQuery("a[href*='customer-login'], a[href*='starlims.webexone.com/login.asp']").attr("target", "_blank");

});

function addStateField()
{

  if (jQuery("#ctl00_plcMain_frmContactRegInput_ctl00_Country_uniSelectorCountry_drpSingleSelect").val() === "USA") {
    if (jQuery("label#stateLabel").length > 0)
    {
    	jQuery('label#stateLabel').show("slow");
    }
    else
    {
    	jQuery('label#countryLabel').after('<label id="stateLabel">State*</label>').fadeIn("slow");
    }
  }
  else {	
    if (jQuery("label#stateLabel").length > 0)
    {
    	jQuery("label#stateLabel").hide("slow");
    }
    else
    {
    	jQuery('label#countryLabel').after('<label id="stateLabel" style="display:none;">State*</label>').fadeOut("slow");
    }       
  }

}
