Browser mode is now standard
[pazpar2-moved-to-github.git] / www / iphone / iphone.js
1 var count = 0;
2 var termlist = {};
3 var JSON = JSON || {};
4 var inApp = false;
5
6 var callback = {};
7
8 callback.init = function() {
9         if (!inApp) {
10                 callback.type = 'browser';
11                 document.getElementById("heading").style.display="";
12         } else
13                 callback.type = 'iphone';
14
15         var searchdiv = document.getElementById("searchdiv");
16         if (this.type != 'iphone') {
17                 searchdiv.style.display = '';
18                 document.search.onsubmit = onFormSubmit;
19         }
20         else
21                 searchdiv.style.display = 'none';
22 };
23
24 String.prototype.replaceAll = function(stringToFind,stringToReplace) {
25                 var temp = this;
26                 var index = temp.indexOf(stringToFind);
27                 while(index != -1){
28                         temp = temp.replace(stringToFind,stringToReplace);
29                         index = temp.indexOf(stringToFind);
30                 }
31                 return temp;
32     }
33
34 callback.send = function() 
35 {
36         var args = [].splice.call(arguments,0);
37         for (var i = 0; i < args.length; i++) {
38                 if (args[i])
39                         args[i] = args[i].replaceAll(':','_'); 
40                 else 
41                         alert("args was null: " + i);
42         }
43         var message = "myapp:" + args.join(":");
44         if (this.type == 'iphone')
45                 document.location = message;
46         else
47                 document.getElementById("log").innerHTML = message;
48 }
49
50 // implement JSON.stringify serialization
51 JSON.stringify = JSON.stringify || function(obj) {
52         var t = typeof (obj);
53         if (t != "object" || obj === null) {
54                 // simple data type
55                 if (t == "string")
56                         obj = '"' + obj + '"';
57                 return String(obj);
58         } else {
59                 // recurse array or object
60                 var n, v, json = [], arr = (obj && obj.constructor == Array);
61                 for (n in obj) {
62                         v = obj[n];
63                         t = typeof (v);
64                         if (t == "string")
65                                 v = '"' + v + '"';
66                         else if (t == "object" && v !== null)
67                                 v = JSON.stringify(v);
68                         json.push((arr ? "" : '"' + n + '":') + String(v));
69                 }
70                 return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
71         }
72 };
73
74 function search(message) {
75         document.search.query.value = message;
76         onFormSubmitEventHandler();
77         return false;
78 }
79
80 function loaded() {
81         callback.init();
82 }
83
84 function onFormSubmit() {
85         return search(document.search.query.value);
86 }