// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

Req.localPath = Req.localPath || '/skin/basic/';
//Req.baseUrl = '/codecentre/jq/';
//Req.joinUrl = '/codecentre/concat/?%{s}|jq';
Req(
  // alphabetized:
  'autovalidate',
  'equalizeheights',
  'eutils',
  'fontsizer',
  'imgpop',
  'labelizor',
  'listscroller',
  'mailtoenabler',
  'simplecarousel',
  'x/cookie',

  function(){

    var $ = jQuery,
        article = $('.article'),
        isIS = $.lang() == 'is';
    $('body').addClass('js-active');



    $('.stafflist')
        .each(function(){
            $(this).find('.item:odd').addClass('alt');
          })
        .Req(
            Req.localPath+'js/stafffilter.js?v2',
            'http://www.verkis.is/bitar/common/personas/loadEmployee-min.js',
            function(){  $(this).find('tbody').eplicaEmployeeLoader();  }
          );


    if (!window.EPLICA_loggedin)
    {

      var extClassNames = {
          pdf:  'pdf',
          doc:  'doc',
          docx: 'doc',
          xls:  'xls',
          xlsx: 'xls'
        };

      $('.article a:not(:has(img)), .articlelist a:not(:has(img))')
          .each(function(){
              var className = extClassNames[ this.href.replace(/.+\.([^.]+)$/, '$1').toLowerCase() ];
              if (className) {
                $(this).addClass(className);
              }
            });
            
      var fbtext = isIS ? 'Senda á facebook' : 'Send to facebook',
          shareText = isIS ? 'Senda í pósti' : 'Send article',
          subjText = isIS ? 'Áhugaverð grein á' : 'Interesting article on';
      $('.buttons')
          .append('<a class="btn-email" target="_blank" href="mailto:?subject='+ subjText +' www.verkis.is&amp;body=\n'+ document.location.href +'">'+ shareText +'</a>')
          .append(
              $('<a class="btn-facebook" href="#">'+ fbtext +'</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;
                    })
            )
          .append(
              $('<a class="btn-twitter" href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a>')
                  .bind('click', function()  {
                      window.open('http://twitter.com/intent/tweet?url='+encodeURIComponent(document.location.href),'tweeter','toolbar=0,status=0,width=626,height=436');
                      return false;
                    })
              )
          .append('<span class="btn-plusone"><g:plusone size="medium" count="false"></g:plusone></span>');


      $('.catlist .summary').equalizeHeights();
      $('.thumbsview .summary').equalizeHeights();

      $('#qstr').labelizor();
 
      $('table.zebra tbody tr:nth-child(2n-1)').addClass('alt');
      
      // initialize the accordion widget (on the front page).
      $('.accordion')
          .Req('x/ui-accordion','x/easing', function(){

              //insert anchors for accordion and collapser
              this
                  .find('div.box h2.boxhead')
                      .wrapInner('<a href="#"></a>')
                      .find('a')
                          .click(function(e){ e.preventDefault(); });
              this
                  .accordion({
                      header         : 'h2.boxhead',
                      selectedClass  : 'boxhead-open',
                      animation      : 'easeInOut'
                    });
            });


      // background-color transformation
      $('.catlist .item')
          .Req('x/ui-fxcore', function(){

            this
                .bind('mouseenter', function(e){
                    $(this)
                        .stop()
                        .animate({
                            backgroundColor : '#ffffff'
                          }, 300)
                        .find('span.more a')
                            .stop()
                            .css({ opacity : 1 })
                            .fadeIn(300);
                  })
                .bind('mouseleave', function(e){
                    $(this)
                        .stop()
                        .animate({
                            backgroundColor : '#4C5E68'
                          }, 300)
                        .find('span.more a')
                            .stop()
                            .fadeOut(300)
                  })
                .bind('click', function(){
                    window.location=$(this).find('h3 a').attr('href'); return false;
                  })
                .find('img')
                .each(function(i){
                    if ( i % 3 == 0 ) {
                      $(this).addClass('left');
                    }
                  });
          });
        
        
      $('.mediagallery .thumbsview li')
        .Req('x/ui-fxcore', function(){

          this
              .bind('mouseenter', function(e){
                  $(this)
                      .stop()
                      .animate({
                          backgroundColor : '#ffffff'
                        }, 300)
                })
              .bind('mouseleave', function(e){
                  $(this)
                      .stop()
                      .animate({
                          backgroundColor : '#4C5E68'
                        }, 300)
                })
        });

      $('.albums .thumbsview li')
          .bind('click', function(){
              window.location=$(this).find('a').attr('href'); return false;
            });



      /* *** project-images & galleries (carousel & lightbox) *** */
        var imglists = $('.project-images, .imagelist'),
            imglistpgr = imglists.find('.pgr').clone(),
            navend = '';

        imglists.find('ul a')
            .each(function(){
                var img = $(this).find('img')[0];
                this.href = img.src.replace(/\/[^\/]+\/([^\/]+)$/, '/glarge/$1');
                img.title = img.alt;
                img.alt = $(this).parent().find('p.imgtext').text();
              })
            //init popups
            .imgPopper({
                curtainColor : '#4f4f4f',
                curtainOpacity : '0.75',
                disableIeFading : 1
              });

        if (imglistpgr.length) {

          imglistpgr.find('a').each(function(i){
              navend = $(this).is('.prev') ? 'last' : 'first';
              $(this).attr('href', $(this).attr('href') + '?openPop=' + navend );
            });

          imglists.find('ul a')
              .bind('click', function (e) {
                  setTimeout(function() {
                      imglistpgr.removeClass(navend);
                      var popup = $('.ipopup-container-wrapper');
                      navend = '';
                      if (popup.find('.prev.nav-end').length) {
                        navend = 'navend-prev';
                      } else if (popup.find('.next.nav-end').length) {
                        navend = 'navend-next';
                      } else {
                        navend = '';
                      }
                      popup.find('.paging').append( imglistpgr.addClass(navend) );
                  }, 200);
                });

          var docloc = document.location.search.match(/openPop=([^&]+)/);
          if (docloc && docloc[1]) {
            imglists.find('ul a').filter(':' + docloc[1] ).trigger('click');
          }

        }



          var imagelist = imglists.filter('.project-images').find('ul');
          if( imagelist.find('li').length > 4 ) {
            imagelist
                .wrap('<div class="container" id="imagelist" />')
                .addClass('carousel-active')
                .parent()
                .listscroller({ 
                    item : 'li',
                    aspect:      'horizontal',
                    paging:      false,
                    animation:   'carousel',
                    windowSize:  4,
                    stepSize:    4
                  });
          }



      article.find('.netfang').mailtoEnabler();

      $(window).bind('load', function() {
          var imgb = $('.imgbox'),
              gimgb = $('table.gimgbox');
          imgb.width( imgb.find('img').width() );
          gimgb.width( gimgb.find('img').width() + 6 );
        });
    } // end logged in

    







    /* ***  map navigation *** */
    var mapnav = $('.mapnav'),
        mnItms = $('.item', mapnav),
        closeBtn = $('<div class="act"><a class="close" title="Loka" href="#">Loka</a></div>')
            .find('a')
                .bind('click', function(e){
                      $(this).closest('.item').fadeOut(300);
                      return false;
                  });

    mapnav.find('.boxbody').prepend('<ul class="map" />');

    if (!window.EPLICA_loggedin)
    {

      mnItms
          .hide()
          .each(function(){ $(this).append( closeBtn.clone(true) );  }) // workaround for jQuery 1.3
          .find('h3')
              .each(function(i){
                  var link = $('<li class="cat' + (i+1) + '"><a href=""><span>' + $(this).text() + '</span></a></li>');
                  $('.map', mapnav).append(link);
                  
                  link.click(function()
                  {
                    mnItms.eq(i)
                      .fadeIn(300)
                      .find('h3 a')
                          .focus();
                    return false;
                  })
              });
      }


    

    /** Christmas splash screen (remove after christmas;)
    
    
    var splash = $('<div class="ipopup-curtain"></div>' +
                  '<div class="splash">' +
                  '<div class="boxbody">' +
                    '<img src="/skin/basic/design/i/splash-banner.png" />' + // local "../design/i/splash-banner.png"
                  '</div>' +
                '</div>' +
                '<hr class="stream" />');
                  
    function closeSplash() { splash.fadeOut(1000); }
    
    if(!$.cookie('splash'))
    {
      $('body.home')
          .append(splash)
              .click(function(){
                  closeSplash();
              });
    }
    
    $.cookie('splash', '1', { path: '/' });
    
    setTimeout(function(){ closeSplash(); }, 8000);
    
    **/

    $('form').autoValidate(); // validate all forms
    $('td.netfang, ul.info > li.netfang > span, .footer .netfang').mailtoEnabler();
    $('.pagestyle').fontsizer();
    $('#noflickerCSS').remove();

  }
);

