(function() {
	
	if(!window.beyondo) {
		window.beyondo = {};
	}
	
	if(!beyondo.util) {
		beyondo.util = {
			widget : {},
			widget_index : -1,
			parseJSParams: function(obj, jsName){		
				var scripts = document.getElementsByTagName('script');				
				for(var i=0; i<scripts.length; i++){
					var url = scripts[i].src;
					if(url.indexOf(jsName) > 0){
						var q = url.indexOf("?");
						if (q > 0) {
							var qs = url.substr(q + 1);
							var q2 = qs.indexOf("?");
							if(q2 > 0){
								this.parseURLParameters(qs.substr(0, q2), obj);
							} else {
								this.parseURLParameters(qs, obj);
							}
						}
					}
				}
			},
			parseURLParameters: function(parameters, obj){
				var params = parameters.split("&");
			    for (var p = 0; p < params.length; p++) {
			        var pair = params[p].split("=");
			        obj[pair[0]] = pair[1];
			    }
			},
			includeCss: function(doc, url){
				var link = {};
				link = document.createElement("link");
				link.type = "text/css";
				link.rel = "stylesheet";
				link.href = url;
				doc.appendChild(link);
			},
			includeJs: function(doc, url){
				var script = {};
				script = document.createElement('script');
				script.type = 'text/javascript';
			    script.language = 'JavaScript';
			    script.charset = 'UTF-8';
			    script.src = url;
			    doc.appendChild(script);
			},
			messageFormat: function(msg, args){
				if(typeof args == 'object' && args.length){
					for(var i=0; i<args.length; i++){
						msg = msg.replace(new RegExp('\{['+i+']\}','g'), args[i]);
					}
				} else {
					msg = msg.replace(new RegExp('\{[0]\}','g'), args);
				}
				return msg;
			},
			hc: function(data){
				this.widget[data['index']].complete(data);
			}
		}
	}
	
	if(!beyondo.Widget) {
		beyondo.Widget = function(index, onJsOpts, div, opts){
			this.index = index;
			for(var key in opts){
				this.conf[key] = opts[key];
			}
			this.init(onJsOpts, div);
		},
		
		beyondo.Widget.prototype = {
			conf: {
				w: 600,
				h: 400,
				logo: 'http://cdn1.kau-switch.com/all/modules/b_api/img/Beyondo_LOGO.jpg',
				css: 'http://www.kau-switch.com/sites/all/modules/b_api/css/bbp-type-{0}.css',
				db: 'http://www.kau-switch.com/b_api/bbp15/{0}/title/?cb=beyondo.util.hc&t={1}&pi={2}',
				library: 'http://cdn1.kau-switch.com/all/modules/b_api/js/librarys-min.14.1.js',
				type: 'http://www.kau-switch.com/sites/all/modules/b_api/js/bbp-type-{0}.js'
			},
			init: function(onJsOpts, div){
				var self = this;
				if(onJsOpts) {
					beyondo.util.parseJSParams(this.conf, 'bbp-common.js');
				}
				this.conf.widget = 'byd_widget_' + this.index;
				var html = '<div class="byd-widget" id="' + this.conf.widget + '" style="width:' + this.conf.w + 'px;height:' + this.conf.h + 'px;">'
				+ '<div class="container">'
				+ '<img src="'+ this.conf.logo +'" width="' + this.conf.w + '" height="' + this.conf.h + '"/>'
				+ '</div></div>';
				(typeof div == "undefined")?document.write(html):document.getElementById(div).innerHTML = html;
				var isLogoReady = false;
				window.setTimeout(function(){
					self.isLogoReady = true;
				},3000);
				
				var dom_widget = document.getElementById(this.conf.widget);		
				beyondo.util.includeJs(dom_widget, this.conf.library);
				beyondo.util.includeJs(dom_widget, beyondo.util.messageFormat(this.conf.db, [this.index, encodeURI(document.title), this.conf.pi]));
			},
			error: function(err){
				this.container.append('<span class="error">#'+ err['code'] + ':' + err['msg'] +'</span>');	
			},
			complete: function(data){
				var self = this;
				var dom_widget = document.getElementById(this.conf.widget);
				this.data = data;				
				var res_time = window.setInterval(function(){
					if(self.isLogoReady && ("bbpjQuery" in window)) {
						window.clearInterval(res_time);
//						bbpjQuery.noConflict();
						self.container = bbpjQuery('#' + self.conf.widget).find('.container');
						if(self.data['error']) {
							self.error(self.data['error']);
						} else {
							beyondo.util.includeCss(dom_widget, beyondo.util.messageFormat(self.conf.css, (self.data['bbp']['tid']?self.data['bbp']['tid']:2)));		
							beyondo.util.includeJs(dom_widget, beyondo.util.messageFormat(self.conf.type, (self.data['bbp']['tid']?self.data['bbp']['tid']:2)));							
						}				
					}
				},500);
			}
		}
	}	
})();

beyondo.util.widget_index++;
beyondo.util.widget[beyondo.util.widget_index] = new beyondo.Widget(beyondo.util.widget_index, true);
