﻿//
// Namespace
// WebCounter
WebCounter = {}
;
//
// Namespace
// WebCounter.Utils
WebCounter.Utils = {}
;
//
// Namespace
// WebCounter.Flex
WebCounter.Flex = {}
;
//
// Enum
// WebCounter.CommandType
WebCounter.CommandType = {}
;
//
// Enum
// WebCounter.EventType
WebCounter.EventType = {
	ServerPostback:"ServerPostback"
};
//
// Method
// WebCounter.Utils.marshallObj()
WebCounter.Utils.marshallObj = function WebCounter_Utils_marshallObj(obj)
{
    var m = new Array();
    if (!obj)
        return m;
    if ("object" != typeof(obj))
		obj = {__default: obj};
    for (key in obj)
    {
        var item = obj[key];
        if (null == item)
            item = "";
        if (!item.isPrototypeOf(obj))
        {
            switch (typeof(item))
            {
                case "number":m[key] = item;break;
                case "boolean":m[key] = item;break;
                case "string":m[key] = item.toString();break;
                case "object":m[key] = WebCounter.Utils.MarshallObj(item);break;
                case "function":break;
                case "undefined":break;
                default:m[key] = item.toString();break;
            }
        }
    }
    return m;
};
//
// Class
// WebCounter.Flex.FxApplication
WebCounter.Flex.FxApplication = function WebCounter_Flex_FxApplication(bridgeId,instance,_window,clientId){
	this._bridgeId = bridgeId;
	this._instance = instance;
	this._window = _window;
	if (!clientId)
	{
		this.clientId = null;
		if (_window)
		{
			if (_window.frameElement)
				this.clientId = _window.frameElement.id;
		}
	}
	else
	{
		this.clientId = clientId;
	}
};
//
// Method
// WebCounter.Flex.FxApplication.getInstance()
function WebCounter_Flex_FxApplication_getInstance() {
	return this._instance;
};
//
// Method 
// WebCounter.Flex.FxApplication.postCommand()
function WebCounter_Flex_FxApplication_postCommand(command) {
	if (!this._window.__postCommand)return;
	this._window.__postCommand(command);
};
//
// Method
// WebCounter.Flex.FxApplication.registerEventHandler()
function WebCounter_Flex_FxApplication_registerEventHandler(eventType,handlerFn) {
	if (!eventType) return;
	if ("function" != typeof(handlerFn)) return;
	this._instance.addEventListener(eventType.toString(), handlerFn);
};
//
// Prototype
// WebCounter.Flex.FxApplication
WebCounter.Flex.FxApplication.prototype = {
	getInstance: WebCounter_Flex_FxApplication_getInstance,
	postCommand: WebCounter_Flex_FxApplication_postCommand,
	registerEventHandler: WebCounter_Flex_FxApplication_registerEventHandler
};
//
// Method
// WebCounter.Flex.getFxApplication()
WebCounter.Flex.getFxApplication = function WebCounter_Flex_getFxApplication(_window){
	var bridge = _window.FABridge;
	if (!bridge) return null;
	if (!bridge.__FxApplication) return null;
	if (!bridge.__FxApplication.root) return null;
	return bridge.__FxApplication.root();
};
//
// Method
// WebCounter.Flex.startFxApplication()
WebCounter.Flex.startFxApplication = function WebCounter_Flex_startFxApplication(_window,appParams,clientId){
	var appInstance = WebCounter.Flex.getFxApplication(_window);
	if (!appInstance) return null;
	if ("function" != typeof(appInstance.__StartApplication))
		return null;
	var fxApp = new WebCounter.Flex.FxApplication("__FxApplication",appInstance,_window,clientId);
	_window.__FxApplication = fxApp;
	//Solo funciona en explorer
	appParams["dpi"] = screen.deviceXDPI;
	appInstance.__StartApplication(appParams);
	return fxApp;
};
//
// Method
// WebCounter.Flex.processFxCommand()
WebCounter.Flex.processFxCommand = function WebCounter_Flex_processFxCommand(fxApp,command){
	if (!fxApp) return false;
	var appInstance = fxApp.getInstance();
	if (!appInstance) return false;
	if ("function" != typeof(appInstance.__ProcessCommand))
		return false;
	appInstance.__ProcessCommand(command);
	return true;
};
//
// Method
// WebCounter.Flex.invokeFxCommand()
WebCounter.Flex.invokeFxCommand = function WebCounter_Flex_invokeFxCommand(fxApp,command){
	if (!fxApp) return null;
	var appInstance = fxApp.getInstance();
	if (!appInstance) return null;
	if ("function" != typeof(appInstance.__ProcessCommand))
		return null;
	return appInstance.__ProcessCommand(command);
};
//
// Class
// WebCounter.Command
WebCounter.Command = function WebCounter_Command(commandId,params){
	this.id = commandId;
	this.params = params;
};
//
// Method
// WebCounter.getApplication()
WebCounter.getApplication = function WebCounter_getApplication(clientId) {
	if (!clientId) return null;
	var appWindow;
	if (document.getElementById(clientId))
	{
		appWindow = window;
	}
	else
	{
		if(document.frames)
			appWindow = document.frames[clientId];
		if(!appWindow){
			var l1 = document.getElementsByTagName("IFRAME");
			for ( var i1=0; i1<l1.length; ++i1 ){
				var f1 = l1[i1];
				if (clientId == f1.id)
				{
					appWindow = f1.contentWindow;
					break;
				}
			}
		}
	}
	if(!appWindow) return null;
	var app = appWindow.__FxApplication;
	if(!app) return null;
	return app;
};
//
// Method
// WebCounter.createCommand()
WebCounter.createCommand = function WebCounter_createCommand(commandId,params) {
	return new WebCounter.Command(commandId,params);
};
//
// Method
// WebCounter.postApplicationCommand()
WebCounter.postApplicationCommand = function WebCounter_postApplicationCommand(clientId,commandId,params) {
	var app = WebCounter.getApplication(clientId);
	if (!app)
		throw new Error(0,"Flex application not found [" + clientId +"]");
	var cmd = WebCounter.createCommand(commandId,params);
	app.postCommand( cmd );
};

