/*
	instantiate the DirProxy class
*/

// create proxy to director shockwave using a hardcoded id: sdkID
// the same id is found in the sdk, so the two can speak
if ( typeof dirProxy != 'undefined' ) {
	//alert ('dirProxy is valid so we do nothing');
} else {
	var dirProxy = new DirProxy('sdkID', '/content/highscores/JavaScriptDirectorGateway.swf');
}


var StringBufferBufs;
var StringBufferSends;
var StringBufferCurrentSize;
var StringBufferTotalSize;
var StringBufferFunctionToCall;

// n = how many arguments
// f = function to call
// t = total length of all strings (for error checking)
// i = 0, 1, ... the array position of the buffered string
// txt = the text you want to append
// end = 0 or 1, indicates whether we are done
function InitStringBuffers(n, f, t, i, txt, end) {
	StringBufferTotalSize = t;
	StringBufferCurrentSize = 0;
	StringBufferSends = 0;
	StringBufferFunctionToCall = eval(f);
	StringBufferBufs = [];
	for (var j = 0; j < n; ++j) {
		var buf = [];
		StringBufferBufs.push(buf);
	}
	// we send a chunk of data with the init call
	AppendStringBuffer(i, txt, end);
}


// i = 0, 1, ... the array position of the buffered string
// txt = the text you want to append
// end = 0 or 1, indicates whether we are done
// if end == 1 and the last string has been buffered, and the expected/actual string lengths match, call the function
function AppendStringBuffer(i, txt, end) {
	StringBufferCurrentSize+=txt.length;
	StringBufferSends++;
	StringBufferBufs[i].push(txt);
	if (end == 1) {
		StringBufferBufs[i] = StringBufferBufs[i].join("");
		if (i == StringBufferBufs.length-1) {
			if (StringBufferTotalSize - StringBufferCurrentSize < 100) {
				var argArray = [];
				for (var j = 0; j < StringBufferBufs.length; ++j) {
					argArray.push(StringBufferBufs[j]);
				}
				StringBufferFunctionToCall.apply(StringBufferFunctionToCall, argArray);
			} else {
				alert ("Error: StringBuffer failed to receive the entire string. Expected:" + StringBufferTotalSize + " Received:" + StringBufferCurrentSize);
				//dirProxy.call('continueDispatch');
			}
		}
	}
}