//dojo.require("dojo.io.*");
//dojo.require("dojo.event.*");

arg0Hold = "";
arg1Hold = "";

var $CM = function(){
	if( arguments[0] != null && arguments[0] != ""){
		var strCMID = arguments[0];
		var strCMValue = null;
		if( arguments.length == 2 )
			strCMValue = arguments[1];
		//var strCMCookieData = $C( 'CM' );
		var strCMCookieData = dojo.cookie('CM') != null ? dojo.cookie('CM') : "";
		if( arguments.length == 1 ){
			var aryDataPairs = strCMCookieData.split( '&' );
			for( intDataIndex = 0; intDataIndex < aryDataPairs.length; intDataIndex++ ){
				var intPosition = aryDataPairs[intDataIndex].indexOf('=');
				if( intPosition == -1 ) continue;
				var strDataName = aryDataPairs[intDataIndex].substring( 0, intPosition );
				var strDataValue = aryDataPairs[intDataIndex].substring( intPosition + 1 );
				if( strDataName == strCMID){
					return unescape( strDataValue );
				}
			}
		}
		else if( arguments.length == 2 ){
			var strCMData = '';
			var aryDataPairs = strCMCookieData.split( '&' );
			var objCM = new Object();
			for( intDataIndex = 0; intDataIndex < aryDataPairs.length; intDataIndex++ ){
				var intPosition = aryDataPairs[intDataIndex].indexOf('=');
				if( intPosition == -1 ) continue;
				var strDataName = aryDataPairs[intDataIndex].substring( 0, intPosition );
				var strDataValue = aryDataPairs[intDataIndex].substring( intPosition + 1 );
				objCM[strDataName] = unescape( strDataValue );
			}
			objCM[strCMID] = strCMValue;
			for( var strValues in objCM ){
				if( typeof(objCM[strValues]) != 'function' && objCM[strValues] != null && objCM[strValues] != 'null' ) {
					strCMData += "&" + strValues + '=' + escape( objCM[strValues] );
				}
			}
			strCMData = strCMData.substring( 1 );
			/*
			var objDate = new Date();
			objDate.setTime( objDate.getTime() + ( 1000 * 60 * 60 * 24 * 365 ) );
			document.cookie = "CM=" + escape( strCMData ) + "; domain=.napster.com; path=/; expires=" + objDate.toGMTString();
			*/
// if our cookie is this big, something is wrong
//if (strCMData.length > 4096) { debugger; return; }
			// alert(strCMData.length);
			if(strCMData.length > 4096) {
				if(initialized) {
					initialized = false;
					collection.store.initialize();
					arg0Hold = arguments[0];
					arg1Hold = arguments[1];
					// dojo.debug("entering cmInterval");
					cmInterval = setInterval( function(){
						if(initialized) {
							clearInterval(cmInterval);
							// dojo.debug("running $CM(" + arg0Hold + "," + arg1Hold);
							return $CM(arg0Hold, arg1Hold);
						}
					}, 500);
				} else {
					// dojo.debug("not initialized" + strCMData);
				}
			} else {
				//dojo.cookie("CM", strCMData, 365, "/", ".napster.com", null);
				dojo.cookie("CM", strCMData, {expire: 365, path: "/", domain: ".napster.com"});
			}
			return 1;
		}
	}
	return 0;
}

