cirandas.net

ref: master

public/javascripts/application.js


   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
/*
* third party libraries
*= require vendor/lodash.js
*= require vendor/jquery-2.1.1.min.js
*= require vendor/jquery-migrate-1.2.1.js
*= require vendor/jquery.cycle.all.min.js
*= require vendor/jquery.colorbox-min.js
*= require vendor/jquery-ui-1.10.4/js/jquery-ui-1.10.4.min.js
*= require vendor/jquery.scrollTo.js
*= require vendor/jquery.form.js
*= require vendor/jquery-validation/jquery.validate.js
*= require vendor/jquery.cookie.js
*= require vendor/jquery.ba-bbq.min.js
*= require vendor/jquery.tokeninput.js
*= require vendor/jquery-timepicker-addon/dist/jquery-ui-timepicker-addon.js
*= require vendor/inputosaurus.js
*= require vendor/reflection.js
*= require vendor/rails.js
*= require vendor/jrails.js
*= require vendor/slick.js
*= require vendor/autogrow.js
*= require vendor/favico.js
*= require rails-extended.js
*# require message-bus.js
*= require vendor/riot-2.5.0
*= require i18n
*= require i18n/translations
*= require js-routes
*
* noosfero libraries
*= require_self
*= require modal.js
*= require form.js
*= require timezone.js
*= require loading-overlay.js
*= require pagination.js
*
* views speficics
*= require add-and-join.js
*= require followers.js
*= require manage-followers.js
*= require report-abuse.js
*= require require_login.js
*= require block-store.js
*= require email_templates.js
*= require categories_selector.js
*/

// lodash configuration
_.templateSettings = {
  interpolate: /\{\{(.+?)\}\}/g,
};

// scope for noosfero stuff
noosfero = {
};

if (window.MessageBus) {
  MessageBus.start()
  MessageBus.callbackInterval = 500;
}

function noosfero_init() {
  // focus_first_field(); it is moving the page view when de form is down.
}

var __noosfero_root = null;
function noosfero_root() {
  if (__noosfero_root == null) {
    __noosfero_root = jQuery('meta[property="noosfero:root"]').attr("content") || '';
  }
  return __noosfero_root;
}

/* If applicable, find the first field in which the user can type and move the
 * keyboard focus to it.
 *
 * ToDo: focus only inside the view box to do not roll the page.
 */
function focus_first_field() {
  form = document.forms[0];
  if (form == undefined) {
    return;
  }

  for (var i = 0; i < form.elements.length; i++) {
    field = form.elements[i];
    if (field.type == 'text' || field.type == 'textarea') {
      try {
        field.focus();
        return;
      } catch(e) { }
    }
  }
}

/* * * Convert a string to a valid login name * * */
function convToValidLogin( str ) {
  if (str.indexOf('@') == -1)
    return convToValidUsername(str);
  else
    return convToValidEmail(str);
}

function convToValidUsername( str ) {
  return convToValidIdentifier(str, '');
}

/* * * Convert a string to a valid login name * * */
function convToValidIdentifier( str, sep ) {
  return str.toLowerCase()
            .replace( /@.*$/,     ""  )
            .replace( /á|à|ã|â/g, "a" )
            .replace( /é|ê/g,     "e" )
            .replace( /í/g,       "i" )
            .replace( /ó|ô|õ|ö/g, "o" )
            .replace( /ú|ũ|ü/g,   "u" )
            .replace( /ñ/g,       "n" )
            .replace( /ç/g,       "c" )
            .replace( /[^-_a-z0-9.]+/g, sep )
}

