$(document).ready(function(){

  /**
   * qtips
   */

	$('a[title], area[title]').qtip({
		style: {
			classes: 'ui-tooltip-blue ui-tooltip-rounded'
		},
		position: {
			target: 'mouse',
			viewport: true,
      my: 'bottom left',
      at: 'top right',
			adjust: {
				x: 5,
				y: -5
			}
		}
	});

  /**
   * search field
   * clear on focus, replace with default on blur
   */

  $.fn.search = function() {
    return this.focus(function() {
      if( this.value == this.defaultValue ) {
        this.value = "";
      }
    }).blur(function() {
      if( !this.value.length ) {
        this.value = this.defaultValue;
      }
    });
  };
  $("#searchfield").search();


  // colorbox
  $('a[rel^="lightbox"]').colorbox({
    'opacity': 0.2,
    'photo': true,
		'title': function(){ return $(this).children('img').attr('alt')},
    'current': "{current} / {total}"
  });


  /**
   * external links
   *
   * filters href != hostname or rel="external"
   * adds .external to elements not containing img, div or mailto
   * opens external links in new window/tab
   */

  $('a, area').filter(function() {
    return this.hostname && (this.hostname).split(":")[0] !== (location.hostname).split(":")[0] || $(this).attr('rel') == 'external';
  })
  .not(':has(img, div, mailto)')
  .addClass('external')
  .end()
  .click(function(e) {
    open(this.href); 
    e.preventDefault();
  });

});
