// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req(
  'fontsizer',
  'autovalidate',
  'x/ifixpng',
  'imgpop',
  'x/innerfade',
  'filtertable',
  'zebralists',
  'mailtoenabler',
  function(){
    var $ = jQuery,
        i18n = {
            is: {  months: 'jan,feb,mar,apr,maí,jún,júl,ágú,sep,okt,nóv,des'.split(',')  },
            en: {  months: 'jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec'.split(',')  }
          };

    $('body').addClass('js-active');

    // remove flicker trick
    $('#noflickerCSS').remove();


    if (!window.EPLICA_loggedin)
    {

      //introbox innerfade
      var heights = [],
          maxHeight;
      $('.introbox ul')
          .filter(function(){ return $(this).find('li').length > 1; })
              .each(function() {
                  $(this).find('li')
                      .each(function() {
                            heights.push( $(this).height() )
                          });
                  maxHeight = Math.max.apply(null, heights);
                })
              .innerfade({
                  animationtype: 'fade',
                  speed: 1000,
                  timeout: 3000,
                  type: 'sequence',
                  containerheight: '113'
                })
              .parent()
                  .css({ height : maxHeight });

      // frontpage eventlist
      $('.pgmain .events .item .meta')
          .each(function(){
              var metaElm = $(this),
                  dateElm = metaElm.find('> .date'),
                  dt = $.trim( dateElm.text() ).split(','), // split "12.8.2009 - 19.8.2009, kl. 14:00 - 18:00" into date and time substrings
                  t = dt[1],             // get the time part
                  d = dt[0].split('.'), // get the date part
                  lang = metaElm.closest('[lang]').attr('lang'),
                  month = (i18n[lang] || i18n.en).months[ parseInt(d[1],10) - 1 ];

              // move the .meta to after the `h3 a` link
              metaElm.appendTo( metaElm.parent() );

              dateElm
                  .empty()
                  .addClass('date-split')
                  .append('<span class="day">' + d[0] + '</span> <span class="month">' + month + '</span> <span class="year">' + d[2] + '</span>')
                  .after( t ? '<span class="time">' + t + '</span>' : '' );
            });



      //move einfo box to top of article
      $('.article .einfo').each(function() {
          $(this).prependTo( $(this).parent() );
        });


      //mnav bubbles
      var isIe = $.browser.msie && $.browser.version < 9;
      $('.mnav li').each(function() {
          var classname = this.className,
              catname = $.trim( classname.replace(/twocol|threecol|milli|branch|current|parent|homecurrent/g,'') );
          if (window.mnavtexts && mnavtexts[catname])
          {
            $(this)
              .append('<div class="bubblebox" style="display:none;"><div class="bubblewrap">' +
                      mnavtexts[catname] +
                      '</div></div>')
              .bind('mouseenter', function() {
                  ( $(this).hasClass('current') || $(this).hasClass('parent') ) ?  '' : $(this).addClass('hover');
                  $(this).find('.bubblebox').fadeIn( isIe ? 0 : 200, function() { if(isIe) { this.style.removeAttribute('filter') } } );
                })
              .bind('mouseleave', function() {
                  $(this).removeClass('hover').find('.bubblebox').stop().css({opacity:1}).fadeOut( isIe ? 0 : 100 );
                });
          }
        });

      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').ifixpng();
      }

      var article = $('.article');

      //zebra tables
      $('tbody tr:nth-child(2n)').addClass('alt');

      //popup in articles
      article.find('.imgbox a:has(img)')
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
              curtainColor : '#000000',
              curtainOpacity : '0.75'
            });

      //Article collapser
      var collapsearticle = article.filter('.collapse');
      if ( collapsearticle.length) {
        $('h4', collapsearticle).each(function() {
            var heading = $(this);
            heading.wrapInner('<a href="#" />');
            heading.add( heading.nextUntil('h2, h4') ).wrapAll('<div class="collapsebox" />');
          });
        $('.collapsebox', collapsearticle)
            .each(function() {
                var collapsebox = $(this);
                collapsebox.find('> *:not(h4)').wrapAll('<div class="collapsecontent" />').parent().hide();
                collapsebox
                    .find('h4 a').bind('click', function() {
                        collapsebox.find('.collapsecontent').slideToggle(100);
                        collapsebox.toggleClass('collapse-open');
                        return false;
                      });
              });
      }

      var hash = document.location.href.split(/[?#]/)[1];
      if ( hash )
      {
        hash = hash.replace('id=', '#');
        $(hash).find('a').trigger('click');
      }

      $('.netfang').mailtoEnabler()
          .filter('em').zap();

      var zebraize = function (e) {
              $(this).find('tbody')
                  .zebraLists({
                      //subItems:    '.subrow',  // filter for rows to consider part of/subservient to the previous row
                      //resetItems:  '',         // filter for rows to consider to be a zebra-splitter/-resetter (like sub-headings)
                      oddClass:    '',
                      evenClass:   'alt'
                    });
            };

      //stafflist table filter
      $('table.filtertable')
          .each(function(){
              var table = $(this),
                  isDbDb = table.is('.databasedatabase');
              table.find('tr').removeClass('alt').end().run( zebraize );;
              if (isDbDb)
              {
                var lang = table.closest('[lang]').attr('lang') || 'en';
                table.find('tbody tr')
                    .filter(function(){  // filter out rows in other languages - to speed up the sorting
                        var rowLang = $(this).attr('lang');
                        return  rowLang  &&  rowLang != lang;
                      })
                    .remove();
              }
              table
                  .filterTable({
                      multiFilter: isDbDb,
                      includeCols: 'th:not(.nofilter)'
                    });

            })
          .bind('filter.filterTable', zebraize);
            
      var sharethisWrap = $('body').not('.home').find('.pgmain .wrap');
      if ( sharethisWrap.length ) {
          sharethisWrap
              .append('<div class="sharethis"><span class="st_facebook" displayText="Facebook" /><span class="st_twitter" displayText="Tweet" /><span class="st_email" displayText="Email" /><span class="st_sharethis" displayText="Share" /><span class="st_plusone_button" style="position: relative; top: 3px;" /><span class="st_fblike" /></div>')
              .Req(
                  'http://w.sharethis.com/button/buttons.js',
                  function() {
                      stLight.options({publisher:'003b8661-1549-44d1-a33e-d481130a2827'});
                  });
      }

    } // end logged in


    var dnav = $('.dnav'),
         dnavHover = false;
    dnav
        .find('ul').hide().end()
        .find('h2.boxhead')
            .wrapInner('<a href="#"/>')
            .find('a')
                .bind('click', function() {
                    dnav.find('ul').slideToggle(300);
                    return false;
                })
            .end()
        .end()
        .bind('mouseleave focusout', function(e){
            dnavHover = false;
            setTimeout(function(){
                if(!dnavHover) {
                  dnav.find('ul').slideUp(300)
                }
              }, 400);
          })
        .bind('mouseenter focusin', function(e){
            dnavHover = true;
          });

/*
    $('body.loginajax').run(function(){
        var pgm = $(this).find('.pgmain .wrap');
        if (!pgm.find('> div').length) {
          $.get(
              'http://www.opnihaskolinn.is/gestir',
              { justPicPos: 'pgmain' },
              function(data){
                  $(data).find('.subscribe').appendTo( pgm ).addClass('contactform box').find('input[name=TargetURL]').val('/vefsvaedi-kennara/gogn');
                }
            );
        }
      });
*/



    // fontsizer
    $('div.pagestyle').fontsizer();

    // validate all forms
    $('form').autoValidate();

    //temp skype fix
    $('.skype_pnh_print_container').zap();
    $('.skype_pnh_container').remove();
  }
);
// **** /jqreq *****



