9 Client: function(core) {
10 if(core) this.core = core;
13 APE.Client.prototype.eventProxy = [];
14 APE.Client.prototype.fireEvent = function(type, args, delay) {
15 this.core.fireEvent(type, args, delay);
18 APE.Client.prototype.addEvent = function(type, fn, internal) {
19 var newFn = fn.bind(this), ret = this;
20 if(this.core == undefined){
21 this.eventProxy.push([type, fn, internal]);
23 var ret = this.core.addEvent(type, newFn, internal);
24 this.core.$originalEvents[type] = this.core.$originalEvents[type] || [];
25 this.core.$originalEvents[type][fn] = newFn;
29 APE.Client.prototype.removeEvent = function(type, fn) {
30 return this.core.removeEvent(type, fn);
33 APE.Client.prototype.onRaw = function(type, fn, internal) {
34 this.addEvent('raw_' + type.toLowerCase(), fn, internal);
37 APE.Client.prototype.onCmd = function(type, fn, internal) {
38 this.addEvent('cmd_' + type.toLowerCase(), fn, internal);
41 APE.Client.prototype.onError = function(type, fn, internal) {
42 this.addEvent('error_' + type, fn, internal);
45 APE.Client.prototype.cookie = {};
47 APE.Client.prototype.cookie.write = function (name, value) {
48 document.cookie = name + "=" + encodeURIComponent(value) + "; domain=" + document.domain;
51 APE.Client.prototype.cookie.read = function (name) {
52 var nameEQ = name + "=";
53 var ca = document.cookie.split(';');
54 for(var i=0;i < ca.length;i++) {
56 while (c.charAt(0)==' ') c = c.substring(1,c.length);
57 if (c.indexOf(nameEQ) == 0){
58 return decodeURIComponent(c.substring(nameEQ.length,c.length));
64 APE.Client.prototype.load = function(config){
66 config = config || {};
68 config.transport = config.transport || APE.Config.transport || 0;
69 config.frequency = config.frequency || 0;
70 config.domain = config.domain || APE.Config.domain || document.domain;
71 config.scripts = config.scripts || APE.Config.scripts;
72 config.server = config.server || APE.Config.server;
73 config.secure = config.sercure || APE.Config.secure;
75 config.init = function(core){
77 for(var i = 0; i < this.eventProxy.length; i++){
78 this.addEvent.apply(this, this.eventProxy[i]);
83 if (config.transport != 2) {
84 if (config.domain != 'auto') document.domain = config.domain;
85 if (config.domain == 'auto') document.domain = document.domain;
89 var cookie = this.cookie.read('APE_Cookie');
90 var tmp = eval('(' + cookie + ')');
93 config.frequency = tmp.frequency+1;
95 cookie = '{"frequency":0}';
98 var reg = new RegExp('"frequency":([ 0-9]+)' , "g")
99 cookie = cookie.replace(reg, '"frequency":' + config.frequency);
100 this.cookie.write('APE_Cookie', cookie);
102 var iframe = document.createElement('iframe');
103 iframe.setAttribute('id','ape_' + config.identifier);
104 iframe.style.display = 'none';
105 iframe.style.position = 'absolute';
106 iframe.style.left = '-300px';
107 iframe.style.top = '-300px';
109 document.body.insertBefore(iframe,document.body.childNodes[0]);
111 var initFn = function() {
112 iframe.contentWindow.APE.init(config);
115 if (iframe.addEventListener) {
116 iframe.addEventListener('load', initFn, false);
117 } else if (iframe.attachEvent) {
118 iframe.attachEvent('onload', initFn);
121 if (config.transport == 2) {
122 var doc = iframe.contentDocument;
123 if (!doc) doc = iframe.contentWindow.document;//For IE
125 //If the content of the iframe is created in DOM, the status bar will always load...
126 //using document.write() is the only way to avoid status bar loading with JSONP
128 var theHtml = '<html><head>';
129 for (var i = 0; i < config.scripts.length; i++) {
130 theHtml += '<script type="text/JavaScript" src="' + config.scripts[i] + '"></script>';
132 theHtml += '</head><body></body></html>';
136 //iframe.setAttribute('src',(config.secure ? 'https': 'http') + '://' + config.frequency + '.' + config.server + '/?[{"cmd":"script","params":{"domain":"' + document.domain +'","scripts":["' + config.scripts.join('","') + '"]}}]');
137 iframe.setAttribute('src',(config.secure ? 'https': 'http') + '://' + config.server + '/?[{"cmd":"script","params":{"domain":"' + document.domain +'","scripts":["' + config.scripts.join('","') + '"]}}]');
138 if (navigator.product == 'Gecko') {
139 //Firefox fix, see bug #356558
140 // https://bugzilla.mozilla.org/show_bug.cgi?id=356558
141 iframe.contentWindow.location.href = iframe.getAttribute('src');
147 if (Function.prototype.bind == null) {
148 Function.prototype.bind = function(bind, args) {
149 return this.create({'bind': bind, 'arguments': args});
152 if (Function.prototype.create == null) {
153 Function.prototype.create = function(options) {
155 options = options || {};
157 var args = options.arguments || arguments;
158 if(args && !args.length){
161 var returns = function(){
162 return self.apply(options.bind || null, args);