$(document).ready(function() {

  $.fn.containerHover = function() {
    this.each(function() {
      var el = this;
      $('img', this).hover(
        function() {
          $(el).addClass('hover');
        },
        function() {
          $(el).removeClass('hover');
        }
      );
    });
  };

  $('.article').containerHover();

  $.fn.prompt = function() {
    var focus = function(event) {
      if ($(this).val() == $(this).attr('title')) {
        $(this).val('').removeClass('prompting');
      }
    };
    
    var blur = function(event) {
      if ($(this).val().match(/^\s*$/)) {
        $(this).val($(this).attr('title')).addClass('prompting');
      }
    };
     $(this).focus(focus);
     $(this).blur(blur).blur();
  };

  $('input.prompt').prompt();
  
	$('.searchfilters input[type="radio"]').css('display', 'none');
	$('.searchfilters').addClass('enhanced');
	
	$('.searchfilters label').click(function() {
		$(this).closest('ul').find('.current').removeClass('current');
		$(this).closest('li').addClass('current');
	});
	
	
  $('.mp3-module').megaphone({
      setup: $.fn.megaphone.single,
      selectors: {
        PLAYER: '.mp3-module',
        PROGRESS_PARENT: '.mp3-progress-bar',
        PLAY_PROGRESS: '.mp3-play-bar',
        LOAD_PROGRESS: '.mp3-load-bar', 
        PLAY_BUTTON: '.mp3-control-btn'
      }
    });
  
  $('.mp3-track-module').megaphone({
    setup: $.fn.megaphone.multi,
    selectors: {
      TRACK_LIST: '.mp3-tracklist-module',
      PLAYER: '.mp3-track-module',
      PLAY_BUTTON: '.mp3-control-btn',
      PLAY_PROGRESS: '.mp3-play-bar',
      LOAD_PROGRESS: '.mp3-load-bar',
      PROGRESS_PARENT: '.mp3-progress-bar',
      TIME_REMAINING: '.mp3-time-remaining'
    }
  });
		
  // $('.trackplayer').megaphone();
  // 
  // $('a.streaming.mp3').each(function(index, element) {
  //   var template, href, player, p;
  //   
  //   template = $('.trackplayer_template');
  //   href = $(element).attr('href');
  //   p = $(element).closest('p');
  //   
  //   player = template[0].cloneNode(true);
  //   $(player).removeClass('trackplayer_template').addClass('trackplayer');
  //   $(player).attr('data-path', href);
  //   
  //   p.replaceWith(player);
  //   
  //   $(player).megaphone();
  //   
  // });
	
	$('a.smallbuy').click(function(event) {
	  var select = $(this).prev('select.buy_track');
		if(select.val() == '') {
			alert("Please select a store from the drop-down next to this 'Buy' button.")
		} else {
			window.open(select.val());
		}
	  return false;
	});
	
	// $('select.buy_track').change(function(event) {
	//   var link = $(this).next('a.smallbuy');
	//   link.toggle(!$(this).val() == '');
	// }).trigger('change');
	
		
	// homepage slideshow

  var slideshowInterval;
  
  var transition = function(show) {
    $('.news .slideshowcontrols li.num a.current').removeClass('current');
    $('.news .article:visible').fadeOut('fast', function() {
	    $('.news .' + show).fadeIn('fast');
	  });
	  $('.news .slideshowcontrols li.num a.' + show).addClass('current');
  }
	
	var start = function() {
	  slideshowInterval = setInterval(function() {
      var current = $('.news .slideshowcontrols li.num a.current');
      var next = current.closest('li').next('.num').find('a');
      if (next.length == 0) {
        next = $('.news .slideshowcontrols li:first-child a');
      }
      var klass = next[0].className;
      transition(klass);
    }, 5000);
    $('div.controls').addClass('playing');
	};
	
	var stop = function() {
	  clearInterval(slideshowInterval);
	  $('div.controls').removeClass('playing');
	};
	
	$('.news .slideshowcontrols li:first-child a').addClass('current');
	
	$('.news .slideshowcontrols li.num a').click(function(event) {
	  if (!$(this).hasClass('current')) {
  	  var klass = event.target.className;
  	  transition(klass);
  	  stop();
	  }
	  return false;	    
	});
	
	$('.news .slideshowcontrols li.pause a').click(function(event) {
	  if ($(event.target).closest('div.playing')[0]) {
	   	stop(); 
	  } else {
	    start();
	  }
	  return false;
  });
	
	// sidebar most read, etc lists
	$('ul.mostlinks a').click(function(event) {
	  var klass = event.target.className;
    $('ul.mostlinks a').not('.' + klass).parents().removeClass('current');
    $('ul.mostlinks a.' + klass).parents().addClass('current');
	  
	  $('ul.mostlist').not('.' + klass).removeClass('current');
	  $('ul.mostlist.' + klass).addClass('current');
	  return false;
	});
	
	if ($('.news .slideshowcontrols')[0]) {
	  start();	  
	}


  // file upload box on profile
  
  $('input#user_profile_attributes_avatar').change(function() {
    // $('input.fake').val($(this).val()).addClass('ready');
    $('input.fake').val('click submit to save').addClass('ready');
  }).each(function() {
    $(this).attr('autocomplete', 'off');
  });

  // video player!
  
  $('.videoset #vimeo_player').each(function() {
    var swfURL = 'http://www.vimeo.com/moogaloop.swf';
    var vimeo_id = $(this).attr('data-vimeo-id')
    var params = {
      clip_id: vimeo_id,
      width: 563,
      height: 324
    };
    swfobject.embedSWF(swfURL, "vimeo_player", "563", "324", "9.0.0", null, params, {wmode:'transparent'});    
  });
	
	$('a.toggle_bio').click(function(event) {
	  $('.full_bio, .bio_summary').toggle();
	  $(this).closest('.row').hide();
	  return false;
	});
	
	$('select.event_filter').change(function(event) {
	  var val, url;
	  
	  val = $(this).val();
	  
	  if (val == '') {
	    document.location.href = document.location.pathname;
	    return false;
	  }
	  
	  if ($(this).hasClass('country')) {
	    url = $.query.set('country', val).remove('state').toString();
	  } else {
	    url = $.query.set('state', val).remove('country').toString();
	  }
	  
    document.location.href = url;
	  
	  return false;
	});
	
	// Open external links in new window
	$("a[href^='http']").attr('target','_blank');
	
});