function convToValidEmail( str ) {
  return str.toLowerCase()
            .replace( /á|à|ã|â/g, "a" )
            .replace( /é|ê/g,     "e" )
            .replace( /í/g,       "i" )
            .replace( /ó|ô|õ|ö/g, "o" )
            .replace( /ú|ũ|ü/g,   "u" )
            .replace( /ñ/g,       "n" )
            .replace( /ç/g,       "c" )
            .replace( /[^@a-z0-9!#$%&'*+-/=?^_`{|}~.]+/g, '' )
}

function updateUrlField(name_field, id) {
   url_field = jQuery('#'+id);
   old_url_value = url_field.val();
   new_url_value = convToValidIdentifier(name_field.value, "-");

   url_field.val(new_url_value);

  if (old_url_value && !/^\s*$/.test(old_url_value) && old_url_value != new_url_value) {
    warn_value_change(url_field);
  }
}



jQuery.fn.centerInForm = function () {
  var $ = jQuery;
  var form = $(this).parent('form');
  this.css("position", "absolute");
  this.css("top", (form.height() - this.height())/ 2 + form.scrollTop() + "px");
  this.css("left", (form.width() - this.width()) / 2 + form.scrollLeft() + "px");
  this.css("width", form.width() + "px");
  this.css("height", form.height() + "px");
  return this;
}

jQuery.fn.center = function () {
  var $ = jQuery;
  this.css("position", "absolute");
  this.css("top", ($(window).height() - this.height())/ 2 + $(window).scrollTop() + "px");
  this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
  return this;
}

function show_warning(field, message) {
  jQuery('#'+field).effect('highlight');
  jQuery('#'+message).show();
}

function hide_warning(field) {
   jQuery('#'+field).hide();
}

function enable_button(button) {
  button = jQuery(button)
  button.prop('disabled', false);
  button.removeClass("disabled");
}

function disable_button(button) {
  button = jQuery(button)
  button.prop('disabled', true);
  button.addClass("disabled");
}

function toggleDisabled(enable, element) {
   if (enable) {
      enable_button(element);
   }
   else {
      disable_button(element);
   }
}

/* ICON SELECTOR - LinkListBlock */

function showIconSelector(main_div) {
   iconSelector = jQuery(main_div).children('.icon-selector')[0];
   jQuery(iconSelector).toggle();
}

function changeIcon(iconSelected, iconName) {
   iconSelector = iconSelected.parentNode;
   setTimeout('iconSelector.style.display = "none"', 100);
   main_div = iconSelector.parentNode;
   span = main_div.getElementsByTagName('span')[0];
   span.className = iconSelected.className;
   iconInput = main_div.getElementsByTagName('input')[0];
   iconInput.value = iconName;
}

function hideOthersIconSelector(current_div) {
   jQuery('.icon-selector').not(jQuery(current_div).children('.icon-selector')).hide();
}

function loading(element_id, message) {
   jQuery('#'+element_id).addClass('loading');
   if (message) {
      jQuery('#'+element_id).html(message);
   }
}
function small_loading(element_id, message) {
   $('#'+element_id).addClass('small-loading');
   if (message) {
      $('#'+element_id).text(message);
   }
}
function loading_done(element_id) {
   jQuery('#'+element_id).removeClass('loading');
   jQuery('#'+element_id).removeClass('small-loading');
   jQuery('#'+element_id).removeClass('small-loading-dark');
}
function open_loading(message) {
   jQuery('body').prepend("<div id='overlay_loading' class='ui-widget-overlay' style='display: none'/><div id='overlay_loading_modal' style='display: none'><p>"+message+"</p><img src='" + noosfero_root() + "/images/loading-dark.gif'/></div>");
   jQuery('#overlay_loading').show();
   jQuery('#overlay_loading_modal').center();
   jQuery('#overlay_loading_modal').fadeIn('slow');
}
function close_loading() {
   jQuery('#overlay_loading_modal').fadeOut('slow', function() {
      jQuery('#overlay_loading_modal').remove();
      jQuery('#overlay_loading').remove();
   });
}
function update_loading(message) {
   jQuery('#overlay_loading_modal p').text(message);
}

function redirect_to(url) {
  document.location=url;
}

function numbersonly(e, separator) {
  var key;
  var keychar;
  if (window.event) {
    key = window.event.keyCode;
  }
  else if (e) {
    key = e.which;
  }
  else {
    return true;
  }
  keychar = String.fromCharCode(key);

  if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
    return true;
  }
  else if ((("0123456789").indexOf(keychar) > -1)) {
    return true;
  }
  else if (keychar == separator) {
    if (e.target.value.indexOf(separator) > -1) {
      return false;
    }
    return true;
  }
  else
    return false;
}

// transform all element with class ui_button in a jQuery UI button
function render_jquery_ui_buttons(element_id) {
   if (element_id) {
      element_id = '#' + element_id
      jQuery(element_id).button({
         icons: {
             primary: jQuery(element_id).attr('data-primary-icon'),
             secondary: jQuery(element_id).attr('data-secondary-icon')
            }
         }
      )
   }
   else {
      jQuery('.ui_button').each(function() {
         jQuery(this).button({
            icons: {
                primary: this.getAttribute('data-primary-icon'),
                secondary: this.getAttribute('data-secondary-icon')
               }
            }
         )
      })
   }
}

function render_all_jquery_ui_widgets() {
  jQuery(function() {
    render_jquery_ui_buttons();
    jQuery('.ui-tabs').each(function(){
      jQuery(this).tabs({
        cookie: { name: this.id }
      });
    });
  });
}

function expandCategory(block, id) {
  var link = jQuery('#block_' + block + '_category_' + id);
  if (category_expanded['block'] > 0 && category_expanded['category'] > 0 && category_expanded['block'] == block && category_expanded['category'] != id && link.hasClass('category-root')) {
    expandCategory(category_expanded['block'], category_expanded['category']);
    category_expanded['category'] = id;
    category_expanded['block'] = block;
  }
  if (category_expanded['block'] == 0) category_expanded['block'] = block;
  if (category_expanded['category'] == 0) category_expanded['category'] = id;
  jQuery('#block_' + block + '_category_content_' + id).slideToggle('slow');
  link.toggleClass('category-expanded');
  if (link.hasClass('category-expanded')) link.html(expanded_icon);
  else {
    link.html(collapsed_icon);
    if (link.hasClass('category-root')) {
      category_expanded['block'] = 0;
      category_expanded['category'] = 0;
    }
  }
}

function ieZIndexBugFix(trigger) {
  if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 7) {
    jQuery('#navigation').css({ zIndex : 6 });
    jQuery('.box-2, .box-3').css({ zIndex : 5 });
    jQuery(trigger).parents('.box-2, .box-3').css({ zIndex : 11 });
  }
}

