1 | (function() |
---|
2 | { |
---|
3 | |
---|
4 | var _cookies = new IMCookies; |
---|
5 | var _running = _cookies.get('IM_running'); |
---|
6 | if ( !_running ) |
---|
7 | { |
---|
8 | _cookies.set('IM_running', 'true', 5); |
---|
9 | |
---|
10 | function _config(pObj, pEvent, pHandler) |
---|
11 | { |
---|
12 | if ( typeof pObj == 'object' ) |
---|
13 | { |
---|
14 | if ( pEvent.substring(0, 2) == 'on' ) |
---|
15 | pEvent = pEvent.substring(2, pEvent.length); |
---|
16 | |
---|
17 | if ( pObj.addEventListener ) |
---|
18 | pObj.addEventListener(pEvent, pHandler, false); |
---|
19 | else if ( pObj.attachEvent ) |
---|
20 | pObj.attachEvent('on' + pEvent, pHandler); |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | _config(window, 'onbeforeunload', |
---|
25 | function() |
---|
26 | { |
---|
27 | var _running = _cookies.get('IM_running'); |
---|
28 | _cookies.clear('IM_running'); |
---|
29 | } |
---|
30 | ); |
---|
31 | |
---|
32 | if ( !(top.document.getElementById('instant_messenger_content')) ) |
---|
33 | { |
---|
34 | var _im_content = top.document.createElement('div'); |
---|
35 | _im_content.setAttribute('id', 'instant_messenger_content'); |
---|
36 | _im_content = top.document.body.appendChild(_im_content); |
---|
37 | } |
---|
38 | |
---|
39 | var buffer = ''; |
---|
40 | var _disconnected = false; |
---|
41 | |
---|
42 | var _conn = new IMConnector(path_im); |
---|
43 | var _xtools = new XTools(path_im); |
---|
44 | var _window = new Windows(_xtools, _im_content); |
---|
45 | var _ldap = new InstantMessengerLDAP(_conn, _xtools, _window); |
---|
46 | var _showhidden = new ShowHidden(1500); |
---|
47 | var _jabber = new Jabber(_conn, _xtools, _window, _ldap, _showhidden, _cookies); |
---|
48 | |
---|
49 | // XSL preload |
---|
50 | var _xsl_preload = [ |
---|
51 | 'window.xsl', |
---|
52 | 'layer.xsl', |
---|
53 | 'contacts.xsl', |
---|
54 | 'chat.xsl', |
---|
55 | 'options.xsl' |
---|
56 | ]; |
---|
57 | var _xsl_preload_legth = _xsl_preload.length; |
---|
58 | for ( var i = 0; i < _xsl_preload_legth; i++ ) |
---|
59 | _xtools.load(_xsl_preload[i]); |
---|
60 | |
---|
61 | function _stream(data) |
---|
62 | { |
---|
63 | data = data.replace(/^ | $/, ''); |
---|
64 | |
---|
65 | if ( !(data.length) ) |
---|
66 | return false; |
---|
67 | |
---|
68 | if ( data.indexOf('disconnected') != -1 || data.indexOf('</stream:stream>') != -1 ) |
---|
69 | _disconnect(); |
---|
70 | else |
---|
71 | { |
---|
72 | if ( data.lastIndexOf('>') != data.length - 1) |
---|
73 | buffer += data + ' '; |
---|
74 | else |
---|
75 | { |
---|
76 | if ( buffer.length ) |
---|
77 | { |
---|
78 | data = buffer + data; |
---|
79 | buffer = ''; |
---|
80 | } |
---|
81 | |
---|
82 | // with xmlns, occurs problems during parse between |
---|
83 | // xml and xsl, and for that reason it was removed |
---|
84 | data = data.replace(/ xmlns='.*?'/g, ''); |
---|
85 | data = '<handler>' + data + '</handler>'; |
---|
86 | |
---|
87 | var _xml = _xtools.convert(data); |
---|
88 | |
---|
89 | if ( _xml ) |
---|
90 | { |
---|
91 | var _node = _xml.documentElement.firstChild; |
---|
92 | |
---|
93 | while ( _node ) |
---|
94 | { |
---|
95 | if ( _node.nodeType == 1 ) |
---|
96 | _jabber.action(_node); |
---|
97 | //else |
---|
98 | // alert('name : ' + _node.nodeName + |
---|
99 | // "\n\nhas children:" + _node.hasChildNodes() + |
---|
100 | // "\nvalue\n\n" + _node.nodeValue); |
---|
101 | |
---|
102 | _node = _node.nextSibling; |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | var _count_reconnect = 0; |
---|
110 | function _request() |
---|
111 | { |
---|
112 | if ( !_disconnected ) |
---|
113 | return _conn.go( |
---|
114 | '$this.Ujabber.listen', |
---|
115 | {'stream':_stream, 'request':_wait}, |
---|
116 | 'classCostructor=read' |
---|
117 | ); |
---|
118 | else |
---|
119 | setTimeout(_reconnect, (++_count_reconnect * 2000)); |
---|
120 | } |
---|
121 | |
---|
122 | function _wait() |
---|
123 | { |
---|
124 | setTimeout(_request, 3000); |
---|
125 | } |
---|
126 | |
---|
127 | function _reconnect() |
---|
128 | { |
---|
129 | _disconnected = false; |
---|
130 | _request(); |
---|
131 | } |
---|
132 | |
---|
133 | function _disconnect() |
---|
134 | { |
---|
135 | _disconnected = true; |
---|
136 | _conn.abort(); |
---|
137 | _loading(); |
---|
138 | var _status = top.document.getElementById('im_status'); |
---|
139 | if ( _status ) |
---|
140 | _status.style.backgroundImage = 'url(' + im_unavailable.src + ')'; |
---|
141 | |
---|
142 | var _win_contacts = _window.get('contact_list'); |
---|
143 | if ( _win_contacts ) |
---|
144 | _win_contacts.icon(im_unavailable.src); |
---|
145 | } |
---|
146 | |
---|
147 | function _contacts() |
---|
148 | { |
---|
149 | if ( !_disconnected ) |
---|
150 | { |
---|
151 | _conn.go('t.update'); |
---|
152 | |
---|
153 | setTimeout(function() |
---|
154 | { |
---|
155 | var _contact_list = top.document.getElementById('im_contact_list'); |
---|
156 | if ( !_contact_list ) |
---|
157 | _contacts(); |
---|
158 | }, 5000); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | function _loading() |
---|
163 | { |
---|
164 | var _win_contact = _window.get('contact_list'); |
---|
165 | if ( !_win_contact ) |
---|
166 | { |
---|
167 | _win_contact = _window.load('contact_list'); |
---|
168 | _win_contact.hidden(); |
---|
169 | _win_contact.title('.::Expresso Messenger::.'); |
---|
170 | _win_contact.size(170, 350); |
---|
171 | _win_contact.position(30, 30, true); |
---|
172 | _win_contact.icon(im_unavailable.src); |
---|
173 | _win_contact.bc.style.display = 'none'; |
---|
174 | } |
---|
175 | |
---|
176 | _win_contact.loading(); |
---|
177 | } |
---|
178 | |
---|
179 | function _showContacts() |
---|
180 | { |
---|
181 | var _win = _window.get('contact_list'); |
---|
182 | if ( _win ) |
---|
183 | if ( _win.wm.style.display == 'none' ) |
---|
184 | _win.wm.style.display = 'block'; |
---|
185 | else |
---|
186 | _win.wm.style.display = 'none'; |
---|
187 | } |
---|
188 | |
---|
189 | _loading(); |
---|
190 | |
---|
191 | var local; |
---|
192 | if ( (local = top.document.getElementById('divStatusBar')) ) |
---|
193 | { |
---|
194 | // tentando colocar os icones em local que não seja mexido. |
---|
195 | // VERIFICAR |
---|
196 | var _div = top.document.createElement('div'); |
---|
197 | _div.appendChild(local.parentNode.removeChild(local.previousSibling)); |
---|
198 | local.parentNode.insertBefore(_div, local); |
---|
199 | //_div.appendChild(local); |
---|
200 | |
---|
201 | //local = _div; |
---|
202 | |
---|
203 | local.style.paddingLeft = '30px'; |
---|
204 | |
---|
205 | var _status = top.document.createElement('div'); |
---|
206 | _status.setAttribute('id', 'im_status'); |
---|
207 | _status.style.background = 'no-repeat'; |
---|
208 | _status.style.backgroundImage = 'url(' + im_unavailable.src + ')'; |
---|
209 | _status.style.float = 'left'; |
---|
210 | _status.style.height = '15px'; |
---|
211 | _status.style.left = '20px'; |
---|
212 | _status.style.margin = '0 0 0 10px'; |
---|
213 | _status.style.padding = '0px'; |
---|
214 | _status.style.position = 'absolute'; |
---|
215 | _status.style.width = '15px'; |
---|
216 | |
---|
217 | local.insertBefore(_status, local.firstChild); |
---|
218 | |
---|
219 | _config(_status, 'onclick', _showContacts); |
---|
220 | |
---|
221 | var _menu; |
---|
222 | _menu = top.document.createElement('span'); |
---|
223 | _menu.setAttribute('id','fast_menu_im'); |
---|
224 | _menu.style.backgroundColor = 'b4cfe5'; |
---|
225 | _menu.style.display = 'none'; |
---|
226 | _menu.style.margin = '15px 0 0 -15px'; |
---|
227 | _menu.style.position = 'absolute'; |
---|
228 | _menu.style.zIndex = '99999'; |
---|
229 | local.insertBefore(_menu, local.firstChild); |
---|
230 | |
---|
231 | var _menu_img = top.document.createElement('div'); |
---|
232 | _menu_img.style.background = 'no-repeat'; |
---|
233 | _menu_img.style.backgroundImage = 'url(' + im_fast_menu.src + ')'; |
---|
234 | _menu_img.style.float = 'left'; |
---|
235 | _menu_img.style.height = '10px'; |
---|
236 | _menu_img.style.left = '23px'; |
---|
237 | _menu_img.style.margin = '0px'; |
---|
238 | _menu_img.style.padding = '0px'; |
---|
239 | _menu_img.style.position = 'absolute'; |
---|
240 | _menu_img.style.width = '10px'; |
---|
241 | local.insertBefore(_menu_img, local.firstChild); |
---|
242 | |
---|
243 | _config( |
---|
244 | _menu_img, |
---|
245 | 'onclick', |
---|
246 | function() |
---|
247 | { |
---|
248 | var _options = [ |
---|
249 | 'available', |
---|
250 | 'away', |
---|
251 | 'dnd', |
---|
252 | 'unavailable' |
---|
253 | ]; |
---|
254 | |
---|
255 | var _style_text = 'cursor: pointer; padding:2px 4px 2px 20px; background: no-repeat '; |
---|
256 | |
---|
257 | var _xml = _xtools.xml('menu'); |
---|
258 | var _option; |
---|
259 | var _item; |
---|
260 | var _action; |
---|
261 | var _style; |
---|
262 | |
---|
263 | for ( var i in _options ) |
---|
264 | { |
---|
265 | _option = _xml.createElement('option'); |
---|
266 | |
---|
267 | _item = _xml.createElement('item'); |
---|
268 | _item.appendChild(_xml.createTextNode(imGetLang(_options[i]))); |
---|
269 | |
---|
270 | _style = _xml.createElement('style'); |
---|
271 | _style.appendChild(_xml.createTextNode(_style_text + 'url(' + eval('im_' + _options[i] + '.src') + ')')); |
---|
272 | |
---|
273 | _option.appendChild(_item); |
---|
274 | _option.appendChild(_style); |
---|
275 | |
---|
276 | _xml.documentElement.appendChild(_option); |
---|
277 | } |
---|
278 | |
---|
279 | _menu.innerHTML = _xtools.parse(_xml, 'options.xsl'); |
---|
280 | _showhidden.action('onmouseover', 'onmouseout', _menu); |
---|
281 | |
---|
282 | _item = _menu.firstChild; |
---|
283 | |
---|
284 | for ( var i in _options ) |
---|
285 | { |
---|
286 | _item.childNodes[i].setAttribute('presence', _options[i]); |
---|
287 | _config( |
---|
288 | _item.childNodes[i], |
---|
289 | 'onclick', |
---|
290 | function(e) |
---|
291 | { |
---|
292 | var el = ( e.target ) ? e.target : e.srcElement; |
---|
293 | var _presence = el.getAttribute('presence'); |
---|
294 | _jabber.setPresence(_presence); |
---|
295 | } |
---|
296 | ); |
---|
297 | } |
---|
298 | } |
---|
299 | ); |
---|
300 | } |
---|
301 | |
---|
302 | function Client() |
---|
303 | { |
---|
304 | _request(); |
---|
305 | setTimeout(_contacts, 3000); |
---|
306 | } |
---|
307 | |
---|
308 | window.InstantMessengerClient = Client; |
---|
309 | } |
---|
310 | } |
---|
311 | )(); |
---|