// 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/basicpe/'
Req(
  'eutils',
  'fontsizer',
  'autovalidate',
  'anchortags',
  'mailtoenabler',
  'listscroller',

  function(){
    var $ = jQuery,
        body = $('body'),
        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;

    //cleanup
    article.find('table .alt').removeClass('alt');


    $('.fpimages').listscroller({ 
          item       : 'li',
          animation  : 'crossfade',
          windowSize : 1,
          stepSize   : 1,
          paging     : true,
          statusPager: false,
          jumpPager  : false,
          autoScrollDelay: 5000
        });

    if (!window.EPLICA_loggedin)
    {
      $('.articlelist .summary').each(function () {
          var imgs = $(this).find('img');
          if (imgs.length == 1) 
          {
            imgs.prependTo( $(this) );
          }
          else if (imgs.length > 1)
          {
            for (var i=0, l=imgs.length; i<l; i+=3)
            {
              $('<div class="imagelist" />').append(imgs.slice(i,i+3)).appendTo( $(this) );
            }
          }
          $(this).find('p').filter(function() { return (!$.trim($(this).text()) && !$(this).find('img').length )  }).remove();
        });

      //zebra tables
      $('table:not(.sortable) tbody tr:nth-child(2n)').addClass('odd');


      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;
      $('a').anchorTags({
                usePatterns:  ['doc', 'pdf', 'xls'],
                localDomains:  ['(www\\d?.)?fa.is', 'favefur.eplica.is']
              })
            .filter('.external, .file_pdf, .file_doc, .file_xls')
                    .each(function() {
                        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+')')
                              .not(':has(img)')
                                  [useappend ? 'append' : 'prepend'](' <span class="icon">('+ text +')</span> ');
                        }
                      });


      $('table.sortable').Req(
          Req.localPath + 'js/jquery.tablesorter.min.js',
          function(){
            $(this).tablesorter(); 
          }
        );


      //video popper
      var videolinks = $('table.videotable a');
      videolinks
          .each(function(i) {
              var href = $(this).attr('href'),
                  dim = href.split('.');
              if (dim[2]) {
                var dim2 = dim[1].split('x'),
                    asp = dim2[1] / dim2[0],
                    calcHeight = 690 * asp;
              } else {
                var calcHeight = 498;
              }
              calcHeight += 20;
              $(this).click(function () {
                    $('#video').remove();
                    
                    if ( $(this).is('[href$=".wmv"]') ) {
                        $('h1').before('' +
                            '<p id="video"><object width="690" height="498" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112">' +
                            ' <param name="url" value="' + href + '"><param name="autostart" value="true" />' +
                            ' <embed src="' + href + '" width="690" height="498" AutoStart="true" name="MediaPlayer" type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"></embed>' +
                            '</object></p>');
                    } else {
                        $('h1').before('' +
                            '<p id="video"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8.0.0.0" width="690" height="'+ calcHeight +'">' +
                            '<param name="movie" value="/media/videos/mediaplayer.swf?file=' + href + '&autostart=true" /><param name="wmode" value="window" /><param name="allowFullScreen" value="true" />' +
                            '<embed src="/media/videos/mediaplayer.swf?file=' + href + '&autostart=true"' +
                            'width="690" height="'+ calcHeight +'" wmode="transparent" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
                            '</object></p>');
                    }
                    return false;
                });
            })
          .filter(':first').trigger('click');


      //employee list ajax
      $('.netfang').mailtoEnabler();
    } // 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
    $('#noFOUC-main').remove();
    $('.pagestyle').fontsizer();
    $('form').autoValidate();

    //loading body class scripts 
    Req.loadPageScripts();

  }
);
// **** /jqreq *****

