
(function($){
  $.prototype.extend({

    clip: function(track,autoplay) {
      if(autoplay) autoplay='&autoplay=1'; else autoplay = '';
      this.attr('style','position:relative;visibility:visilble;position:block');
      swfobject.embedSWF("cr/img/corpmp3.swf?track="+track+"&mini=1"+autoplay,
        this.attr('id'),"300","20","8.0.0",false,false,{'wmode':'transparent'});
      return this;
    },

    play: function(track,type) {
      $(this).slideDown();
      $('.stream_embed',this).html('<div id="stream_div"></div>');
      $('.stream_embed div',this).clip(track,true);
      $('.stream_title',this).empty();
      var link = $(".track_link_"+track).first().clone();
      if(link) {
        $('.stream_title',this).append(link).append(' - ');
      }
      var title = ''
      if (type == 'FULL') title = 'Full track preview';
      if (type == '30SECS') title = '30 second preview';
      if (type == '60SECS') title = '60 second preview';
      $('.stream_title',this).append(title);

      return this;
    },

    outline: function(options) {
      if(!options) options = {};
      if(!options.color) options.color = 'black';
      var copies = [[-1,-1],[-1,+1],[+1,+1],[+1,-1]];
      for ( var i = 0, l = this.length; i < l; i++ ) {
        var elem = this[i];
        var position = $(elem).position();
        for(var e in copies) {
          var x = position.left - copies[e][0];
          var y = position.top - copies[e][1];
          $(elem).clone().css({
            'position':'absolute',
            'left':x+'px',
            'top':y+'px',
            'color':options.color
          }).insertBefore(elem);
        }
      }
      return this;
    },

    helpme: function() {
      var helpdiv = this;
      var helpname = helpdiv.attr("id");
      $('.helptext',helpdiv).hide().removeClass('hidden');
      $('.showhelp',helpdiv).button().click(function(){
        $('.helptext',helpdiv).slideDown();
        $('.showhelp',helpdiv).hide();
      });
      $('.hidehelp',helpdiv).button().click(function(){
        $('.helptext',helpdiv).slideUp();
        $('.showhelp',helpdiv).show();
        $.cookie("hidehelp_"+helpname, 1, {expires: 5000});
      });
      if($.cookie("hidehelp_"+helpname) != 1) $('.showhelp',helpdiv).click();
      return this;
    },

    iconButton: function(){
      this.each(function(i,el){
        var icon = $(el).attr('title');
        if(icon) {
          $(el).button({icons:{primary: 'ui-icon-'+icon}});
          $(el).attr('title','');
        }
        else $(el).button();
      });
      return this;
    },

    ajaxButton: function(ajax){
      ajax['dataType'] = "json";
      ajax['type'] = "POST";
      var success = ajax['onSuccess'];
      var error = ajax['onError'];
      var pre = ajax['pre'];
      this.each(function(i,el){
        $(el).click(function(){
          ajax['error'] = function(data){
            $(el).removeClass('disabled');
            if(error) error(data,el);
          }
          ajax['success'] = function(data) {
            $(el).removeClass('disabled');
            if(success) success(data,el);
          }
          $(el).addClass('disabled');
          if(pre) pre(ajax,el);
          $.ajax(ajax);
          return false;
        });
      });
      return this;
    }

  });

})(jQuery);

$(document).ready(function() {
  $('.capsule').html(function(index,html) {
    return '<table><tr><td><div capsule_div"><div class="capsule_start">'+html+'</div><div class="capsule_end"></div></div></td></tr></table>';
  });
  $('input:button').button();
  $('input:submit').button();
  $('a.button').button();
  $('.chart_rank').outline({'color':'#812713'});

  var findmusic = '  find music...';
  $('#header_search_box').focus(
    function() {
      if($('#header_search_box').val() == findmusic) {
        $('#header_search_box').removeClass('header_sidebar_gray').val('');
      }
    }).blur(
    function() {
      if($('#header_search_box').val() == '') {
        $('#header_search_box').addClass('header_sidebar_gray').val(findmusic);
      }
    }).val('').blur();
  $('#header_search_button').click(
    function() {
      if($('#header_search_box').val() != findmusic) {
        document.getElementById('header_search_form').submit();
      }
    });

});



