// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

Req(
  'fontsizer', 
  'autovalidate', 
  'labelizor', 
  'equalizeheights', 
  'listscroller', 
  'easing-mini', 
  'x/ui-accordion',
  'x/easing', 
  '/skin/basic/js/jquery.ahover.js',
  '/skin/basic/js/fancybox/jquery.fancybox-1.3.4.pack.js',

  function(){
    var $ = jQuery,
        body = $('body').addClass('js-active'),
        article = $('.article');

    // labelize search input
    $('form .fi_txt label acronym').after('<span> </span>');
    $('#qstr, .contactform .fi_txt :input, .contactform .fi_bdy textarea, .subscr .fi_txt :input').labelizor({ lRe: /(:[\W\S]*$)/g });
    

    //remove flicker trick
    $('#noflickerCSS').remove();


    //frontpage image carousel
    body.filter('.home')
          .find('.imagelist')
            .listscroller({ 
                item : 'li',
                aspect:      'horizontal',
                paging:      false,
                animation:   'carousel',
                windowSize:  5,
                stepSize:    5
              });

    //accordion
    body.filter('.home')
          .find('.eventlist .boxbody')
            .accordion({
                header         : 'h3',
                selectedClass  : 'item-open',
                animation      : 'easeInOut',
                event          : 'mouseover'
              });

    body.filter('.twocol')
          .find('.yearindex li:not(:last) a')
            .each(function() {
                $(this).after(', ');
            });


    if (!window.EPLICA_loggedin) {

      // Add "send to facebook" link to articles
      article.find('.buttons')
          .prepend(
              $('<a class="btnfacebook" href="#">Facebook</a>')
                  .bind('click', function()  {
                      window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(document.location.href),'sharer','toolbar=0,status=0,width=626,height=436');
                      return false;
                    })
            );

      //funky hover effects
      $('.mnav > .boxbody li').ahover({ moveSpeed: 60, toggleSpeed: 100, toggleEffect: 'width' });

      //snav arrow
      $('.snav li.current > a').append('<span class="arrow">&nbsp;</span>');

      //zebra tables
      article.find('tbody tr:nth-child(2n)').addClass('alt');

      // totop link on twocol
      if ($('.pgmain').height() > window.innerHeight)
      {
        body.filter('.twocol')
            .attr('id', 'top')
            .find('.pgmain .wrap .box:last')
                .append('<a class="totop" href="#top">Efst á síðu</a>');
      }

      // equalize heights
      body.filter('.twocol').find('.pgmain .wrap .box, .pgextra2 .wrap').equalizeHeights();
      body.filter('.onecol').find('.pgmain .wrap .box, .pgextra2 .wrap').equalizeHeights();
      body.filter('.home').find('.pgextra1 .notes .box').equalizeHeights();

      /*
      if($.browser.msie && $.browser.version > 7) {
        $('body.home .mainarticle .boxhead').append('');
      }
      */

    } //end if

    // frontpage newsticker
    var fpnewsbody = $('body.home .frontpagenews .boxbody')
            .addClass( 'bodboxy-active' );

    if (fpnewsbody.length)
    {
      var newsitems = fpnewsbody.find('> .item'),
          visibleIndex = 0,
          switchHlTimeout,
          headlinelist = $('<ul class="newsheadlinelist itemlist" />'),
          _activeClass = 'active';
      
      //limit char length in frontpage articlelist
      fpnewsbody.find('.summary')
          .each(function(){
              var imgElm = $('img', this).clone(),
                  moreBtnParent = $('span.more', this),
                  moreBtn = $('a', moreBtnParent).clone().addClass('meira').text('meira');

              moreBtnParent.remove();

              var tabtext = $(this).text();
              if (tabtext.length > 175) {
                //only use first 180 letters of summary in frontpage tab articles
                tabtext = tabtext.substr(0,175) + '... ';
              }

              $(this).text( tabtext ).prepend( imgElm ).append(moreBtn);
            });


      newsitems
          .slice( 1 )
              .hide()
          .end()
          .each(function(i){
              var _this = $(this),
                  newsheadline = $('<li>')
                      .addClass( this.className.replace(/(^| )((item|itm\d*|firstitem)( |$))+/g, '$1') )
                      .addClass( i===0 ? _activeClass:'' )
                      .append( _this.find('span.meta').clone() )
                      .append( _this.find('h3 > a').clone() )
                      .append( '<i class="bottom" />' )
                      .bind('mouseenter', function (e) {
                          var _thisHeadline = this;
                          if (!headlinelist.queue().length)
                          {
                            switchHlTimeout = setTimeout(function () {
                                var visibleIndexAtStart = visibleIndex;
                                visibleIndex = _thisHeadline.listIndex;
                                headlinelist
                                    .queue(function(){
                                        newsitems
                                            .eq( visibleIndexAtStart )
                                                .fadeOut( 125 )
                                                .queue(function(){
                                                    headlinelist.dequeue();
                                                    $(this).dequeue();
                                                  });
                                      })
                                    .queue(function(){
                                        headlines
                                            .eq( visibleIndexAtStart )
                                                .removeClass( _activeClass );
                                        $(_thisHeadline)
                                            .addClass( _activeClass );
                                        headlinelist.dequeue();
                                        newsitems
                                            .eq( _thisHeadline.listIndex )
                                                .fadeIn(
                                                    400,
                                                    'easeIn',
                                                    ($.browser.msie && $.browser.version < 8) ? 
                                                        function () { this.style.removeAttribute('filter') }:
                                                        undefined
                                                  );
                                      });

                              }, 350);
                          }
                        })
                      .bind('mouseleave', function (e) {
                          clearTimeout( switchHlTimeout );
                          switchHlTimeout = null;                            
                        })
                      .appendTo( headlinelist );
              newsheadline[0].listIndex = i;
            });

      headlinelist.prependTo(fpnewsbody);
      headlines = headlinelist.find('li');

    }


    if (!window.EPLICA_loggedin) {
      article.find('.imgbox a')
          .fancybox({
              'zoomSpeedIn': 300, 
              'zoomSpeedOut': 200,
              'padding'     : 0,
              'overlayOpacity' : 0.5
            });
      var imglist = article.find('ul.imagelist'),
          imgcount = imglist.find('li').length;
      imglist
          .wrap('<div class="imgcontainer" />')
          .find('a')
              .each(function(){ $(this).attr('rel','group') })
              .fancybox({
                  'zoomSpeedIn': 300, 
                  'zoomSpeedOut': 200,
                  'padding'     : 0
                });
      if(imgcount > 1) {
        imglist
            .parent()
            .listscroller({ 
                item : 'li',
                aspect     : 'horizontal',
                paging     : true,
                animation  : 'carousel',
                windowSize : 1,
                stepSize   : 1,
                labelNext  : '→',
                labelPrev  : '←'
              })
      }

      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').Req('x/ifixpng', function(){
            $(this).ifixpng();
          });
      }

    };


    $('form').autoValidate();
    $('.pagestyle').fontsizer();

  }
);

