// 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',
  'anchortags',
  'labelizor',
  'imgpop',

  function(){
    var $ = jQuery,
        body = $('body').addClass('js-active'),
        article = $('.article'),
        msie =   $.browser.msie   &&  parseInt($.browser.version,10)<9;  // or: $('html').is('.msie');

    var texts = {
        is: {
            newWindow:        'Opnast í nýjum vafraglugga',
            fileText:         'skjal'
          },
        en: {
            newWindow:        'Opens in new window',
            fileText:         'file'
          }
      };
    texts = texts[ $.lang() ]  ||  texts.en;

    // labelize search input
    $('#qstr').labelizor();

    if (!window.EPLICA_loggedin)
    {
      var article = $('.article');

      article.find('a.videolink').Req(
          'q-videolinks',
          function() {
            $(this).videoLinks();
          }
        );

      //zebra tables
      $('tbody tr:nth-child(2n-1)').addClass('odd');
      //$('tbody tr:nth-child(2n)').addClass('even');


      //popup in articles
      article.find('.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.find('a.videolink').Req(
          'q-videolinks',
          function() {
            $(this).videoLinks(); //{ vidWidth:'auto',vidHeight:'auto',aspect4x3:false }  Set width and height (default auto) and aspect 4x3 autocalc (default 16x9)
          }
        );


      //tag external urls
      var link, text, useappend, match;
      article.find('a').not('.kennsluefni a').anchorTags({
                patterns: {
                    'xlm': { check: /\.(xlsm)(#|$|\?)/i } // add Excel macro files
                  },
                usePatterns:  ['doc', 'pdf', 'xls', 'xlm']
              })
            .filter('.external, .file_pdf, .file_doc, .file_xls, .file_xlm')
                .not(':has(img)')
                    .each(function() {
                        if (this.className == 'file_xlm') {
                          this.className = 'file_xls'; // treat Excel macros as XLS files
                        }
                        link = $(this);
                        text = '';
                        useappend = false;
                        match = this.className.match(/(^| )file_([a-z]+)( |$)/);
                        if ( match )
                        {
                          text = match[2].toUpperCase() +' '+ texts.fileText;
                        }
                        else if ( $(this).is('.external') )
                        {
                          text = texts.newWindow;
                          useappend = true;
                        }
                        if (text)
                        {
                          link
                              .attr('target', '_blank')
                              .attr('title', (link.attr('title')||link.text()) +' ('+ text+')')
                              [useappend ? 'append' : 'prepend'](' <span class="icon">('+ text +')</span> ');
                        }
                      });

    } // end loggedin

    //sitemap collapser
    $('.sitemap').Req('treecollapse', function() { 
        $(this).treeCollapse({ 
            branch: 'li:has(ul)', 
            doTogglers: 1, 
            toggler: '> a.expand',
            startOpen: 'ul.level1 > li:has(ul)'
          }); 
        });

    //remove flicker trick
    $('#noflickerCSS').remove();

    // fontsizer
    $('.pagestyle').fontsizer();

    // validate all forms
    $('form').autoValidate();

    //loading body class scripts 
    Req.loadPageScripts();

  }
);
// **** /jqreq *****

