$(function(){
var numTracks = 2;
function getUserDetails(){
SC.get("/me", function(me){
$("#username").html("" + me.username + "");
$("#description").val(me.description);
});
}
function getUserTracks() {
SC.get("/me/tracks", function(tracks){
$("#tracks").html("
");
$.each(tracks, function(key,track){
$("#tracks ul").append("");
$("li." + track.id).load('sc-track.php',track, function(){
if(track.sharing == 'private'){
$("li." + track.id + " .controls").append("private on SoundCloud");
$(".private_track_" + track.id).attr("checked", "checked");
}
if($.cookie('track_' + track.id)){
$("input#track_" + track.id).attr("checked","checked");
}
});
});
});
}
SC.initialize({
client_id: "56bfc07c55c34f4c208ed0d6fffa311a",
redirect_uri: "http://afx.cc/sandbox/callback.html",
scope: "non-expiring"
});
// $(window).bind("load", function() {
//shitty hack to reset checkboxes because the callback isn't working.
// setTimeout(function(){
// var bol = $('input[name^="track_"]:checked').length >= numTracks;
// $('input[name^="track_"]').not(":checked").attr("disabled",bol);
// }, 3000)
// });
if(SC.isConnected()){
getUserDetails();
getUserTracks();
$("#connect").hide();
$(".logged-in").show();
$(".tracks-allowed span").html(numTracks);
//alert('connected');
}else {
//alert('not connected');
}
$("#connect").on("click", function(){
SC.connect(function(){
getUserDetails();
getUserTracks();
$("#connect").hide();
$(".logged-in").show();
});
});
$("#disconnect").on("click", function(){
SC.get("/me/soundcloud/revoke",function(){
$(".logged-in").hide();
$("#connect").show();
});
});
$('#get-me').on("click",function() {
SC.get("/me", function(me){
$('#me-data').html(me.username);
});
});
$('input[name^="track_"]').live("click", function() {
var bol = $('input[name^="track_"]:checked').length >= numTracks;
$('input[name^="track_"]').not(":checked").attr("disabled",bol);
var cookieData = $(this).attr('rel').split('_');
if($(this).attr('checked') == 'checked'){
//write cookie
$.cookie('track_' + cookieData[0], cookieData[1]);
}else{
//delete cookie
$.cookie('track_' + cookieData[0], null);
}
});
$("#update").on("click", function(){
SC.put("/me", {user: {description: $("#description").val()}}, function(response, error){
if(error){
alert("Some error occured: " + error.message);
}else{
alert("Profile description updated!");
}
});
});
});