// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req.localPath = Req.localPath || '/skin/basic/'
Req(
  'fontsizer',
  'autovalidate',
  'labelizor',
  'imgpop',
  'anchortags',
  'easing-mini',
  'equalizeheights',
  
  function(){
    var $ = jQuery;
    isIS = $('html').attr('lang') == 'is';

    // labelize postlist input
    $('#s_email').labelizor();

    if (!window.EPLICA_loggedin)
    {
      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').Req('x/ifixpng', function(){
            $(this).ifixpng();
          });
      }

      //zebra tables
      $('tbody tr:nth-child(2n-1)').addClass('odd');
      //$('tbody tr:nth-child(2n)').addClass('even');


      //popup in articles
      $('div.article div.imagebox a.img')
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
              curtainColor : '#000000',
              curtainOpacity : '0.75'
            });
            
    }

    //remove flicker trick
    $('#noflickerCSS').remove();

    // fontsizer
    $('.pagestyle').fontsizer();

    // validate all forms
    $('form').autoValidate();
    
    // calendar date info pop-ups
    $('body.home div.monthlist').not('.calendar-large .monthlist')
    .Req(
        '/bitar/common/calendar/calendarPager.js',
        function(){
            var initCalendars = function ( e ) {
                $('div.dateinfo', this)
                    .hide()
                    .parent()
                    .bind('mouseenter focusin', function(e){
                        $(this).find('div.dateinfo')
                            .stop()
                            .css({ opacity : 1 })
                            .css({ height : 'auto' })
                            .css({ width : 'auto' })
                            .show(200, 'easeOut');
                      })
                    .bind('mouseleave focusout', function(e){
                        $(this).find('div.dateinfo')
                            .stop()
                            .css({ opacity : 1 })
                            .css({ height : 'auto' })
                            .css({ width : 'auto' })
                            .hide(200, 'easeIn');
                      });
              };

            this
                .eplicaCalendarLoader()
                .bind('reload', initCalendars);

            initCalendars.call(this);
          }
      );

    // Ajax for full page calendar  
    var ajaxContainer = $('<div class="ajax-container" />')
                            .delegate('a.closer', 'click', function (e) {
                                ajaxContainer
                                    .detach()
                                    .empty();
                                return false;
                              });

    $('.calendar-large .dateinfo li a')
        .bind('click', function (e) {
            var popLink = $(this),
                dd = popLink.closest('td').find('>a:first').text().replace(/\D/g, ''),
                mmYY = popLink.closest('.calwrap').find('>h4').text(),
                targetDate = (' ' + dd + '. ' + mmYY ).replace(/\s+/g, ' ')
                url = popLink.attr('href'),
                selector = '.event .boxbody';
            ajaxContainer
                .text('Sæki gögn...')
                .appendTo( popLink.closest('td') )
                .focus()
                .load(url+' '+selector, function () {
                    // the events may have multiple date values - each contained inside a `ul.meta li strong` (Yes, I know...)
                    // we need to weed out the correct one.
                    var uls = $(this).find('ul.meta'),
                        myDateLi = uls.find('strong')
                                      .filter(function(){
                                          var elmText = $(this).text().replace(/\s+/g, ' ');
                                          return elmText.indexOf( targetDate ) > -1;
                                        })
                                      .closest('li');
                    $('<ul class="meta"/>')
                        .append( myDateLi )
                        .insertBefore( uls[0] );
                    uls.remove();
                    // Appending of the close link must be done in the callback function
                    // lest it gets wiped out by the incoming content
                    ajaxContainer
                        .append('<a href="#" class="closer">Loka</a>');
                  });
            return false;
          });
      
      
    // Create 2 columns out of a single list, first column with closest upcoming events
      var eventList =  $('.events ul'),
          eventListItems = eventList.find('li'),
          eventItemsHalf = Math.ceil( eventListItems.length / 2 ),
          newList = $('<ul class="col1"/>');
      eventListItems
          .filter( ':lt('+eventItemsHalf+')' )
           .appendTo( newList );
      newList.insertBefore( eventList );
      
    // equalizeheights
      $('.pgmain, .pgextra2, .pgextra1').not('.calendar-large .pgmain, .calendar-large .pgextra2').equalizeHeights();
      $('.upcoming .events .boxbody, .upcoming .datepicker .boxbody').equalizeHeights();
           

  }
);
// **** /jqreq *****

