$(document).ready(function() {
  $('.dialog').submit(function() {
    openDialog(this.action, $(this).serializeArray());
    return false;
  });
  
  VideoJS.setupAllWhenReady();
  
  // 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-numbers').show();
  $('div.header-numbers a').click(function(){
    $('div.header-image.lastactive').removeClass('lastactive');
    var selectedIndex = $(this).index();
    $('div.header-numbers a.current').removeClass('current');
    $(this).addClass('current');
    
    $('div.header-image.active').removeClass('active').addClass('lastactive');
    $('div.header-image:eq(' + selectedIndex + ')')
      .stop()
      .addClass('active')
      .css({ 'opacity': 0 })
      .animate({ 'opacity': 1 }, 1000);
    return false;
  });
  var bannerAnimation = setInterval('animateBanner()', 5000);
  $('div.header-numbers a').bind('mousedown', function(){ clearInterval(bannerAnimation); });
  
  // 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;
    });
  });
  
  animateNews();
  
});

function openDialog(target, attributes)
{
  if ($('div#dialog').length == 0)
  {
    $('<div id="overlay" />').appendTo('body').css('height', $(document).height());
    $('<div id="dialog" />').appendTo($('body'))
  }
  $('div#dialog')
    .load(target, attributes, function(){ 
      targetTop = ($(window).height() / 2) - ($(this).height() / 2) + $(document).scrollTop();
      $(this)
        .css({ left: ($(window).width() / 2) - ($(this).width() / 2), top: targetTop - 20, opacity: 0, display: 'block' })
        .animate({ opacity: 1, top: targetTop });
      initialiseDialog(target, attributes);
    });
  return false;
}

function initialiseDialog(target, attributes)
{
  $('div#dialog')
    .find('a.cancel').click(cancelDialog).end()
    .find('form').submit(function(){ 
      var action = this.action;
      if (this.target == '')
      {
        if (action == '')
          action = target;
          
        $.post(action, $(this).serialize(), function(data){ 
          $('div#dialog').html(data); 
          initialiseDialog(target, attributes);
        }); 
        return false;
      }
    });
}

function cancelDialog()
{
  $('div#dialog').animate({ opacity: 0, top: parseInt($('div#dialog').css('top')) + 20 }, function(){
    $('div#dialog').remove();
    $('div#overlay').remove();
  });
  return false;
}

function animateNews()
{
  var w = $('div#news-scroller a:first-child').width();
  $('div#news-scroller a:first-child').animate({ marginLeft: -1 * w }, { easing: 'linear', duration: ((w + 20) / 2) * 50, complete: function(){
    $(this).css('margin-left','30px').appendTo($('div#news-scroller'));
    animateNews();
  }});
}  

function animateBanner()
{
  if ($('div.header-numbers a.current').next().length > 0)
    $('div.header-numbers a.current').next().click();
  else
    $('div.header-numbers a:first-child').click();
}

// 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);@*/
