
var _moviebox = {

	config : {
		playermaxwidth : 680,
		sidebarminwidth : 40,
		stageratio : 1.778,
		controlbarheight : 19,
		element : {},
		dataprefix : 'http://moviebox.kinoundco.de/',
		datasuffix : 'box.aspx',
		boxtype : 'movie',
		elementid : 'moviebox'
	},

	opts : {
		dummy : ''
	},

	init : function(opt) {

		function b64enc(input) {
			var b64key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
			var output = "", chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0;
			input = input.replace(/\r\n/g,"\n");
			var utftext = "";
			for (var n = 0; n < input.length; n++) {
				var c = input.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);
				}
			}
			input = utftext;
			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 += b64key.charAt(enc1) + b64key.charAt(enc2) + b64key.charAt(enc3) + b64key.charAt(enc4);
			}
			return output;
		}
		var param = new Array;
		param['vk'] = 'c4fb7f24b4ca9ddd037370ac9dd82d5e134fd6c8';
		if (opt.customerid) { param['c'] = opt.customerid; } else { return false; }
		if (opt.cityid) { param['cc'] = opt.cityid; }
		if (opt.operatorid) { param['co'] = opt.operatorid; }
		if (opt.movieid) { param['k'] = opt.movieid; }
		if (opt.boxtype) { _moviebox.config.boxtype = opt.boxtype; }
		if (!opt.movieid && !opt.boxtype) { return false; }
		if (opt.clipxml) { param['cx'] = opt.clipxml; }
		if (opt.trailerurl) {
			if (opt.trailerurl['low']) { param['tl'] = opt.trailerurl['low']; }
			if (opt.trailerurl['high']) { param['th'] = opt.trailerurl['high']; }
			if (opt.trailerurl['age']) { param['ta'] = opt.trailerurl['age']; }
		}
		if (opt.trailerthumb) { param['a'] = opt.trailerthumb; }
		if (opt.website) { param['w'] = opt.website; }
		if (opt.share) { param['s'] = opt.share; }
		if (opt.bgimg) { param['b'] = opt.bgimg; }
		if (opt.showdelay) { param['sd'] = opt.showdelay; }
		if (opt.defaultbandwidth) { param['bw'] = opt.defaultbandwidth; }
		if (opt.firstclip) { param['fc'] = opt.firstclip; }
		param['r'] = window.location.href;
		if (window.location.hash) {
			param['r'] = param['r'].substr(0,param['r'].indexOf(window.location.hash));
		}
		_moviebox.opts['refer'] = param['r'];
		if (opt.elementid) { _moviebox.config.elementid = opt.elementid; }
		mbd = document.getElementById(_moviebox.config.elementid);
		if (opt.width) { _moviebox.opts['width'] = opt.width; } else { _moviebox.opts['width'] = mbd.offsetWidth; }
		if (opt.height) { _moviebox.opts['height'] = opt.height; }
		var qstr = '';
		for (var paramk in param) {
			if (param.hasOwnProperty(paramk)) {
				qstr += '&' + paramk + '=' + escape(b64enc(param[paramk].toString()));
			}
		}
		qstr = qstr.substr(1);
		if (_moviebox.opts['width'] > _moviebox.config.playermaxwidth) {
			_moviebox.opts['width'] = _moviebox.config.playermaxwidth;
		}
		if (!_moviebox.opts['height']) {
			_moviebox.opts['height'] = Math.ceil((_moviebox.opts['width'] - _moviebox.config.sidebarminwidth) / _moviebox.config.stageratio) + _moviebox.config.controlbarheight;
		}
		_moviebox.config.element = document.createElement('iframe');
		_moviebox.config.element.src = _moviebox.config.dataprefix + _moviebox.config.boxtype + _moviebox.config.datasuffix + '?' + qstr;
		_moviebox.config.element.width = _moviebox.opts['width']; _moviebox.config.element.height = _moviebox.opts['height']; _moviebox.config.element.frameBorder = 0;
		document.getElementById(_moviebox.config.elementid).appendChild(_moviebox.config.element);

	},

	size : function(w,h) {
		_moviebox.config.element.width = w;
		_moviebox.config.element.height = h;
	},

	shutdown : function() {
		document.getElementById(_moviebox.config.elementid).parentNode.removeChild(document.getElementById(_moviebox.config.elementid));
	}

};

function debug(text) { if (console) { if (console.log) { console.log(text); } } }
