// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req.localPath = Req.localPath || '';
Req(
  'eutils',
  'fontsizer',
  'autovalidate',
  'labelizor',
  'x/ifixpng',
  'imgpop',
  'listscroller',
  'mailtoenabler',
  'equalizeheights',
  function(){
    var $ = jQuery;

    $('body').addClass('js-active');

    // labelize search input
    $('#qstr').labelizor();

    if (!window.EPLICA_loggedin)
    {
      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').ifixpng();
      }

      //zebra tables
      $('div.article table tr:even').addClass('alt');

      //popup in articles
      $('div.article div.imgbox a:has(img)')
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
              curtainColor : '#000000',
              curtainOpacity : '0.75'
            });
            
      // Add "send to facebook" link to articles
      $('p.buttons')
          .append(
              $('<a class="facebook" href="#">Facebook</a>')
                  .bind('click', function()  {
                      window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(document.location.href)+'&t='+encodeURIComponent($('h1').text()),'sharer','toolbar=0,status=0,width=626,height=436');
                      return false;
                    })
            );
    }


    $('.pgmain, .pgextra2').equalizeHeights();
    $('.graylist .item').equalizeHeights();


    // starfsmannalisti (hljómsveitin)
    $('.memberlist').each(function(){
        var memberlist = $(this);

        memberlist.find('table')
            .each(function(){
                var uls = $('<div class="cols"><ul /><ul /></div>').insertBefore(this).find('ul'),
                    tRows = $(this).find('tbody tr:has(td)'),
                    colLength = (tRows.length-1)/2;
                tRows
                    .each(function(i){
                        var cells = $(this).find('>*'),
                            toCol = i<=colLength ? 0 : 1,
                            ct1   = cells.eq(0).html() ? cells.eq(0).html() : '',
                            ct2   = cells.eq(1).html() ? ', ' + cells.eq(1).html() : '',
                            newLi = $('<li>').appendTo( uls[toCol] ).append( ct1 + ct2 );
                      });

              })
            .remove();

        memberlist.Req(
            Req.localPath+'/bitar/common/personas/loadEmployee-min.js',
            function(){
                this.eplicaEmployeeLoader({
                    getSelector:     'div.persona',
                    preprocessDom:   function(){
                                          this.attr('class', 'wrp1').find('>*').attr('class', 'wrp2');
                                          var title = this.find('li.title').detach();
                                          title.find('b').remove();
                                          if( title.html() ) { this.find('h3').append( ', '+title.html() ); }
                                        },
                    togglerSelector: 'a',
                    rowSelector:     'li',
                    subrowSelector:  'li.details',
                    subrowHideClass: 'details-closed',
                    subrowHtml:      '<li class="details"><a class="close"/><i id="insertPoint" /></li>',
                    toggleOpen:      true
                  });
              }
          );

      });


      //init calendars
      $('.monthlist').Req('/bitar/common/calendar/calendarPager.js', function(){
            var initCalendars = function ( e ) {
                $('div.dateinfo', this)
                    .hide()
                    .parent()
                    .bind('mouseenter focusin', function(e){
                        $(this).addClass('hover').find('div.dateinfo')
                            .stop()
                            .css({ opacity : 1 })
                            .css({ height : 'auto' })
                            .css({ width : 'auto' })
                            //.css({ 'margin-left': '6px' })
                            .show(200);
                      })
                    .bind('mouseleave focusout', function(e){
                        $(this).find('div.dateinfo')
                            .stop()
                            .css({ opacity : 1 })
                            .css({ height : 'auto' })
                            .css({ width : 'auto' })
                            .hide(200, function() { $(this).closest('td').removeClass('hover') });
                      });
                      

                // monthlist (remove empty dates)
                var mlth = $('.monthlist-h th'),
                    mltd = $('.monthlist-h td');
                mltd
                  .each(function(i) {
                      if($(this).hasClass('o'))
                      {
                          $(this).remove();
                          mlth.eq(i).remove();
                      }
                  });

              };

            this
                .eplicaCalendarLoader()
                .bind('reload', initCalendars);

            initCalendars.call(this);
        });
      $('.monthlist-h .paging').clone().appendTo( $('.pgmain .eventlist') );


    // event
    var eventInner = $('div.event > div.boxbody');

    eventInner
        .prepend('<div class="rightcol"></div>')
        .end()
        .find('div.imgbox, ul.actions, ul.reviews')
        .appendTo($('div.rightcol', eventInner));


    // frontpage image carousel
    $('div.home div.carousel div.boxbody')
          .listscroller({
              item :       '.item',
              aspect:      'horizontal',
              paging:      true,
              animation:   'carousel',
              windowSize:  1,
              stepSize:    1,
              startPos: 'random',
              autoScrollDelay: 7000
            });






    // home articlelist
    $('div.home div.articlelist div.boxbody').append('<div class="new" />')

    // remove flicker trick
    $('#noflickerCSS').remove();
    $('div.pagestyle').fontsizer();
    $('form').autoValidate();


  }
);
// **** /jqreq *****
