$(function () {
  includeScripts();
  cssReset();
  cssTweaks();
  browserHacks();
  newPageLinks();
  activateLightboxLinks();
  activatePopUpLinks();
});

function includeScripts() {
  var s = document.createElement('script');
  s.type = 'text/javascript';
  s.async = true;
  s.src = document.location.protocol + '//apis.google.com/js/plusone.js';
  document.getElementsByTagName('head')[0].appendChild(s);

  s = document.createElement('script');
  s.type = 'text/javascript';
  s.async = true;
  s.src = document.location.protocol + '//platform.twitter.com/widgets.js';
  document.getElementsByTagName('head')[0].appendChild(s);

  s = document.createElement('script');
  s.type = 'text/javascript';
  s.async = true;
  s.src = document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1';
  document.getElementsByTagName('head')[0].appendChild(s);
}

/*
 * Additional CSS reset actions that can't be performed from CSS.
 */
function cssReset() {
  $('table').attr('cellspacing', 0);
}

/*
 * Additional CSS that can't or shouldn't be performed from the HTML or CSS.
 */
function cssTweaks() {
  $('.news').addClass('ui-corner-all');
}

/*
 * Apply browser specific hacks.
 */
function browserHacks() {
  if ($.browser.msie) {
    // IE6.x
    if ($.browser.version.charAt(0) == '6') {
      // PNG transparency hack (only if width and height provided)
      $('img').each(function() {
        // Check if both width and height attributes are provided. Must use "getAttribute(name, 2)" for IE6 to return
        // actual attribute value, see http://msdn.microsoft.com/en-us/library/ms536429(VS.85).aspx
        if ((this.getAttribute('width', 2) && this.getAttribute('height', 2)) ||
            // Check if both width and height CSS are specified in absolute pixels.
            (/px$/.test($(this).css('width')) && /px$/.test($(this).css('height')))) {
          $(this).css('behavior', 'url(/css/pngHack/pngHack.htc)');
        }
      });
    }
  }
}

function newPageLinks() {
  $('a.new-page').attr('target', '_blank');
}

/*
 * Turn all anchors with rel=lightbox into lightbox links.
 */
function activateLightboxLinks() {
  $('a[rel*=lightbox]').each(function() {
    $(this).attr('title', $(this).children('img').attr('title'));
    $(this).lightBox({
      imageLoading: '/images/loading.gif',
      imageBtnClose: '/js/jquery/plugins/lightbox/images/close.gif',
      imageBtnPrev: '/js/jquery/plugins/lightbox/images/prev.gif',
      imageBtnNext: '/js/jquery/plugins/lightbox/images/next.gif',
      imageBlank: '/images/blank.gif',
      fixedNavigation: true
    });
  });
}

function activatePopUpLinks() {
  $('a.popup600').click(function() {
    if(this.target == '_blank' ) {
      return true;
    } else {
      window.open(this.href, 'popupwindow', 'scrollbars=yes,width=600,height=600');
      return false;
    }
  });
}

/*
 * jQuery plugin: Image preloader.
 * Preloads all provided image arguments.
 * Usage: $.preLoadImages('/path/to/image1.png', 'image2.png');
 */
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  };
})(jQuery);

/*
 * jQuery plugin: Image mouseover/mouseout swap.
 * Auto preloads image argument.
 * Usage: $.imgHoverSwap($('img#target'), 'hover-img.png');
 */
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.imgHoverSwap = function() {
    var jq = arguments[0];
    var over = arguments[1];
    $.preLoadImages(over);
    var out = jq.attr('src');

    jq.mouseover(function() {
      $(this).attr('src', over);
    });
    jq.mouseout(function() {
      $(this).attr('src', out);
    });
  };
})(jQuery);

function clearSearch(el) {
  el.value = '';
  el.style.color = '#000';
}

function searchClicked(searchBoxDomId) {
  try {
    var searchBox = document.getElementById(searchBoxDomId);
    if (searchBox.value == 'Search...') {
      searchBox.value = '';
      searchBox.focus();
      return false;
    }
  } catch (err) {
        // ignore
  }
}

function downloadWindowForIE(uri) {
  if (navigator.userAgent.match(' MSIE ') != null) {
    window.open(uri, '_blank', 'width=100,height=100');
  }
}

