Ti.App = {};
Ti.API = {};
Ti.App._listeners = {};
Ti.App._listener_id = 1;
Ti.App.id = Ti.appId;
Ti.App._xhr = XMLHttpRequest;
Ti._broker = function(module,method,data)
{
	try
	{
		var url = 'app://'+Ti.appId+'/_TiA0_'+Ti.pageToken+'/'+module+'/'+method+'?'+Ti.App._JSON(data,1);
		var xhr = new Ti.App._xhr();
		xhr.open('GET',url,false);
		xhr.send();
	}
	catch(X)
	{
	}	
};
Ti._hexish = function(a){var r='';var e=a.length;var c=0;var h;while(c<e){h=a.charCodeAt(c++).toString(16);r+='\\\\u';var l=4-h.length;while(l-->0){r+='0'};r+=h;}return r;};
Ti._bridgeEnc = function(o){return '<'+Ti._hexish(o)+'>';};
Ti.App._JSON = function(object,bridge)
{
		var type = typeof object;
		switch (type) 
		{
			case 'undefined': case 'function': case 'unknown': return undefined;
			case 'number': case 'boolean': return object;
			case 'string': if (bridge===1) return Ti._bridgeEnc(object); return '\"'+object.replace(/\"/g,'\\\\\"').replace(/\\n/g,'\\\\n').replace(/\\r/g,'\\\\r')+'\"';
		}
		if((object===null) || (object.nodeType==1)) return 'null';
		if(object.constructor.toString().indexOf('Date') != -1) 
		{
			return 'new Date(' + object.getTime() + ')';
		}
		if(object.constructor.toString().indexOf('Array') != -1) {
			var res='[';var pre='';var len=object.length;
			for(var i=0;i<len;i++){
				var value = object[i];
				if(value !== undefined)value=Ti.App._JSON(value,bridge);
				if(value !== undefined){res+=pre+value;pre=', ';}
			}
			return res + ']';
		}
		var objects = [];
		for (var prop in object)
		{
			var value = object[prop];
			if (value !== undefined){value = Ti.App._JSON(value,bridge);}
			if (value !== undefined){objects.push(Ti.App._JSON(prop,bridge) + ': ' + value);}
		}
		return '{' + objects.join(',') + '}';
};
Ti.App._dispatchEvent = function(type,evtid,evt)
{
	var listeners = Ti.App._listeners[type];
	if (listeners)
	{
		for (var c=0;c<listeners.length;c++)
		{
			var entry = listeners[c];
			if (entry.id==evtid)
			{
				entry.callback.call(entry.callback,evt);
			}
		}
	}			
};
Ti.App.fireEvent = function(name,evt)
{
	Ti._broker('App','fireEvent',{name:name,event:evt});
};	
Ti.API.log = function(a,b)
{
    Ti._broker('API','log',{level:a,message:b});
};
Ti.API.debug = function(e)
{
	Ti._broker('API','log',{level:'debug',message:e});
};
Ti.API.error = function(e)
{
	Ti._broker('API','log',{level:'error',message:e});
};
Ti.API.info = function(e)
{
	Ti._broker('API','log',{level:'info',message:e});
};
Ti.API.fatal = function(e)
{
	Ti._broker('API','log',{level:'fatal',message:e});
};
Ti.API.warn = function(e)
{
	Ti._broker('API','log',{level:'warn',message:e});
};
Ti.App.addEventListener = function(name,fn)
{
	var listeners = Ti.App._listeners[name];
	if (typeof(listeners)=='undefined')
	{
		listeners = [];
		Ti.App._listeners[name] = listeners;
	}
	var newid = Ti.pageToken + Ti.App._listener_id++;
	listeners.push({callback:fn,id:newid});
	Ti._broker('App','addEventListener',{name:name,id:newid});
};
Ti.App.removeEventListener = function(name,fn)
{
	var listeners = Ti.App._listeners[name];
	if (listeners)
	{
		for (var c=0;c<listeners.length;c++)
		{
			var entry = listeners[c];
			if (entry.callback == fn)
			{
				listeners.splice(c,1);
				Ti._broker('App','removeEventListener',{name:name,id:entry.id});
				break;
			}
		}
	}
};