// 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(
  // alphabetized:
  'autovalidate',
  'equalizeheights',
  'eutils',
  'fontsizer',
  'imgpop',
  'labelizor',
  'listscroller',
  'mailtoenabler',
  'simplecarousel',
  'x/cookie',

  function(){

    var $ = jQuery;
    $('body').addClass('js-active');

    // validate all forms
    $('form').autoValidate(); // validate all forms

    $('div.pagestyle').fontsizer();
    $('td.netfang, ul.info > li.netfang > span, .footer .netfang').mailtoEnabler();

    if (!window.EPLICA_loggedin)
    {
      $('.article .netfang').mailtoEnabler();
      
      var extClassNames = {
          pdf:  'pdf',
          doc:  'doc',
          docx: 'doc',
          xls:  'xls',
          xlsx: 'xls'
        };

      $('div.article a:not(:has(img)), div.articlelist a:not(:has(img))')
          .each(function(){
              var className = extClassNames[ this.href.replace(/.+\.([^.]+)$/, '$1').toLowerCase() ];
              if (className) {
                $(this).addClass(className);
              }
            });


      $('div.catlist div.summary').equalizeHeights();
      $('ul.thumbsview div.summary').equalizeHeights();

      $('#qstr').labelizor();
 
      $('table.zebra tbody tr:nth-child(2n-1)').addClass('alt');
      
      // initialize the accordion widget (on the front page).
      $('div.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
      $('div.catlist div.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');
                  }
                });
        });
        
        
      $('div.mediagallery ul.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)
                })
        });
        
      $('div.albums ul.thumbsview li')
          .bind('click', function(){
              window.location=$(this).find('a').attr('href'); return false;
            });




      /* *** project-images & galleries (carousel & lightbox) *** */

        // ATH : Þarf að laga þegar nýjar myndir eru settar inná myndaalbúmið!!!

        // $('ul.project-images ul a, div.imagelist ul a') // FIXME: might be needed later
        $('div.project-images ul a, div.imagelist 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();
              });


          var imagelist = $('div.project-images ul');
        /* 
          // FIXME: might be needed later
          var imagelist = $('ul.project-images')
                              .removeClass('project-images')
                              .wrap('<div class="project-images" />')
                              .parent();
        */
          if( imagelist.find('li').length > 4 ) {
            imagelist
                .wrap('<div class="container" id="imagelist"></div>')
                .addClass('carousel-active')
                .parent()
                .listscroller({ 
                    item : 'li',
                    aspect:      'horizontal',
                    paging:      false,
                    animation:   'carousel',
                    windowSize:  4,
                    stepSize:    4
                  });
          }

      //init popups
      $('div.project-images ul.thumbsview a, div.imagelist ul a')
          .imgPopper({
              curtainColor : '#4f4f4f',
              curtainOpacity : '0.75'
            });

    } // end logged in

    
    $('div.stafflist')
        .each(function(){
            $(this).find('div.item:odd, tbody tr:odd').addClass('alt');
          })
        .Req(
            'http://www.verkis.is/bitar/common/personas/loadEmployee-min.js',
            function(){  $(this).find('tbody').eplicaEmployeeLoader();  },
            Req.localPath+'js/stafffilter.js?v2'
          )
        

                  



      $(window).bind('load', function() {
        var imgb = $('div.imgbox'),
            gimgb = $('table.gimgbox');
        imgb.width( imgb.find('img').width() );
        gimgb.width( gimgb.find('img').width() + 6 );
      });

      
    


      
      


    /* ***  map navigation *** */

    (function(){

      var mapnav = $('div.mapnav'),
          mnItms = $('div.item', mapnav),
          closeBtn = $('<div class="act"><a class="close" title="Loka" href="#">Loka</a></div>')
              .find('a')
                  .bind('click', function(e){
                        ;;;window.console&&console.log( this, $(this).parents('div.item:first')[0] );
                        $(this).parents('div.item:first').fadeOut(300);
                        return false;
                    });

      $('div.boxbody', mapnav).prepend('<ul class="map" />');

      if (!window.EPLICA_loggedin)
      {

        mnItms
            .hide()
            //.append(closeBtn) // works in jQuery 1.2.6
            .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>');
                    $('ul.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);
    
    **/

  
      
    $('#noflickerCSS').remove();
      


  }
);
