//
// Copyright (c) 2006 monstar.fm
//
var JavaScriptProxyManager;
if(JavaScriptProxyManager == undefined) {
	JavaScriptProxyManager = {
		instances : new Array(),
		getInstance : function(lcid) {
			for(i in this.instances) {
				if( lcid == this.instances[i].uid ) {
					return this.instances[i];
				}
			}
		},
		callEvent : function(lcid, eventName) {
			var jb = this.getInstance(lcid);
			var args = new Array();
			for(var i=2; i < arguments.length; i++) {
				args.push(arguments[i]);
			}
			if(pkg.isDebug) {
				debug("JavaScriptProxyManager.callEvent : " + eventName + "(" + args + ")");
			}
			jb[eventName].apply(jb,args);
		}
	}
}

/**
 * TempoPad Widget class.
 * 
 */
TempoPad = Class.create();
Object.extend(TempoPad.prototype,{
	executer : undefined,
	callstack : new Array(),
	callCount : 0,
	area : undefined,
	width : "550px",
	height: "200px",
	swfName : "tempopad.swf",
	bpm : undefined,
	
	initialize : function() {
		this.uid = "TP" + new Date().getTime();
		this.name = "TempoPad";
		this.path = pkg.getComponentUrl() + this.swfName + "?v=" + pkg.revision;
		this.flashProxySrc = pkg.getScriptUrl() + "JavaScriptFlashGateway.swf?v=" + pkg.revision;
		this.flashProxy = new FlashProxy(this.uid, this.uid,this.flashProxySrc);
		this.tag = new FlashTag(this.path, this.width, this.height); // path, width, height
		this.tag.setFlashvars('lcId='+this.uid + '&contextPath=' + pkg.contextPath + '&debug=' + pkg.isDebug);
		this.tag.setId(this.name);
		this.tag.menu = "false";
		this.tag.scale = "noscale";
		this.tag.setBgcolor("333333");
		JavaScriptProxyManager.instances.push(this);
	},
	periodicalCall : function() {
		if(this.callstack.length > 0 ) {
			var args = this.callstack.shift();
			this.flashProxy.call.apply(this.flashProxy, args);
		} else {
			this.executer.deregisterCallback();
		}
	},
	
	callFunction : function() {
		if(this.executer == undefined) {
			this.executer = 
				new monstar.event.PeriodicalExecuter(this.periodicalCall.bind(this), 0.02);
		}
		var myArgs = new Array();
		for(var i=0; i < arguments.length; i++ ) {
			myArgs.push(arguments[i]);
		}
		this.callstack.push(myArgs);
		if(pkg.isDebug) debug("TempoPad." + myArgs.toString().escapeHTML());
		this.executer.registerCallback();
	},

	updateBpm : function(bpm) {
		this.bpm = bpm;
		if(pkg.isDebug) debug("update bpm : " + bpm);
	},

	getBpm : function() {
		return this.bpm;
	},
	
	create : function( areaId ) {
		this.area = $(areaId);
		if( this.area != undefined ) {
			if(monstar.browser.ie) {
				this.area.outerHTML = 
					'<div id="' + areaId + '">' + this.tag.toString() + '</div>';
			} else {
				this.area.innerHTML = this.tag.toString();
			}
			this.area = $(areaId);
			with( this.area.style ) {
				visibility = "hidden";
				width = this.width;
				height = this.height;
			}
		}
	},
	
	onLoad : function() {
		if(this.area) {
			with( this.area.style ) {
				visibility = "visible";
				width = this.width;
				height = this.height;
				margin = "0px auto";
			}
		}
	}

});
