add domain to redirection
[wolnelektury.git] / apps / wolnelektury_core / static / js / widget.js
1 /* create basic elements */
2 var id          = "wl";
3 var widget      = document.getElementById(id);
4 var linkLogo    = document.createElement('a');
5 var logo        = document.createElement('img');
6 var form        = document.createElement('form');
7 var inputText   = document.createElement('input');
8 var inputSubmit = document.createElement('input');
9 var body        = document.getElementsByTagName('body')
10 var stylesheet = document.createElement('link');
11 var stylesheetJQUI = document.createElement('linl');
12
13 var host = 'wolnelektury.pl';
14
15 /* set attributes of created elements */
16 stylesheet.setAttribute('type', 'text/css');
17 stylesheet.setAttribute('rel', 'stylesheet');
18 stylesheet.setAttribute('href', 'http://'+host+'/static/css/widget.css');
19 stylesheetJQUI.setAttribute('type', 'text/css');
20 stylesheetJQUI.setAttribute('rel', 'stylesheet');
21 stylesheetJQUI.setAttribute('href', 'http://'+host+'/static/css/ui-lightness/jquery-ui-1.8.16.custom.css');
22 linkLogo.setAttribute('href', 'http://'+host);
23 logo.setAttribute('src', 'http://'+host+'/static/img/logo-bez.png');
24 form.setAttribute('action', 'http://'+host+'/szukaj/');
25 form.setAttribute('method', 'get');
26 form.setAttribute('accept-charset', 'utf-8');
27 form.setAttribute('id', 'wl-form');
28 inputText.setAttribute('type', 'text');
29 inputText.setAttribute('title', 'tytul, autor, motyw/temat, epoka, rodzaj, gatunek');
30 inputText.setAttribute('value', '');
31 inputText.setAttribute('name', 'q');
32 inputText.setAttribute('id', 'id_qq');
33 inputText.setAttribute('data-source', 'http://'+host+'/szukaj/hint');
34 /*inputText.setAttribute('size', '13');*/
35 inputSubmit.setAttribute('type', 'image');
36 inputSubmit.setAttribute('src', 'http://'+host+'/static/img/search.png');
37 /* inputSubmit.setAttribute('style', 'position:relative; top:5px; margin-left:5px');*/
38
39 /* import jquery and autocomplete */
40 var scriptJ = document.createElement('script');
41 scriptJ.setAttribute('type', 'text/javascript');
42 scriptJ.setAttribute('src', 'http://'+host+'/static/js/jquery.js');
43
44 var scriptUI = document.createElement('script');
45 scriptUI.setAttribute('type', 'text/javascript');
46 scriptUI.setAttribute('src', 'http://'+host+'/static/js/jquery-ui-1.8.2.custom.min.js');
47 scriptUI.setAttribute('id', 'wl-jquery-ui-script')
48
49 var scriptSearch = document.createElement('script');
50 scriptSearch.setAttribute('type', 'text/javascript');
51 scriptSearch.setAttribute('src', 'http://'+host+'/static/js/search.js');
52 scriptSearch.setAttribute('id', 'wl-search-script')
53
54 body[0].appendChild(scriptJ);
55 scriptJ.onload = function() { body[0].appendChild(scriptUI); };
56 scriptJ.onreadystatechange = function() { if (scriptJ.readyState == 'complete') { scriptJ.onload(); } };
57
58 scriptUI.onload = function() { body[0].appendChild(scriptSearch); };
59 scriptUI.onreadystatechange = function() { if (scriptUI.readyState == 'complete') { scriptUI.onload(); } };
60
61 scriptSearch.onload = function() {
62         var s = $('#id_qq');
63         var url = s.attr('data-source');
64         s.search({source: 
65                         function(req, cb) {
66                         $.ajax({url: url,
67                                 dataType: "jsonp",
68                                 data: { term: req.term },
69                                 type: "GET",
70                                 success: function(data) { cb(data); },
71                                 error: function() { cb([]); }
72                     });
73                         },
74             dataType: "jsonp",
75             host: "http://"+host});
76 }
77 scriptSearch.onreadystatechange = function() { if (scriptSearch.readyState == 'complete') { scriptSearch.onload(); } };
78
79 /* append elements to widget */
80 widget.appendChild(stylesheet);
81 //widget.appendChild(stylesheetJQUI);
82 widget.appendChild(linkLogo);
83 linkLogo.appendChild(logo);
84 widget.appendChild(form);
85 form.appendChild(inputText);
86 form.appendChild(inputSubmit);
87
88 /* ...and a little make-up */
89 /*
90 widget.style.borderColor = "#84BF2A";
91 widget.style.borderWidth = "2px";
92 widget.style.borderStyle = "solid";
93 widget.style.width = "160px";
94 widget.style.padding = "10px";
95 widget.style.fontSize = "12px";
96 form.style.paddingTop = "10px";
97 */
98
99 /* resize - if needed */
100 if(widget.getAttribute('width') == '140'){
101     logo.setAttribute('width', '140');
102     inputText.setAttribute('size', '10');
103     widget.style.width = "140px";
104 }
105
106
107