10 APE.Client = new Class({
14 fireEvent: function(type, args, delay){
15 return this.core.fireEvent(type, args, delay);
18 addEvent: function(type, fn, internal){
19 var newFn = fn.bind(this), ret = this;
20 if(!$defined(this.core)) this.eventProxy.push([type, fn, internal]);
22 ret = this.core.addEvent(type, newFn, internal);
23 this.core.$originalEvents[type] = this.core.$originalEvents[type] || [];
24 this.core.$originalEvents[type][fn] = newFn;
29 onRaw: function(type, fn, internal) {
30 return this.addEvent('raw_' + type.toLowerCase(), fn, internal);
33 removeEvent: function(type, fn) {
34 return this.core.removeEvent(type, fn);
37 onCmd: function(type, fn, internal) {
38 return this.addEvent('cmd_' + type.toLowerCase(), fn, internal);
41 onError: function(type, fn, internal) {
42 return this.addEvent('error_' + type, fn, internal);
45 load: function(config){
46 config = $merge({}, APE.Config, config);
48 // Init function called by core to init core variable
49 config.init = function(core){
51 for(var i = 0; i < this.eventProxy.length; i++){
52 this.addEvent.apply(this, this.eventProxy[i]);
57 if (config.transport != 2) {
58 if (config.domain != 'auto') document.domain = config.domain;
59 if (config.domain == 'auto') document.domain = document.domain;
62 var tmp = JSON.decode(Cookie.read('APE_Cookie'), {'domain': document.domain});
65 config.frequency = tmp.frequency.toInt();
67 tmp = {'frequency': 0};
70 tmp.frequency = config.frequency + 1;
72 Cookie.write('APE_Cookie', JSON.encode(tmp), {'domain': document.domain});
74 var iframe = new Element('iframe', {
75 id: 'ape_' + config.identifier,
82 }).inject(document.body);
84 iframe.addEvent('load', function() {
85 if (!iframe.contentWindow.APE) setTimeout(iframe.onload, 100);//Sometimes IE fire the onload event, but the iframe is not loaded -_-
86 else iframe.contentWindow.APE.init(config);
89 if (config.transport == 2) {//Special case for JSONP
90 var doc = iframe.contentDocument;
91 if (!doc) doc = iframe.contentWindow.document;
93 //If the content of the iframe is created in DOM, the status bar will always load...
94 //using document.write() is the only way to avoid status bar loading with JSONP
96 var theHtml = '<html><head>';
97 for (var i = 0; i < config.scripts.length; i++) {
98 theHtml += '<script src="' + config.scripts[i] + '"></script>';
100 theHtml += '</head><body></body></html>';
104 iframe.set('src', (config.secure ? 'https' : 'http') + '://' + config.frequency + '.' + config.server + '/?[{"cmd":"script","params":{"domain":"' + document.domain + '","scripts":["' + config.scripts.join('","') + '"]}}]');
105 if (Browser.Engine.gecko) {
106 // Firefox fix, see bug #356558
107 // https://bugzilla.mozilla.org/show_bug.cgi?id=356558
108 iframe.contentWindow.location.href = iframe.get('src');