text changes to registration mail content
[namibia] / public / js / vendor / ape.js
1 var APE = {
2         Config: {
3                 identifier: 'ape',
4                 init: true,
5                 frequency: 0,
6                 scripts: []
7         },
8
9         Client: function(core) {
10                         if(core) this.core = core;      
11         }
12 }
13 APE.Client.prototype.eventProxy = [];
14 APE.Client.prototype.fireEvent = function(type, args, delay) {
15         this.core.fireEvent(type, args, delay);
16 }
17
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]);
22         }else{
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;
26         }
27         return ret;
28 }
29 APE.Client.prototype.removeEvent = function(type, fn) {
30         return this.core.removeEvent(type, fn);
31 }
32
33 APE.Client.prototype.onRaw = function(type, fn, internal) {
34                 this.addEvent('raw_' + type.toLowerCase(), fn, internal); 
35 }
36
37 APE.Client.prototype.onCmd = function(type, fn, internal) {
38                 this.addEvent('cmd_' + type.toLowerCase(), fn, internal); 
39 }
40
41 APE.Client.prototype.onError = function(type, fn, internal) {
42                 this.addEvent('error_' + type, fn, internal); 
43 }
44
45 APE.Client.prototype.cookie = {};
46
47 APE.Client.prototype.cookie.write = function (name, value) {
48         document.cookie = name + "=" + encodeURIComponent(value) + "; domain=" + document.domain;
49 }
50
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++) {
55                 var c = ca[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));
59                 }
60         }
61         return null;
62 }
63
64 APE.Client.prototype.load = function(config){
65
66         config = config || {};
67
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;
74
75         config.init = function(core){
76                 this.core = core;
77                 for(var i = 0; i < this.eventProxy.length; i++){
78                         this.addEvent.apply(this, this.eventProxy[i]);
79                 }
80         }.bind(this);
81
82         //set document.domain
83         if (config.transport != 2) {
84                 if (config.domain != 'auto') document.domain = config.domain;
85                 if (config.domain == 'auto') document.domain = document.domain;
86         }
87
88         //Get APE cookie
89         var cookie = this.cookie.read('APE_Cookie');
90         var tmp = eval('(' + cookie + ')');
91
92         if (tmp) {
93                 config.frequency = tmp.frequency+1;
94         } else {
95                 cookie = '{"frequency":0}';
96         }
97
98         var reg = new RegExp('"frequency":([ 0-9]+)' , "g")
99         cookie = cookie.replace(reg, '"frequency":' + config.frequency);
100         this.cookie.write('APE_Cookie', cookie);
101
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';
108
109         document.body.insertBefore(iframe,document.body.childNodes[0]);
110
111         var initFn = function() {
112                 iframe.contentWindow.APE.init(config);
113         }
114
115         if (iframe.addEventListener) {
116                 iframe.addEventListener('load', initFn, false);
117         } else if (iframe.attachEvent) {
118                 iframe.attachEvent('onload', initFn);
119         }
120
121         if (config.transport == 2) {
122                 var doc = iframe.contentDocument;
123                 if (!doc) doc = iframe.contentWindow.document;//For IE
124
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
127                 doc.open();
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>';
131                 }
132                 theHtml += '</head><body></body></html>';
133                 doc.write(theHtml);
134                 doc.close();
135         } else {
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');
142                 }
143         }
144
145 }
146
147 if (Function.prototype.bind == null) {
148         Function.prototype.bind = function(bind, args) {
149                 return this.create({'bind': bind, 'arguments': args});
150         }
151 }
152 if (Function.prototype.create == null) {
153         Function.prototype.create = function(options) {
154                         var self = this;
155                         options = options || {};
156                         return function(){
157                                 var args = options.arguments || arguments;
158                                 if(args && !args.length){
159                                         args = [args];
160                                 }
161                                 var returns = function(){
162                                         return self.apply(options.bind || null, args);
163                                 };
164                                 return returns();
165                         };
166         }
167 }
168