﻿function JavascriptAPI(divid, playerpath, ready_delegate, swfPath, width, height, targetid) {
	if(JavascriptAPI.instances == null) JavascriptAPI.instances = [];
	JavascriptAPI.instances.push(this);
	
	this.onReady = ready_delegate;
	this.asReadyInterval = 0;
	this.listenerProxies = [];
	this.asListenerProxies = []; 
	this.id = guid();
	this._targetid = targetid || "_default_target_";
	this._playerPath = playerpath || null
	this._corePath = swfPath || "http://openchannel.multichanneltv.com/core/openchannelCore.swf";
	
	if(!window._openchannelModules) window._openchannelModules = {};
	var flashvars = {id: this.id,
									 targetid: this._targetid,
									 playerPath: this._playerPath, 
									 targetW: width, 
									 targetH:height, 
									 seekBarColor:_global_flash.seekBarColor, 
									 timerColor:_global_flash.timerColor, 
									 forceWidth:_global_flash.forceWidth,
									 forceWidthW:_global_flash.forceWidthW,
									 forceWidthH:_global_flash.forceWidthH,
									 showMCLogo:_global_flash.showMCLogo,
									 volumeInit: _global_flash.volumeInit
									 };
	var params = {menu: "false",scale: "noScale",allowFullscreen: "true",allowScriptAccess: "always",bgcolor: "#000000",wmode:"transparent", align:"TL", salign:"TL"}; 
	var attributes = {id:divid,name:divid};
	window._openchannelModules["_openchannelModule_"+this.id] = this;
	swfobject.embedSWF(this._corePath, divid, width, height, "9.0.0", "expressInstall.swf", flashvars, params, attributes, delegate(this, this.onEmbed));
};

JavascriptAPI.prototype.callMethod = function() {
	var methodname = arguments[0];
	var params = [];
	for (var i=1; i<arguments.length; i++) {
		params.push(arguments[i]);
	}
	if(params.length>0) {
		if(this.swfmodule[methodname] != undefined) {
			return this.swfmodule[methodname].apply(this.swfmodule, params);
		} else {
			return this.swfmodule.callMethod.apply(this.swfmodule, arguments);
		}
	} else {
		if(this.swfmodule[methodname] != undefined) {
			return this.swfmodule[methodname]();
		} else {
			return this.swfmodule.callMethod.apply(this.swfmodule, arguments);
		}
	}
};

JavascriptAPI.prototype.callCoreMethod = function() {
	var methodname = arguments[0];
	var params = [];
	for (var i=1; i<arguments.length; i++) {
		params.push(arguments[i]);
	}
	if(params.length>0) {
		if(this.swfmodule[methodname] != undefined) {
			return this.swfmodule[methodname].apply(this.swfmodule, params);
		} else {
			return this.swfmodule.callCoreMethod.apply(this.swfmodule, arguments);
		}
	} else {
		if(this.swfmodule[methodname] != undefined) {
			return this.swfmodule[methodname]();
		} else {
			return this.swfmodule.callCoreMethod.apply(this.swfmodule, arguments);
		}
	}
};

JavascriptAPI.prototype.getProperty = function(prop) {
	return this.swfmodule.getProperty(prop);
};

JavascriptAPI.prototype.setProperty = function(prop, value) {
	return this.swfmodule.setProperty(prop, value);
};

JavascriptAPI.prototype.onEmbed = function(e) {
	this.swfmodule = e.ref;
}

JavascriptAPI.prototype.addEventListener = function(type, delegate, id) {
	for (var i=0; i<this.listenerProxies.length; i++) {
		if(this.listenerProxies[i].callback == delegate && this.listenerProxies[i].type == type) {
			return null;
		}
	}
	var proxy_id = this.swfmodule.addJSEventListener(type, id);
	var newproxy = new ListenerProxy(type, delegate, proxy_id)
	this.listenerProxies.push(newproxy);
	window[proxy_id] = delegate;
};
	
JavascriptAPI.prototype.removeEventListener = function(type, delegate, proxy_id) {
	for (var i=0; i<this.listenerProxies.length; i++) {
		if(this.listenerProxies[i].callback == delegate && this.listenerProxies[i].type == type) {
			proxy_id = this.listenerProxies[i].id
			this.listenerProxies.splice(i, 1);
			break;
		}
	}
	if (proxy_id == null) return null;
	this.swfmodule.removeJSEventListener(proxy_id);
};

JavascriptAPI.prototype.addASEventListener = function(type, targetid) {
	if(!targetid) targetid = this._targetid
	var callbackdelegate = delegate(this, this.dispatchASEvent)
	var newProxy = new ListenerProxy(type, callbackdelegate);
	this.asListenerProxies.push(newProxy);
	window._openchannelModules["_openchannelModule_" + targetid].addEventListener(newProxy.type, delegate(newProxy, newProxy.dispatchProxy), newProxy.id);
	return newProxy.id;
}

JavascriptAPI.prototype.removeASEventListener = function(proxy_id, targetid) {
	if(!targetid) targetid = this._targetid
	for (var i = 0; i <this.asListenerProxies.length ; i++) {
		var listenerProxy = this.asListenerProxies[i];
		if (listenerProxy.id == proxy_id) {
			window._openchannelModules["_openchannelModule_" + targetid].removeEventListener(listenerProxy.type, listenerProxy.callback, proxy_id);
			this.asListenerProxies.splice(i, 1);
			break;
		}
	}
}

JavascriptAPI.prototype.dispatchASEvent = function(event, listenerProxy) {
	this.swfmodule["callback_" + listenerProxy.id](event);
}

JavascriptAPI.prototype.init = function() {
	setTimeout(delegate(this, this.oninit), 100);
};

JavascriptAPI.prototype.oninit = function() {
	if(this.onReady) this.onReady();
}