$(function(){  
  $('input.genre').simpleImageCheck({
    image: '/images/unchecked.png',
    imageChecked: '/images/checked-genre.png'
  });

  $('input.instrument').simpleImageCheck({
    image: '/images/unchecked.png',
    imageChecked: '/images/checked-instrument.png'
  });

  $('input.blue-instrument').simpleImageCheck({
    image: '/images/unchecked-blue.png',
    imageChecked: '/images/checked-instrument.png'
  });

  $('input.blue-genre').simpleImageCheck({
    image: '/images/unchecked-blue.png',
    imageChecked: '/images/checked-genre.png'
  });
  
  /*$('.profile .overview h4').mouseleave(function(){
    $('.fb', this).fadeOut(100);
  });
  
  $('.profile .overview h4 a').mouseenter(function(){
    $('.fb', $(this).parents("h4")).fadeIn(100);
  });*/
  
  $('.profile').each(function(){
    var id = $('.info-inner', this).attr('id').replace('info_','');
    var profile_url = "http://www.bandship.com/profile/" + id;
    var fb_code = "<iframe class=\"fb\" src=\"http://www.facebook.com/plugins/like.php?href=" + escape(profile_url) +"&amp;layout=button_count&amp;show_faces=false&amp;width=90&amp;action=like&amp;colorscheme=light&amp;height=20\" scrolling=\"no\" frameborder=\"0\"allowTransparency=\"true\" >";
    $('h4', this).append(fb_code);
  });
  
  $('.more_tags').each(function(){
    $(this).appendTo($('body'));
  });
  
  
  $('.more_button').mouseover(function(){ hide_more_tags(this); show_more_tags(this) });
  $('.more_tags').mouseleave(function(){ hide_more_tags(this) });

  $('.profile .play').mouseenter(function(){
    $('a', this).animate({opacity:1}, 0);
  });

  $('.profile .play').mouseleave(function(){
    $('a', this).animate({opacity:.5}, 0);
  });
  
  $('.profile .play').click(function(){
    $('a', this).click();
  });

  $('.profile a.fade').mouseenter(function(){
    $(this).animate({opacity:1}, 0);
  });

  $('.profile a.fade').mouseleave(function(){
    if($(this).hasClass('mp3')) return;
    $(this).animate({opacity:.5}, 100);
  });

  $('.profile a.about_me').click(function(){
    var info_inner = $('.info-inner', $(this).parents('.profile_inner'));
    $('.info-inner').stop().animate({marginTop:0}, 200);
    info_inner.stop().animate({marginTop:-90}, 200);
    $('.lower_half_inner', info_inner).stop().animate({marginTop:4}, 200);
    return false;
  });

  $('.profile img.back').live('click', function(){reset_profile(this);});

  $('.profile a.contact').click(function(){
    $('.info-inner').animate({marginTop:0}, 200);
    var info_inner = $('.info-inner', $(this).parents('.profile_inner'));
    info_inner.stop();
    if($('div.contact_me', info_inner).html() == ''){
      $('div.contact_me', info_inner).html($('#contact_template').html());
  
      if($('#sign-up-link').css('display') == 'inline'){
        $('div.contact_me label', info_inner).text('You must be logged in to send messages');
      }else{
        $('div.contact_me label', info_inner).text($('div.contact_me label', info_inner).text() + ' ' + $('h4', info_inner).text());
      }
    
      $('div.contact_me form', info_inner).attr('action', '/profile/contact/' + info_inner.attr('id').substr(5));
      $('div.contact_me form input.profile_id', info_inner).val(info_inner.attr('id').substr(5));

      $('div.contact_me form', info_inner).submit(function(){
        if($('textarea', this).val().trim() == '') return false;
        $('input.button', this).addClass("disabled").attr("disabled", "disabled").blur();
        $.post($(this).attr('action'), $(this).serialize(), 
          function(data){
            handle_contact_return(data)
          }, 'json');
            return false;
        });
     }

     info_inner.stop().animate({marginTop:-90}, 200);
     $('.lower_half_inner', info_inner).animate({marginTop:-84}, 200);
     return false;
  });

  function handle_contact_return(data){
    if(data.success){
      var form_holder = $('#info_' + data.profile_id + ' .contact_form');
      $('textarea', form_holder).val('');
      $('.sent_message', form_holder).css({'opacity':0, 'display':'block'}).animate({'opacity':1})
        .delay(5000).animate({'opacity':0}, 500, 
          function(){
            var form = $(this).parents('.contact_form');
            $(this).css('display','none'); 
            $('textarea', form).blur();
            $('input.button', form).removeClass("disabled").removeAttr("disabled");
          })
    }
  }

  function reset_profile(s){
    $(s).parents('.info-inner').stop().animate({marginTop:0}, 200);
  }

  $('a.mp3').click(function(){
    if($(this).hasClass('paused')){
      $(this).removeClass('paused');
      niftyplayer('nifty_mp3').play();
    }else if($(this).hasClass('playing')){
      $(this).addClass('paused');
      niftyplayer('nifty_mp3').pause();
    }else{
      $('a.playing').css('opacity', .5);
      $('a.mp3').removeClass('playing').removeClass('paused');
      $(this).addClass('playing');
      niftyplayer('nifty_mp3').loadAndPlay($(this).attr('href'));
    }
    return false;
  });

});

function show_more_tags(s){
  var more_el_id = "more_";
  var profile_id = $(s).parents('.info-inner').attr('id').replace('info_','');
  
  more_el_id += $(s).hasClass('genre') ? "genre_tags" : "instrument_tags";
  more_el_id += "_" + profile_id;
  
  $("#" + more_el_id).css({
    'display':'block',
    'top':$(s).offset().top - 5,
    'left':$(s).offset().left - 5,
    'min-width':$(s).width() + 5
  });
}

function hide_more_tags(s){
  $('.more_tags').css('display', 'none');
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