function toggleSubmenu(trigger, title, link_list) {
  ieZIndexBugFix(trigger);
  trigger.onclick = function() {
    ieZIndexBugFix(trigger);
    var submenu = jQuery(trigger).siblings('.menu-submenu');
    var hide = false;
    if (submenu.length > 0 && submenu.is(':visible')) hide = true;
    hideAllSubmenus();
    // Hide or show this submenu if it already exists
    if (submenu.length > 0) {
      if (!hide) {
        var direction = 'down';
        if (submenu.hasClass('up')) direction = 'up';
        jQuery(submenu).fadeIn();
      }
    }
    return false;
  }

  hideAllSubmenus();
  // Build and show this submenu if it doesn't exist yet
  var direction = 'down';
  if (jQuery(trigger).hasClass('up')) direction = 'up';
  var submenu = jQuery('<div></div>').attr('class', 'menu-submenu ' + direction).attr('style', 'display: none');
  var header = jQuery('<div></div>').attr('class', 'menu-submenu-header');
  var content = jQuery('<div></div>').attr('class', 'menu-submenu-content');
  var list = jQuery('<ul></ul>').attr('class', 'menu-submenu-list');
  var footer = jQuery('<div></div>').attr('class', 'menu-submenu-footer');
  content.append('<h4>' + title + '</h4>');
  jQuery.each(link_list, function(index, link_hash) {
    for (label in link_hash) {
      if(link_hash[label]!=null) {
        if(label=='link' && jQuery.type(link_hash[label])=="string") {
          list.append('<li>' + link_hash[label] + '</li>');
        } else {
          options = "";
          jQuery.each(link_hash[label], function(option, value){
            options += option +'="'+ value + '" ';
          })
          list.append('<li><a '+ options +'>' + label + '</a></li>');
        }
      }
    }
  });
  content.append(list);
  submenu.append(header).append(content).append(footer);
  jQuery(trigger).before(submenu);
  jQuery(submenu).fadeIn();
}

