/*
 * InSkin Load - Javascript loader for InSkin integration.
 * Author: Catalin Ciocov <catalin_ciocov@yahoo.co.uk>
 */

if (typeof(InSkinLoadedFiles) == 'undefined') {
	InSkinLoadedFiles = 'inskin_load';
}

if (typeof(InSkinLoad) == 'undefined') {
	var InSkinLoad = {};
	InSkinLoad['BASE_URL'] = 'http://inskin.vo.llnwd.net/o21/ikit/v1';
	InSkinLoad['VERSION'] = (new Date()).getTime();
	InSkinLoad['load_queue'] = new Array();

	InSkinLoad['init'] = function(strparams) {
		changePlayerContentDivId();
		
		strparams = strparams + "plr_TopBarVersion=2&";
		strparamsarray = strparams.split('&');
		var params = [];
		for (var p = 0; p < strparamsarray.length; p++) {
			var prop = strparamsarray[p].split('=');
			params[prop[0]] = prop[1];
			
		}
		
		var base_url = (params['BASE_URL'] && params['BASE_URL'] != '' ? params['BASE_URL'] : this.BASE_URL);
		var required_files = new Array();
		var required_libraries = new Array();

		params['BASE_URL'] = base_url;
		params['VERSION'] = this.VERSION;

		this.loadLibrary(base_url + '/js/3rdparty/jquery-1.2.6.min.js', 'jQuery');
		required_libraries[required_libraries.length] = 'jQuery';

		this.loadLibrary(base_url + '/js/3rdparty/swfobject_2x.js', 'SWFObject2x');
		required_libraries[required_libraries.length] = 'SWFObject2x';

		this.loadFile(base_url + '/js/inskin_calc.js?v=' + this.VERSION, 'inskin_calc');
		required_files[required_files.length] = 'inskin_calc';
		
		this.loadFile(base_url + '/js/inskin_base.js?v=' + this.VERSION, 'inskin_base');
		required_files[required_files.length] = 'inskin_base';

		if (params['plr_ContentType'] && params['plr_ContentType'] != '') {
			var lc = params['plr_ContentType'].toLowerCase();
			var url = base_url + '/js/api/' + lc + '.js?v=' + this.VERSION;
			var label = 'api_' + lc.toUpperCase();

			this.loadFile(url, label);
			required_files[required_files.length] = label;
		}
		
		if (params['plr_LoadedContentTypes'] && params['plr_LoadedContentTypes'] != '') {
			var arr = params['plr_LoadedContentTypes'].split(/,/);
			for (var i = 0; i < arr.length; i++) {
				var lc = arr[i].toLowerCase();
				var url = base_url + '/js/api/' + lc + '.js?v=' + this.VERSION;
				var label = 'api_' + lc.toUpperCase();

				this.loadFile(url, label);
				required_files[required_files.length] = label;
			}
		}

		this.wait(required_files, required_libraries, params);
	}

	InSkinLoad['loadFile'] = function(url, label) {
		if (InSkinLoadedFiles.indexOf(label) != -1 || this.load_queue[label]) return;
		this.load_queue[label] = true;
		this.load(url);
	}

	InSkinLoad['loadLibrary'] = function(url, label) {
		if (this.checkLibraryLoaded(label) || this.load_queue[label]) return;
		this.load_queue[label] = true;
		this.load(url);
	}

	InSkinLoad['load'] = function(url) {
		head = document.getElementsByTagName('head')[0];
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src =  url;
		head.appendChild(script);
		//document.write('<!-- inskin test -->');
		//document.write('<script type="text/javascript" src="' + url + '"></script>');
	}

	InSkinLoad['wait'] = function(required_files, required_libraries, params) {
		var sw = true;
		for (var i = 0; i < required_files.length && sw; i++) {
			if (InSkinLoadedFiles.indexOf(required_files[i]) == -1) {
				sw = false;
			}
		}
		for (var i = 0; i < required_libraries.length && sw; i++) {
			sw = this.checkLibraryLoaded(required_libraries[i]);
		}
		
		if (sw) {
			new InSkinBase(params);
		}
		else {
			setTimeout(function() { InSkinLoad.wait(required_files, required_libraries, params); }, 250);
		}
	}

	InSkinLoad['checkLibraryLoaded'] = function(label) {
		switch (label) {
			case 'SWFObject2x':
				return (typeof(swfobject) != 'undefined');
			case 'jQuery':
				return (typeof(jQuery) != 'undefined');
		}
		return false;
	}
	
	function changePlayerContentDivId() {
          document.getElementById('vid_wrp').className = 'inskin';
	}
}
