// 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)
    {

     //init newsticker
      var fadelisthnit = {
          '13'  : {  x:117,  y:153 }, //Haukadalsskógur
          '12'  : {  x:107,  y:161 }, //Laugarvatnsskógur
          '11'  : {  x:129,  y:184 }, //Múlakot
          '10'  : {  x:168,  y:187 }, //Skógarreitur á Kirkjubæjarklaustri
          '9'   : {  x:96,  y:158 }, //Furulundurinn á Þingvöllum
          '8'   : {  x:118,  y:166 }, //Þjórsárdalur
          '7'   : {  x:136,  y:189 }, //Þórsmörk
          '6'   : {  x:123,  y:186 }, //Tumastaðir
          '19'  : {  x:91,  y:144 }, //Vatnshornsskógur
          '18'  : {  x:84,  y:157 }, //Mógilsá í Kollafirði
          '17'  : {  x:92,  y:133 }, //Norðtunguskógur
          '16'  : {  x:87,  y:145 }, //Selskógur
          '15'  : {  x:87,  y:137 }, //Stálpastaðaskógur
          '14'  : {  x:78,  y:133 }, //Jafnaskarðsskógur við Hreðavatn
          '29'  : {  x:203,  y:63 }, //Ásbyrgi
          '28'  : {  x:181,  y:74 }, //Fellsskógur í Köldukinn
          '27'  : {  x:163,  y:84 }, //Grundarreitur í Eyjafirði
          '26'  : {  x:165,  y:84 }, //Kristnesskógur í Eyjarfirði
          '25'  : {  x:168,  y:68 }, //Mela- og skuggabjargaskógur
          '24'  : {  x:137,  y:88 }, //Reykjarhólsskógur
          '23'  : {  x:172,  y:72 }, //Sigríðarstaðaskógur í Ljósavatnsskarði
          '22'  : {  x:173,  y:80 }, //Þórðastaðaskógur í Fnjóskadal
          '21'  : {  x:171,  y:73 }, //Vaglaskógur
          '20'  : {  x:163,  y:74 }, //Vaglir á Þelamörk
          '32'  : {  x:240,  y:123 }, //Arnardalsstaðarskógur í Fljótsdal
          '31'  : {  x:245,  y:111 }, //Hallormsstaðarskógur
          '30'  : {  x:254,  y:121 }  //Jórvík í Breiðdal
        };

     $('body.home div.fadelist div.boxbody')
        .addClass('fadelist-active')
        .innerfade({
            animationtype: 'fade',
            speed: 1000,
            timeout: 4000,
            type: 'random',
            containerheight: '300px',
            'children': 'div.item'
          })
        .find('div.item')
            .each(function() {
                var itemId = $('h3 a', this).attr('href').replace(/^.*\/(\d+)(\/|$)/, '$1');
                if ( fadelisthnit[ itemId ] ) {
                  var  dotpos = fadelisthnit[ itemId ];
                  $(this).css({ backgroundPosition: (dotpos.x - 11) + 'px ' + (dotpos.y - 11) +'px' });
                }
              });


      //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 : '#ffffff',
                    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(/\/[^\/]+\/([^\/]+)$/, '/myndasafn-storar/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
                curtainColor : '#ffffff',
                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 : '#ffffff',
                    curtainOpacity : '0.75',
                    disableIeFading : 1,
                    setContainerWidth : 1,
                    yOffset: 20
                  });


      //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;
                                })
            });


      // structure forest infobox liveinclude
      $('.pgmain .infobox')
          .insertBefore( $('h1', article) )
          .find('ul.activities li')
              .bind('mouseover', function() {
                  $(this).addClass('hover');
                })
              .bind('mouseout', function() {
                  $(this).removeClass('hover');
                })
              .find('span')
                  .each(function() {
                      if ( /^(|nei)$/i.test( $.trim( $(this).html() ) ) ) {
                          $(this).parent().remove()
                      }
                    })
              .end()
          .end()
          .find('ul:not(.activities) li span')
              .each(function() {
                  if ( /^()$/i.test( $.trim( $(this).html() ) ) ) {
                      $(this).parent().remove()
                  }
                });


      // Add "send to facebook" link to articles
      $('p.buttons', article)
          .append(
              $('<a class="btnfacebook" href="#">Senda á 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;
                    })
            );



      // 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/2));
              })
            .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(/\/[^\/]+\/([^\/]+)$/, '/myndasafn-storar/$1');
                  $(this).parent().attr('href', popupSrc);
              })
            .parent()
            .imgPopper({
                    curtainColor : '#ffffff',
                    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


    //init mappopulizer
    var forestlists = $('body.map div.forestlist');
    if (!window.EPLICA_loggedin && forestlists)
    {
      forestlists
          .removeClass('articlelist')
          .find('div.item')
              .wrapAll('<div class="maplist"><div class="boxbody"></div></div>')
              .each(function(i) {
                  $(this).attr('class','mapitem mapitm' + (i+1));
                })
              .parent()
              .parent()
              .appendTo( $('div.pgmain div.wrap') );
      forestlists.remove();

      var columns = {
        su  : { label : 'Suðurland' },
        vl  : { label : 'Vesturland' },
        nl  : { label : 'Norðurland' },
        au  : { label : 'Austurland' }
      };

      var hnit = {
        13  : {  x:291,  y:365, group : 'su' }, // Haukadalsskógur
        12  : {  x:262,  y:381, group : 'su' }, // Laugavatnsskógur
        11  : {  x:323,  y:463, group : 'su' }, // Múlakot
        10  : {  x:450,  y:457, group : 'su' }, // Skógarreitur á Kirkjubæjarklaustri
        9   : {  x:241,  y:374, group : 'su' }, // Þingvellir
        8   : {  x:314,  y:403, group : 'su' }, // Þjórsárdalur
        7   : {  x:347,  y:470, group : 'su' }, // Þórsmörk
        6   : {  x:305,  y:459, group : 'su' }, // Tumastaðir
        19  : {  x:224,  y:338, group : 'vl' }, // Klausturskógur (Vatnshornsskógur)
        18  : {  x:194,  y:379, group : 'vl' }, // Mógilsá í Kollafirði
        17  : {  x:212,  y:293, group : 'vl' }, // Norðtunguskógur
        16  : {  x:204,  y:337, group : 'vl' }, // Selskógur
        15  : {  x:214,  y:321, group : 'vl' }, // Stálpastaðaskógur
        14  : {  x:188,  y:293, group : 'vl' }, // Ystatunga, Hreðavatn og Jafnaskarðsskógur
        29  : {  x:552,  y:93,  group : 'nl' }, // Ásbyrgi
        28  : {  x:490,  y:128, group : 'nl' }, // Fellsskógur í Köldukinn
        27  : {  x:435,  y:175, group : 'nl' }, // Grundarreytur í Eyjafirði
        26  : {  x:448,  y:162, group : 'nl' }, // Kristnesskógur í Eyjarfirði
        25  : {  x:457,  y:114, group : 'nl' }, // Mela- og skuggabjargaskógur (Dalsmynni)
        24  : {  x:354,  y:163, group : 'nl' }, // Reykjarhólsskógur
        23  : {  x:472,  y:133, group : 'nl' }, // Sigríðarstaðaskógur í Ljósavatnsskarði
        22  : {  x:471,  y:154, group : 'nl' }, // Þórðastaðaskógur í Fnjóskadal
        21  : {  x:456,  y:138, group : 'nl' }, // Vaglaskógur
        20  : {  x:434,  y:133, group : 'nl' }, // Vaglir á Þelamörk
        32  : {  x:662,  y:260, group : 'au' }, // Arnardalsstaðarskógur í Fljótsdal
        31  : {  x:682,  y:239, group : 'au' }, // Hallormsstaðarskógur
        30  : {  x:707,  y:271, group : 'au' }  // Jórvík í Breiðdal
      };

      var myMaplist = $('body.map div.maplist div.boxbody');

      var colContainer = $(
        '<div class="col-row">'+
          '<div class="col-1_4 col-first"></div>' + 
          '<div class="col-1_4"></div>' + 
          '<div class="col-1_4"></div>' + 
          '<div class="col-1_4"></div>' + 
        '</div>' );
      // over-write column Objects with <div />s
      var c = 0;
      var cols = colContainer.find( 'div.col-1_4' );
      for (var col in columns) {
        columns[col] = $(
          '<div class="sector"><h3>'+columns[col].label+'</h3><ul class="placenames" /></div>' )
              .appendTo( cols.eq( c++ ) )
              .find( 'ul' );
      }

      myMaplist
          .mapPopulizor({
              mappoints: hnit,
              itemcolumns : columns,
              itemselector : 'div.mapitem',
              x_flip : 600,
              dotOffset : [-13, -15]
            })
          .append( colContainer )
          .find('div.mapitem')
              .removeClass( 'mapitem' )
              .addClass( 'bubble' )
          .end()
          .find('ul.itemlist')
              .find('li')
                  .each(function(i){
                      var itemId = $('a', this).attr('href').replace(/^.*\/(\d+)(\/|$)/, '$1');
                      var itemData = hnit[itemId] || {};
                      columns[ itemData.group ].append( this );
                    })
              .end()
              .remove();


      if (msie6)
      {
        myMaplist.find('.marker').hoverClass('marker-hover');
      }

    }// end maps


    // fontsizer
    $('div.pagestyle').fontsizer();

    // validate all forms
    $('form').autoValidate();

  }
);
// **** /jqreq *****