function toggleMenu(trigger) {
  jQuery(trigger).siblings('.simplemenu-submenu').toggle();
}

function hideAllSubmenus() {
  jQuery('.menu-submenu.up:visible').fadeOut('slow');
  jQuery('.simplemenu-submenu:visible').hide().toggleClass('opened');
  jQuery('.menu-submenu.down:visible').fadeOut('slow');
  jQuery('#chat-online-users-content').hide();
}

// Hide visible ballons when clicked outside them
jQuery(document).ready(function() {
  jQuery('body').live('click', function() { hideAllSubmenus(); });
  jQuery('.menu-submenu-trigger').live('click', function(e) { e.stopPropagation(); });
  jQuery('.simplemenu-trigger').live('click', function(e) { e.stopPropagation(); });
  jQuery('#chat-online-users').live('click', function(e) { e.stopPropagation(); });
});

function loading_for_button(selector) {
  jQuery(selector).append("<div class='small-loading' style='width:16px; height:16px; position:absolute; top:0; right:-20px;'></div>");
  jQuery(selector).css('cursor', 'progress');
}

// override this to take action after user_data load
function customUserDataCallback() {
};

function hide_loading_for_button(selector) {
  selector.css("cursor","");
  $(".small-loading").remove();
}

function new_qualifier_row(selector, select_qualifiers, delete_button) {
  index = jQuery(selector + ' tr').size() - 1;
  jQuery(selector).append("<tr><td>" + select_qualifiers + "</td><td id='certifier-area-" + index + "'><select></select>" + delete_button + "</td></tr>");
}

function userDataCallback(data) {
  noosfero.user_data = data;
  customUserDataCallback();
  if (data.login) {
    // logged in
    jQuery('head').append('<meta content="authenticity_token" name="csrf-param" />');
    jQuery('head').append('<meta content="'+jQuery.cookie("_noosfero_.XSRF-TOKEN")+'" name="csrf-token" />');
  }
  if (data.notice) {
    display_notice(data.notice);
    // clear notice so that it is not display again in the case this function is called again.
    data.notice = null;
  }
  // Bind this event to do more actions with the user data (for example, inside plugins)
  jQuery(window).trigger("userDataLoaded", data);
};

// controls the display of the login/logout stuff
jQuery(function($) {
  $.ajaxSetup({
    cache: false,
    headers: {
      'X-XSRF-TOKEN': $.cookie("_noosfero_.XSRF-TOKEN")
    }
  });

  var user_data = noosfero_root() + '/account/user_data';
  $.getJSON(user_data, userDataCallback)

  $.ajaxSetup({ cache: false });
});

// controls the display of contact list
function check_contact_list(contact_list) {
  jQuery(function($) {
    var verify_url = $('#verify-contact-list').attr('href');
    var add_contacts_url = $('#add-contact-list').attr('href');
    var cancel_contacts_fetching_url = $('#cancel-fetching-emails').attr('href');
    var interval = setInterval(function() {
      $.getJSON(verify_url, function(data) {
        if (data.fetched) {
          clearInterval(interval);
          if (data.error) {
            $("#loading-dialog").dialog('close');
            $.get(cancel_contacts_fetching_url);
            redirect_to($('#invitation_back_button').attr('href'));
            display_notice(data.error);
          } else {
            $.get(add_contacts_url, function(data){
              $("#contacts-list").html(data);
            });
          };
          $("#loading-dialog").dialog('close');
        }
      });
    }, 5000);
    setTimeout(function() {
      clearInterval(interval);
      $("#loading-dialog").dialog('close');
      $.get(cancel_contacts_fetching_url);
      redirect_to($('#invitation_back_button').attr('href'));
    }, 600000);
  });
}

