// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req(
  'fontsizer',
  'autovalidate',
  'x/ifixpng',
  'imgpop',
  'tabswitcher',
  'equalizeheights',
  'easing-mini',
  'listscroller',
  Req.baseUrl.replace(/jq\/$/, 'js/') + 'utils_1.1.js',
  { src: Req.baseUrl.replace(/jq\/$/, 'js/') + 'datepicker/1.0/dp.js', charset: 'iso-8859-1' },
  function(){
    var $ = jQuery;
    
    $('body').addClass('js-active');
    
    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)').imgPopper();
      
      // blockquotes
      $('div.article blockquote')
          .wrapInner('<div></div>')
              .find('div')
                  .prepend('<span class="quoteart" />');
      
    }
    
    // eq heights
    var pgmain = $('div.pgmain div.wrap');
    var pgextra2 = $('div.pgextra2 div.wrap');
  
    if(pgextra2.height() > pgmain.height()){
      pgmain.add(pgextra2).equalizeHeights();
    }
    
    
    // news 
    var months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
            
    $('.news .item .date')
        .each(function(){
            var dt = $(this).html().split('.');
            $(this).html('<span class="month">' + months[dt[1]-1] + '</span> <span class="day">' + dt[0] + '.</span> <span class="year">' + dt[2] + '</span>');
        });
        
    $('.fcon').each(function(){
        $(this)
            .find('div.fbox .boxbody')
                .equalizeHeights();
      });
      
      
    // monthlist
    $('div.monthlist').Req('http://ehfcnvefur.eplica.is/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);
      });


    datePicker.init();

      
      
    // spotlight
    var spotlight = $('div.spotlight');
    
    if($('.item', spotlight).length > 1) {
    
        spotlight
            .listscroller({
                item          : 'div.item',
                windowSize    : 1,
                stepSize      : 1,
                paging        : true,
                speed         : 500,
                autoScrollDelay : 7000,
                controls      : 'below',
                hideClass     : 'overflow',
                wrap          : 'both',
                animation     : 'accordion'
            });
    }

      

    // dropdown menu
    $('.mnav li.branch')
      .addClass('closed')
      .each(function(i){
      
        var li = $(this);
        var trigger = false;
      
        li.hover(
            function(){ // in
              trigger = false;
              setTimeout(function(){
                if(!trigger) {
                  li.removeClass('closed')
                  li.addClass('open');
                }
              }, 400);
            },
            function(){ // out
              trigger = true;
              setTimeout(function(){
                if(trigger) {
                  li.removeClass('open');
                  li.addClass('closed');
                }
              }, 400);

            });
      });


    if (window.EPLICA_loggedin)
    {
      EPLICA.inlineEditor.addEvent('Save', function (e) {
        var editElm = $(e.target);
        if (e.targetType == 'html')
        {
          // footnotes
          var heimildir = $('.fn', editElm);
          var index = 1;
          
          if(heimildir.length)
          {
            if(!$('ol.footnotes', editElm).length)
            {
              editElm
                  .append('<h3>Footnotes</h3><ol class="footnotes"></ol>');
            }
            else
            {
              index = parseInt($('ol.footnotes li', editElm).length, 10) + 1;
            }
            
            heimildir.each(function(){
              
              $('ol.footnotes', editElm)
                .append('<li id="fn' + index +'">' + $(this).html() + '</li>');
                
              $(this)
                  .after('<a class="footnote" href="#fn'+ index +'">['+ index +']</a>')
                  .remove();
                  
              index++;
              
            });
          }
        }
      });
    
    } //end loggedin
    
    
    if (!window.EPLICA_loggedin)
    {
      // newsletter 
      var overview = $('.newsletter .overview .boxbody').append('<ul class="right" />'),
          overviewUl = overview.find('ul:last'),
          overviewLis = overview.find('li');
      
      overviewLis.each(function(i){
      
        if(i > parseInt(overviewLis.length/2, 10))
        {
          $(this).appendTo(overviewUl);
        }
      
      });

      //video popper
      var videolinks = $('a.video:not(.external), .videotable a:not(.external)');
      videolinks
          .each(function(i) {
              $(this).click(function () {
                      $('#video').remove();
                      var href = $(this).attr('href');
                      $('table.videotable').before('' +
                          '<p id="video"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8.0.0.0" width="596" height="354">' +
                          '<param name="movie" value="/bitar/common/media/mediaplayer.swf" /><param name="wmode" value="transparent" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" />' +
                          '<param name="flashVars" value="file=' + href + '&type=video&width=580&height=326&bufferlength=3&autostart=true" />' +
                          '<embed src="/bitar/common/media/mediaplayer.swf"' +
                          'flashVars="file=' + href + '&type=video&width=580&height=326&bufferlength=3&autostart=true"' +
                          'width="596" height="354" wmode="transparent" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
                          '</object></p>' +
                        '');
                    return false;
                });
            })
          .filter(':first').trigger('click');

    }



    // milliforsidur boxhead fix
    $('div.pgmain .boxhead')
        .each(function(i){
      
          if(i != 1) {
            $('this').addClass('boxhead-first');
          }
          
        });
    
    // eventslist
    $('.twocol div.eventlist .meta').each(function(){
      $(this).appendTo($(this).parent());
    })
    
    
    // challenges
    $('div.challenges p:last').addClass('last');
    
    
    // tabswitcher
    $('ul.tabs')
        .tabSwitcher({ cssHide : true });


    // remove flicker trick
    $('#noflickerCSS').remove();


    // fontsizer
    $('div.pagestyle').fontsizer();


    // validate all forms
    $('form').autoValidate();
        
  }
);
// **** /jqreq *****

