// 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',
  'mailtoenabler',
  function(){
    var $ = jQuery;

    // 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:      false,
                speed:       1000,
                item:        '.item'
              });

    // 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)
    {



      //create tabs
      var tabarticle = $('div.event:has(h2.tab), div.article: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();
        });



      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').ifixpng();
      }


      //popup in articles
      $('div.article div.imagebox a.img')
          .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();
          
          
      // yearindex
      $('.pgmain .yearindex li')
          .equalizeHeights();
          



      $('.mediagallery.albums li').each(function() {
          $(this).bind('click', function() {
              window.location = $(this).parent().find('a:first').attr('href');
            })
        });
/*
      $('.article .imagelist a.img')
          .imgPopper({
              curtainColor : '#ffffff',
              curtainOpacity : '0.75',
              setContainerWidth : 1,
              yOffset: 10 
            });
*/


      //employee list ajax
      $('div.people').Req('/bitar/common/personas/loadEmployee.js', function(){ this.eplicaEmployeeLoader(); });


      //enable mailto links
      $('div.pgmain span.netfang, div.pgfoot span.netfang, div.pgmain td.netfang').mailtoEnabler();




      //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;
            });
    }
    
    
    // monthlist
    $('.monthlist td .dateinfo')
        .hover(
            function(){
                $(this).parent().addClass('hover');
            },
            function(){
                $(this).parent().removeClass('hover');
            });
    
    

    //remove flicker trick
    $('#noflickerCSS').remove();

    // validate all forms
    $('form').autoValidate();

  }
);
// **** /jqreq *****