function display_notice(message) {
   var $noticeBox = jQuery('<div id="notice"></div>').html(message).appendTo('body').fadeTo('fast', 0.8);
   $noticeBox.click(function() { $(this).hide(); });
   setTimeout(function() { $noticeBox.fadeOut('fast'); }, 5000);
}

jQuery(function($) {
   /* Adds a class to "opera" to the body element if Opera browser detected.
    */
   if ( navigator.userAgent.indexOf("Opera") > -1 ) {
     $('body').addClass('opera');
   }

   /* Adds a class to "msie" to the body element if a Microsoft browser is
    * detected. This is needed to workaround several of their limitations.
    */
   else if ( navigator.appVersion.indexOf("MSIE") > -1 ) {
     document.body.className += " msie msie" +
       navigator.appVersion.replace(/^.*MSIE\s+([0-9]+).*$/, "$1");
   }

   /* Adds a class to "webkit" to the body element if a Webkit based browser
    * detected.
    */
   else if (window.devicePixelRatio) {
     $('body').addClass('webkit');
   }
});

function hide_and_show(hide_elements, show_elements) {
  for(i=0; i < hide_elements.length; i++){
    jQuery(hide_elements[i]).hide();
  }
  for(i=0; i < show_elements.length; i++){
    jQuery(show_elements[i]).show();
  }
}

function limited_text_area(textid, limit) {
  var text = jQuery('#' + textid).val();
  var textlength = text.length;
  jQuery('#' + textid + '_left span').html(limit - textlength);
  if (textlength > limit) {
    jQuery('#' + textid + '_left').hide();
    jQuery('#' + textid + '_limit').show();
    jQuery('#' + textid).val(text.substr(0,limit));
    return false;
  } else {
    jQuery('#' + textid + '_left').show();
    jQuery('#' + textid + '_limit').hide();
    return true;
  }
}

jQuery(function($) {
  $('.autogrow').autogrow();
});

jQuery(function($) {
  $('a').each(function() {
    if (this.href == document.location.href) {
      $(this).addClass('link-this-page');
    }
  });
});

jQuery(function($) {
  if ($.browser.msie) {
    $('.profile_link').click(function() {
      document.location.href = this.href;
    })
  }
  $('.manage-groups > a').live('click', function() {
    toggleMenu(this);
    return false;
  });
});

function add_comment_reply_form(button, comment_id) {
  //var container = jQuery(button).parents('.comment_reply');
  var container = jQuery('#comment_reply_to_'+comment_id);
  var f = container.find('.comment_form');
  if (f.length == 0) {
    comments_div = jQuery(button).parents('.comments');
    f = comments_div.find('.comment_form').first().clone();
    f.find('.errorExplanation').remove();
    f.append('<input type="hidden" name="comment[reply_of_id]" value="' + comment_id + '" />');
    container.append('<div class="page-comment-form"></div>');
    container.find('.page-comment-form').append(f);
  }
  if (container.hasClass('closed')) {
    container.removeClass('closed');
    container.addClass('opened');
    container.find('.comment_form input[type=text]:visible:first').focus();
  }
  jQuery('.display-comment-form').hide();
  return f;
}

function update_comment_count(element, new_count) {
  var $ = jQuery;
  var content = '';
  var parent_element = element.parent();

  write_out = parent_element.find('.comment-count-write-out');

  element.html(new_count);

  if(new_count == 0) {
    content = NO_COMMENT_YET;
    parent_element.addClass("no-comments-yet");
  } else if(new_count == 1) {
    parent_element.removeClass("no-comments-yet");
    content = ONE_COMMENT;
  } else {
    content = new_count + ' ' + COMMENT_PLURAL;
  }

  if(write_out){
    write_out.html(content);
  }

}

