$(document).ready(function() {
  
  // open links with class 'popup' in new windows
  $('a.popup, a#print').click(function() {
    window.open($(this).attr('href'));
    return false;
  });

  // Search box with default text which disappears when focus is given
  $('input#search-query').focus(function(){
    if (this.value == 'Search...')
      $(this).val('').removeClass('faded');
  }).blur(function(){
    if (this.value == '')
      $(this).val('Search...').addClass('faded');
  }).blur().parents('form').submit(function(){
    if ($('input#search-query').val() == 'Search...')
      $('input#search-query').val('');
  });

  $('header nav li:first-child').addClass('first-child');
  
  // Home page cross-fader
  $('div#home-fader').each(function(){
    var fader = this;
    $(this).addClass('js');
    $('li', fader).each(function(i){
      $(this).css('z-index', 500 - i);
    });
    
    setInterval(function(){
      $(fader).find('li:first-child').animate({ opacity: 0 }, 1500, function(){
        $(this).appendTo($(fader).find('ul'));
        $('li', fader).each(function(i){
          $(this).css({ opacity: 1, zIndex: 500 - i });
        });
      });
    }, 3000);
  });

	$.expr[':'].external = function(obj){
	    return !obj.href.match(/^mailto\:/)
	            && (obj.hostname != location.hostname);
	};
	$('a:external').bind('click', function(){ window.open(this.href); return false; });
	
	
	// Inline gallery
  $('div.inline-gallery').addClass('js').find('ul').each(function(){
    $(window).load(jQuery.proxy(function(){
      var ulWidth = 0;
      $(this).find('li').each(function(){
        ulWidth += $(this).outerWidth(true);
      });
      
      $(this).css('width', ulWidth + 'px').wrap('<div class="inline-gallery-thumbs"></div>');
    }, this));
    
    $(this).find('a').click(function(){
      var gallery = $(this);
      var im = new Image();
      im.onload = function(){
        gallery.parents('div.inline-gallery')
          .find('div.inline-gallery-full')
            .css({ height: gallery.find('div.inline-gallery-full img').height() + 'px', overflow: 'hidden' })
            .animate({ height: im.height });
        gallery.parents('div.inline-gallery').find('div.inline-gallery-full img').attr('src', im.src);
        gallery.parents('div.inline-gallery')
          .find('div.inline-gallery-full div.inline-gallery-title')
            .replaceWith($('<div class="inline-gallery-title"></div>').html('<strong>'+gallery.attr('title')+'</strong><br />' + gallery.find('img').attr('alt')));
      };
      im.src = this.href;
      return false;
    });
  });
});



// html5shiv MIT @rem remysharp.com/html5-enabling-script
// iepp v1.6.2 MIT @jon_neal iecss.com/print-protector
/*@cc_on(function(m,c){var z="abbr|article|aside|audio|canvas|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video";function n(d){for(var a=-1;++a<o;)d.createElement(i[a])}function p(d,a){for(var e=-1,b=d.length,j,q=[];++e<b;){j=d[e];if((a=j.media||a)!="screen")q.push(p(j.imports,a),j.cssText)}return q.join("")}var g=c.createElement("div");g.innerHTML="<z>i</z>";if(g.childNodes.length!==1){var i=z.split("|"),o=i.length,s=RegExp("(^|\\s)("+z+")",
"gi"),t=RegExp("<(/*)("+z+")","gi"),u=RegExp("(^|[^\\n]*?\\s)("+z+")([^\\n]*)({[\\n\\w\\W]*?})","gi"),r=c.createDocumentFragment(),k=c.documentElement;g=k.firstChild;var h=c.createElement("body"),l=c.createElement("style"),f;n(c);n(r);g.insertBefore(l,
g.firstChild);l.media="print";m.attachEvent("onbeforeprint",function(){var d=-1,a=p(c.styleSheets,"all"),e=[],b;for(f=f||c.body;(b=u.exec(a))!=null;)e.push((b[1]+b[2]+b[3]).replace(s,"$1.iepp_$2")+b[4]);for(l.styleSheet.cssText=e.join("\n");++d<o;){a=c.getElementsByTagName(i[d]);e=a.length;for(b=-1;++b<e;)if(a[b].className.indexOf("iepp_")<0)a[b].className+=" iepp_"+i[d]}r.appendChild(f);k.appendChild(h);h.className=f.className;h.innerHTML=f.innerHTML.replace(t,"<$1font")});m.attachEvent("onafterprint",
function(){h.innerHTML="";k.removeChild(h);k.appendChild(f);l.styleSheet.cssText=""})}})(this,document);@*/
