;(function($) {  
  $.fn.current_navigation = function() {
    return this.each( function() {
      if (location.pathname.match($(this).attr('href'))) {
        $(this).addClass('current');
      }
    });
  }
  
  $.fn.slideshow = function(time, speed) {
    if (this.size()) {
      var images = $(this);
      var total  = this.size() - 1;
      var count  = 0;
      var time   = time*1000 || 5000;
      var speed  = speed || 'slow';
      var timer  = setInterval(toggle, time);
      
      $(this).css('position', 'absolute').hide();
      $(images[count]).show();
      
      function toggle() {
        $(images[count]).fadeOut(speed);
        count = (count >= total) ? 0 : count += 1;
        $(images[count]).fadeIn(speed);
      }
    }
        
    return this;
  }
  
  $.fn.open_external = function() {
    return this.each( function() {
      $(this).click(function(e) {
        if (!this.href.match(document.domain) && !this.href.match('app.topspin.net/store')) {
          e.preventDefault();
          window.open(this.href, '_blank').focus();
        }
      });
    });
  }

  $.fn.to_slug = function() {
    return this.html().replace(' ', '-').toLowerCase();
  }
  
  $.fn.tab_menu = function() {
    return $(this).each(function() {
      var sections = $(this).find('.section');
      var menu     = $('<ul>').addClass('tab-menu');
      var active   = null;
      
      // Hide the menus
      sections.hide();

      // Default to the first as active
      active = sections.first();

      // Build the navigation
      sections.each( function() {
        var section = $(this)
        var heading = section.find('h2');
        var link    = $('<a>').html(heading.html()).attr('href', '#');
        var item    = $('<li>').html(link).addClass(heading.to_slug());

        // Hide the headings
        heading.hide();

        // Display the current section
        section.find('a').each(function() {
          if (location.pathname == $(this).attr('href')) {
            active = section;
            link.addClass('active');            
          }
        });

        // Observe the click
        link.click(function(e) {
          e.preventDefault();
          
          // Hide all the sections
          sections.hide();
          
          // Show the current one
          section.show();
          
          // Toggle the active states
          menu.find('a').removeClass('active');
          $(this).addClass('active');
        });

        menu.append(item);
      });

      $(this).prepend(menu);
      $(active).show();
      
      if (location.pathname.match('/'+ $('body').attr('id') +'/?$')) {
        menu.find('li:first-child a').addClass('active');
      }
    });
  }

  $.fn.paginated_list = function(parent, load) {
    return $(this).live('click', function(e) {
      $('#' + parent).load(this.href + ' #' + load, function() {
        $(this).trigger('ajax_loaded');
      });
      
      e.preventDefault();
    });
  }

  $.fn.clickable = function(options) {
    var options = options || {};
    var target  = options.target || 'a';
    
    return $(this).bind('mouseenter mouseleave click', function(e) {
      if (e.type == 'click') {
        e.preventDefault();
        e.stopImmediatePropagation();
        
        if (options.callback) {
          options.callback.call(this);
        } else {
          window.location = $(this).find(target).first().attr('href'); 
        }
      } else {
        $(this).toggleClass('hover');
      }
    }).css('cursor', 'pointer');    
  }

  $.fn.equal_heights = function() {
    var height = 0;
    
    this.each(function() {
      var h = $(this).height();

      if (h > height) {
        height = h;
      }
    });

    return $(this).height(height);
  }
})(jQuery);


jQuery(function($) {
  // Set up fancybox default styles
  if ($.fancybox) {
    $.fn.fancybox.defaults = $.extend($.fn.fancybox.defaults, {
      'overlayColor'   : '#000000',
      'overlayOpacity' : 0.8,
      'titlePosition'  : 'inside'
    });    
  }
    
  // Open external links in a new window
  $("a[href^='http://']").open_external();

  // Audioplayer
  $('.audio-player a').click(function(e) {
    e.preventDefault();
    window.open(this.href, 'audioplayer', 'width=400,height=400,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no').focus();
  });

  // Mailing list form
  $('.mailing-list a').fancybox({type : 'ajax'});

  // Homepage
  if ($('#home').size()) {
    $('#slideshow img').slideshow();      
  }

  // News
  if ($('#news').size()) {
    $('.pagination a').paginated_list('sidebar', 'paginated-list');
  }

  // Discography
  if ($('#discography').size()) {
    $('#discography .image-menu').tab_menu();     
  }

  // Tabs
  if ($('#tabs').size()) {
    $('#tabs .image-menu').tab_menu();
  }

  // Press
  if ($('#press').size()) {
    $('#sidebar').live('ajax_loaded', function() {
      $(this).tab_menu();
    }).trigger('ajax_loaded');

    $('.pagination a').paginated_list('sidebar', 'paginated-list');
  }

  // Photos
  if ($('#photos').size()) {
    var selector = '.images a';
    
    $(selector).fancybox().parents('li').clickable({
      callback : function() {
        $(this).find(selector + ':first-child').trigger('click');
      }
    });
  }
  
  // Show archive photos
  if ($('#launch-gallery').size()) {
    var selector = '.images a';
    
    $(selector).fancybox();    
    $('#launch-gallery').click(function(e) {
      e.preventDefault();
      $(selector + ':first-child').trigger('click');
    });
  }

  // Clickable sidebar links
  if ($('#sidebar').size()) {
    $('#sidebar .clickable li').clickable();
  }

  // Videos
  if ($('#videos').size()) {
    var selector = '#content .section li';
    
    // Fix the margin on every 4th item
    $(selector + ':nth-child(4n)').css('margin', 0);
        
    // Setup a clickable link with a callback
    $(selector).equal_heights().append($('<div>', {'class' : 'button'})).clickable({
      callback : function() {
        var options = {
          'type'  : 'swf',
          'href'  : $(this).find('a').attr('href'),
          'title' : $(this).find('h4').text()
        }

        console.log(this);
        
        if ($(this).hasClass('youtube')) {
          options.type = 'iframe';
          options.href = options.href.replace(new RegExp('watch\\?v=', 'i'), 'embed/');
        }
      
        $.fancybox($.extend(options, {
          'width'  : 640,
          'height' : 385,
          'swf'    : {
            'wmode'           : 'transparent',
            'allowfullscreen' : 'true'
          }
        }));        
      }
    });
  };

  // Contact Page  
  if ($('#contact-page').size()) {
    $('#sidebar a').hover(function(e) {
      $(this).fadeTo('fast', e.type == "mouseenter" ? 0.6 : 1);
    });
  };  
});