function remove_comment(button, url, msg) {
  var $ = jQuery;
  var $button = $(button);
  if (msg && !confirm(msg)) {
    $button.removeClass('comment-button-loading');
    return;
  }
  $button.addClass('comment-button-loading');
  $.post(url, function(data) {
    if (data.ok) {
      var $comment = $button.closest('.article-comment');
      var $replies = $comment.find('.comment-replies .article-comment');

      var $comments_div = $button.closest('.comments');

      var comments_removed = 1;
      $comment.slideUp(400, function() {
        if ($button.hasClass('remove-children')) {
          comments_removed = 1 + $replies.size();
        } else {
          $replies.appendTo('.article-comments-list');
        }

        $comments_div.find('.comment-count').add('#article-header .comment-count').each(function() {
          var count = parseInt($(this).html());
          update_comment_count($(this), count - comments_removed);
        });
        $(this).remove();
      });

    }else{
      $button.removeClass('comment-button-loading');
      return;
    }
  });
}

function remove_item_wall(button, item, url, msg) {
  var $ = jQuery;
  var $wall_item = $(button).closest(item);
  $wall_item.addClass('remove-item-loading');
  if (msg && !confirm(msg)) {
    $wall_item.removeClass('remove-item-loading');
    return;
  }
  $.post(url, function(data) {
    if (data.ok) {
      $wall_item.slideUp();
    } else {
      $wall_item.removeClass('remove-item-loading');
      window.location.replace(data.redirect);
    }
  });
}

function original_image_dimensions(src) {
  var img = new Image();
  img.src = src;
  return { 'width' : img.width, 'height' : img.height };
}

function gravatarCommentFailback(img) {
  var link = img.parentNode;
  link.href = "//www.gravatar.com";
  img.src = img.getAttribute("data-gravatar");
}

jQuery(function() {
  jQuery("#ajax-form").before("<div id='ajax-form-loading-area' style='display:block;width:16px;height:16px;'></div>");
  jQuery("#ajax-form").before("<div id='ajax-form-message-area'></div>");
  jQuery("#ajax-form").ajaxForm({
    beforeSubmit: function(a,f,o) {
      jQuery('#ajax-form-message-area').html('');
      o.loading = small_loading('ajax-form-loading-area');
    },
    success: function() {
      loading_done('ajax-form-loading-area');
    },
    target: "#ajax-form-message-area"
  })
});

// from http://jsfiddle.net/naveen/HkxJg/
// Function to get the Max value in Array
Array.max = function(array) {
  return Math.max.apply(Math, array);
};
// Function to get the Min value in Array
Array.min = function(array) {
  return Math.min.apply(Math, array);
};

function hideAndGetUrl(link) {
  document.body.style.cursor = 'wait';
  jQuery(link).hide();
  url = jQuery(link).attr('href');
  jQuery.get(url, function( data ) {
    document.body.style.cursor = 'default';
  });
}

jQuery(function($){
  $('.submit-with-keypress').live('keydown', function(e) {
     field = this;
     if (e.keyCode == 13) {
       e.preventDefault();
       var form = $(field).closest("form");
       $.ajax({
           url: form.attr("action"),
           data: form.serialize(),
           beforeSend: function() {
             loading_for_button($(field));
           },
           success: function(data) {
             var update = form.attr('data-update');
             $('#'+update).html(data);
             $(field).val($(field).attr('title'));
           }
       });
       return false;
     }
   });

  $('#content').delegate( '.view-all-comments a', 'click', function(e) {
     hideAndGetUrl(this);
     return false;
   });

  $('#content').delegate('.view-more-replies a', 'click', function(e) {
    hideAndGetUrl(this);
    return false;
  });

  $('#content').delegate('.view-more-comments a', 'click', function(e) {
    hideAndGetUrl(this);
    return false;
  });

  $('.focus-on-comment').live('click', function(e) {
     var link = this;
     $(link).parents('.profile-activity-item').find('textarea').focus();
     return false;
  });
});

/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) {
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) {
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);

/*
 * altBeautify: put a styled tooltip on elements with
 * HTML on title and alt attributes.
 */

