bpAPI = {};

//////////////// events ////////////////////

bpAPI.Events = {};
bpAPI.Events.temp = [];
bpAPI.Events.add = function(eventName, eventParam){
    bpAPI.Events.temp[eventName] = eventParam;
}
bpAPI.Events.fire = function(location){
    bpAPI.sendFunction('bpApp.API.WebEvents.handle',bpAPI.Events.temp,location);
}


/////////////// send ///////////////////

bpAPI.sendFunction = function(functionName, param, location){
    var tmp = [];
    tmp['func'] = functionName,
    tmp['param'] = param;
    if(location == undefined)
        tmp['referer'] = top.location.href;
    else
        tmp['referer'] = location;
    bpAPI.send(bpAPI.base64.encode(bpAPI.json.encode(tmp)));
}
bpAPI.send = function(str){
    top.location = 'bp://' + str;
}

////////////////// json //////////////////

bpAPI.json = {};
bpAPI.json.encode = function(input){
    if (!input) return 'null'
    switch (input.constructor) {
        case String: return '"' + input + '"'
        case Number: return input.toString()
        case Boolean:
            if(input === true)
                return 'true';
            else
                return 'false';
        case Array :
            var buf = [];
            for (i in input)
                buf.push('"' + i + '" : ' + bpAPI.json.encode(input[i]))
            return '{' + buf.join(', ') + '} ';
        case Object:
            var buf = [];
            for (k in input)
                buf.push('"' + k + '" : ' + bpAPI.json.encode(input[k]))
            return '{ ' + buf.join(', ') + '} ';
        default:
        return 'null'
    }
}

////////////// base64 ///////////////

bpAPI.base64 = {};
bpAPI.base64.keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
bpAPI.base64.encode = function(input){
    var output = "";
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i = 0;
	input = bpAPI.utf8.encode(input);
	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
		output = output +
		bpAPI.base64.keyStr.charAt(enc1) + bpAPI.base64.keyStr.charAt(enc2) +
		bpAPI.base64.keyStr.charAt(enc3) + bpAPI.base64.keyStr.charAt(enc4);
	}
	return output;
}

////////////// UTF8 /////////////

bpAPI.utf8 = {};
bpAPI.utf8.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;
}

