/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

(function($){
  
  if ($.fn.fontsizer) {
    $('.fontsizer, .pagestyle').fontsizer();
  }

  $('div.article table tr:even').addClass('alt');

  if ($.mailtoEnabler) {  
    $('a').mailtoEnabler();
  }
  
  if ($.fn.imgPopper) {
      //popup in articles
      $('.mimg .image img')
          .each(function() {
            var imgsrc = $(this).attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/xlarge/$1');
            $(this).wrap('<a href="'+ imgsrc +'" />')
          })
          .parent()
              .imgPopper({
                  curtainColor : '#f0f0f0',
                  curtainOpacity : '0.7'
                });
  }
  
  // add class to hide text in articles before an image
  if(/mynd/.test(document.location)) {
    $('body').addClass('mynd');
  }

  // popup fyrir flash leiki 
  $('#leikir-link a').each(function (i) {
    $( this )
      .bind('click', function (e) {
        var box = $([
          '<div class="leikjabox" id="leikjabox'+ i +'">',
            '<object width="1038" height="742" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">',
            '<param value="' + $(this).attr('href') + '" name="movie"/>',
            '<param value="high" name="quality"/>',
            '<param value="transparent" name="wmode"/>',
            '<embed src="' + $(this).attr('href') + '" width="1038" height="742" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />',
            '</object>',
          '</div>'
        ].join(''));
        var close = $('<div class="closeFlBtn"><a href="#closeFlash">Loka glugga</a></div>');
        close.find('a').one('click', function (e) {
          box.remove();
          return false;
        })
        $( document.body ).append( box.append( close ) );
        return false;
      })
  });
  
  
  // video popper á millisíðum
  $('body.home div.stnav').append('<span class="videolink"><a class="video-popup" href="/media/video/1_velkomin.flv" title="Smelltu hér til að fá lýsingu á táknmáli">Táknmál - Velkomin</a></span>');
  var appto = $('body.twocol') ? '1' : '2',
      popupLink = $('a.video-popup');
  
    if(!$('body.home').length) {
      popupLink
          .appendTo('div.pageextra' + appto + ' div.wrapper')
          .wrap('<span class="videolink"></span>');
    };
    
    popupLink
      .attr('title', 'Smelltu hér til að fá lýsingu á táknmáli')

      .bind('click', function (e) {

          var popupLink = this;
          var popWrapper = $(
                  '<div class="popup-wrapper">' +
                    '<div class="popup-inner-wrapper">' +
                    '<div id="popupvideocontainer" class="videocontainer">' +

                      // videoPlayer goes here

                      '<div class="video">' +
                        '<div class="videoname">' + $('div.pagebody h2.boxhd:first').html() + '</div>' +
                      '</div>' +
                      
                      '<div class="act">' +
                        '<a class="close">' +
                          '<strong>Loka glugga</strong>' +
                        '</a>' +
                      '</div>' +

                    '</div>' +
                    '</div>' +
                  '</div>'
                );
          var videoPlayer = $(
                  '<object width="530" height="390" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">' +
                    '<param name="movie" value="/media/flash/mediaplayer/player.swf?file=' + popupLink.href + '&width=530&height=390&autostart=true&bufferlength=5" /><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" />' +
                    '<embed src="/media/flash/mediaplayer/player.swf?file=' + popupLink.href + '&width=530&height=390&autostart=true&bufferlength=5" width="530" height="390" allowfullscreen="true" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
                  '</object>'
                );

          popWrapper
              .appendTo('div.mwrapper')
              .hide()
              .fadeIn(500, function () {
                  videoPlayer.prependTo('#popupvideocontainer');
                })
              .bind('click', function(e){
                  videoPlayer.remove();
                  popWrapper
                      .fadeOut(500, function(){ $(this).remove(); });
                })
              .find('a.close')
                  .bind('click', function(e){ popWrapper.trigger('click'); return false; })
              .end()
              // Contain clicks so they don't bubble up to the curtain element
              .find('> *')
                  .bind('click', function(e){  e.stopPropagation(); return false; })
              .end();

                    
          return false;
        });


  // map
  var sectors = {
    rvk : { label : 'Höfuðborgarsvæðið' },
    ne  : { label : 'Norðurland eystra' },
    vl  : { label : 'Vesturland' },
    au  : { label : 'Austurland' },
    vf  : { label : 'Vestfirðir' },
    su  : { label : 'Suðurland' },
    nv  : { label : 'Norðurland vestra' }
  };

  var hnit = {
    nr_444  : {  x:161,  y:177,  label : "Staðarkirkja á Reykjanesi", sector : 'vf' },
    nr_373  : {  x:154,  y:111,  label : "Hjallur í Vatnsfirði", sector : 'vf' },
    nr_430  : {  x:136,  y:102,  label : "Litlibær í Skötufirði", sector : 'vf' },
    nr_vmv  : {  x:133,  y:81,   label : "Vindmylla í Vigur", sector : 'vf' },
    nr_hiv  : {  x:131,  y:73,   label : "Viktoríuhús í Vigur", sector : 'vf' },
    nr_399  : {  x:69,   y:101,  label : "Hraunskirkja í Keldudal", sector : 'vf' },
    nr_432  : {  x:259,  y:197,  label : "Kirkjuhvammskirkja", sector : 'nv' },
    nr_376  : {  x:228,  y:315,  label : "Reykholtskirkja í Borgarfirði", sector : 'vl' },
    nr_336  : {  x:225,  y:449,  label : "Húsið og Assistentahúsið á Eyrarbakka", sector : 'su' },
    nr_443  : {  x:167,  y:445,  label : "Krýsuvíkurkirkja", sector : 'su' },
    nr_337  : {  x:317,  y:368,  label : "Tungufellskirkja", sector : 'su' },
    nr_377  : {  x:310,  y:467,  label : "Keldur á Rangárvöllum", sector : 'su' },
    nr_340  : {  x:432,  y:515,  label : "Sauðahús í Álftaveri", sector : 'su' },
    nr_341  : {  x:472,  y:439,  label : "Bænhús á Núpsstað", sector : 'su' },
    nr_342  : {  x:534,  y:427,  label : "Selið í Skaftafelli", sector : 'au' },
    nr_343  : {  x:551,  y:448,  label : "Hofskirkja í Öræfum", sector : 'au' },
    nr_hoe  : {  x:443,  y:196,  label : "Hólar í Eyjafirði", sector : 'ne' },
    nr_400  : {  x:456,  y:186,  label : "Klukknaportið á Möðruvöllum í Eyjafirði", sector : 'ne' },
    nr_357  : {  x:438,  y:147,  label : "Skipalón", sector : 'ne' },
    nr_358  : {  x:416,  y:114,  label : "Arngrímsstofa í Svarfaðardal", sector : 'ne' },
    nr_362  : {  x:384,  y:151,  label : "Nýibær á Hólum í Hjaltadal", sector : 'nv' },
    nr_359  : {  x:366,  y:175,  label : "Stóru-Akrar í Skagafirði", sector : 'nv' },
    nr_360  : {  x:351,  y:181,  label : "Víðimýrarkirkja", sector : 'nv' },
    nr_431  : {  x:348,  y:166,  label : "Glaumbær í Skagafirði", sector : 'nv' },
    nr_401  : {  x:347,  y:156,  label : "Bæjardyr á Reynistað", sector : 'ne' },
    nr_366  : {  x:347,  y:146,  label : "Sjávarborgarkirkja", sector : 'ne' },
    nr_363  : {  x:368,  y:127,  label : "Grafarkirkja á Höfðaströnd", sector : 'nv' },
    nr_447  : {  x:367,  y:113,  label : "Pakkhús á Hofsósi", sector : 'nv' },
    nr_402  : {  x:455,  y:119,  label : "Laufás í Eyjafirði", sector : 'ne' },
    nr_416  : {  x:500,  y:136,  label : "Grenjaðarstaður í Aðaldal", sector : 'ne' },
    nr_350  : {  x:509,  y:150,  label : "Þverá í Laxárdal", sector : 'ne' },
    nr_415  : {  x:528,  y:182,  label : "Grænavatn í Mývatnssveit", sector : 'ne' },
    nr_348  : {  x:577,  y:151,  label : "Sæluhús við Jökulsá á Fjöllum", sector : 'ne' },
    nr_347  : {  x:643,  y:59,   label : "Sauðanes á Langanesi", sector : 'ne' },
    nr_338  : {  x:653,  y:163,  label : "Bustarfell í Vopnafirði", sector : 'au' },
    nr_345  : {  x:700,  y:191,  label : "Galtastaðir fram í Hróarstungu", sector : 'au' },
    nr_2369 : {  x:724,  y:255,  label : "Sómastaðir við Reyðarfjörð", sector : 'au' },
    nr_392  : {  x:699,  y:298,  label : "Teigarhorn við Berufjörð", sector : 'au' },
    nr_429  : {  x:444,  y:186,  label : "Saurbæjarkirkja í Eyjafirði", sector : 'ne' },
    nr_333  : {  x:169,  y:398,  label : "Nesstofa", sector : 'rvk' }    
  };

  var map_fix = { x:-6, y:-8 };
  var mapcontainer = $('div.maplist').removeClass('articlelist').find('div.content');
  if (mapcontainer.length) {

    var map = $('<div class="map"></div>');
    var cols = $(
      '<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>' )
      .appendTo( mapcontainer ).find( 'div.col-1_4' );
    var c = 0;
    for (var sec in sectors) {
      sectors[sec].jq = $(
        '<div class="sector">'+
          '<h3>'+sectors[sec].label+'</h3>'+
          '<ul class="placenames"></ul>'+
        '</div>' )
        .appendTo( cols.eq( Math.floor( c++ / 2 ) ) ).find( 'ul' );
    }
    mapcontainer.addClass( 'maplist-active' );
    mapcontainer.prepend( map );

    $( 'div.item', mapcontainer ).hide().each(function () {
      var place  = $( this );
      var title  = $.trim( place.find( 'h3' ).eq( 0 ).text() );
      var link   = place.find( 'h3 > a, span.more a').eq(0);
      var pls    = $( '<li><a href="#">' + title + '</a></li>' );   // should link to place -> id 
      var marker = $( '<span class="marker"><a href="#"></a><i><b>' + title + '</b></i></span>' );
      var close  = $( '<a href="#" title="Loka" class="close">x</a>' );
      // try to pull the item's id from it's link
      var nr = '';
      if (link.length) {
        nr = link.attr('href').replace(/^.*?\/nr\/(\d+)($|\/.*$)/, 'nr_$1');
      }
      if ( !nr || nr == 'nr_' ) { // still nothing? - find ID by title
        for (var h in hnit) { if ( hnit[h].label == title ) { nr = h; break; } }
      }
      if ( nr ) {
  
        var c = hnit[ nr ] || { x:0, y:0 };
        // remove links of headings
        place.find( 'h3' ).html( '<span>'+title+'</span>' );
        $([ marker[0], place.find('h3 span')[0], pls.find('a')[0], close[0] ]).click(function (){
          if (place.is(':visible')) {
            place.stop().fadeOut(function (){ 
              $( this ).css( 'opacity', '' ); 
              pls.removeClass('marker-active');
            });
            marker.removeClass('marker-active');
          }
          else {
            place.stop().fadeIn(function () {
              $( this ).css( 'opacity', '' ); 
              marker.addClass('marker-active');
            });
            pls.addClass('marker-active');
          }
          return false;
        });
        marker
          .css({ top : c.y + map_fix.y, left : c.x + map_fix.x })
          .hover(
            function (e) {
              $( 'i', this ).stop().fadeIn(function(){
                  $( this ).css( 'opacity', '' ); 
                });
              marker.addClass('marker-hover');
            },
            function (e) {
              $( 'i', this ).stop().fadeOut(function (){ 
                $( this ).css( 'opacity', '' ); 
                marker.removeClass('marker-hover');
              });
            });
        if ( c.x >= 530 ) {
          place.addClass('place-flip');
          marker.addClass('marker-flip');
        }
        place
          .css({ top : c.y + map_fix.y, left : c.x + map_fix.x })
          .addClass( 'place' ).removeClass( 'item' )
          .hide().append( close );
  
        sectors[ c.sector ].jq.append( pls );
        map.append( place );
        map.prepend( marker );
        
      }

    });
    
  }


  $('form').autoValidate();
  $('#noContentFlashHack').remove();




})(jQuery);