/* ex: set tabstop=2 expandtab softtabstop=2 shiftwidth=2 noai: */

$(document).ready(function() {

  /* add target='_blank' to links with rel == 'external' for XHTML strict */
  $("a[rel=external]").attr('target', '_blank');

  $("a button").click(function() {
    location.href=this.parentNode.href;
    return false;
  });

  /* set focus to first enabled text or password field, except for IE7 */
  if (!stoha.isIE7) {
    $('form input:enabled:text, form input:enabled:password').filter(':first').focus();
  }

  if ($.fn.expander) {
    $(".hoofdartikel-tekst-kader").expander({
      slicePoint: 350,
      expandPrefix: '',
      expandText: ''
    });

    $(".hoofdartikel-tekst-kader .read-more").each(function() {
      $(".hoofdartikel-voet a").show();
    });
    $('.stoha-nieuws-bol').fadeIn(4000);
  }

  if ($.fn.truncate) {
    $(".nieuws-tekst").truncate(150, {
      chars: /\s/,
      trail: [ "&hellip; <a href='#' class='truncate_show'>&gt;&gt;</a>", "&nbsp;<a href='#' class='truncate_hide'>&lt;&lt;</a>" ]
    });

    $(".truncate_less").find('p:last').css('display', 'inline');
  }

  if ($.fn.jCarouselLite) {
    $(".jCarouselLite-bulletin").jCarouselLite( {
      btnNext: ".next-bulletin",
      btnPrev: ".prev-bulletin",
      visible: 4,
      scroll: 4,
      circular: false
    });

    $(".jCarouselLite-excursie").jCarouselLite( {
      btnNext: ".next-photo",
      btnPrev: ".prev-photo",
      visible: 4,
      scroll: 4,
      circular: false
    });

    $(".jCarouselLite-reunie-1").jCarouselLite( {
      btnNext: ".next-photo-1",
      btnPrev: ".prev-photo-1",
      visible: 4,
      scroll: 4,
      circular: false
    });

    $(".jCarouselLite-reunie-2").jCarouselLite( {
      btnNext: ".next-photo-2",
      btnPrev: ".prev-photo-2",
      visible: 4,
      scroll: 4,
      circular: false
    });

    $(".jCarouselLite-reunie-3").jCarouselLite( {
      btnNext: ".next-photo-3",
      btnPrev: ".prev-photo-3",
      visible: 4,
      scroll: 4,
      circular: false
    });

    $(".jCarouselLite-series").jCarouselLite( {
      btnNext: ".next-photo",
      btnPrev: ".prev-photo",
      visible: 4,
      scroll: 4,
      circular: false
    });

    $(".jCarouselLite-series2").jCarouselLite( {
      btnNext: ".next-photo",
      btnPrev: ".prev-photo",
      visible: 2,
      scroll: 2,
      circular: false
    });

    $(".jCarouselLite-yearphoto").jCarouselLite( {
      btnNext: ".next-photo",
      btnPrev: ".prev-photo",
      visible: 2,
      scroll: 2,
      circular: false
    });
  }

  if ($.fn.lightbox) {
    $(".lightbox").lightbox({
      fitToScreen: true,
      fileLoadingImage: stoha.baseURI + "img/lightbox/loading.gif",
      fileBottomNavCloseImage : stoha.baseURI + "img/lightbox/closelabel.gif"
    });
  }

});

/* EditPerson form */

EditPerson = {
  init_check_saved: function(form_id) {
    var frm = $(form_id);

    frm.submit(function() { EditPerson.form_saved = true; });

    $(form_id + ' input').each(function(i) {
      var itype = $(this).attr('type');
      if (itype != 'hidden' && itype != 'submit' && itype !='reset') {
        $(this).change(function() { EditPerson.form_changed = true; });
      }
    });
    $(form_id + ' select').each(function(i) {
        $(this).change(function() { EditPerson.form_changed = true; });
    });
    $(form_id + ' textarea').each(function(i) {
        $(this).change(function() { EditPerson.form_changed = true; });
    });

    $(window).unload(function() {
      if (!EditPerson.form_saved) { 
        if (!EditPerson.form_changed && typeof(tinyMCE) != 'undefined' && tinyMCE.activeEditor.isDirty) {
          EditPerson.form_changed = tinyMCE.activeEditor.isDirty();
        }
        if (EditPerson.form_changed && confirm('De gegevens zijn nog niet opgeslagen, opslaan?')) {
          frm.submit();
          return true;
        }
        return false;
      }
      return true;
    })
  },

  initFormNewsItem: function() {
    $(document).ready(function() {
      EditPerson.form_saved = false;
      EditPerson.form_changed = false;
      EditPerson.init_check_saved('#EditForm');

      $('#NewsItemItemId').change(function() {
        this.form.submit();
      });
      $('#NewsItemItemType1').click(function() {
        $('#NewsItemItemUrl').attr('disabled', 'disabled');
        $('#NewsItemItemText').removeAttr('disabled');
      });
      $('#NewsItemItemType2').click(function() {
        $('#NewsItemItemText').attr('disabled', 'disabled');
        $('#NewsItemItemUrl').removeAttr('disabled');
      });

      $('#NewsItemDatumkeuze0').click(function() {
        $('#NewsItemEndDate').attr('disabled', 'disabled');
      });
      $('#NewsItemDatumkeuze1').click(function() {
        $('#NewsItemEndDate').removeAttr('disabled');
      });

      $('#wysiwyg').click(function() {
        tinyMCE.execCommand('mceToggleEditor',false,'NewsItemItemText');
      });

    });
  },

  initProjectReport: function() {
    $(document).ready(function() {
      $('select#ProjectReportPrjId').change(function() {
        if ($(this).val() > 0) {
          v = $(this).find('option:selected').text();
        } else {
          v = '';
        }
        $('input#ProjectReportProjectNaam').val(v);
      });
      EditPerson.form_saved = false;
      EditPerson.form_changed = false;
      EditPerson.init_check_saved('#ProjectReportAddForm');
    });
  },


  initPersonForm: function(form) {
    $(document).ready(function() {
      $('#PersonLand').attr('disabled', $('#PersonLandkeuze0').is(':checked'));

      $('#PersonLandkeuze0').click(function() {
        $('#PersonLand').attr('disabled', 'disabled');
      });
      $('#PersonLandkeuze1').click(function() {
        $('#PersonLand').removeAttr('disabled');
      });

      EditPerson.form_saved = false;
      EditPerson.form_changed = false;
      EditPerson.init_check_saved(form);
    });
  },

  initForm: function(form) {
    $(document).ready(function() {
      EditPerson.form_saved = false;
      EditPerson.form_changed = false;
      EditPerson.init_check_saved(form);
    });
  }

}

