if (window.loadFirebugConsole != undefined) {
  window.loadFirebugConsole();
} else {
  window.console = { log: function() {} };
}

$.fn.replaceDefaultText = function(def) {
  var element = this.eq(0);
  element.focus(function() {
      if ($(this).val() == def) $(this).val('');
    })
    .blur(function() {
      if ($(this).val() == '') $(this).val(def);
    });
  return this;
}

function loggedIn() {
  return $('#header #login_signup a:contains("Logout")').length;
}

function playSong(url) {
  // Only load songs in the iframe if the user is logged in.
  // The previews don't need to use this method.
  if (!loggedIn()) {
    return true;
  } else {
    if (!$('iframe#play_in_wmp').length) {
      var iframe = '<iframe id="play_in_wmp" FRAMEBORDER="0" width="0" height="0" src=""></iframe>';
      $('body').append(iframe);
    }
    $('iframe#play_in_wmp').attr('src', url);
    return false; 
  }
}

function downloadAlbum(url) {
  if (!loggedIn() || url.match(/payment/)) {
    return true;
  } else {
    /*console.log(amp.isFF, 'is firefox');
    console.log(amp.isIE, 'is ie');
    console.log(amp.detectplugin(), 'plugin detected?');
    console.log(amp.BrowserOK(), 'is browser ok');*/
    $('a[href^="amp.DownloadFile"]').each(function() {
      downloadSong($(this).attr('href'));
    });
    return false;
  }
}

/**
 * Sample download link that works: amp.DownloadFile('01_That_Body.wma','Jeremih','Jeremih','downloads.kazaa.com/music/23/602527094823/Jeremih/Jeremih/','5671911','602527094823');
 */
function downloadSong(url) {
  if (!loggedIn() || url.match(/payment/)) {
    return true;
  } else {
    var ampcall = decodeURIComponent(url.slice(url.indexOf('amp.DownloadFile')));
    /*console.log(amp.isFF, 'is firefox');
    console.log(amp.isIE, 'is ie');
    console.log(amp.detectplugin(), 'plugin detected?');
    console.log(amp.BrowserOK(), 'is browser ok');*/
    console.log(ampcall, 'downloading song');
    setTimeout(ampcall, 1);
    return false;
  }
}
