// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req(
  'autovalidate',
  'easing-mini',
  'equalizeheights',
  'eutils',
  'filtertable',
  'fontsizer',
  'imgpop',
  'labelizor',
  'listscroller',
  'mailtoenabler',
  'zebralists',
  'x/easing',
  'x/ifixpng',
  'x/innerfade',
  'x/ui-accordion',
  
  function(){
    var $ = jQuery,
        msie6 = $.browser.msie && $.browser.version < 7,
        userStylesOn = !!$('body.userstyles-on')[0];

    $('body').addClass('js-active');

    if ( !userStylesOn ) 
    {

      $('#qstr').labelizor();

      var qsboxbody = $('.qsearch .boxbody');
      qsboxbody.hide().parent()
          .find('.boxhead')
              .wrapInner('<a href="#" />')
              .bind('click', function() {
                  if(qsboxbody.is(':visible')) {
                      qsboxbody.fadeOut(200);
                  } else {
                      qsboxbody.fadeIn(100);
                      $('#qstr', qsboxbody).focus();
                  }
                  return false;
                });


      var flnav = $('div.flnav'),
           flnavHover = false;
      flnav
          .find('ul').hide().end()
          .find('h2.boxhead')
              .wrapInner('<a href="#"/>')
              .find('a')
                  .bind('click', function() {
                      $(this).parent().parent().find('ul').slideToggle(300, 'easeInOut');
                      return false;
                  })
              .end()
          .end()
          .bind('mouseleave focusout', function(e){
              flnavHover = false;
              setTimeout(function(){
                  if(!flnavHover) {
                    flnav.find('ul').slideUp(600, 'easeOut')
                  }
                }, 400);
            })
          .bind('mouseenter focusin', function(e){
              flnavHover = true;
            });
    }


     // frontpage image carousel
    var carousel = $('.carousel');
    if (carousel[0])
    {
      var carouselUl = $('<ul />'),
          isCrossfading = carousel.is('.crossfade'),
          setMarginTop = function (e) {
                              var elm = $(this);
                              elm.css({ 'margin-top' : 114.5 - (elm[0].height/2) });
                            };
      carousel
          .prepend(carouselUl)
          .find('img')
              .each(function() {
                  var elm = $(this).bind('load readystatechange', setMarginTop);
                  $('<li />')
                      .append( elm.parent().attr('href') ? elm.parent() : elm )
                      .appendTo( carouselUl );
                  this.src += ''; //IE force readystate hack
                })
          .end()
          .find('.boxbody, .boxhead')
              .remove();

      if (!window.EPLICA_loggedin && !userStylesOn) 
      {
          carouselUl
              .filter(':has(li:odd)') // contains more than one <li />
                  .listscroller({
                      item : 'li',
                      aspect: 'horizontal',
                      paging: false,
                      animation: isCrossfading ? 'crossfade' : 'carousel',
                      speed: 600,
                      windowSize: 1,
                      autoScrollDelay: 6000
                    });
      }
      userStylesOn  &&  carouselUl.find('li:gt(0)').remove();
    }

    if (userStylesOn) 
    {
      $('.pgmain .imgbanner').remove();
    }


    if (!window.EPLICA_loggedin && !userStylesOn)
    {

      $('div.article blockquote:not(.ath)').wrapInner('<span></span>');

      $('div.pgmain, div.pgextra2').equalizeHeights();
      $('body.home div.bnav').equalizeHeights();
      $('.milli div.box-1_2:even')
          .each(function() {
              var box = $(this);
                  boxes = box.nextAll('div.box-1_2:first').add(box);
              if (boxes.length>1)
              {
                boxes.find('.boxbody').equalizeHeights();
              }
            });

      //$('div.box-1_3:not(.darkbox):not(.quotes):not(.qlinks) > .boxbody').equalizeHeights();

      $('div.box-left').each(function(){
          $(this).nextUntil('div.box-left').andSelf().filter('div:not(.darkbox):not(.quotes):not(.qlinks)').find('> .boxbody').equalizeHeights();
        });

      //popup in articles
      $('div.article div.imgbox a:has(img)')
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
              curtainColor : '#000000',
              curtainOpacity : '0.75'
            });


      //accordion yearindex
      $('div.yearindex div.boxbody')
          .find('p')
              .wrap('<span></span>')
          .end()
          .accordion({
              header : 'h4',
              selectedClass : 'boxhead-open',
              animation : 'easeInOut',
              autoHeight : false
            });

      $('.addrss .moreolder').each(function() {
          var moreLink = $(this).find('a').attr('href');
          $(this).append('<a class="rss" href="' + moreLink + '/rss.xml">RSS</a>')
        });

      // hacks for IE6
      if (msie6) {
        $('div.mnav li')
            .bind('mouseenter focusin', function(e){
                $(this).addClass('hover');
            })
            .bind('mouseleave focusout', function(e){
                $(this).removeClass('hover');
            })
        $('img[src$=".png"]').ifixpng();
      }


      //Article collapser
      var collapsearticle = $('.collapse');
      if ( collapsearticle.length) {
        $('h4', collapsearticle).each(function() {
            var heading = $(this);
            heading.wrapInner('<a href="#" />');
            heading.add( heading.nextUntil('h2, h4') ).wrapAll('<div class="collapsebox" />');
          });
        $('.collapsebox', collapsearticle)
            .each(function() {
                var collapsebox = $(this);
                collapsebox.find('> *:not(h4)').wrapAll('<div class="collapsecontent" />').parent().hide();
                collapsebox
                    .find('h4 a').bind('click', function() {
                        collapsebox.find('.collapsecontent').slideToggle(100);
                        collapsebox.toggleClass('collapse-open');
                        return false;
                      });
              });
      }
      
    }


    //remove flicker trick
    $('#noflickerCSS').remove();


    
    if (!window.EPLICA_loggedin)
    {

      $('.netfang').mailtoEnabler()
          .filter('em').zap();


      var zebraize = function (e) {
              $(this).find('tbody')
                  .zebraLists({
                      //subItems:    '.subrow',  // filter for rows to consider part of/subservient to the previous row
                      //resetItems:  '',         // filter for rows to consider to be a zebra-splitter/-resetter (like sub-headings)
                      oddClass:    '',
                      evenClass:   'even'
                    });
            };

      $('table.filtertable')
          .each(function(){
              var table = $(this),
                  isDbDb = table.is('.databasedatabase');
              if (isDbDb)
              {
                var lang = table.closest('[lang]').attr('lang') || 'en';
                table.find('tbody tr')
                    .filter(function(){  // filter out rows in other languages - to speed up the sorting
                        var rowLang = $(this).attr('lang');
                        return  rowLang  &&  rowLang != lang;
                      })
                    .remove();
              }
              table
                  .filterTable({
                      multiFilter: isDbDb,
                      includeCols: 'th:not(.nofilter)'
                    });

            })
          .bind('filter.filterTable', zebraize);


      $('body').run( zebraize );


      var hash = document.location.href.split(/[?#]/)[1];
      if ( hash )
      {
        hash = hash.replace('id=', '#');
        $(hash).find('a').trigger('click');
      }


      //popup window for videos
      $('a').filter(function() { return $(this).is('.video') || $(this).is('.letterbox') })
          .bind('click', function() {
              var src = $(this).attr('href'),
                  vidHeight = $(this).is('.letterbox') ? '500' : '385',
                  fadespeed = $.browser.msie && $.browser.version < 9 ? 0 : 200; //disable fading in IE<9, stupid png24 alpha + opacity fading bug
                  
              if( src.match(/youtube/i) || src.match(/vimeo/i) ) {
                $('body').append(''+
                  '<div class="ipopup-curtain" style="background-color:#fff;"></div>',
                  '<div class="ipopup-container videopopup" style="display:none">' +
                    '<div class="ipopup-container-wrapper">' +
                      '<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>' +
                  '');
              } else {
                $('body').append(''+
                  '<div class="ipopup-curtain" style="background-color:#fff;"></div>',
                  '<div class="ipopup-container videopopup" style="display:none">' +
                    '<div class="ipopup-container-wrapper">' +
                      '<object width="640" height="' + vidHeight + '">' +
                      '<param name="movie" value="/bitar/common/media/mediaplayer.swf"></param>' +
                      '<param name="allowFullScreen" value="true"></param>' +
                      '<param name="allowscriptaccess" value="always"></param>' +
                      '<param name="flashVars" value="file=' + src + '&autostart=true&bufferlength=4" />' +
                      '<embed flashVars="file=' + src + '&autostart=true&bufferlength=4" src="/bitar/common/media/mediaplayer.swf" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="' + vidHeight + '"></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.7' }, 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;
            });


      //introbox innerfade
      $('.introbox ul')
          .filter(function(){ return $(this).find('li').length > 1 })
              .innerfade({
                  animationtype: 'fade',
                  speed: 1000,
                  timeout: 5000,
                  type: 'sequence'
                });

    } //end loggedin


    $('div.pagestyle').fontsizer();
    $('form').autoValidate();


  });
// **** /jqreq ***** */