// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req.localPath = Req.localPath || '/skin/basic/';
Req(
  'eutils',
  'fontsizer',
  'autovalidate',
  'labelizor',
  'imgpop',
  'mailtoenabler',
  'easing-mini',
  'switchlist',
  'x/ui-accordion 1.7',
  'tabswitcher',
  'equalizeheights',
  'anchortags',
  'q-alistdatesplit',

  function(){
    var $ = jQuery,
        body = $('body').addClass('js-active'),
        bVer =   parseInt( $.browser.version, 10 ),
        msie =   $.browser.msie   &&  bVer<9,  // or: $('html').is('.msie');
        msie6 =  msie  &&  bVer<7,
        isIS  = $('html').attr('lang'),
        homePage = body.is('.home');


    //Equalize heights
    if ( !homePage )
    {
      $('.pgextra2, .pgmain').equalizeHeights();
    }


    // labelize search input
    $('#s_email, #v_search').labelizor();
    
    // labelize search input
    $('.netfang').mailtoEnabler();

    // Fix mnav jumping - li:hover problem.
    $('.mnav li')
        .bind('focusin', function (e) {
            $(this).addClass('hover');
          })
        .bind('focusout', function (e) {
            $(this).removeClass('hover');
          });

    //addclass for 2liners in snav
    $('.twocol .snav .level1 li > a').each(function () {
        if ($(this).outerHeight() > 38)
        {
          $(this).closest('li').addClass('dline');
        }
      });


    if (!window.EPLICA_loggedin)
    {

      // pngfix for IE6
      if (msie6)
      {
        $('img[src$=".png"]').Req('x/ifixpng', function(){
            $(this).ifixpng();
          });
      }

      //zebra tables
      $('tbody tr:nth-child(2n-1)').addClass('odd');


      //popup in articles
      $('.article .imagebox a.img')
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc);
          })
          .imgPopper({
              curtainColor : '#ffffff',
              curtainOpacity : '0.75',
              yOffset: 15
            });
            
      $('.article a.videolink').Req(
          'q-videolinks',
          function() {
            $(this).videoLinks();
          }
        );

      //Add classess for filetypes
      var anchors = $('.mwrap a');
      anchors
          .anchorTags({
              usePatterns:  ['doc', 'pdf', 'xls']
            })
          .filter('a.external, a.file_pdf, a.file_doc, a.file_xls')
              .filter(function() { return !$('img', this).length })
                  .each(function() {
                      var match = $(this)[0].className.match(/(^| )file_([a-z]+)( |$)/);
                      var action = $(this).closest('.pgmain').length ? 'append' : 'prepend';
                      if ( match ) 
                      {
                        var text = match[2];
                        $(this).attr('title', text+' skjal');
                      }
                      else if ( $(this).is('.external') )
                      {
                        var externalText = isIS ? 'Opnast í nýjum vafraglugga' : 'Open in new window';
                        $(this).attr('title', externalText);
                      }
                    });


      //add wrapper to buttons
      $('.buttons a').wrapInner('<span/>');

    } // end loggedin


    if ( homePage )
    {
      
      //trimSummaries function
        var trimSummaries = function(s, l)
        {
          if( $.trim(s.text()).length > l ) {
            var imgElm = $('img', s).clone(),
                moreBtn = $('span.more', s).clone();
            imgElm.remove();
            moreBtn.remove();

            var summText = $.cropText(s.text(), l);

            s.text( summText ).prepend( imgElm ).append(moreBtn);
          }
        };
        
        var fpbox = $('.fpbox');
        fpbox.find('.summary')
                .each(function () {
                    trimSummaries($(this), 130);
                  });

        //switchlist on front page
        fpbox.find('.boxbody')
            .switchlist({
                items         : '.item',
                inclmeta      : false,
                switchdelay   : 350,
                fadeoutspeed  : 125,
                fadeoutease   : 'easeOut',
                fadeinspeed   : 400,
                fadeinease    : 'easeIn'
              })
            .find('.itemlist li a')
                .wrapInner('<span/>');

        //accordion box 
        $('.accbox')
            .wrapAll('<div class="accbox" />')
            .removeClass('accbox')
            .find('.boxhead').wrapInner('<a href="#" />').end()
            .parent()
                .accordion({
                    icons: { 'header': 'ui-icon-closed', 'headerSelected': 'ui-icon-open' },
                    header     : '.boxhead',
                    fillSpace  :  true
                  })
                //stupid jquery ui bug making headers not keyboard navigateable with tabindex: -1
                .bind('accordionchange', function(event, ui) {
                    ui.oldHeader.attr('tabindex', '');
                  })
                .find('h2.boxhead')
                    .attr('tabindex', '');

        var newslist = $('.newslist');
        newslist
            .makeTabbox()
                .find('a')
                    .each(function(){
                        var link = $(this);
                      })
                .end()
                .tabSwitcher();


        //alist date stuff
        newslist
            .alistDateSplit()
            .find('ul')
                .alistDateSplit({ dateSel: 'li .date' });
    }


    //sitemap collapser
    $('.sitemap').Req('treecollapse', function() { 
        $(this).treeCollapse({ 
            branch: 'li:has(ul)', 
            doTogglers: 1, 
            toggler: '> a.expand',
            startOpen: 'ul.level1 > li:has(ul)'
          }); 
        });

    $('#noflickerCSS').remove();
    $('.pagestyle').fontsizer();
    $('form').autoValidate();

  }
);
// **** /jqreq *****

