var interval;
var animating = false;

$(window).load(function () {
//  setupSectionTitle();
  setupSpotlightBanners();
  setupTabbedPanels();
  setupVideoOverlay();
  setupPhotoOverlay();
  setupGalleryZeitgeist();
  setupSubscribeBox();
  setupSearchBox();
  
  $(document).keyup(function(e) {
    if (e.keyCode == 27) {
      $('a.close').click();
    }
  });
  
  if ($('div#search-bar ul.types').length > 0) {
    $('div#search-bar ul.types input#t0').click(function () {
      if ($(this).not(':checked')) {
        $('div#search-bar ul.types input:not([id="t0"])').attr('checked', false);
      }
    });
    $('div#search-bar ul.types input:not([id="t0"])').click(function () {
      $('div#search-bar ul.types input#t0').attr('checked', false);
    });
  }
});

function setupSectionTitle() {
  o = $('div.breadcrumbs-title a:first, div.breadcrumbs-title span').first().addClass('first');
  if (o.length == 1) {
//    o.addClass('lone');
  }
}

function setupSpotlightBanners() {
  if ($("div#slideshow ul.slides li").length > 0) {
    $("div#slideshow ul.slides li:first").addClass("active").show();
    interval = setInterval(function () {
      forwardSlide();
    }, 6000);
  }
  $("div#slideshow ul.navigator li a").click(function () {
    if (!animating && !$(this).hasClass("active")) {
      index = $("div#slideshow ul.navigator li a").index($(this));
      forwardSlide(index);
      clearInterval(interval);
    }
    
    return false;
  });
}

function forwardSlide(idx) {
  var outgoing, incoming, buttons;
  outgoing = $("div#slideshow ul.slides li.active");
  buttons  = $("div#slideshow ul.navigator");

  if (idx !== undefined) {
    next = $("div#slideshow ul.slides li.slide_" + idx);
    if (next) {
      cloned = next.clone();
      outgoing.after(cloned);
      next.remove();
    }
  }
  incoming = outgoing.next();
  incoming.find("div.blurb").hide();
  
  index = incoming.attr("class").replace("slide_", "");
  buttons.find("a").removeClass("active");
  buttons.find("a:eq(" + index + ")").addClass("active");

  animating = true;
  incoming.addClass("active");
  outgoing.removeClass("active");
  incoming.fadeIn("slow", function () {
    $(this).find("div.blurb").show();
//    outgoing.removeClass("active");
    outgoing.hide();
    $(this).parent().append(outgoing.clone());
    outgoing.remove();
//    $(this).addClass("active");
    animating = false;
  });
  
  if ($('a[href*="youtube.com/watch"], a[href*="youtu.be"]').length > 0) {
    setupVideoOverlay();
  }
}

function setupTabbedPanels() {
  if ($('div.tabbed-panels').length > 0) {
    $('.tabbed-panels ul.tabs li a').click(function () {
      clickIndex = $('.tabbed-panels ul.tabs li a').index($(this));
      $('.tabbed-panels ul.tabs li a').removeClass('active');
      $(this).addClass('active');
      $('.tabbed-panels ul.panels > li').removeClass('active');
      $('.tabbed-panels ul.panels > li:eq(' + clickIndex + ')').addClass('active');
      
      return false;
    });
  }
}

function setupVideoOverlay() {
  $('a.video-link, a.video, a[href*="youtube.com/watch"], a[href*="youtu.be"]').each(function () {
    if (!$(this).hasClass('iconised') && $(this).find("img").length > 0) {
      iconSpan = $('<span class="icon">');
      iconSpan.width($(this).find("img").width());
      iconSpan.height($(this).find("img").height());
      iconSpan.css('margin-top', -iconSpan.height() + 'px');
      $(this).append(iconSpan);
      $(this).addClass('iconised');
    }
  });
  $('a.video-link, a.video, a[href*="youtube.com/watch"], a[href*="youtu.be"]').click(function () {
    playerWidth = $('div#video-overlay iframe').width();
    playerHeight = $('div#video-overlay iframe').height();
    viewportWidth = $(window).width();
    viewportHeight = $(window).height();
    videoEmbedUrl = $(this).attr('href').replace('watch?v=', 'embed/');
    
    $('div#video-overlay iframe').attr('src', videoEmbedUrl + '?autoplay=1');
    $('div#video-overlay div.content')
      .css('left', (viewportWidth - playerWidth) / 2)
      .css('top', (viewportHeight - playerHeight) / 2 + $(document).scrollTop() - 30);

    $('div#video-overlay')
      .css('width', viewportWidth)
      .css('height', $(document).height())
      .show();

    return false;
  });
  $('div#video-overlay a.close').click(function () {
    $('div#video-overlay iframe').attr('src', '/templates/asef/default_video.html');
    $('div#video-overlay').hide();
    
    return false;
  });
}

