function ellipsize(text) {
  var max_char = 25;
  var i = 1;
  var ret = '';
  var truncated = false;
  text = text.replace(/^\s+|\s+$/g,"");
  words = text.split(' ');
  ret = words[0];
  while(i < words.length) {
    if (ret.length + words[i].length + 1 < max_char) {
      ret += ' ' + words[i];
    } else {
      truncated = true;
      break;
    }
    i++;
  }
  if (truncated == true) {
    ret += ' ...';
  }
  return ret;
}

jQuery(document).ready(function($){
	$('.custom-select').jqTransform();
	
	$('#navigation ul li:first').addClass('first');
	$('#navigation ul li:last').addClass('last');
	$('#footer .bottom-menu span:last').remove();
	$('.widget .menu li:last').addClass('last');

        // For home page, make smaller boxen clickable to title link
        $.each($('.col-short.fl, .col-short.fr, .col-wide.fl, .col-wide.fr'), function(index, item) {
          var link = $(item).find('a');
          $(item).wrap('<a href="' + $(link).attr('href') + '" />');
        });

        // Live chat link opens in new resized window.
        $('.live-chat a').live('click', function(){
              window.open('http://server.iad.liveperson.net/hc/70406058/?cmd=file&amp;file=visitorWantsToChat&amp;site=70406058&amp;referrer='+document.location,'chat70406058','width=472,height=320');
              return false;
        });

        if ((navigator.userAgent.indexOf("Windows NT 5.1")!=-1) || (navigator.userAgent.indexOf("Windows XP")!=-1)) {
	  $('body').addClass('platform_xp');
	}

        var p = $('.col-short.fr').find('.post-sparkle-like-the-stars, .post-in-the-community');
        if ($(p).length != 0) {
          $('.col-short.fr').css('margin-right', '2px');
        }

        if ($('.col-short.fr').length > 0) {

          $('.col-short.fl .post-container .entry h2 a, .col-short.fr .post-container .entry h2 a').each(function(index, value) {
            $(value).html(ellipsize($(value).html()));
          });
          var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
          var is_safari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
          var top_right_height = parseInt($('.col-wide.fr').height());// - 15;
          if ((is_safari || is_chrome) && top_right_height < 170) {
            top_right_height += 50;
          }
          if ($('.col-short.fl .post-in-the-community').length > 0) {
            top_right_height += 7;
          }

          $('div.banner-1').css('margin-top', (198 - top_right_height) + 'px');

          var middle_height = parseInt($('.col-wide.fl').height());// - 15;
          if ((is_safari || is_chrome) && middle_height < 170) {
            middle_height += 50;
          }
          $('div.banner-2').css('margin-top', (198 - middle_height) + 'px');
        }

	$(function(){
		$('.blink').
	        focus(function() {
	            if(this.title==this.value) {
	                this.value = '';
	            }
	        }).
	        blur(function(){
	            if(this.value=='') {
	                this.value = this.title;
	            }
	        });
    });

    changeSearchBoxBackground();
    $('#searchform').submit(function(){
      var keyword = $('#searchform input#s').attr('value');
      if (keyword == 'Search') {
        $('#searchform input#s').val('');
      }
      //return false;
    });
    function changeSearchBoxBackground(){
        var searchBox = $('#searchform input#s');
        var defaultText = searchBox.attr('value');
        var focusClass = 'focus';
        searchBox.focus(function(){
        if(defaultText == this.value) this.value = "";
        this.className += " " + focusClass;
        this.parentNode.className += " parent-" + focusClass;});

        searchBox.blur(function(){
        if(this.value == "") {this.value = defaultText;}        
        this.className = this.className.replace(focusClass, "");
        this.parentNode.className = this.parentNode.className.replace("parent-"+focusClass, "");});
}

});

