//encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

Req(
  'autovalidate',
  'equalizeheights',
  'eutils',
  'fontsizer',
  'labelizor',
  'simplecarousel',
  'tabswitcher',
  'x/bookmark',
  'x/ifixpng',

// loaded on server
  Req.isLocal ? '' : '/bitar/c2/concat.jsp?dwr/interface/Event.js;dwr/engine.js;dwr/util.js',
// loaded on local file:/// pages
  Req.isLocal ? './../js/dwr/interface/Event.js' : '',
  Req.isLocal ? './../js/dwr/engine.js' : '',
  Req.isLocal ? './../js/dwr/util.js' : '',

  function(){
    var $ = jQuery
        isIE6 = $.browser.msie && $.browser.version < 7;
    
    $('body.home .pb-bottom .box').equalizeHeights();


    // BEGIN: Content cleanup
    $('.tabbox')
        .removeClass('tabs-active');
    $('.tabpane .focustarget').remove();

    // END: Content cleanup


    // dynamic calendar thingy on home page
    /*
    $('body.home .monthlist .calwrap')
        .Req(
            'https://secure.eplica.is/codecentre/js/utils_1.1.js',
            (Req.isLocal ? './../../..' : '' ) +'/bitar/borgarleikhus/dagatal/eventcalendar.js',
            function(){
              var calElm = this[0];
              EPLICA.eventCalendar.init(calElm, {
                  initDate: (new Date( parseInt( calElm.id.replace(/\D/g, '') ) )),
                  rotation: (7 - $('thead th.sun', this).prevAll('th').length) % 7 // 0 if Sunday is first day of week, 1 if Monday, etc.
                });
            }
          );
            */
      $('body.home div.monthlist').Req('/bitar/common/calendar/calendarPager.js', function(){ this.eplicaCalendarLoader();});



    if (!window.EPLICA_loggedin) {

      $('div.article ul.tabs')
          .tabSwitcher({
              monitorFragment: true,
              setCookie:       false
            });

      $('body.home div.showlist div.item').equalizeHeights();


      $('#qstr').labelizor();

      $('div.pagestyle').fontsizer();


      $('body.home').each(function(){
          isIE6  &&  $('div.monthlist td')
                .hover(
                    function(e){  $(this).addClass('tdhover');  },
                    function(e){  $(this).removeClass('tdhover');  }
                  );

          var activeClass = 'tabcurrent',
              currentTab = $( '.tabs li' )
                  .bind('mouseover', function ( e ) {
                      currentTab  &&  $(currentTab).removeClass( activeClass );
                      $(this).addClass(activeClass);
                      currentTab = this;
                    }).eq(0).addClass( activeClass );
        });

      isIE6  &&  $('img[scr$=".png"]').ifixpng();

      // validate all forms
      $('form').autoValidate();
      
      // Add share buttons to articles
      if ($('.buttons').length) {
          $('.buttons').bookmark({
                sites:   ['facebook', 'twitter', 'delicious'],
                compact: false, // defaults to `true`
                icons:   '/skin/basic/design/i/icons.png',
                iconSize: 16
              })
                .prepend(
                    '<span class="share">Deila þessu efni á:</span>'
                  );
        }
      // Add share buttons to articles
      if ($('.adgerdir').length) {
        $('.adgerdir .boxbody').append('<div class="share-menu" />');
        $('.share-menu').bookmark({
              sites:   ['facebook', 'twitter', 'delicious'],
              compact: false, // defaults to `true`
              icons:   '/skin/basic/design/i/icons.png',
              iconSize: 16
            });
      }

    } //loggedin

    //remove flicker trick
    $('#noflickerCSS').remove();




    // **** image gallery
    $('div.imagelist').each(function(){

        $('div.pgs', this).remove();

        var largeImg = $('<img/>'),
            imgName = $('<h3/>'),
            imgText = $('<p/>'),
            backBtn = '<div class="backbutton"><a class="btnback" href="javascript:history.back();">Til baka á sýningu</a></div>',
            lastItem,

            pos = 0,

            thumbs = $('ul', this)
                .simpleCarousel({
                    item:       'li',
                    wrap:       true
                  })
                .find('li a:first-child')
                    .each(function(i){

                        var linkElm = $(this),
                            imgTag = linkElm.find('img').css('opacity', 0);
                            newLink = imgTag[0].src.replace('/preview/', '/large/');

                        linkElm
                            .css('background-image', 'url(' + imgTag[0].src + ')')
                            .attr('href', newLink)
                            .bind('click', function(e){

                                if (lastItem) { lastItem.removeClass('current'); }
                                lastItem = linkElm.parents('li:first').addClass('current');
                                pos = i+1;

                                largeImg
                                    .fadeOut(200, function(){
                                        imgName.html( imgTag.attr('alt') );
                                        imgText.html( linkElm.parent().find('p').text() );
                                        largeImg
                                            .attr('src', linkElm[0].href)
                                            .fadeIn(500);
                                        // kick MSIE's rendering engine into shape
                                        setTimeout(function(){ document.body.className += ''; }, 10);
                                      });

                                return false;
                              });

                      }),

            showNextImage = function(){
                if ($.browser.msie) { thumbs.eq(pos).css('margin-left', 95); } // ugly hack
                var currPos = pos;
                thumbs.eq(pos)
                    .trigger('focus')
                    .trigger('click')
                    .trigger('blur');
                pos = currPos;
                if ($.browser.msie) { thumbs.eq(pos).css('margin-left', null); } // ugly hack
                pos = (pos < thumbs.length) ? pos+1 : 0;
              },
            autoPlay,
            startPlay = function(delayed){
                autoPlay = setTimeout(function(){
                    showNextImage();
                    autoPlay = setInterval( showNextImage, 4500 );
                  }, delayed ? 2000 : 0);
              },
            stopPlay = function(){
                clearInterval( autoPlay );
              };

        largeImg.add(imgName).add(imgText).add( $('div.container, div.controls', this) ).fin()
            .bind('mouseenter', stopPlay)
            .bind('mouseleave', startPlay);

        startPlay();

        $('> div.boxbody', this)
            .append(largeImg, imgName, imgText)
            .append(backBtn);


      });


  }

);