var $CollMgr = function(){
	if( arguments[0] != null ){
	// dojo.debug(runToSource(arguments));
		var strCollMgrName = arguments[0];
		var strCollMgrValue = null;
		if( arguments.length == 2 )
			strCollMgrValue = arguments[1];
		//var strCollMgrCookieData = $CM($C('NUID')) ? $CM($C('NUID')) : "";
		var strCollMgrCookieData = $CM(dojo.cookie('NUID')) ? $CM(dojo.cookie('NUID')) : "";
		if( arguments.length == 1 ){
			var aryDataPairs = strCollMgrCookieData.split( '##' );
			for( intDataIndex = 0; intDataIndex < aryDataPairs.length; intDataIndex++ ){
				var intPosition = aryDataPairs[intDataIndex].indexOf('!!');
				if( intPosition == -1 ) continue;
				var strDataName = aryDataPairs[intDataIndex].substring( 0, intPosition );
				var strDataValue = aryDataPairs[intDataIndex].substring( intPosition + 2 ); // position is 2 due to 2 char delimiter
				if( strDataName == strCollMgrName ){
					return unescape( strDataValue );
				}
			}
		} else if( arguments.length == 2 ){
			var strCollMgrData = '';
			var aryDataPairs = strCollMgrCookieData.split( '##' );
			var objCollMgr = new Object();
			for( intDataIndex = 0; intDataIndex < aryDataPairs.length; intDataIndex++ ){
				var intPosition = aryDataPairs[intDataIndex].indexOf('!!');
				if( intPosition == -1 ) continue;
				var strDataName = aryDataPairs[intDataIndex].substring( 0, intPosition );
				var strDataValue = aryDataPairs[intDataIndex].substring( intPosition + 2 ); // position is 2 due to 2 char delimiter
				objCollMgr[strDataName] = unescape( strDataValue );
			}
			// add the new val to comma separated list
			objCollMgr[strCollMgrName] = strCollMgrValue == 'null' ? 'null' : strCollMgrValue + (objCollMgr[strCollMgrName] ? "," + objCollMgr[strCollMgrName] : "");
			for( var strValues in objCollMgr ){
				if( typeof(objCollMgr[strValues]) != 'function' && objCollMgr[strValues] != null && objCollMgr[strValues] != 'null' ) {
					strCollMgrData += "##" + strValues + '!!' + escape( objCollMgr[strValues] );
				}
			}
			strCollMgrData = strCollMgrData.substring( 2 ); // 2 char delimiter
			// dojo.debug("strCollMgrCookieData - " + strCollMgrCookieData.length + " - " + strCollMgrCookieData);
			// dojo.debug("strCollMgrData - " + strCollMgrData.length + " - " + strCollMgrData);
			//return $CM( $C( 'NUID' ), strCollMgrData );
			// if(strCollMgrData.length > 4096) {
			if(strCollMgrData.length > 3900) {
				if(escape(strCollMgrValue).length > 3900) { // if what we're passing in is too large, fail with alert
					alert("Unable to perform the requested operation.");
					return;
				} else {
					// dojo.debug("ok strCollMgrValue - " + escape(strCollMgrValue).length + " - " + escape(strCollMgrValue));
				}
				if(initialized) {
					initialized = false;
					collection.store.initialize();
					arg0Hold = arguments[0];
					arg1Hold = arguments[1];
					// dojo.debug("entering cmInterval");
					cmInterval = setInterval( function(){
						if(initialized) {
							clearInterval(cmInterval);
							// dojo.debug("running $CollMgr(" + arguments[0] + "," + strCollMgrValue);
							return $CollMgr(arg0Hold, arg1Hold);
						}
					}, 500);
				} else {
					// dojo.debug("not initialized" + strCMData);
				}
				
			} else {
				// dojo.debug("cookie limit not reached, current length: " + strCollMgrData.length);
				arg0Hold = "";
				arg1Hold = "";
				var returnVal = $CM(dojo.cookie('NUID'), strCollMgrData );
				// dojo.debug("returnVal - " + returnVal.length + " - " + returnVal);
				return returnVal;
			}
			// return $CM(dojo.cookie('NUID'), strCollMgrData );
		}
	}
	return 0;
}

if(typeof collection == 'undefined') {
	collection = new Object();
}

collection.mgr = {
    addItem: function(id,type) {
		// dojo.debug("In addItem - " + id + " / " + type);
        var itemType = new Array();
        itemType[0] = "ADDTRACKS";
        itemType[1] = "ADDALBUM";
        itemType[2] = "ADDPL";
        itemType[7] = "ADDWORK";
        var success = "";
        //if($C('NUID')) {
        if(dojo.cookie('NUID') != null) {
            success = $CollMgr(itemType[type],id);
        } else {
            success = -1; // not logged in
        }
    },

    delItem: function(id,type) {
        var itemType = new Array();
        itemType[0] = "DELTRACKS";
        itemType[1] = "DELALBUM";
        itemType[2] = "DELPL";
        itemType[7] = "DELWORK";
        //if($C('NUID')) {
        if(dojo.cookie('NUID') != null) {
            // collection.store.deleteItem(id,type);
            success = $CollMgr(itemType[type],id);
        } else {
            success = -1; // not logged in
        }
    },

    addPlay: function(id) { // no type needed , you only play tracks
        //if($C('NUID')) {
        if(dojo.cookie('NUID') != null) {
            success = $CollMgr('PLAYS',id);
        }  else {
            success = -1; // not logged in
        }
    },

    syncItems: function(uid) {

    },

    syncPlays: function(uid) {

    },

    clear: function(type) {
        $CollMgr(type,'null');
    }
};
