// 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(
  'jquery',
  'eutils', // $.fn.zap()
  'autovalidate',
  'labelizor',
  'x/ifixpng',
  'imgpop',
  'equalizeheights',
  'listscroller',
  'tabswitcher',
  'x/bookmark',
  'mailtoenabler',
  function(){
    var $ = jQuery,
        isIS = $('html').attr('lang') == 'is',
        isEN = $('html').attr('lang') == 'en',
        isDK = $('html').attr('lang') == 'dk';

    // labelize search input
    $('#qstr').labelizor();


    $('.articlelist .item img')
        .wrap('<span class="imginner" />')
        .each(function() {
            $(this).css({ 'margin-top' : ( $(this).parent().height() / 2 ) - ( $(this).height() / 2 )  })
          });


    //boxhead line
    $('body.home .pgmain .boxhead, body.home .pgbottom .boxhead').each(function() {
        $(this).wrapInner('<span class="text" />').prepend('<span class="line"></span>');
        var width = $(this).outerWidth(),
            textWidth = $(this).find('.text').outerWidth();
        $(this).find('.line').css({ width: width - textWidth - 5 })
      });


    //init funky navigation
    var mnav = $('.mnav');
    $('area', mnav).each(function() {
        $(this)
            .bind('mouseenter focusin', function() {
                mnav.addClass( this.id );
              })
            .bind('mouseleave focusout', function() {
                mnav.removeClass( this.id );
              });
      });


    //event scroller og frontpage
    $('body.home .eventlist .boxbody')
        .filter(function(){ return $(this).find('.item').length > 1; })
            .listscroller({
                windowSize:  1,
                animation:   $.browser.msie && parseInt($.browser.version, 10) < 8 ? 'none' : 'crossfade',
                paging:      true,
                autoScrollDelay: 5000,
                speed:       1000,
                item:        '.item'
              })
        .end()
        .find('.item .meta')
            .find('> span.stream:first-child')
                .remove()
            .end()
            .each(function(){
                $('> span.date:not(:first)', this).remove();
              });

    // strip out old HTML snippets for tabs and convert them to The New Style.
    $('.event .boxbody:has(div.tab)')
        .each(function(i, tabEvent){
            tabEvent = $(tabEvent);
            tabEvent.find('hr.tabsplit').remove();
            tabEvent.find('div.tab').find('> div').andSelf().zap();
            tabEvent.find('h3.tabhd')
                .each(function(i, h3){
                    h3 = $(h3);
                    $('<h2 class="tab" />')
                        .insertAfter(h3)
                        .text( h3.text() );
                  })
                .remove();
          })
    // END: strip


    if (!window.EPLICA_loggedin)
    {
      
      var article = $('div.article');

      //create tabs
      var tabarticle = article.filter(':has(h2.tab)'),
          nextText = {
              is:"Næst",
              en:"Next",
              dk:"næste"
            },
          nextTabText = nextText[tabarticle.lang()] || nextText.en;

      tabarticle.each(function(i, article){
          article = $(article)
          article.find('h2.tab').each(function(i, h2){
              h2 = $(h2);
              h2.add( h2.nextUntil('h2') ).wrapAll('<div class="tabpane" />');
            });
          article.find('.tabpane').makeTabbox().tabSwitcher();
        });


        
      $('> .boxbody', article).run(function() {

          //share / bookmark
          var bookmarksOpen,
              shareText = isIS ? 'Segðu frá' : 'Tell a friend',
              emailTextText = isIS ? 'Senda í pósti' : 'Send as e-mail',
              emailSubjectText = isIS ? 'Áhugaverð grein á vef Norræna hússins' : 'Interesting article on the Nordic house website',
              emailBodyText = isIS ? 'Hugsa að þú hefðir áhuga á þessu:\n\n{t}\n{u}' : 'I thought you might find this page interesting:\n\n{t}\n{u}',
              shareHover = false,
              shareBtn = $('<span class="share"><a href="#">'+ shareText +'</a></span>'),
              bookmarks = $('<div class="share-menu" />').bookmark({
                                                      title:   $('h1:first').text(),
                                                      addEmail: true,
                                                      emailText: emailTextText,
                                                      emailSubject: emailSubjectText,
                                                      emailBody: emailBodyText,
                                                      sites:   ['facebook', 'twitter', 'google', 'yahoobm', 'stumbleupon', 'digg', 'reddit', 'delicious', 'spurl'],
                                                      compact: false, // defaults to `true`
                                                      icons:   '/skin/basic/design/i/icons.png'
                                                    });

          shareBtn.appendTo( this );
          bookmarks.hide().appendTo(shareBtn);

          shareBtn
              .find('> a')
                  .bind('click', function (e) {
                      bookmarksOpen ?
                          bookmarks.slideUp(200):
                          bookmarks.slideDown(300);
                      bookmarksOpen = !bookmarksOpen;
                      shareBtn.toggleClass('share-open', bookmarksOpen);
                      return false;
                    })
              .end()
              .bind('mouseleave focusout', function(e){
                  shareHover = false;
                  setTimeout(function(){
                      if(!shareHover) {
                        bookmarks.slideUp(200);
                        bookmarksOpen = false;
                        shareBtn.removeClass('share-open');
                      }
                    }, 1000);
                })
              .bind('mouseenter focusin', function(e){
                  shareHover = true;
                });
        });
        

      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').ifixpng();
      }


      //popup in articles
      $('div.imagebox a.img', article)
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/popup/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
              curtainColor : '#ffffff',
              curtainOpacity : '0.75',
              setContainerWidth : 1,
              yOffset: 10 
            });


      //mediagallery fixes and popups
      $('div.mediagallery li img').each(function() { $(this).wrap('<span class="img"></span>') });
      var imagelist = $('div.mediagallery.imagelist');
      if(imagelist.length) {
        $(window).bind('load', function() {
            $('li', imagelist)
                .equalizeHeights()
                .each(function() {
                  var ht = $(this).height(),
                      mt = (ht - $('a', this).height()) / 2;
                  $('a', this).css({ 'margin-top' : mt })
                  })
          });
        $('.thumbsview a', imagelist)
          .each(function() {
                var imgSrc = $.trim( $('img', this).attr('src') ),
                    popupSrc = imgSrc.replace(/\/[^\/]+\/([^\/]+)$/, '/popup/$1');
                $(this).attr('href', popupSrc);
            })
          .imgPopper({
              curtainColor : '#ffffff',
              curtainOpacity : '0.75',
              disableIeFading : 1,
              setContainerWidth : 1,
              yOffset: 10 
            });

      } // end imagelist

      
      
      // overview
      $('.overview .item').equalizeHeights();
      $('.pgmain .yearindex li').equalizeHeights();
          

      $('.mediagallery.albums li').each(function() {
          $(this).bind('click', function() {
              window.location = $(this).parent().find('a:first').attr('href');
            })
        });


      //employee list ajax
      $('div.people').Req('/bitar/common/personas/loadEmployee.js', function(){ this.eplicaEmployeeLoader(); });


      //popup window for youtube videos
      $('a.youtube-popup, .youtube-popup a')
          .bind('click', function() {
              var title = $(this).attr('title'),
                  src = $(this).attr('href'),
                  fadespeed = $.browser.msie && $.browser.version < 9 ? 0 : 200; //disable fading in IE<9, stupid png24 alpha + opacity fading bug

              $('body').append(''+
                '<div class="ipopup-curtain" style="background-color:#fff;"></div>',
                '<div class="ipopup-container videopopup" style="display:none">' +
                  '<div class="ipopup-container-wrapper">' +
                    '<h2>'+ title +'</h2>' +
                    '<object width="640" height="385">' +
                    '<param name="movie" value="' + src + '"></param>' +
                    '<param name="allowFullScreen" value="true"></param>' +
                    '<param name="allowscriptaccess" value="always"></param>' +
                    '<embed src="'+ src +'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>' +
                    '<div class="paging"><ul class="stepper"><li class="close"><a href="#">Loka</a></li></ul></div>' +
                  '</div>' +
                '</div>' +
                '')

              $('.ipopup-curtain')
                  .css({ opacity : '0' })
                  .animate({ opacity : '0.75' }, fadespeed)
                  .bind('click', function() {
                      $('.ipopup-curtain').fadeOut(fadespeed, function() { $(this).remove() });
                      $('.ipopup-container').fadeOut(fadespeed, function() { $(this).remove() });
                    })
              $('.ipopup-container').css({ 'top' : $(document).scrollTop() + 10 }).fadeIn(fadespeed);

              $('.ipopup-container .close a').bind('click', function() {
                  $('.ipopup-curtain').fadeOut(fadespeed, function() { $(this).remove() });
                  $('.ipopup-container').fadeOut(fadespeed, function() { $(this).remove() });
                  return false;
                })

              return false;
            });
            
      //enable mailto links
      $('div.pgmain span.netfang, div.pgfoot span.netfang, div.pgmain td.netfang').mailtoEnabler();
    }
    
    
    // monthlist
    var monthlist = $('.pgextra2 .monthlist > .boxbody'),
          mlistText = "Sjá yfirlit yfir mánuðinn",
          mlistTextEN = "See all events",
          mlistTextDK  = "Se alle begivenheder";
          
          if (!isIS) {
              if (isEN) {
                  var mlistText= mlistTextEN;
              } else {
                    var mlistText = mlistTextDK;
                  }
          }
    $('td .dateinfo', monthlist)
        .bind('mouseenter mouseleave', function (e) {
            $(this).parent().toggleClass('hover');
          });
    $('h4 a', monthlist).clone().wrap('<span class="more" />').text(mlistText).parent().appendTo(monthlist);

          
//    $('div.monthlist').Req('/bitar/common/calendar/calendarPager.js', function(){ this.eplicaCalendarLoader() });
          


    $('#noflickerCSS').remove();
    $('form').autoValidate();

  }
);
// **** /jqreq *****
