// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req(
  'eutils', 
  'fontsizer', 
  'autovalidate', 
  'labelizor', 
  'x/ifixpng', 
  'mailtoenabler', 
  'listscroller', 
  'imgpop', 
  'x/innerfade',
  'tabswitcher',
  'equalizeheights',

  function(){

    var $ = jQuery;

    var langIs = $('html').attr('lang') == 'is',
        nextText = langIs ? 'Næsta' : 'Next',
        prevText = langIs ? 'Fyrri' : 'Previous',
        nextTitle = langIs ? 'Næsta mynd' : 'Next image',
        prevTitle = langIs ? 'Fyrri mynd' : 'Previous image',
        pageText = langIs ? 'Mynd ' : 'Image ',
        ofText = langIs ? ' af  ' : ' of  ';

    $('body.home div.imgbanner strong').wrapInner('<span></span>')

    // labelize search input
    $('#qstr').labelizor();

    //remove flicker trick
    $('#noflickerCSS').remove();
    $('body').addClass('js-active');

    $('body.home div.pgextra3 ul.tabs').tabSwitcher();

    if (!window.EPLICA_loggedin) {

      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').ifixpng();
        $('div.mnav li').bind('mouseenter', function() { $(this).addClass('hover') })
                        .bind('mouseleave', function() { $(this).removeClass('hover') })
      }

      $('body.home .pgextra3 .logolisti .boxbody ul')
          .each(function() {
              if( $(this).find('li img').length > 4 ) {
                $(this)
                    .wrap('<div class="logocontainer"></div>')
                    .parent()
                    .listscroller({ 
                        item       : 'li',
                        aspect     : 'horizontal',
                        paging     : true,
                        statusPager: true,
                        jumpPager: false,
                        animation  : 'carousel',
                        windowSize : 4,
                        stepSize   : 4,
                        ofTotalSeparator  : ofText,
                        labelNext  : nextText,
                        labelPrev  : prevText,
                        statusLabel  : pageText,
                        titleNext  : nextTitle,
                        titlePrev  : prevTitle
                    })
              }
        });


      //zebra tables
      $('div.article table tr:even').addClass('alt');
      $('div.people table tr:odd').addClass('alt');

      //searchbox collapser
      var searchOpen = 0;
      $('div.qsearch')
          .addClass('search-active')
          .bind('mouseenter', function() {
              $(this).addClass('search-hover')
            })
          .bind('mouseleave', function() {
              if(!searchOpen) {
                $(this).removeClass('search-hover');
              }
            })
          .find('h2.boxhead')
              .wrapInner('<a href="#"></a>')
              .find('a')
                  .bind('click', function() {
                      var toHeight = searchOpen ? 16 : 60;
                      $(this).parents('div.qsearch').stop().animate({ height : toHeight }, 200);
                      searchOpen = searchOpen ? 0 : 1;
                      return false;
                    });
      
      // article imagelist
      
      $('ul.imagelist')
          .after('<span class="zoomicon" title="Smelltu á myndina til að stækka">#</span>')
          .filter(function(){ return $(this).find('li').length > 1; })
              .wrap('<div class="imgcontainer"></div>')
              .removeClass('imagelist')
              .parent()
              .listscroller({ 
                  item : 'li',
                  aspect     : 'horizontal',
                  paging     : true,
                  statusPager: true,
                  jumpPager  : false,
                  animation  : 'carousel',
                  windowSize : 1,
                  stepSize   : 1,
                  ofTotalSeparator: ofText,
                  labelNext  : nextText,
                  labelPrev  : prevText,
                  statusLabel: pageText,
                  titleNext  : nextTitle,
                  titlePrev  : prevTitle
                })
          .end()
              .find('img')
                  .each(function(i, img) {
                      $(this).bind('load readystatechange', function() {
                          $(this).css({ 'margin-top' : ( parseInt( $(this).parent().css('height') ) / 2 ) - ( this.height / 2 )  })
                        });
                      this.src += ''; //IE force readystate hack
                    });

      $('body.home ul.thumbsview')
          .wrap('<div class="imgcontainer"></div>')
          .parent()
          .filter(function(){ return $(this).find('li').length > 1; })
              .listscroller({ 
                  item : 'li',
                  aspect     : 'horizontal',
                  paging     : true,
                  statusPager: true,
                  jumpPager  : false,
                  animation  : 'carousel',
                  windowSize : 1,
                  stepSize   : 1,
                  ofTotalSeparator: ofText,
                  labelNext  : nextText,
                  labelPrev  : prevText,
                  statusLabel: pageText,
                  titleNext  : nextTitle,
                  titlePrev  : prevTitle
              })
              .find('ul.listscroller-clip a')
                  .each(function() {
                      var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
                      $(this).attr('href', imgsrc);
                    })
                  .imgPopper({
                      curtainColor : '#000000',
                      curtainOpacity : '0.7'
                    });


        var logoList = $('div.logolisti');
        $('a img', logoList)
            .each(function(i, img) {
                $(this).wrap('<span class="img"></span>').parent().append('<span class="arrow"></span>');

                $(this)
                    .bind('click', function (e) {
                        window.location = $(this).closest('a').attr('href');
                      })
                    .bind('load readystatechange', function() {
                        $(this).css({ 'margin-top' : ( parseInt( $(this).parent().css('height') ) / 2 ) - ( this.height / 2 )  })
                      });
                this.src += ''; //IE force readystate hack
              });
        $('li, .item', logoList).equalizeHeights();
        $('.item h3', logoList).equalizeHeights();

      //imagebox popup
      $('.article .imgbox a, .product .imagebox a.img')
              .imgPopper({
                  curtainColor : '#000000',
                  curtainOpacity : '0.7'
                });

              
      //enable mailto links
      $('span.netfang, td.netfang').mailtoEnabler();

    } // end logged in


    //init stafflist ajax
    $('div.people').Req('http://www.mp.is/bitar/common/personas/loadEmployee.js', function(){ this.eplicaEmployeeLoader(); });


    // fontsizer
    $('div.pagestyle').fontsizer();

    // validate all forms
    $('form').autoValidate();

});
// **** /jqreq *****