var altBeautify = jQuery('<div id="alt-beautify" style="display:none; position: absolute"/>')
  .append('<div class="alt-beautify-content"/>')
  .append('<div class="alt-beautify-arrow-border alt-beautify-arrow"/>')
  .append('<div class="alt-beautify-arrow-inner alt-beautify-arrow"/>');
var altTarget;
jQuery(document).ready(function () {
  jQuery('body').append(altBeautify);
});

function altTimeout() {
  if (!altTarget)
    return;
  altBeautify.css('top', jQuery(altTarget).offset().top + jQuery(altTarget).height());
  altBeautify.css('left', jQuery(altTarget).offset().left);
  altBeautify.find('.alt-beautify-content').html(jQuery(altTarget).attr('alt-beautify'));
  altBeautify.show();
}

function altHide() {
  altTarget = null;
  altBeautify.hide();
}

function altShow(e) {
  alt = jQuery(this).attr('title');
  if (alt != '') {
    jQuery(this).attr('alt-beautify', alt);
    jQuery(this).attr('title', '');
  }

  altTarget = this;
  setTimeout("altTimeout()", 500);
}

(function($) {

  jQuery.fn.altBeautify = function() {
    return this.each(function() {
      jQuery(this).bind('mouseover', altShow);
      jQuery(this).bind('mouseout', altHide);
      jQuery(this).bind('click', altHide);
    });
  }

})(jQuery);

// enable it generally
// jQuery('*[title]').live('mouseover', altShow);
// jQuery('*[title]').live('mouseout', altHide);
// jQuery('*[title]').live('click', altHide);
// jQuery('image[alt]').live('mouseover', altShow);
// jQuery('image[alt]').live('mouseout', altHide);
// jQuery('image[alt]').live('click', altHide);


if ( !console ) console = {};
if ( !console.log ) console.log = function(){};

// Two ways to call it:
// log(mixin1[, mixin2[, ...]]);
// log('<type>', mixin1[, mixin2[, ...]]);
// Where <type> may be: log, info warn, or error
window.log = function log() {
  var type = arguments[0];
  var argsClone = jQuery.merge([], arguments); // cloning the read-only arguments array.
  if ( ['info', 'warn', 'error'].indexOf(type) == -1 ) {
    type = 'log';
  } else {
    argsClone.shift();
  }
  var method = type;
  if ( !console[method] ) method = 'log';
  console[method].apply( console, jQuery.merge([(new Date).toISOString()], argsClone) );
}

// Call log.info(mixin1[, mixin2[, ...]]);
log.info = function() {
  window.log.apply(window, jQuery.merge(['info'], arguments));
}

// Call log.warn(mixin1[, mixin2[, ...]]);
log.warn = function() {
  window.log.apply(window, jQuery.merge(['warn'], arguments));
}

// Call log.error(mixin1[, mixin2[, ...]]);
log.error = function() {
  window.log.apply(window, jQuery.merge(['error'], arguments));
}

function showHideTermsOfUse() {
  if( jQuery("#article_has_terms_of_use").attr("checked") )
    jQuery("#text_area_terms_of_use").show();
  else {
    jQuery("#text_area_terms_of_use").hide();
    jQuery("#article_terms_of_use").val("");
    jQuery("#article_terms_of_use_ifr").contents().find("body").html("");
  }
}

jQuery('.profiles-suggestions .explain-suggestion').live('click', function() {
  var clicked = jQuery(this);
  clicked.toggleClass('active');
  clicked.next('.extra_info').toggle();
  return false;
});

jQuery('.suggestions-block .block-subtitle').live('click', function() {
  var clicked = jQuery(this);
  clicked.next('.profiles-suggestions').toggle();
  clicked.nextAll('.more-suggestions').toggle();
  return false;
});

