Changed zouba directory heirarchy.
[ptas] / zouba / misc / rss / preview / script / lib / loader.js
1 /*
2  * Emulator, which manages the device interacations
3  */
4 if (typeof _BRIDGE_REF == "undefined" || !_BRIDGE_REF) {
5
6         var _BRIDGE_REF = {
7                 parent: window.parent || false,
8                 nokia: window.parent.NOKIA || false,
9                 sysInfoObject : null
10         };
11
12         _BRIDGE_REF.namespace = function(name){
13                 var parts = name.split('.');
14                 var current = _BRIDGE_REF;
15                 for (var key in parts) {
16                         if (!current[parts[key]]) {
17                                 current[parts[key]] = {};
18                         }
19                         current = current[parts[key]];
20                 }
21         };
22         
23         /*
24          * _BRIDGE_REF.helper functions
25          */
26         _BRIDGE_REF.namespace('helper.loadScript');
27         _BRIDGE_REF.helper = {
28                 path: document.location.pathname,
29                 loadScript: function(path){
30                         var head = document.getElementsByTagName("head")[0] || document.documentElement;
31                         var script = document.createElement("script");
32                         
33                         script.type = "text/javascript";
34                         script.src = path;
35                         head.appendChild(script);
36                 },
37                 
38                 createCookie: function(name, value){
39                         var days = 240000;
40                         if (days) {
41                                 var date = new Date();
42                                 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
43                                 var expires = "; expires=" + date.toGMTString();
44                         }
45                         else 
46                                 var expires = "";
47                         var value = "Nokia_WRT#" + _BRIDGE_REF.helper.path + "#" + name + "=" + value;
48                         document.cookie = value + expires + "; Emulator.path=/"
49                 },
50                 
51                 readCookie: function(name){
52                         name = "Nokia_WRT#" + _BRIDGE_REF.helper.path + "#" + name;
53                         var nameEQ = name + "=";
54                         var ca = document.cookie.split(';');
55                         for (var i = 0; i < ca.length; i++) {
56                                 var c = ca[i];
57                                 while (c.charAt(0) == ' ') 
58                                         c = c.substring(1, c.length);
59                                 if (c.indexOf(nameEQ) == 0) {
60                                         return c.substring(nameEQ.length, c.length);
61                                 }
62                         }
63                         return undefined;
64                 },
65                 
66                 updateMainCookie: function(doucment){
67                         var temp = "";
68                         name = "Nokia_WRT#" + widget.path;
69                         for (var k = 0; k < widget.preferenceArray.length; k++) {
70                                 temp = temp + "|" + widget.preferenceArray[k];
71                         }
72                         this.createCookie(document, name, temp, 24000);
73                 },
74                 
75                 addEvent: function(obj, type, fn){
76                         if (obj.addEventListener) {
77                                 obj.addEventListener(type, fn, false);
78                         }
79                         else 
80                                 if (obj.attachEvent) {
81                                         obj["e" + type + fn] = fn;
82                                         obj[type + fn] = function(){
83                                                 obj["e" + type + fn](window.event);
84                                         }
85                                         obj.attachEvent("on" + type, obj[type + fn]);
86                                 }
87                 },
88                 
89                 getElementsLengthInObject : function(items){
90                         var count = 0;
91                         for (var i in items) 
92                                 count++;
93                         
94                         return count;
95                 },
96                 
97                 getBatteryStrength : function(){
98                         
99 /*                      if(!_BRIDGE_REF.sysInfoObject)
100                                 _BRIDGE_REF.sysInfoObject = device.getServiceObject("Service.SysInfo", "ISysInfo");
101                         
102                         var result = _BRIDGE_REF.sysInfoObject.ISysInfo.GetInfo({
103                                 Entity  : 'Battery',
104                                 Key             :       'ChargingStatus'
105                         });
106                         
107                         if(!result.ErrorCode)
108                         {
109                                 return result.Status;
110                         }else
111                         {
112                                 alert('Error in getting Battery Strength: '+result.ErrorCode);
113                                 return 0;
114                         }
115                         if(typeof device.implementation.data["Service.SysInfo"].default.battery.batterystrength.Status != 'undefined')
116                                 return device.implementation.data["Service.SysInfo"].default.battery.batterystrength.Status;
117                         else
118                                 return 0;
119 */
120                 },
121                 
122                 console : function(){
123                         if (!typeof window.console) {
124                                 _BRIDGE_REF.helper.loadScript("preview/script/lib/console.js");
125                         }                       
126                 }
127                 
128         };
129         
130         
131         /*
132          Load Scripts
133          */
134         _BRIDGE_REF.helper.loadScript("preview/script/lib/widget.js");
135         _BRIDGE_REF.helper.loadScript("preview/script/lib/systeminfo.js");
136         _BRIDGE_REF.helper.loadScript("preview/script/lib/menu.js");
137         _BRIDGE_REF.helper.loadScript("preview/script/lib/menuItem.js");
138         _BRIDGE_REF.helper.loadScript("preview/script/lib/console.js");
139
140         //      Inject SAPI scripts     
141         if (_BRIDGE_REF.nokia) {
142                 var wrtVersion = _BRIDGE_REF.nokia.helper.readCookie('_WRT_VERSION');
143                 if ((typeof wrtVersion == 'undefined') || (wrtVersion == 'WRT 1.1')) {
144                         _BRIDGE_REF.nokia.version = 'WRT 1.1';
145                         _BRIDGE_REF.nokia.helper.createCookie('_WRT_VERSION', 'WRT 1.1');
146                         _BRIDGE_REF.helper.loadScript("preview/script/lib/device.js");
147                 }
148                 else {
149                         _BRIDGE_REF.nokia.version = 'WRT 1.0';
150                 }
151         }
152         else {
153                 _BRIDGE_REF.helper.loadScript("preview/script/lib/device.js");
154         }
155
156         /*
157          window native functions over-riding
158          */
159         if ( (typeof window.frameElement != 'undefined') && (typeof _BRIDGE_REF.nokia  != 'undefined') && window !== window.parent) {
160                 //      alert
161                 window.alert = function(msg){
162                         return window.parent.alert(msg);
163                 };
164                 
165                 //      confirm
166                 window.confirm = function(msg){
167                         return window.parent.confirm(msg);
168                 };
169                 
170                 //      prompt
171                 window.prompt = function(msg, str){
172                         return window.parent.prompt(msg, str)
173                 };
174         }
175
176         //      make TRUE loader.js script loaded
177         window.parent.NOKIA.scriptsLoaded.loader = true;
178
179 }