Merge remote-tracking branch 'origin/new/bug_6448'
[srvgit] / koha-tmpl / opac-tmpl / ccsr / en / lib / greybox / GreyBox_v5_5 / greybox_source / base / base.js
1 var GB_CURRENT = null;
2
3 GB_hide = function() {
4     GB_CURRENT.hide();
5 }
6
7 GreyBox = new AJS.Class({
8     init: function(options) {
9         this.use_fx = AJS.fx;
10         this.type = "page";
11         this.overlay_click_close = false;
12         this.salt = 0;
13         this.root_dir = GB_ROOT_DIR;
14         this.callback_fns = [];
15         this.reload_on_close = false;
16         this.src_loader = this.root_dir + 'loader_frame.html';
17
18         //Solve the www issue
19         var h_www = window.location.hostname.indexOf('www');
20         var src_www = this.src_loader.indexOf('www');
21         if(h_www != -1 && src_www == -1)
22             this.src_loader = this.src_loader.replace('://', '://www.');
23
24         if(h_www == -1 && src_www != -1)
25             this.src_loader = this.src_loader.replace('://www.', '://');
26
27         this.show_loading = true;
28         AJS.update(this, options);
29     },
30
31     addCallback: function(fn) {
32         if(fn) this.callback_fns.push(fn);
33     },
34
35     show: function(url) {
36         GB_CURRENT = this;
37         this.url = url;
38
39         var elms = [AJS.$bytc("object"), AJS.$bytc("select")];
40         AJS.map(AJS.flattenList(elms), function(elm) {
41             elm.style.visibility = "hidden";
42         });
43
44         this.createElements();
45         return false;
46     },
47
48     hide: function() {
49         var c_bs = this.callback_fns;
50         if(c_bs != []) {
51             AJS.map(c_bs, function(fn) {
52                 fn();
53             });
54         }
55
56         this.onHide();
57         if(this.use_fx) {
58             var elm = this.overlay;
59             AJS.fx.fadeOut(this.overlay, {
60                 onComplete: function() {
61                     AJS.removeElement(elm);
62                     elm = null;
63                 },
64                 duration: 300
65             });
66             AJS.removeElement(this.g_window);
67         }
68         else {
69             AJS.removeElement(this.g_window, this.overlay);
70         }
71
72         this.removeFrame();
73
74         AJS.REV(window, "scroll", _GB_setOverlayDimension);
75         AJS.REV(window, "resize", _GB_update);
76
77         var elms = [AJS.$bytc("object"), AJS.$bytc("select")];
78         AJS.map(AJS.flattenList(elms), function(elm) {
79             elm.style.visibility = "visible";
80         });
81
82         GB_CURRENT = null;
83
84         if(this.reload_on_close)
85             window.location.reload();
86     },
87
88     update: function() {
89         this.setOverlayDimension();
90         this.setFrameSize();
91         this.setWindowPosition();
92     },
93
94     createElements: function() {
95         this.initOverlay();
96
97         this.g_window = AJS.DIV({'id': 'GB_window'});
98         AJS.hideElement(this.g_window);
99         AJS.getBody().insertBefore(this.g_window, this.overlay.nextSibling);
100
101         this.initFrame();
102         this.initHook();
103         this.update();
104
105         var me = this;
106         if(this.use_fx) {
107             AJS.fx.fadeIn(this.overlay, {
108                 duration: 300,
109                 to: 0.7,
110                 onComplete: function() {
111                     me.onShow();
112                     AJS.showElement(me.g_window);
113                     me.startLoading();
114                 }
115             });
116         }
117         else {
118             AJS.setOpacity(this.overlay, 0.7);
119             AJS.showElement(this.g_window);
120             this.onShow();
121             this.startLoading();
122         }
123
124         AJS.AEV(window, "scroll", _GB_setOverlayDimension);
125         AJS.AEV(window, "resize", _GB_update);
126     },
127
128     removeFrame: function() {
129         try{ AJS.removeElement(this.iframe); }
130         catch(e) {}
131
132         this.iframe = null;
133     },
134
135     startLoading: function() {
136         this.iframe.src = this.src_loader + '?s='+this.salt++;
137         AJS.showElement(this.iframe);
138     },
139
140     setOverlayDimension: function() {
141         var page_size = AJS.getWindowSize();
142         if(AJS.isMozilla() || AJS.isOpera())
143             AJS.setWidth(this.overlay, "100%");
144         else
145             AJS.setWidth(this.overlay, page_size.w);
146
147         var max_height = Math.max(AJS.getScrollTop()+page_size.h, AJS.getScrollTop()+this.height);
148
149         if(max_height < AJS.getScrollTop())
150             AJS.setHeight(this.overlay, max_height);
151         else
152             AJS.setHeight(this.overlay, AJS.getScrollTop()+page_size.h);
153     },
154
155     initOverlay: function() {
156         this.overlay = AJS.DIV({'id': 'GB_overlay'});
157
158         if(this.overlay_click_close)
159             AJS.AEV(this.overlay, "click", GB_hide);
160
161         AJS.setOpacity(this.overlay, 0);
162         AJS.getBody().insertBefore(this.overlay, AJS.getBody().firstChild);
163     },
164
165     initFrame: function() {
166         if(!this.iframe) {
167             var d = {'name': 'GB_frame', 'class': 'GB_frame', 'frameBorder': 0};
168             this.iframe = AJS.IFRAME(d);
169             this.middle_cnt = AJS.DIV({'class': 'content'}, this.iframe);
170
171             this.top_cnt = AJS.DIV();
172             this.bottom_cnt = AJS.DIV();
173
174             AJS.ACN(this.g_window, this.top_cnt, this.middle_cnt, this.bottom_cnt);
175         }
176     },
177
178     /* Can be implemented */
179     onHide: function() {},
180     onShow: function() {},
181     setFrameSize: function() {},
182     setWindowPosition: function() {},
183     initHook: function() {}
184
185 });
186
187 _GB_update = function() { if(GB_CURRENT) GB_CURRENT.update(); }
188 _GB_setOverlayDimension = function() { if(GB_CURRENT) GB_CURRENT.setOverlayDimension(); }
189
190 AJS.preloadImages(GB_ROOT_DIR+'indicator.gif');
191
192 script_loaded = true;