jQuery(document).ready(function(){
  showHideTermsOfUse();

  jQuery("#article_has_terms_of_use").click(function(){
    showHideTermsOfUse();
  });

  // Suggestions on search inputs
  (function($) {
    var suggestions_cache = {};
    $(".search-input-with-suggestions").autocomplete({
      minLength: 2,
      select: function(event, ui){
        $(this).val(ui.item.value);
        $(this).closest('form').submit();
      },
      source: function(request, response) {
        var term = request.term.toLowerCase();
        if (term in suggestions_cache) {
          response(suggestions_cache[term]);
          return;
        }
        request["asset"] = this.element.data("asset");
        $.getJSON("/search/suggestions", request, function(data, status, xhr) {
          suggestions_cache[term] = data;
          response(data);
        });
      }
    });
  })(jQuery);
});

function apply_zoom_to_images(zoom_text) {
  jQuery(function($) {
    $(window).load( function() {
      $('#article .article-body img:not(.disable-zoom)').each( function(index) {
        var original = original_image_dimensions($(this).attr('src'));
        if ($(this).width() < original['width'] || $(this).height() < original['height']) {
          $(this).wrap('<div class="zoomable-image" />');
          $(this).parent('.zoomable-image')
            .attr({style: $(this).attr('style')})
            .addClass(this.className)
            .css({
              width: $(this).width(),
              height: $(this).height(),
            });
          $(this).attr('style', '');
          $(this).after('<a href="' + $(this).attr('src') + '" class="zoomify-image"><span class="zoomify-text">'+zoom_text+'</span></a>');
        }
      });
      $('.zoomify-image').fancybox();
    });
  });
}

function notifyMe(title, options) {
  // Let's check if the browser supports notifications
   if (!("Notification" in window)) {
     return null;
   }

  if(PERMANENT_NOTIFICATIONS)
    options.requireInteraction = true

  // Let's check if the user is okay to get some notification
  var notification = null;
  if (Notification.permission === "granted") {
    // If it's okay let's create a notification
    notification = new Notification(title, options);
  }

  // Otherwise, we need to ask the user for permission
  // Note, Chrome does not implement the permission static property
  // So we have to check for NOT 'denied' instead of 'default'
  else if (Notification.permission !== 'denied') {
    Notification.requestPermission(function (permission) {
      // Whatever the user answers, we make sure we store the information
      if (!('permission' in Notification)) {
        Notification.permission = permission;
      }

      // If the user is okay, let's create a notification
      if (permission === "granted") {
        notification = new Notification(title, options);
      }
    });
  }

  notification.onclick = function(){
    notification.close();
    // Chromium tweak
    window.open().close();
    window.focus();
  };

  return notification;
  // At last, if the user already denied any notification, and you
  // want to be respectful there is no need to bother them any more.
}

function start_fetching(element){
  jQuery(element).append('<div class="fetching-overlay">Loading...</div>');
}

function stop_fetching(element){
  jQuery('.fetching-overlay', element).remove();
}

function add_new_file_fields() {
  var cloned = jQuery('#uploaded_files p:last').clone();
  cloned.find("input[type='file']").val('');
  cloned.appendTo('#uploaded_files');
  jQuery('body').scrollTo(cloned);
}

window.isHidden = function isHidden() { return (typeof(document.hidden) != 'undefined') ? document.hidden : !document.hasFocus() };

function $_GET(id){
    var a = new RegExp(id+"=([^&#=]*)");
    var result_of_search = a.exec(window.location.search)
    if(result_of_search != null){
      return decodeURIComponent(result_of_search[1]);
    }
}

var fullwidth=false;
function toggle_fullwidth(itemId){
  if(fullwidth){
    jQuery(itemId).removeClass("fullwidth");
    jQuery("#fullscreen-btn").show()
    jQuery("#exit-fullscreen-btn").hide()
    fullwidth = false;
  }
  else{
    jQuery(itemId).addClass("fullwidth");
    jQuery("#exit-fullscreen-btn").show()
    jQuery("#fullscreen-btn").hide()
    fullwidth = true;
  }
  jQuery(window).trigger("toggleFullwidth", fullwidth);
}

function fullscreenPageLoad(itemId){
  jQuery(document).ready(function(){

    if ($_GET('fullscreen') == 1){
      toggle_fullwidth(itemId);
    }
  });
}