var playNow=1;

function playIt(id, mixalbum, play, caller) {
	openPlayer();
	new Effect.Pulsate(caller, {pulses:1});
	playNow = (play==1?1:0);
	var url = '/fplayer/playerAjax.php?a=preview&id='+id+'&mixalbum='+mixalbum;
	
	createRequest();
	request.open("GET", url, true);
	request.onreadystatechange = playItHandler;
	request.send(null);

// Added by Lawrence 15/07/2009 - Strands Recommendations
DJDSendAjax("/strands/strands_event.php?action=visit&item_id="+id, "DJDDummyHandler", "");
}

function DJDDummyHandler(response){
  // do nothing
}

function failHandler(http) {
	alert('Failed');
}

function playItHandler() {
	if (request.readyState == 4) {
		if (request.status==200) {
			var r = eval('('+ request.responseText+')')
			if (r.status==false) {
				return false;
			}
			if (r.status==true) {
				// call player
				var qs = 'audioProfile=' + r.audioProfile;
				qs += '&dbid=' + r.dbid;
				qs += '&artist=' + encodeURIComponent(Utf8.decode(r.artist));
				qs += '&trackTitle=' + encodeURIComponent(Utf8.decode(r.title));
				qs += '&buy=' + r.buy;
				qs += '&bitRate=' + r.bitrate;
				qs += '&path=' + r.path;
				qs += '&startAt=' + r.startAt;
				qs += '&fileSize=' + r.fileSize;
				qs += '&listenNow=' + playNow;
				qs += '&coverArt=' + r.coverArt;
				qs += '&sampleLength=' + r.sampleLength;		
				//alert(qs.replace(/&/g, "\n&"));
				addTrackQS(qs);
			}
		} else {
			return false;		
		}
	}
}

function playlistAll(playListId) {
	if (playListId.length==0) return false;
		openPlayer();
	new Effect.Pulsate('btnPlaylistAll', {pulses:1});

	// post these to playerAjaxScript
	var param = 'a=playlistAll&';
	for(i=0;i<playListId.length;i++) {
		param += 'id[]='+playListId[i]+'&'; 
	}
	// send	
	var url = '/fplayer/playerAjax.php';
	createRequest();
	request.open("post", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded") ;
    request.setRequestHeader("Content-length", param.length);
	request.onreadystatechange = playListHandler;
	request.send(param);
}


function playListHandler() {
	if (request.readyState == 4) {
		if (request.status==200) {
			var r = eval('('+ request.responseText+')');
			if (r.status==false) { 	return false;}
			if (r.status==true) {
				addTrackXML(r.msg);
			}
			// delete request
			request = null;
		} else {
			// delete request
			request = null;
			return false;
		}
	}
}

function buyTrack(dbid){
	var url = '/playerCartAdd.php';
	var param = 'theData[]='+dbid;
	createRequest();
	request.open("post", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded") ;
    request.setRequestHeader("Content-length", param.length);
	request.onreadystatechange = buyTrackHandler;
	request.send(param);
}

function buyTrackHandler(http) {
	return;
}

function buyAllTracks(dbids){
	var url = '/playerCartAdd.php';
	var param = '';
	for(i=0; i<dbids.length; i++) {
		param += 'theData[]='+dbids[i]+'&';
	}
	createRequest();
	request.open("post", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded") ;
    request.setRequestHeader("Content-length", param.length);
	request.onreadystatechange = buyTrackHandler;
	request.send(param);
}


/*
*
* UTF-8 data encode / decode
* http://www.webtoolkit.info/
* http://www.webtoolkit.info/javascript-utf8.html
*/
var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}