function setupPhotoOverlay() {
  $('a.photo-link').click(function () {
    viewerWidth    = parseInt($(this).attr('photowidth')) + 20;
//    viewerHeight   = parseInt($(this).attr('photoheight'));
    viewportWidth  = $(window).width();
    viewportHeight = $(window).height();
    flickrLinkUrl = $(this).attr('href');
    
//    $('div#photo-overlay div.photo-viewer').append('<img src="' + $(this).attr('href') + '" width="' + $(this).attr('photowidth') + '" height="' + $(this).attr('photoheight') + '" />');
    $('div#photo-overlay div.photo-viewer').append('<img src="' + $(this).attr('photo') + '" width="' + $(this).attr('photowidth') + '" />');
    if ($(this).attr('type') == 'photo') {
      flickrLink = $('<a href="' + flickrLinkUrl + '" target="_blank">View this photo on Flickr »</a>');
    }
    else {
      flickrLink = $('<a href="' + flickrLinkUrl + '" target="_blank">View this photoset on Flickr »</a>');
    }
    flickrLink.click(function () {
      $('div#photo-overlay a.close').click();
      return true;
    });
    flickrLinkDiv = $('<div class="flickr-link"></div>');
    flickrLinkDiv.append(flickrLink);
    flickrLinkDiv.append('<div class="clear"></div>');
    $('div#photo-overlay div.photo-viewer').append(flickrLinkDiv);
    if ($(this).attr('title')) {
      $('div#photo-overlay div.photo-viewer').append('<div class="photo-meta"><span class="title">' + $(this).attr('title') + '</span><span class="description">' + $(this).attr('description') + '</span></div>');
    }
    else {
      $('div#photo-overlay div.photo-viewer').append('<div class="photo-meta"><span class="description">' + $(this).attr('description') + '</span></div>');
    }
    
    viewerHeight = $('div#photo-overlay div.content').height();
    
    $('div#photo-overlay div.content')
      .css('width', viewerWidth)
      .css('height', viewerHeight)
      .css('left', (viewportWidth - viewerWidth) / 2)
      .css('top', (viewportHeight - viewerHeight) / 2 + $(document).scrollTop() - 40);

    $('div#photo-overlay')
      .css('width', viewportWidth)
      .css('height', $(document).height())
      .show();
    
    return false;
  });
  $('div#photo-overlay a.close').click(function () {
    $('div#photo-overlay div.photo-viewer').html('');
    $('div#photo-overlay').hide();

    return false;
  });
}

function setupGalleryZeitgeist() {
  if ($('div.gallery-zeitgeist ul.photos').length > 0) {
    $('div.gallery-zeitgeist ul.photos li').each(function (i) {
      switch (i) {
        case 1:
          $(this).css('margin-top', ($(this).prev().height() + 10) + 'px');
          break;
        case 2:
          $(this).css('margin-top', $(this).prev().css('margin-top'));
          $(this).css('margin-left', ($(this).prev().width() + 20) + 'px');
          break;
        case 3:
          $(this).css('margin-top', parseInt($(this).prev().css('margin-top')) + $(this).prev().height() + 10 + 'px');
          $(this).css('margin-left', $(this).prev().css('margin-left'));
          break;
        case 4:
          anchor = $('div.gallery-zeitgeist ul.photos li:eq(1)');
          $(this).css('margin-top', parseInt(anchor.css('margin-top')) + anchor.height() + 10 + 'px');
          break;
      }
    });
  }
}

function setupSubscribeBox() {
  if ($("table.fknews_subscribe_table").length > 0) {
    div = $('<div class="content fknews_subscribe_table">');
    $("table.fknews_subscribe_table input").each(function () {
      cloned = $(this).clone();
      cloned.attr('id', 'subscribe-field-' + cloned.attr('name'));
      if (cloned.attr('name') == 'email' || cloned.attr('name') == 'first_name')
      {
        if (cloned.attr('name') == 'email')
        {
          cloned.val('Email address');
        }
        else
        {
          cloned.val('Name');
        }
        
        cloned.attr('default_value', cloned.val());
        cloned.addClass('marked');
        cloned.addClass('blur');
        
        cloned.focus(function () {
          $(this).removeClass('blur');
          if ($(this).val() == $(this).attr('default_value')) {
            $(this).val('');
          }
        });
        cloned.blur(function () {
          if ($(this).val() == '' || $(this).val() == $(this).attr('default_value')) {
            $(this).val($(this).attr('default_value'));
            $(this).addClass('blur');
          }
        });
      }
      div.append(cloned);
    });
    $("table.fknews_subscribe_table").after(div).remove();
    $("div.fknews_subscribe_table").parents("form").attr('action', '/newsletter/?p=subscribe&id=1');
    $("div.fknews_subscribe_table").parents("form").submit(function () {
      emailField = $("input#subscribe-field-email");
      nameField  = $("input#subscribe-field-first_name");
      if (emailField.val() == emailField.attr('default_value') ||
          nameField.val()  == nameField.attr('default_value')) {
        alert('Please provide your email address and name.');
        return false;
      }
      return true;
    });
    if ($("div.fknews_subscribe_table").parents("div#left").length > 0) {
      $("div.fknews_subscribe_table").wrap('<div class="module boxed" style="margin-top:10px">');
      $("div.fknews_subscribe_table").before('<h3>Newsletter</h3>');
    }
  }
}

function setupSearchBox() {
  if ($("#site-search").length > 0) {
    input = $("#site-search input[name=q]");
    input.val('Search');
    input.attr('default_value', input.val());
    input.addClass('blur');
    input.focus(function () {
      $(this).removeClass('blur');
      if ($(this).val() == $(this).attr('default_value')) {
        $(this).val('');
      }
    });
    input.blur(function () {
      if ($(this).val() == '' || $(this).val() == $(this).attr('default_value')) {
        $(this).addClass('blur');
        $(this).val($(this).attr('default_value'));
      }
    });
  }
}
