// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req(
  'fontsizer',
  'autovalidate',
  'labelizor',
  'x/ifixpng',
  'easing-mini', 
  'x/ui-core', 
  'x/ui-fxcore', 
  'x/ui-accordion',
  'mailtoenabler',
  'imgpop',
  'x/innerfade', 
  'listscroller',
  'mappopulizor',

  function(){
    var $ = jQuery,
        msie6 = $.browser.msie && $.browser.version < 7

    // labelize search input
    $('#qstr').labelizor();

    //remove flicker trick
    $('#noflickerCSS').remove();


    if (!window.EPLICA_loggedin)
    {


      //format frontpage eventlist & init accordion widget
      $('body.home div.eventlist div.boxbody')
          .find('span.date')
              .each(function(){
                var date = $.trim( $(this).html().split(' - ')[0] ).split('.'),
                    isMonths = ['jan','feb','mar','apr','maí','jún','júl','ágú','sep','okt','nóv','des'];
                $(this).html('<span class="d">' + date[0] + '</span><span class="m">' + isMonths[ ( date[1] - 1 ) ] + '</span>')
              })
          .end()
          .filter(function(){ return $(this).find('.item').length > 1 })
              .accordion({
                  header         : 'h3',
                  selectedClass  : 'item-open',
                  event          : 'mouseover'
                });



      //link icons
      var article = $('.article, .articlelist'),
          articlePics = article.add('.articlelist');
      articlePics.find('a[href$=".pdf"], a[href$=".PDF"]').filter(function(){ return $(this).find('img').length == 0 }).addClass('pdf').attr('title', 'Sækja Acrobat PDF skrá');
      articlePics.find('a[href$=".doc"], a[href$=".DOC"], a[href$=".docx"], a[href$=".DOCX"], a[href$=".wri"], a[href$=".WRI"], a[href$=".rtf"], a[href$=".RTF"]').filter(function(){ return $(this).find('img').length == 0 }).addClass('doc').attr('title', 'Sækja Word DOC skrá');
      articlePics.find('a[href$=".xls"], a[href$=".XLS"], a[href$=".xlsx"], a[href$=".XLSX"], a[href$=".ods"], a[href$=".ODS"]').filter(function(){ return $(this).find('img').length == 0 }).addClass('xls').attr('title', 'Sækja Excel skrá'); 


      //enable mailto
      $('div.pgmain span.netfang, div.pgfoot span.netfang, div.pgmain td.netfang').mailtoEnabler();

      //imagelist carousel & popups
      var imagelist = $('ul.imagelist', article);

      imagelist
          .filter(function(){ return $(this).find('li').length > 4 })
              .wrap('<div class="imagelist" id="imagelist"></div>')
              .removeClass('imagelist')
              .parent()
                  .listscroller({
                      item : 'li',
                      aspect:      'horizontal',
                      paging:      false,
                      animation:   'carousel',
                      windowSize:  4,
                      stepSize:    4
                    });
      imagelist
            .find('a')
                .each(function(){
                
                  var li = $(this).parent(),
                      img = $(this).find('img');
                  
                  img.attr('title', img.attr('alt')); // switch cuz of image content (FIXME?)
                  img.attr('alt', li.find('div.descr').eq(0).text());
                  
                })
                .imgPopper({
                    curtainColor : '#cccccc',
                    curtainOpacity : '0.75',
                    disableIeFading : 1,
                    setContainerWidth : 1,
                    yOffset: 60
                  });


      //popup in articles
      $('.imgbox a:has(img)', article)
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
                curtainColor : '#cccccc',
                curtainOpacity : '0.75',
                disableIeFading : 1,
                setContainerWidth : 1,
                yOffset: 60
              });

      $('a[href$=".jpg"], a[href$=".png", a[href$=".gif"]', article)
          .filter(function() { return $(this).parents('.imagebox').length == 0 && $(this).parents('.imgbox').length == 0 })
              .imgPopper({
                    curtainColor : '#cccccc',
                    curtainOpacity : '0.75',
                    disableIeFading : 1,
                    setContainerWidth : 1,
                    yOffset: 60
                  });


      //init collapser
      $('.collapsebox', article)
          .each(function() {
              $(this)
                  .addClass('collapse-active')
                  .find('> *:not(h3)')
                      .wrapAll('<div class="collapser"></div>')
                      .parent()
                      .hide()
                      .parent()
                      .find('h3')
                          .wrapInner('<a href="#"></a>')
                          .find('a')
                              .bind('click', function() {
                                  $(this).parent().next().slideToggle().parent().toggleClass('collapse-open');
                                  return false;
                                })
            });




      // Add "send to facebook" link to articles
      $('p.buttons', article)
          .append(
              $('<a class="btnfacebook" href="#"><img src="/skin/basic/design/i/fbook.png"></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;
                    })
            );
	    
      Add "mp3 pop" link to articles
       $('mp3', thumb)
         .append(
              $('<a class="popper" href="#">Læðumst</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;
                    })
            );	    


      // hover-test

  //HoverBox  
 var relative = new Control.Window($(document.body).down('[href=#hoverbox]'),{  
     position: 'relative',  
     hover: true,  
     offsetLeft: 75,  
     width: 175,  
     className: 'tooltip'  
 }); 
//  relative.container.insert('This content was inserted with JavaScript.');  

      // mediagallery fixes and popup
      $(window).bind('load', function() {
        $('body').addClass('js-active');
        $('.pgmain .mediagallery li img')
            .each(function() {
                var imgheight = $(this).height();
                $(this).css('margin-top', -(imgheight/90));
              })
            .parent()
            .css({
                  borderLeftColor    : '#EBEBEB', 
                  borderRightColor   : '#EBEBEB',
                  borderTopColor     : '#EBEBEB',
                  borderBottomColor  : '#EBEBEB'
              })
            .bind('mouseenter', function(e){
                $(this)
                    .stop()
                    .animate({
                          borderLeftColor    : '#6A1010', 
                          borderRightColor   : '#6A1010',
                          borderTopColor     : '#6A1010',
                          borderBottomColor  : '#6A1010'
                      }, 200)
              })
            .bind('mouseleave', function(e){
                $(this)
                    .stop()
                    .animate({
                        borderLeftColor    : '#EBEBEB', 
                        borderRightColor   : '#EBEBEB',
                        borderTopColor     : '#EBEBEB',
                        borderBottomColor  : '#EBEBEB'
                    }, 300)
              });
              
        $('.pgmain .mediagallery.imagelist li a img')
            .each(function() {
                  var imgSrc = $.trim( $(this).attr('src') ),
                      popupSrc = imgSrc.replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
                  $(this).parent().attr('href', popupSrc);
              })
            .parent()
            .imgPopper({
                    curtainColor : '#333333',
                    curtainOpacity : '0.75',
                    disableIeFading : 1,
                    setContainerWidth : 1,
                    yOffset: 60
                  });
        
        //employee list ajax
        $('.people').Req('/bitar/common/personas/loadEmployee.js', function(){ this.eplicaEmployeeLoader(); });
            
      }); // end onload

      // fixes for IE6
      if (msie6) {
        $('img[src$=".png"]').ifixpng();
      }

      //zebra tables
      $('tbody tr:nth-child(2n-1)', article).addClass('alt');

    }; // end loggedin



    // fontsizer
    $('div.pagestyle').fontsizer();

    // validate all forms
    $('form').autoValidate();

  }
);
// **** /jqreq *****

