function favorite(song_id, uri) {
    $.post(uri, 
        function(data) {
            $("#follow" + song_id).toggleClass("active");
            // $("#favcap" + song_id).text(data.text);
            // TODO: Change this to display data.text once Jquery interprets JSON. 
            $("#followcap" + song_id).text(data);
            $.growl.settings.displayTimeout = 7000;
            if ($("#follow" + song_id).hasClass("active")) {
                // setMessage("notice", 'This song has been added to your watchlist. <br/> <a href="/my/watchlists">Click here to see your watchlist</a>.');
                $.growl('This song has been added to your watchlist. <br/> <a href="/my/watchlists">Click here to see your watchlist</a>.');
            } else {
                $.growl('This song has been removed from your watchlist. ');
            }
        },
        "json"
    );
}

function follow_user(user_id, uri) {
    $.post(uri, 
        function(data) {
            $("#follow" + user_id).toggleClass("active");
            // $("#favcap" + song_id).text(data.text);
            // TODO: Change this to display data.text once Jquery interprets JSON. 
            $("#followcap" + user_id).text(data);
            $.growl.settings.displayTimeout = 7000;
            if ($("#follow" + user_id).hasClass("active")) {
                $.growl("You are now following this user and will be notified when the user buys new music. <br/><a href=\"/my/watchlists\">Click here to see all the people you're following</a>.");
                // setMessage("notice", 'User followed. <br/> <a href="/my/watchlists">Click here to see your watchlist</a>.');
            } else {
                $.growl("You are no longer following this user.");
            }
        },
        "json"
    );
}

function follow_artist(artist_id, uri) {
    $.post(uri, 
        function(data) {
            $("#follow" + artist_id).toggleClass("active");
            // $("#favcap" + song_id).text(data.text);
            // TODO: Change this to display data.text once Jquery interprets JSON. 
            $("#followcap" + artist_id).text(data);
            $.growl.settings.displayTimeout = 7000;
            if ($("#follow" + artist_id).hasClass("active")) {
                // setMessage("notice", 'Artist followed. <br/> <a href="/my/watchlists">Click here to see your watchlist</a>.');
                $.growl("You are now following this artist. <a href=\"/my/watchlists\"><br/>Click here to see all the artists you're following</a>.");
            } else {
                $.growl("You are no longer following this artist.");
            }                       
        },
        "json"
    );
}

function addToCart(song_id) {
    $.post(
        '/purchase/add_to_cart/' + song_id,
        function(data) {
            if (data == "DUPLICATE") {
                $.growl.settings.displayTimeout = 7000;
                $.growl("You already own that song.", "<a href=\"/my/songs/\">Click here</a> to download it.");
                // setMessage("error", "You already own this song. <a href=\"/my/songs/\">Click here</a> to download it.");
            } else {
                // setMessage("notice", "We've added this song to your cart. <a href=\"/purchase/checkout\">Click here</a> to check out.");
                $.growl.settings.displayTimeout = 7000;
                $.growl.settings.noticeTemplate = "<div class='growlNotice'> <h5>%title%</h5>%message%</div>";
                $.growl("Song added to cart", data);
                updateCartLink();
            }
        }
        );
}

function addAlbumToCart(album_id) {
    $.post(
        '/purchase/add_album_to_cart/' + album_id,
        function(data) {
            if (data == "DUPLICATE") {
                $.growl.settings.displayTimeout = 7000;
                $.growl("You already some of these songs. ", "The rest have been added to your cart.");
                // setMessage("error", "You already own this song. <a href=\"/my/songs/\">Click here</a> to download it.");
            } else {
                // setMessage("notice", "We've added this song to your cart. <a href=\"/purchase/checkout\">Click here</a> to check out.");
                $.growl.settings.displayTimeout = 7000;
                $.growl.settings.noticeTemplate = "<div class='growlNotice'> <h5>%title%</h5>%message%</div>";
                $.growl("Album added to cart", data);
                updateCartLink();
            }
        }
        );
}

function removeFromCart(song_id) {
    $.post(
        '/purchase/remove_from_cart/' + song_id,
        function(data) {
            window.location.reload();
        }
        );
}

function emptyCart() {
    $.post(
        '/purchase/empty_cart/',
        function(data) {
            updateCartLink();
            window.location.reload();
        }
    );
}

function activatePlayer(player_id, swf_uri, file_uri) {
    // Show all images, destroy all players
    $(".miniplayer_swf").remove();
    $(".miniplayer_a").css("display", "inline");
    
    // Now add player to one div and hide image and anchor.
    $("#" + player_id).contents().css("display", "none");
    $("#" + player_id).prepend('<object play="true" class="playbutton miniplayer_swf" type="application/x-shockwave-flash" data="' + swf_uri + '" height="16" width="16"><param name="movie" value="' + swf_uri + '"><param name="FlashVars" value="url=' + file_uri + '"/><param name="quality" value="high"/></object>');
    
    $.get("/song/track_preview?preview_uri=" + file_uri);
    // $("#" + player_id).html('<object class="miniplayer_swf" type="application/x-shockwave-flash" data="' + swf_uri + '" height="17" width="17"><param name="movie" value="' + swf_uri + '"><param name="FlashVars" value="url=' + file_uri + '"/><param name="quality" value="high"/></object>');
}

function togglePref(pref_name) {   
	$.post(
		'/my/account/toggle_pref/' + pref_name, 
        function(data) {
            window.location.reload();
        }
    );
}

/**
 * Displays a flash message. Use this function to display a message on the same page 
 * without reloading. 
 */
function setMessage(msg_class, message) {
    $("#flashmessagewrap").html('<div id="flashmessage" class="' + msg_class + '">' + message + '</div>');
}

function shortify(s) {
	return s.toLowerCase().replace(/[^a-zA-Z0-9 -]/g, "").replace(/[ -]/g, "_");
}

function updateCartLink() {
    $.get("/purchase/cart_link", function(data){
        $("#cart").html(data);
        if (!$("#cart").is(':visible')) {
            $("#cart").slideDown();
        }
    });
}

function showSurvey() {
    $.facebox('<iframe src="http://www.surveygizmo.com/s/81670/popcuts-usability-survey" width="550" height="680" frameborder="0"></iframe>')
}

// Bind events.
$(document).ready(function() {
    $(".miniplayer_img").hover(
      function () {
        // $(this).attr("src", "/images/miniplayer_play_ov.png")
        $(this).attr("src", "/img/icons/play_over.png");
      }, 
      function () {
        // $(this).attr("src", "/images/miniplayer_play.png")
        $(this).attr("src", "/img/icons/play.png");
      }
    );
});

jQuery.fn.preventDoubleSubmit = function() {
  jQuery(this).submit(function() {
    if (this.beenSubmitted)
      return false;
    else
      this.beenSubmitted = true;
  });
};

function radio(uri) {
	window.open(uri, "", "resizable=0, width=400, height=184, scrolling=0");
}
