refinerycms-image-gallery 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1139 @@
1
+ var shiftHeld = false;
2
+ var initialLoad = true;
3
+ $(document).ready(function(){
4
+ init_interface();
5
+ init_sortable_menu();
6
+ init_submit_continue();
7
+ init_modal_dialogs();
8
+ init_tooltips();
9
+ init_ajaxy_pagination();
10
+ });
11
+
12
+ if(typeof(window.onpopstate) == "object"){
13
+ $(window).bind('popstate', function(e) {
14
+ // this fires on initial page load too which we don't need.
15
+ if(!initialLoad) {
16
+ $(document).paginateTo((location.pathname + location.href.split(location.pathname)[1]));
17
+ }
18
+ initialLoad = false;
19
+ });
20
+ }
21
+
22
+ $.fn.paginateTo = function(stateUrl) {
23
+ // Grab the url, ensuring not to cache.
24
+ $.ajax({
25
+ url: stateUrl,
26
+ cache: false,
27
+ success: function(data) {
28
+ $('.pagination_container').slideTo(data);
29
+
30
+ // remove caching _ argument.
31
+ $('.pagination_container .pagination a').each(function(i, a){
32
+ $(this).attr('href', $(this).attr('href').replace(/\?\_\=[^&]+&/, '?'));
33
+ })
34
+ },
35
+ failure: function(data) {
36
+ window.location = popstate_location;
37
+ }
38
+ });
39
+ }
40
+
41
+ $.fn.slideTo = function(response) {
42
+ $(this).html(response);
43
+ $(this).applyMinimumHeightFromChildren();
44
+ $(this).find('.pagination_frame').removeClass('frame_right').addClass('frame_center');
45
+ init_modal_dialogs();
46
+ init_tooltips();
47
+ return $(this);
48
+ }
49
+
50
+ $.fn.applyMinimumHeightFromChildren = function() {
51
+ child_heights = 0;
52
+ $(this).children().each(function(i, child){
53
+ child_heights += $(child).height();
54
+ $.each(['marginTop', 'marginBottom', 'paddingTop', 'paddingBottom'], function(i, attr) {
55
+ child_heights += (parseInt($(child).css(attr)) || 0);
56
+ });
57
+ });
58
+ $(this).css('min-height', child_heights);
59
+ return $(this);
60
+ }
61
+
62
+ init_ajaxy_pagination = function(){
63
+ if(typeof(window.history.pushState) == 'function' && $('.pagination_container').length > 0){
64
+ var pagination_pages = $('.pagination_container .pagination a');
65
+ pagination_pages.live('click',function(e) {
66
+ navigate_to = this.href.replace(/(\&(amp\;)?)?from_page\=\d+/, '');
67
+ navigate_to += '&from_page=' + $(this).parent().find('em').text();
68
+ navigate_to = navigate_to.replace('?&', '?');
69
+
70
+ var current_state_location = (location.pathname + location.href.split(location.pathname)[1]);
71
+ window.history.pushState({
72
+ path: current_state_location
73
+ }, '', navigate_to);
74
+
75
+ $(document).paginateTo(navigate_to);
76
+
77
+ e.preventDefault();
78
+ });
79
+ }
80
+ $('.pagination_container').applyMinimumHeightFromChildren();
81
+ if ($('.pagination_container').find('.pagination').length == 0) {
82
+ $('.pagination_frame').css('top', '0px');
83
+ }
84
+ }
85
+
86
+ init_interface = function() {
87
+ if (parent && parent.document.location.href != document.location.href) {
88
+ $('body#dialog_container.dialog').addClass('iframed');
89
+ }
90
+ $('input:submit:not(.button)').addClass('button');
91
+
92
+ if (!$.browser.msie) {
93
+ $('#page_container, .wym_box').corner('5px bottom');
94
+ $('.wym_box').corner('5px tr');
95
+ $('.field > .wym_box').corner('5px tl');
96
+ $('.wym_iframe iframe').corner('2px');
97
+ $('.form-actions:not(".form-actions-dialog")').corner('5px');
98
+ }
99
+ $('#recent_activity li a, #recent_inquiries li a').each(function(i, a) {
100
+ $(this).textTruncate({
101
+ width: $(this).width()
102
+ , tooltip: false
103
+ });
104
+ });
105
+
106
+ // make sure that users can tab to wymeditor fields and add an overlay while loading.
107
+ $('textarea.wymeditor').each(function() {
108
+ textarea = $(this);
109
+ if ((instance = WYMeditor.INSTANCES[$((textarea.next('.wym_box').find('iframe').attr('id')||'').split('_')).last().get(0)]) != null) {
110
+ if ((next = textarea.parent().next()) != null && next.length > 0) {
111
+ next.find('input, textarea').keydown($.proxy(function(e) {
112
+ shiftHeld = e.shiftKey;
113
+ if (shiftHeld && e.keyCode == $.ui.keyCode.TAB) {
114
+ this._iframe.contentWindow.focus();
115
+ e.preventDefault();
116
+ }
117
+ }, instance)).keyup(function(e) {
118
+ shiftHeld = false;
119
+ });
120
+ }
121
+ if ((prev = textarea.parent().prev()) != null && prev.length > 0) {
122
+ prev.find('input, textarea').keydown($.proxy(function(e) {
123
+ if (e.keyCode == $.ui.keyCode.TAB) {
124
+ this._iframe.contentWindow.focus();
125
+ e.preventDefault();
126
+ }
127
+ }, instance));
128
+ }
129
+ }
130
+ });
131
+
132
+ // ensure that the menu isn't wider than the page_container or else it looks silly to round that corner.
133
+ if (($menu = $('#menu')).length > 0) {
134
+ $menu.jcarousel({
135
+ vertical: false
136
+ , scroll: 1
137
+ , buttonNextHTML: "<img src='/images/refinery/carousel-right.png' alt='down' height='15' width='10' />"
138
+ , buttonPrevHTML: "<img src='/images/refinery/carousel-left.png' alt='up' height='15' width='10' />"
139
+ , listTag: $menu.get(0).tagName.toLowerCase()
140
+ , itemTag: $menu.children(':first').get(0).tagName.toLowerCase()
141
+ });
142
+
143
+ if ($menu.outerWidth() < $('#page_container').outerWidth()) {
144
+ $("#page_container:not('.login #page_container')").corner('5px tr');
145
+ } else {
146
+ $("#page_container:not('.login #page_container')").uncorner();
147
+ }
148
+ }
149
+
150
+ $('#current_locale li a').click(function(e) {
151
+ $('#current_locale li a span').each(function(span){
152
+ $(this).css('display', $(this).css('display') == 'none' ? '' : 'none');
153
+ });
154
+ $('#other_locales').animate({opacity: 'toggle', height: 'toggle'}, 250);
155
+ $('html,body').animate({
156
+ scrollTop: $('#other_locales').parent().offset().top
157
+ }, 250);
158
+ e.preventDefault();
159
+ });
160
+
161
+ $('#existing_image img').load(function(){
162
+ $('form.edit_image .form-actions').css({
163
+ 'margin-top': ($('#existing_image').height() - $('form.edit_image').height() + 8)
164
+ });
165
+ });
166
+
167
+ $('.form-actions .form-actions-left input:submit#submit_button').click(function(e) {
168
+ $("<img src='/images/refinery/ajax-loader.gif' width='16' height='16' class='save-loader' />").appendTo($(this).parent());
169
+ });
170
+
171
+ $('a.suppress').live('click', function(e){
172
+ e.preventDefault();
173
+ });
174
+ };
175
+
176
+ init_modal_dialogs = function(){
177
+ $('a[href*="dialog=true"]').not('#dialog_container a').each(function(i, anchor) {
178
+ $(anchor).data({
179
+ 'dialog-width': parseInt($($(anchor).attr('href').match("width=([0-9]*)")).last().get(0), 10)||928
180
+ , 'dialog-height': parseInt($($(anchor).attr('href').match("height=([0-9]*)")).last().get(0), 10)||473
181
+ , 'dialog-title': ($(anchor).attr('title') || $(anchor).attr('name') || $(anchor).html() || null)
182
+ }).attr('href', $(anchor).attr('href').replace(/(\&(amp\;)?)?dialog\=true/, '')
183
+ .replace(/(\&(amp\;)?)?width\=\d+/, '')
184
+ .replace(/(\&(amp\;)?)?height\=\d+/, '')
185
+ .replace(/(\?&(amp\;)?)/, '?')
186
+ .replace(/\?$/, ''))
187
+ .click(function(e){
188
+ $anchor = $(this);
189
+ iframe_src = (iframe_src = $anchor.attr('href'))
190
+ + (iframe_src.indexOf('?') > -1 ? '&' : '?')
191
+ + 'app_dialog=true&dialog=true';
192
+
193
+ iframe = $("<iframe id='dialog_iframe' frameborder='0' marginheight='0' marginwidth='0' border='0'></iframe>");
194
+ if(!$.browser.msie) { iframe.corner('8px'); }
195
+ iframe.dialog({
196
+ title: $anchor.data('dialog-title')
197
+ , modal: true
198
+ , resizable: false
199
+ , autoOpen: true
200
+ , width: $anchor.data('dialog-width')
201
+ , height: $anchor.data('dialog-height')
202
+ , open: onOpenDialog
203
+ , close: onCloseDialog
204
+ });
205
+
206
+ iframe.attr('src', iframe_src);
207
+ e.preventDefault();
208
+ });
209
+ });
210
+ };
211
+
212
+ init_sortable_menu = function(){
213
+ var $menu = $('#menu');
214
+
215
+ if($menu.length === 0){return;}
216
+
217
+ $menu.sortable({
218
+ axis: 'x',
219
+ cursor: 'crosshair',
220
+ connectWith: '.nested',
221
+ update: function(){
222
+ $.post('/refinery/update_menu_positions', $menu.sortable('serialize', {
223
+ key: 'menu[]'
224
+ , expression: /plugin_([\w]*)$/
225
+ }));
226
+ }
227
+ }).tabs();
228
+ //Initial status disabled
229
+ $menu.sortable('disable');
230
+
231
+ $menu.find('#menu_reorder').click(function(e){
232
+ trigger_reordering(e, true);
233
+ });
234
+
235
+ $menu.find('#menu_reorder_done').click(function(e){
236
+ trigger_reordering(e, false);
237
+ });
238
+
239
+ $menu.find('> a').corner('top 5px');
240
+ };
241
+
242
+ trigger_reordering = function(e, enable) {
243
+ e.preventDefault();
244
+ $('#menu_reorder, #menu_reorder_done').toggle();
245
+ $('#site_bar, #content').fadeTo(500, enable ? 0.35 : 1);
246
+
247
+ if(enable) {
248
+ $menu.find('.tab a').click(function(ev){
249
+ ev.preventDefault();
250
+ });
251
+ } else {
252
+ $menu.find('.tab a').unbind('click');
253
+ }
254
+
255
+ $menu.sortable(enable ? 'enable' : 'disable');
256
+ };
257
+
258
+ init_submit_continue = function(){
259
+ $('#submit_continue_button').click(submit_and_continue);
260
+
261
+ $('form').change(function(e) {
262
+ $(this).attr('data-changes-made', true);
263
+ });
264
+
265
+ if ((continue_editing_button = $('#continue_editing')).length > 0 && continue_editing_button.attr('rel') != 'no-prompt') {
266
+ $('#editor_switch a').click(function(e) {
267
+ if ($('form[data-changes-made]').length > 0) {
268
+ if (!confirm(I18n.t('js.admin.confirm_changes'))) {
269
+ e.preventDefault();
270
+ }
271
+ }
272
+ });
273
+ }
274
+ };
275
+
276
+ submit_and_continue = function(e, redirect_to) {
277
+ // ensure wymeditors are up to date.
278
+ if ($(this).hasClass('wymupdate')) {
279
+ $.each(WYMeditor.INSTANCES, function(index, wym)
280
+ {
281
+ wym.update();
282
+ });
283
+ }
284
+
285
+ $('#continue_editing').val(true);
286
+ $('#flash').fadeOut(250);
287
+
288
+ $('.fieldWithErrors').removeClass('fieldWithErrors').addClass('field');
289
+ $('#flash_container .errorExplanation').remove();
290
+
291
+ $.post($('#continue_editing').get(0).form.action, $($('#continue_editing').get(0).form).serialize(), function(data) {
292
+ if (($flash_container = $('#flash_container')).length > 0) {
293
+ $flash_container.html(data);
294
+
295
+ $('#flash').css({'width': 'auto', 'visibility': null}).fadeIn(550);
296
+
297
+ $('.errorExplanation').not($('#flash_container .errorExplanation')).remove();
298
+
299
+ if ((error_fields = $('#fieldsWithErrors').val()) != null) {
300
+ $.each(error_fields.split(','), function() {
301
+ $("#" + this).wrap("<div class='fieldWithErrors' />");
302
+ });
303
+ } else if (redirect_to) {
304
+ window.location = redirect_to;
305
+ }
306
+
307
+ $('.fieldWithErrors:first :input:first').focus();
308
+
309
+ $('#continue_editing').val(false);
310
+
311
+ init_flash_messages();
312
+ }
313
+ });
314
+
315
+ e.preventDefault();
316
+ };
317
+
318
+ init_tooltips = function(args){
319
+ $($(args != null ? args : 'a[title], span[title], #image_grid img[title], *[tooltip]')).not('.no-tooltip').each(function(index, element)
320
+ {
321
+ // create tooltip on hover and destroy it on hoveroff.
322
+ $(element).hover(function(e) {
323
+ if (e.type == 'mouseenter' || e.type == 'mouseover') {
324
+ $(this).oneTime(350, 'tooltip', $.proxy(function() {
325
+ $('.tooltip').remove();
326
+ tooltip = $("<div class='tooltip'><div><span></span></div></div>").appendTo('#tooltip_container');
327
+ tooltip.find("span").html($(this).attr('tooltip'));
328
+ if(!$.browser.msie) {
329
+ tooltip.corner('6px').find('span').corner('6px');
330
+ }
331
+
332
+ tooltip_nib_image = $.browser.msie ? 'tooltip-nib.gif' : 'tooltip-nib.png';
333
+ nib = $("<img src='/images/refinery/"+tooltip_nib_image+"' class='tooltip-nib'/>").appendTo('#tooltip_container');
334
+
335
+ tooltip.css({
336
+ 'opacity': 0
337
+ , 'maxWidth': '300px'
338
+ });
339
+ required_left_offset = $(this).offset().left - (tooltip.outerWidth() / 2) + ($(this).outerWidth() / 2);
340
+ tooltip.css('left', (required_left_offset > 0 ? required_left_offset : 0));
341
+
342
+ var tooltip_offset = tooltip.offset();
343
+ var tooltip_outer_width = tooltip.outerWidth();
344
+ if (tooltip_offset && (tooltip_offset.left + tooltip_outer_width) > (window_width = $(window).width())) {
345
+ tooltip.css('left', window_width - tooltip_outer_width);
346
+ }
347
+
348
+ tooltip.css({
349
+ 'top': $(this).offset().top - tooltip.outerHeight() - 2
350
+ });
351
+
352
+ nib.css({
353
+ 'opacity': 0
354
+ });
355
+
356
+ if (tooltip_offset = tooltip.offset()) {
357
+ nib.css({
358
+ 'left': $(this).offset().left + ($(this).outerWidth() / 2) - 5
359
+ , 'top': tooltip_offset.top + tooltip.height()
360
+ });
361
+ }
362
+
363
+ try {
364
+ tooltip.animate({
365
+ top: tooltip_offset.top - 10
366
+ , opacity: 1
367
+ }, 200, 'swing');
368
+ nib.animate({
369
+ top: nib.offset().top - 10
370
+ , opacity: 1
371
+ }, 200);
372
+ } catch(e) {
373
+ tooltip.show();
374
+ nib.show();
375
+ }
376
+ }, $(this)));
377
+ } else if (e.type == 'mouseleave' || e.type == 'mouseout') {
378
+ $(this).stopTime('tooltip');
379
+ if ((tt_offset = (tooltip = $('.tooltip')).css('z-index', '-1').offset()) == null) {
380
+ tt_offset = {'top':0,'left':0};
381
+ }
382
+ tooltip.animate({
383
+ top: tt_offset.top - 20
384
+ , opacity: 0
385
+ }, 125, 'swing', function(){
386
+ $(this).remove();
387
+ });
388
+ if ((nib_offset = (nib = $('.tooltip-nib')).offset()) == null) {
389
+ nib_offset = {'top':0,'left':0};
390
+ }
391
+ nib.animate({
392
+ top: nib_offset.top - 20
393
+ , opacity: 0
394
+ }, 125, 'swing', function(){
395
+ $(this).remove();
396
+ });
397
+ }
398
+ }).click(function(e) {
399
+ $(this).stopTime('tooltip');
400
+ });
401
+
402
+ if ($(element).attr('tooltip') == null) {
403
+ $(element).attr('tooltip', $(element).attr('title'));
404
+ }
405
+ // wipe clean the title on any children too.
406
+ $elements = $(element).add($(element).children('img')).removeAttr('title');
407
+ // if we're unlucky and in Internet Explorer then we have to say goodbye to 'alt', too.
408
+ if ($.browser.msie){$elements.removeAttr('alt');}
409
+ });
410
+ };
411
+
412
+ var link_tester = {
413
+ initialised: false
414
+ , init: function(test_url, test_email) {
415
+
416
+ if (!this.initialised) {
417
+ this.test_url = test_url;
418
+ this.test_email = test_email;
419
+ this.initialised = true;
420
+ }
421
+ },
422
+
423
+ email: function(value, callback) {
424
+ $.getJSON(link_tester.test_email, {email: value}, function(data){
425
+ callback(data.result == 'success');
426
+ });
427
+ },
428
+
429
+ url: function(value, callback) {
430
+ $.getJSON(link_tester.test_url, {'url': value}, function(data){
431
+ callback(data.result == 'success');
432
+ });
433
+ },
434
+
435
+ validate_textbox: function(validation_method, textbox_id, callback) {
436
+ var icon = '';
437
+ var loader_img = $("<img id='" + textbox_id.replace('#','') + "_test_loader' src='/images/refinery/ajax-loader.gif' alt='Testing...' style='display: none;'/>");
438
+ var result_span = $("<span id='" + textbox_id.replace('#','') + "_test_result'></span>");
439
+
440
+ loader_img.insertAfter($(textbox_id));
441
+ result_span.insertAfter(loader_img);
442
+
443
+ $(textbox_id).bind('paste blur',function(){
444
+ $(textbox_id).stop(true); // Clear the current queue; if we weren't checking yet, cancel it.
445
+ $(textbox_id + '_test_loader').hide();
446
+ $(textbox_id + '_test_result').hide();
447
+ $(textbox_id + '_test_result').removeClass('success_icon').removeClass('failure_icon');
448
+
449
+ // Wait 300ms before checking.
450
+ $(textbox_id).delay(300).queue(function () {
451
+ $(textbox_id + '_test_loader').show();
452
+ $(textbox_id + '_test_result').hide();
453
+ $(textbox_id + '_test_result').removeClass('success_icon').removeClass('failure_icon');
454
+
455
+ validation_method(this.value, function (success) {
456
+ if (success) {
457
+ icon = 'success_icon';
458
+ }else{
459
+ icon = 'failure_icon';
460
+ }
461
+ $(textbox_id + '_test_result').addClass(icon).show();
462
+ $(textbox_id + '_test_loader').hide();
463
+ });
464
+
465
+ if (callback) { callback($(textbox_id)); }
466
+
467
+ $(this).dequeue();
468
+ }); // queue
469
+ }); // bind
470
+ },
471
+
472
+ validate_url_textbox: function(textbox_id, callback) {
473
+ link_tester.validate_textbox(link_tester.url, textbox_id, callback);
474
+ },
475
+
476
+ validate_email_textbox: function(textbox_id, callback) {
477
+ link_tester.validate_textbox(link_tester.email, textbox_id, callback);
478
+ }
479
+
480
+ };
481
+
482
+ var link_dialog = {
483
+ initialised: false
484
+ , init: function(){
485
+
486
+ if (!this.initialised) {
487
+ this.init_tabs();
488
+ this.init_resources_submit();
489
+ this.init_close();
490
+ this.page_tab();
491
+ this.web_tab();
492
+ this.email_tab();
493
+ this.initialised = true;
494
+ }
495
+ },
496
+
497
+ init_tabs: function(){
498
+ var radios = $('#dialog_menu_left input:radio');
499
+ var selected = radios.parent().filter(".selected_radio").find('input:radio').first() || radios.first();
500
+
501
+ radios.click(function(){
502
+ link_dialog.switch_area($(this));
503
+ });
504
+
505
+ selected.attr('checked', 'true');
506
+ link_dialog.switch_area(selected);
507
+ },
508
+
509
+ init_resources_submit: function(){
510
+ $('#existing_resource_area .form-actions-dialog #submit_button').click(function(e){
511
+ e.preventDefault();
512
+ if((resource_selected = $('#existing_resource_area_content ul li.linked a')).length > 0) {
513
+ resourceUrl = parseURL(resource_selected.attr('href'));
514
+ relevant_href = resourceUrl.pathname;
515
+
516
+ // Add any alternate resource stores that need a absolute URL in the regex below
517
+ if(resourceUrl.hostname.match(/s3.amazonaws.com/)) {
518
+ relevant_href = resourceUrl.protocol + '//' + resourceUrl.host + relevant_href;
519
+ }
520
+
521
+ if (typeof(resource_picker.callback) == "function") {
522
+ resource_picker.callback({
523
+ id: resource_selected.attr('id').replace("resource_", "")
524
+ , href: relevant_href
525
+ , html: resource_selected.html()
526
+ });
527
+ }
528
+ }
529
+ });
530
+
531
+ $('.form-actions-dialog #cancel_button').trigger('click');
532
+ },
533
+
534
+ init_close: function(){
535
+ $('.form-actions-dialog #cancel_button').not('.wym_iframe_body .form-actions-dialog #cancel_button').click(close_dialog);
536
+
537
+ if (parent
538
+ && parent.document.location.href != document.location.href
539
+ && parent.document.getElementById('wym_dialog_submit') != null) {
540
+ $('#dialog_container .form-actions input#submit_button').click(function(e) {
541
+ e.preventDefault();
542
+ $(parent.document.getElementById('wym_dialog_submit')).click();
543
+ });
544
+ $('#dialog_container .form-actions a.close_dialog').click(close_dialog);
545
+ }
546
+ },
547
+
548
+ switch_area: function(area){
549
+ $('#dialog_menu_left .selected_radio').removeClass('selected_radio');
550
+ $(area).parent().addClass('selected_radio');
551
+ $('#dialog_main .dialog_area').hide();
552
+ $('#' + $(area).val() + '_area').show();
553
+ },
554
+
555
+ //Same for resources tab
556
+ page_tab: function(){
557
+ $('.link_list li').click(function(e){
558
+ e.preventDefault();
559
+
560
+ $('.link_list li.linked').removeClass('linked');
561
+ $(this).addClass('linked');
562
+
563
+ var link = $(this).children('a.page_link').get(0);
564
+ var port = (window.location.port.length > 0 ? (":" + window.location.port) : "");
565
+ var url = link.href.replace(window.location.protocol + "//" + window.location.hostname + port, "");
566
+
567
+ link_dialog.update_parent(url, link.rel.replace(/\ ?<em>.+?<\/em>/, ''));
568
+ });
569
+ },
570
+
571
+ web_tab: function(){
572
+ link_tester.validate_url_textbox("#web_address_text", function(){});
573
+
574
+ $('#web_address_text, #web_address_target_blank').change(function(){
575
+ link_dialog.update_parent( $('#web_address_text').val(),
576
+ $('#web_address_text').val(),
577
+ $('#web_address_target_blank').get(0).checked ? "_blank" : ""
578
+ );
579
+ });
580
+ },
581
+
582
+ email_tab: function() {
583
+ link_tester.validate_email_textbox("#email_address_text", function(){});
584
+
585
+ $('#email_address_text, #email_default_subject_text, #email_default_body_text').change(function(e){
586
+ var default_subject = $('#email_default_subject_text').val(),
587
+ default_body = $('#email_default_body_text').val(),
588
+ mailto = "mailto:" + $('#email_address_text').val(),
589
+ modifier = "?",
590
+ additional = "";
591
+
592
+ if(default_subject.length > 0){
593
+ additional += modifier + "subject=" + default_subject;
594
+ modifier = "&";
595
+ }
596
+
597
+ if(default_body.length > 0){
598
+ additional += modifier + "body=" + default_body;
599
+ modifier = "&";
600
+ }
601
+
602
+ link_dialog.update_parent(mailto + additional, mailto.replace('mailto:', ''));
603
+ });
604
+ },
605
+
606
+ update_parent: function(url, title, target) {
607
+ if (parent != null) {
608
+ if ((wym_href = parent.document.getElementById('wym_href')) != null) {
609
+ wym_href.value = url;
610
+ }
611
+ if ((wym_title = parent.document.getElementById('wym_title')) != null) {
612
+ wym_title.value = title;
613
+ }
614
+ if ((wym_target = parent.document.getElementById('wym_target')) != null) {
615
+ wym_target.value = target || "";
616
+ }
617
+ }
618
+ }
619
+ };
620
+
621
+ var page_options = {
622
+ initialised: false
623
+ , init: function(enable_parts, new_part_url, del_part_url){
624
+
625
+ if (!this.initialised) {
626
+ // set the page tabs up, but ensure that all tabs are shown so that when wymeditor loads it has a proper height.
627
+ page_options.tabs = $('#page-tabs');
628
+ page_options.tabs.tabs({tabTemplate: '<li><a href="#{href}">#{label}</a></li>'});
629
+ page_options.tabs.find(' > ul li a').corner('top 5px');
630
+
631
+ part_shown = $('#page-tabs .page_part.field').not('.ui-tabs-hide');
632
+ $('#page-tabs .page_part.field').removeClass('ui-tabs-hide');
633
+
634
+ this.enable_parts = enable_parts;
635
+ this.new_part_url = new_part_url;
636
+ this.del_part_url = del_part_url;
637
+ this.show_options();
638
+ this.title_type();
639
+
640
+ $(document).ready($.proxy(function(){
641
+ // hide the tabs that are supposed to be hidden.
642
+ $('#page-tabs .page_part.field').not(this).addClass('ui-tabs-hide');
643
+ $('#page-tabs > ul li a').corner('top 5px');
644
+ }, part_shown));
645
+
646
+ if(this.enable_parts){
647
+ this.page_part_dialog();
648
+ }
649
+ this.initialised = true;
650
+ }
651
+ },
652
+
653
+ show_options: function(){
654
+ $('#toggle_advanced_options').click(function(e){
655
+ e.preventDefault();
656
+ $('#more_options').animate({opacity: 'toggle', height: 'toggle'}, 250);
657
+
658
+ $('html,body').animate({
659
+ scrollTop: $('#toggle_advanced_options').parent().offset().top
660
+ }, 250);
661
+ });
662
+ },
663
+
664
+ title_type: function(){
665
+ $('input#page_custom_title').parents('.field').find('input:radio').change(function(){
666
+ $('#custom_title_text, #custom_title_image').hide();
667
+ $('#custom_title_' + this.value).show();
668
+ });
669
+ },
670
+
671
+ page_part_dialog: function(){
672
+ $('#new_page_part_dialog').dialog({
673
+ title: 'Create Content Section',
674
+ modal: true,
675
+ resizable: false,
676
+ autoOpen: false,
677
+ width: 600,
678
+ height: 200
679
+ });
680
+
681
+ $('#add_page_part').click(function(e){
682
+ e.preventDefault();
683
+ $('#new_page_part_dialog').dialog('open');
684
+ });
685
+
686
+ $('#new_page_part_save').click(function(e){
687
+ e.preventDefault();
688
+
689
+ var part_title = $('#new_page_part_title').val();
690
+
691
+ if(part_title.length > 0){
692
+ var tab_title = part_title.toLowerCase().replace(" ", "_");
693
+
694
+ if ($('#part_' + tab_title).size() === 0) {
695
+ $.get(page_options.new_part_url, {
696
+ title: part_title
697
+ , part_index: $('#new_page_part_index').val()
698
+ , body: ''
699
+ }, function(data, status){
700
+ $('#submit_continue_button').remove();
701
+ // Add a new tab for the new content section.
702
+ $('#page_part_editors').append(data);
703
+ page_options.tabs.tabs('add', '#page_part_new_' + $('#new_page_part_index').val(), part_title);
704
+ page_options.tabs.tabs('select', $('#new_page_part_index').val());
705
+
706
+ // hook into wymeditor to instruct it to select this new tab again once it has loaded.
707
+ WYMeditor.onload_functions.push(function() {
708
+ page_options.tabs.tabs('select', $('#new_page_part_index').val());
709
+ });
710
+
711
+ // turn the new textarea into a wymeditor.
712
+ $('#page_part_new_' + $('#new_page_part_index').val()).appendTo('#page_part_editors')
713
+ WYMeditor.init();
714
+
715
+ // Wipe the title and increment the index counter by one.
716
+ $('#new_page_part_index').val(parseInt($('#new_page_part_index').val(), 10) + 1);
717
+ $('#new_page_part_title').val('');
718
+
719
+ page_options.tabs.find('> ul li a').corner('top 5px');
720
+
721
+ $('#new_page_part_dialog').dialog('close');
722
+ }
723
+ );
724
+ }else{
725
+ alert("A content section with that title already exists, please choose another.");
726
+ }
727
+ }else{
728
+ alert("You have not entered a title for the content section, please enter one.");
729
+ }
730
+ });
731
+
732
+ $('#new_page_part_cancel').click(function(e){
733
+ e.preventDefault();
734
+ $('#new_page_part_dialog').dialog('close');
735
+ $('#new_page_part_title').val('');
736
+ });
737
+
738
+ $('#delete_page_part').click(function(e){
739
+ e.preventDefault();
740
+
741
+ if(confirm("This will remove the content section '" + $('#page_parts .ui-tabs-selected a').text() + "' immediately even if you don't save this page, are you sure?")) {
742
+ var tabId = page_options.tabs.tabs('option', 'selected');
743
+ $.ajax({
744
+ url: page_options.del_part_url + '/' + $('#page_parts_attributes_' + tabId + '_id').val(),
745
+ type: 'DELETE'
746
+ });
747
+ page_options.tabs.tabs('remove', tabId);
748
+ $('#page_parts_attributes_' + tabId + '_id').remove();
749
+ $('#submit_continue_button').remove();
750
+ }
751
+
752
+ });
753
+
754
+ }
755
+
756
+ };
757
+
758
+ var image_dialog = {
759
+ initialised: false
760
+ , callback: null
761
+ , chunk: null
762
+
763
+
764
+ , init: function(callback, chunk){
765
+
766
+ if (!this.initialised) {
767
+ this.callback = callback;
768
+ this.chunk = chunk;
769
+ this.init_tabs();
770
+ this.init_select();
771
+ this.init_actions();
772
+ this.initialised = true;
773
+ }
774
+ return this;
775
+ }
776
+
777
+ , init_tabs: function(){
778
+ var radios = $('#dialog_menu_left input:radio');
779
+ var selected = radios.parent().filter(".selected_radio").find('input:radio').first() || radios.first();
780
+
781
+ radios.click(function(){
782
+ link_dialog.switch_area($(this));
783
+ });
784
+
785
+ selected.attr('checked', 'true');
786
+ link_dialog.switch_area(selected);
787
+ }
788
+
789
+ , switch_area: function(radio){
790
+ $('#dialog_menu_left .selected_radio').removeClass('selected_radio');
791
+ $(radio).parent().addClass('selected_radio');
792
+ $('#dialog_main .dialog_area').hide();
793
+ $('#' + radio.value + '_area').show();
794
+ }
795
+
796
+ , init_select: function(){
797
+ $('#existing_image_area_content ul li img').click(function(){
798
+ image_dialog.set_image(this);
799
+ });
800
+ //Select any currently selected, just uploaded...
801
+ if ((selected_img = $('#existing_image_area_content ul li.selected img')).length > 0) {
802
+ image_dialog.set_image(selected_img.first());
803
+ }
804
+ }
805
+
806
+ , set_image: function(img){
807
+ if ($(img).length > 0) {
808
+ $('#existing_image_area_content ul li.selected').removeClass('selected');
809
+
810
+ $(img).parent().addClass('selected');
811
+ var imageId = $(img).attr('data-id');
812
+ var geometry = $('#existing_image_size_area li.selected a').attr('data-geometry');
813
+ var size = $('#existing_image_size_area li.selected a').attr('data-size');
814
+ var resize = $("#wants_to_resize_image").is(':checked');
815
+
816
+ image_url = resize ? $(img).attr('data-' + size) : $(img).attr('data-original');
817
+
818
+ if (parent) {
819
+ if ((wym_src = parent.document.getElementById('wym_src')) != null) {
820
+ wym_src.value = image_url;
821
+ }
822
+ if ((wym_title = parent.document.getElementById('wym_title')) != null) {
823
+ wym_title.value = $(img).attr('title');
824
+ }
825
+ if ((wym_alt = parent.document.getElementById('wym_alt')) != null) {
826
+ wym_alt.value = $(img).attr('alt');
827
+ }
828
+ if ((wym_size = parent.document.getElementById('wym_size')) != null
829
+ && typeof(geometry) != 'undefined') {
830
+ wym_size.value = geometry.replace(/[<>=]/g, '');
831
+ }
832
+ }
833
+ }
834
+ }
835
+
836
+ , submit_image_choice: function(e) {
837
+ e.preventDefault();
838
+ if((img_selected = $('#existing_image_area_content ul li.selected img').get(0)) && $.isFunction(this.callback))
839
+ {
840
+ this.callback(img_selected, this.chunk);
841
+ }
842
+
843
+ close_dialog(e);
844
+ }
845
+
846
+ , init_actions: function(){
847
+ var _this = this;
848
+ // get submit buttons not inside a wymeditor iframe
849
+ $('#existing_image_area .form-actions-dialog #submit_button')
850
+ .not('.wym_iframe_body #existing_image_area .form-actions-dialog #submit_button')
851
+ .click($.proxy(_this.submit_image_choice, _this));
852
+
853
+ // get cancel buttons not inside a wymeditor iframe
854
+ $('.form-actions-dialog #cancel_button')
855
+ .not('.wym_iframe_body .form-actions-dialog #cancel_button')
856
+ .click($.proxy(close_dialog, _this));
857
+
858
+ $('#existing_image_size_area ul li a').click(function(e) {
859
+ $('#existing_image_size_area ul li').removeClass('selected');
860
+ $(this).parent().addClass('selected');
861
+ $('#existing_image_size_area #wants_to_resize_image').attr('checked', 'checked');
862
+ image_dialog.set_image($('#existing_image_area_content ul li.selected img'));
863
+ e.preventDefault();
864
+ });
865
+
866
+ $('#existing_image_size_area #wants_to_resize_image').change(function(){
867
+ if($(this).is(":checked")) {
868
+ $('#existing_image_size_area ul li:first a').click();
869
+ } else {
870
+ $('#existing_image_size_area ul li').removeClass('selected');
871
+ image_dialog.set_image($('#existing_image_area_content ul li.selected img'));
872
+ }
873
+ });
874
+
875
+ image_area = $('#existing_image_area').not('#wym_iframe_body #existing_image_area');
876
+ image_area.find('.form-actions input#submit_button').click($.proxy(function(e) {
877
+ e.preventDefault();
878
+ $(this.document.getElementById('wym_dialog_submit')).click();
879
+ }, parent));
880
+ image_area.find('.form-actions a.close_dialog').click(close_dialog);
881
+ }
882
+ };
883
+
884
+ var list_reorder = {
885
+ initialised: false
886
+ , init: function() {
887
+
888
+ if (!this.initialised) {
889
+ $('#reorder_action').click(list_reorder.enable_reordering);
890
+ $('#reorder_action_done').click(list_reorder.disable_reordering);
891
+ if(list_reorder.tree === false) {
892
+ list_reorder.sortable_list.find('li').addClass('no-nest');
893
+ }
894
+ list_reorder.sortable_list.nestedSortable({
895
+ disableNesting: 'no-nest',
896
+ forcePlaceholderSize: true,
897
+ handle: list_reorder.tree ? 'div' : null,
898
+ items: 'li',
899
+ opacity: .6,
900
+ placeholder: 'placeholder',
901
+ tabSize: 25,
902
+ tolerance: 'pointer',
903
+ toleranceElement: list_reorder.tree ? '> div' : null,
904
+ disabled: true,
905
+ start: function () {
906
+ },
907
+ change: function () {
908
+ if (list_reorder.tree) {
909
+ list_reorder.reset_branch_classes(this);
910
+ }
911
+ },
912
+ stop: function () {
913
+ if (list_reorder.tree) {
914
+ list_reorder.reset_branch_classes(this);
915
+ } else {
916
+ list_reorder.reset_on_off_classes(this);
917
+ }
918
+ }
919
+ });
920
+ if (list_reorder.tree) {
921
+ list_reorder.reset_branch_classes(list_reorder.sortable_list);
922
+ } else {
923
+ list_reorder.reset_on_off_classes(list_reorder.sortable_list);
924
+ }
925
+ this.initialised = true;
926
+ }
927
+ }
928
+ , reset_on_off_classes: function(ul) {
929
+ $("> li", ul).each(function(i, li) {
930
+ $(li).removeClass('on off on-hover').addClass(i % 2 === 0 ? 'on' : 'off');
931
+ });
932
+ }
933
+
934
+ , reset_branch_classes: function (ul) {
935
+ $("li.ui-sortable-helper", this).removeClass("record").removeClass("branch_start").removeClass("branch_end");
936
+ $("li", ul).removeClass("branch_start").removeClass("branch_end");
937
+
938
+ $("> li:first", ul).addClass("branch_start");
939
+ $("> li:last", ul).addClass("branch_end");
940
+
941
+ var nested_ul = $("ul", ul);
942
+ $("> li:last", nested_ul).addClass("branch_end");
943
+ }
944
+
945
+ ,enable_reordering: function(e) {
946
+ if(e) { e.preventDefault(); }
947
+ $('#sortable_list').addClass("reordering");
948
+
949
+ $('#sortable_list .actions, #site_bar, header > *:not(script)').fadeTo(500, 0.3);
950
+ $('#actions *:not("#reorder_action_done, #reorder_action")').not($('#reorder_action_done').parents('li, ul, div')).fadeTo(500, 0.55);
951
+
952
+ list_reorder.sortable_list.nestedSortable("enable");
953
+ $('#reorder_action').hide();
954
+ $('#reorder_action_done').show();
955
+ }
956
+
957
+ , disable_reordering: function(e) {
958
+ if($('#reorder_action_done').hasClass('loading')){
959
+ return false;
960
+ }
961
+ if(e) { e.preventDefault(); }
962
+ $('#reorder_action_done').addClass('loading');
963
+ list_reorder.sortable_list.nestedSortable("disable");
964
+
965
+ $('#sortable_list').removeClass("reordering");
966
+
967
+ if (list_reorder.update_url != null) {
968
+
969
+ var serialized = list_reorder.sortable_list.serializelist();
970
+
971
+ $.post(list_reorder.update_url, serialized, function(data) {
972
+ list_reorder.restore_controls(e);
973
+ });
974
+ } else {
975
+ list_reorder.restore_controls(e);
976
+ }
977
+ }
978
+
979
+ , restore_controls: function(e) {
980
+ $(list_reorder.sortable_list).removeClass('reordering');
981
+
982
+ $('#sortable_list .actions, #site_bar, header > *:not(script)').fadeTo(250, 1);
983
+ $('#actions *:not("#reorder_action_done, #reorder_action")').not($('#reorder_action_done').parents('li, ul, div')).fadeTo(250, 1, function() {
984
+ $('#reorder_action_done').hide().removeClass('loading');
985
+ $('#reorder_action').show();
986
+ });
987
+ }
988
+ };
989
+
990
+ var image_picker = {
991
+ initialised: false
992
+ , options: {
993
+ selected: ''
994
+ , thumbnail: 'medium'
995
+ , field: '#image'
996
+ , image_display: '.current_picked_image'
997
+ , no_image_message: '.no_picked_image_selected'
998
+ , image_container: '.current_image_container'
999
+ , remove_image_button: '.remove_picked_image'
1000
+ , picker_container: '.image_picker_container'
1001
+ , image_link: '.current_image_link'
1002
+ , image_toggler: null
1003
+ }
1004
+
1005
+ , init: function(new_options){
1006
+
1007
+ if (!this.initialised) {
1008
+ this.options = $.extend(this.options, new_options);
1009
+ $(this.options.picker_container).find(this.options.remove_image_button)
1010
+ .click($.proxy(this.remove_image, {container: this.options.picker_container, picker: this}));
1011
+ $(this.options.picker_container).find(this.options.image_toggler)
1012
+ .click($.proxy(this.toggle_image, {container: this.options.picker_container, picker: this}));
1013
+
1014
+ this.initialised = true;
1015
+ }
1016
+
1017
+ return this;
1018
+ }
1019
+
1020
+ , remove_image: function(e) {
1021
+ e.preventDefault();
1022
+
1023
+ $(this.container).find(this.picker.options.image_display)
1024
+ .removeClass('brown_border')
1025
+ .attr({'src': '', 'width': '', 'height': ''})
1026
+ .css({'width': 'auto', 'height': 'auto'})
1027
+ .hide();
1028
+ $(this.container).find(this.picker.options.field).val('');
1029
+ $(this.container).find(this.picker.options.no_image_message).show();
1030
+ $(this.container).find(this.picker.options.remove_image_button).hide();
1031
+ $(this).hide();
1032
+ }
1033
+
1034
+ , toggle_image: function(e) {
1035
+ $(this.container).find(this.options.image_toggler).html(
1036
+ ($(this.container).find(options.image_toggler).html() == 'Show' ? 'Hide' : 'Show')
1037
+ );
1038
+ $(this.container).find(this.options.image_container).toggle();
1039
+ e.preventDefault();
1040
+ }
1041
+
1042
+ , changed: function(e) {
1043
+ $(this.container).find(this.picker.options.field).val(
1044
+ this.image.id.replace("image_", "")
1045
+ );
1046
+
1047
+ var size = this.picker.options.thumbnail || 'original';
1048
+ this.image.src = $(this.image).attr('data-' + size);
1049
+ current_image = $(this.container).find(this.picker.options.image_display);
1050
+ current_image.replaceWith(
1051
+ $("<img src='"+this.image.src+"?"+Math.floor(Math.random() * 100000)+"' id='"+current_image.attr('id')+"' class='"+this.picker.options.image_display.replace(/^./, '')+" brown_border' />")
1052
+ );
1053
+
1054
+ $(this.container).find(this.picker.options.remove_image_button).show();
1055
+ $(this.container).find(this.picker.options.no_image_message).hide();
1056
+ }
1057
+ };
1058
+
1059
+ var resource_picker = {
1060
+ initialised: false
1061
+ , callback: null
1062
+
1063
+ , init: function(callback) {
1064
+
1065
+ if (!this.initialised) {
1066
+ this.callback = callback;
1067
+ this.initialised = true;
1068
+ }
1069
+ }
1070
+ };
1071
+
1072
+ close_dialog = function(e) {
1073
+ if (iframed())
1074
+ {
1075
+ the_body = $(parent.document.body);
1076
+ the_dialog = parent.$('.ui-dialog-content');
1077
+ } else {
1078
+ the_body = $(document.body).removeClass('hide-overflow');
1079
+ the_dialog = $('.ui-dialog-content');
1080
+ the_dialog.filter(':data(dialog)').dialog('close');
1081
+ the_dialog.remove();
1082
+ }
1083
+
1084
+ // if there's a wymeditor involved don't try to close the dialog as wymeditor will.
1085
+ if (!$(document.body).hasClass('wym_iframe_body')) {
1086
+ the_body.removeClass('hide-overflow');
1087
+ the_dialog.filter(':data(dialog)').dialog('close');
1088
+ the_dialog.remove();
1089
+ e.preventDefault();
1090
+ }
1091
+ };
1092
+
1093
+ //parse a URL to form an object of properties
1094
+ parseURL = function(url)
1095
+ {
1096
+ //save the unmodified url to href property
1097
+ //so that the object we get back contains
1098
+ //all the same properties as the built-in location object
1099
+ var loc = { 'href' : url };
1100
+
1101
+ //split the URL by single-slashes to get the component parts
1102
+ var parts = url.replace('//', '/').split('/');
1103
+
1104
+ //store the protocol and host
1105
+ loc.protocol = parts[0];
1106
+ loc.host = parts[1];
1107
+
1108
+ //extract any port number from the host
1109
+ //from which we derive the port and hostname
1110
+ parts[1] = parts[1].split(':');
1111
+ loc.hostname = parts[1][0];
1112
+ loc.port = parts[1].length > 1 ? parts[1][1] : '';
1113
+
1114
+ //splice and join the remainder to get the pathname
1115
+ parts.splice(0, 2);
1116
+ // ensure we don't destroy absolute urls like /system/images/whatever.jpg
1117
+ loc.pathname = (loc.href[0] == '/' ? ("/" + loc.host) : '');
1118
+ loc.pathname += '/' + parts.join('/');
1119
+
1120
+ //extract any hash and remove from the pathname
1121
+ loc.pathname = loc.pathname.split('#');
1122
+ loc.hash = loc.pathname.length > 1 ? '#' + loc.pathname[1] : '';
1123
+ loc.pathname = loc.pathname[0];
1124
+
1125
+ //extract any search query and remove from the pathname
1126
+ loc.pathname = loc.pathname.split('?');
1127
+ loc.search = loc.pathname.length > 1 ? '?' + loc.pathname[1] : '';
1128
+ loc.pathname = loc.pathname[0];
1129
+
1130
+ var options = url.split('?')[1];
1131
+ loc.options = options;
1132
+
1133
+ //return the final object
1134
+ return loc;
1135
+ };
1136
+
1137
+ iframed = function() {
1138
+ return (parent && parent.document && parent.document.location.href != document.location.href && $.isFunction(parent.$));
1139
+ };