SN.Ajax = new Object();

SN.Ajax.createXMLHttp = function createXMLHttp() {
	var xmlhttp;
	if (typeof XMLHttpRequest != 'undefined') {
	    try {
	        xmlhttp = new XMLHttpRequest();
	    } catch (e) {
	        xmlhttp = false;
	    }
	}
	if (!xmlhttp && window.createRequest) {
	    try {
	        xmlhttp = window.createRequest();
	    } catch (e) {
	        xmlhttp = false;
	    }
	}
	if (!xmlhttp) {
		alert("Your browser does not support AJAX Technology.  Please update your dinosaur of a browser!!");
	}
	return xmlhttp;
};


SN.Ajax.easyCall = function(url, callback, postVars) {
    var ajaxObj = SN.Ajax.createXMLHttp();
    ajaxObj.open((postVars) ? "POST" : "GET", url + ((url.indexOf('?') == -1) ? "?" : "&") + "rand=" + Math.random(), true);
    ajaxObj.onreadystatechange = function() {
        if (ajaxObj.readyState == 4 && callback) {
            callback(ajaxObj);
        }
    };
    if (postVars) {
        ajaxObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    }
    ajaxObj.send((postVars) ? postVars : null);
};

SN.Ajax.easyResponse = function(url, callback, postVars) {
	SN.Ajax.easyCall(url, function(obj) {
		callback(obj.responseText);
	}, postVars);
};

SN.Ajax.easyXML = function(url, callback, postVars) {
	SN.Ajax.easyCall(url, function(obj) {
		callback(obj.responseXML.documentElement);
	}, postVars);
};

SN.Ajax.easyLogin = function(username, password, callback) {
    SN.Ajax.easyResponse("/App_Info/public/login.aspx?u=" + username + "&p=" + password + "&r=false", callback);
};

// The callback is passed a boolean... whether it has succeeded or not :)
SN.Ajax.addToPlaylist = function(songID, callback) {
    SN.Ajax.easyXML("/app_info/FlashPlayList2.aspx?cmd=add&songid=" + songID, function(r) {
        callback(r.getElementsByTagName('playlist')[0].getAttribute('status') == "success");
    });
};

SN.Ajax.removeFromPlaylist = function(songID, callback) {
    SN.Ajax.easyXML("/app_info/FlashPlayList2.aspx?cmd=remove&songid=" + songID, function(r) {
		callback(r.getElementsByTagName('playlist')[0].getAttribute('status') == "success");
	});
};

SN.Ajax.isInPlaylist = function(songID, callback) {
    SN.Ajax.easyXML("/app_info/FlashSongInfo.aspx?songID=" + songID, function(r) {
        callback(r.getAttribute('inplaylist') == "yes");
    });
};

SN.Ajax.getSongInfo = function(songID, callback) {
    SN.Ajax.easyXML("/app_info/FlashSongInfo.aspx?songID=" + songID, callback);
};

SN.Ajax.submitMusicComment = function(songID, body, callback) {
    SN.Ajax.easyResponse("/App_Info/public/addNewComment.aspx?objectType=music&objectID=" + songID, callback, "body=" + body + "&force=true");
};
