// 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',
  'x/cookie',
  'fontsizer',
  'autovalidate',
  'anchortags',
  'labelizor',
  'imgpop',
  'tabswitcher',
  'twitterize',
  'listscroller',
  'q-alistdatesplit',
  'q-videolinks',

  function(){
    var $ = jQuery,
        body = $('body').addClass('js-active'),
        article = $('body.blog .article'),
        msie =   $.browser.msie   &&  parseInt($.browser.version,10)<9;

    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
    $('.contactform').find('.fi_txt input, textarea').labelizor();


    $('.tabpane').makeTabbox().tabSwitcher();
      
      
    // mnav icons
    var mnav = $('.mnav'),
        mnavAs = mnav.find('li a').append('<span />'),
        mnavCurrent = mnav.find('.parent a, .current a');

    if ( !mnavCurrent[0]  &&  body.is('.home') )
    {
      mnavCurrent = mnav.find('.home a');
    }
    mnavCurrent.css('padding-right',61).find('span').css('width',54);
    
    mnavAs
        .bind('mouseenter showicon', function (e) {
            if ( !$(this).parent().is('.current, .parent') )
            {
              mnavCurrent.stop().trigger('hideicon');
            }
            $(this)
                .stop()
                .animate({'padding-right': 61}, { queue: false, duration: 250 })
                .find('span')
                    .stop()
                    .animate({width: 54}, { queue: false, duration: 250 });
          })
        .bind('mouseleave hideicon', function (e) {
            mnavCurrent.stop().trigger('showicon');
            if (!($(this).parent().is('.current, .parent') && e.type == 'mouseleave') )
            {
              $(this)
                  .stop()
                  .animate({'padding-right': 30}, { queue: false, duration: 250 })
                  .find('span')
                      .stop()
                      .animate({width: 0}, { queue: false, duration: 250 });
            }
          });



    if (!window.EPLICA_loggedin)
    {

      //zebra tables
      $('tbody tr:nth-child(2n-1)').addClass('odd');
      
      $('.articlelist').alistDateSplit();
      
      //image centering stuff
      $.fn.imgOffset = function() {
        this.each(function () {
            if ($(this).is('img'))
            {
              $(this)
                  .css({ 'margin-top' : ( $(this).parent().height() / 2 ) - ( this.height / 2 )  })
                  .bind('load readystatechange', function() {
                      var img = $(this)
                      img.css({ 'margin-top' : ( img.parent().height() / 2 ) - ( this.height / 2 )  });
                      img.closest('span.image').css({ width : img.width() + 8 })
                    });
              this.src += ''; //IE force readystate hack
            }
          });
        return this;
      }

      if (article.length)
      {
        
        article.find('> img, p > img').each(function () {
            var img = $(this),
                cn = ' ' + img[0].className || '',
                an = ' ' + img.attr('align') || '',
                alt = img.attr('alt') || 0,
                imgSrc = img.attr('src'),
                isLarge = /large/.test(imgSrc);
            
            img.wrap('<span class="image '+ cn + an +'" />');
            

            if (alt)
            {
              img.after('<span class="alt">'+ alt +'</span>')
            }
            
            imgSrc = imgSrc.replace(/\/[^\/]+\/([^\/]+)$/, '/popup/$1');
            img.wrap('<a href="'+ imgSrc +'"></a>');
            
            if (isLarge)
            {
              img.wrap('<span class="imgwrap" />');
            }
            
            img.not('.notcentered').imgOffset();

          });

        //popup in articles
        article.find('.imagebox a.img')
            .each(function() {
              var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/popup/$1');
              $(this).attr('href', imgsrc);
              $(this).find('img').imgOffset();
            })
            
        article.find('a:has(img)')
            .append('<span class="zoomicon" />')
            .imgPopper({
                      curtainColor : '#000000',
                      curtainOpacity : '0.9',
                      yOffset: 15
              });

        article.alistDateSplit({ dateSel: '.boxbody .date' });

        article.find('a.videolink').videoLinks();
      }

      //tag external urls
      var link, text, useappend, match;
      $('.pgmain a').anchorTags({
                usePatterns:  ['doc', 'pdf', 'xls']
              })
            .filter('.external, .file_pdf, .file_doc, .file_xls')
                .not(':has(img)')
                    .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+')')
                              [useappend ? 'append' : 'prepend'](' <span class="icon">('+ text +')</span> ');
                        }
                      });
                      
                                
      $('.portfoliolist .item').bind('click', function (e) {
          window.location = $(this).find('h3 a:first').attr('href');
        });
      
      $('.portfolio-single .imagebox')
          .listscroller({ 
              item       : 'li',
              autoScrollDelay: 5000,
              paging     : true,
              statusPager: false,
              jumpPager  : true,
              controls   : 'above',
              animation  : 'crossfade',
              windowSize : 1,
              stepSize   : 1,
              labelNext  : '',
              labelPrev  : '',
              titleNext  : '',
              titlePrev  : '',
              wrap       : 'loop'
            });

      $('.pgextra2 .collapse').each(function () {
          var cbox = $(this).addClass('collapse-active'),
              cType = cbox[0].className.split(' ')[0],
              cBb = cbox.find('.boxbody').hide(),
              isOpen = false;
        
          cbox
            .find('.boxhead')
            .wrapInner('<a href="#" />')
            .bind('click', function (e) {
                cbox.toggleClass('collapse-open');
                cBb.slideToggle();
                isOpen = !isOpen;
                $.cookie(cType+'-open', isOpen, { expires: 30, path: '/' });
                e.preventDefault();
              });
          if ( $.cookie(cType+'-open') == 'true' )
          {
            cbox.find('.boxhead').trigger('click');
          }
        });
        
        
      $('.projectslides .boxbody')
          .wrapInner('<div class="hornklofi" />')
          .listscroller({ 
              item       : '.item',
              aspect     : 'horizontal',
              autoScrollDelay: 4000,
              paging     : true,
              statusPager: false,
              jumpPager  : false,
              animation  : 'carousel',
              windowSize : 1,
              stepSize   : 1,
              labelNext  : 'Næsta',
              labelPrev  : 'Fyrri',
              titleNext  : 'Næsta verkefni',
              titlePrev  : 'Fyrra verkefni',
              wrap       : 'loop'
            });

      $('.hbox .item').bind('click', function (e) {
          window.location = $(this).find('a:first').attr('href');
        });
      $('.blogbox img').imgOffset();

      var contactbox = $('#contact');
      contactbox.find('.intro').insertBefore( contactbox.find('.boxbody') );

      $('.pgextra3 .wrap').append(
          $('<a href="#" class="totop">Aftur upp</a>').bind('click', function (e) {
              $('html, body').animate({scrollTop: 0}, 250, 'swing');
            })
        );
    } // 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)'
          }); 
        });



    $('#noflickerCSS').remove();
    $('form').autoValidate();
    Req.loadPageScripts();
      

    var comments = $('.fb-comments')
    comments.append('<fb:comments width="'+ comments.width() +'" migrated="1" href="'+ document.location.toString().split('?')[0] +'"></fb:comments>');
    setTimeout(function(){
        FB.XFBML.parse();
      }, 100);

  }
);
// **** /jqreq *****

  
//init xfbml facebook stuff

