/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/
// encoding: utf-8

(function(){

  if (window.collapseSections)
  {
    collapseSections.config = {
      'div.sitenav' : {
        handle      : "collapse",
        headSel     : "h2.boxhead",
        startOpen   : 0,
        showControls : 0,  // 0=off; 1=above; 2=below; 3=both
        hoverOpen   : false,
        hoverClose  : false
      },
      'div.dropmenu div.dropmenu' : {
        handle      : "collapse",
        headSel     : "h3.label",
        startOpen   : 0,
        showControls : 0,  // 0=off; 1=above; 2=below; 3=both
        hoverOpen   : false,
        hoverClose  : false
      }
    };
    collapseSections.init();
  }

  if (window.cbBlockToggler)
  {
    cbBlockToggler.triggers = {
      'rf_heilgamur'   : 'gamastaerd'
    };
    cbBlockToggler.init()
  }

  (function(){
    var tabs = DOM.get( 'div.fnav' );
    var activeClass = 'fnav-current';
    var currentTab;

    // hover event
    function switchTo ( e ) {
      if (currentTab) { DOM.removeClass( currentTab, activeClass ); }
      DOM.addClass( this, activeClass ); 
      currentTab = this;
    }

    // init  
    if (tabs && tabs.length) {
      for (var i=0; i < tabs.length; i++) {
      
        var focusElm = DOM.get('h2.boxhd a', tabs[i])[0];
        
        if(focusElm) {
          Event.add( focusElm, 'focus', switchTo, tabs[i]);
        }
        
        Event.add( tabs[i], 'mouseover', switchTo );
        
      }
      DOM.addClass( tabs[0], activeClass ); 
      currentTab = tabs[0];
    }
  })();


})();


(function($){

  $('body.home .containerno label')
      .bind('click', function() {
          $(this).hide()
          $('#containerno1').focus();
        });
        
  $('a.assistance')
      .bind('click', function(){
          window.open( $(this).attr('href'), 'popupInfo', 'width=700,height=500' );
          return false;
      });
      
  $('div.serviceform form')
      .bind('submit', function(){
        
          $('input#tracking').attr('value', $('#containerno1').attr('value') + '/' + $('#containerno2').attr('value'));
          
          window.open( $(this).attr('action'), 'popupWin', 'width=700,height=500' );
          
      });
  
  
  $('.home a.interview').appendTo('.home div.fpnewslist div.boxbody');

      
/* 
  // Christmas countdown
  function plural(n) { return n%10 != 1  ||  n%100 == 11; };
  function daysLeftToXmas() {
    var now = new Date() ;
    var xmas = (new Date(now.getFullYear(),11,24));
    var msInADay  = 1000*60*60*24;
    return Math.ceil((xmas-now)/msInADay);
  };
  var daysLeft = daysLeftToXmas();
  var xmasText = $("#pgmain .xmasbox h2")[0];
  if (xmasText)
  {
    if (daysLeft <= 0 || daysLeft > 350)
    {
    text = "Gleðileg jól!";
    }
    else if(plural(daysLeft))
    {
    text = daysLeft + " dagar til jóla";
    }
    else
    {
    text = daysLeft + " dagur til jóla";
    }
    xmasText.innerHTML = text;
  }
*/
      
  //webservice opens countdown
  function plural(n) { return n%10 != 1  ||  n%100 == 11; };
  function daysLeftToXmas() {
    var now = new Date() ;
    var tdate = (new Date(now.getFullYear(),12-1,8));
    var msInADay  = 1000*60*60*24;
    return Math.ceil((tdate-now)/msInADay);
  };
  var daysLeft = daysLeftToXmas();
  var targetText = $("div.pagebody div.fbox h2")[0];
  if(targetText)
  {
    var text = daysLeft;
    if (daysLeft <= 0 || daysLeft > 350)
    {
      text = "The Service web is open!";
    }
    else if(plural(daysLeft))
    {
      text = "<strong>" + text + "</strong> days <br /> until <span>the Service web</span> opens";
    }
    else
    {
      text = "<strong>" + text + "</strong> day <br /> until <span>the Service web</span> opens";
    }
    targetText.innerHTML = text;
  }


  //destination matrix
  var matrix = $('table.logistic-matrix');
  if (matrix.length && !window.EPLICA_loggedin)
  {
    matrix
        .delegate('th, td', 'mouseenter', function (e) {
            var idx = $(this).index();
            matrix.find('tr').each(function () {
                $(this).find('th, td').eq(idx).addClass('highlight');
              });
          })
        .delegate('th, td', 'mouseleave', function (e) {
            var idx = $(this).index();
            matrix.find('tr').each(function () {
                $(this).find('th, td').eq(idx).removeClass('highlight');
              });
          });

    matrix.find('td:has(a)').each(function () {
        var thisTd = $(this).wrapInner('<div class="iw" />'),
            titleText = $.trim($(this).find('a').attr('title')),
            href = $.trim($(this).find('a').attr('href')),
            isOver;
        thisTd.find('a').removeAttr('title').bind('click', function (e) {return false;});

        if (titleText)
        {
          titleText = titleText.split(';');
          $.each(titleText, function (i) {
              if (/@/.test(titleText[i]))
              {
                var text = $.trim(titleText[i].replace(/email:/i,''));
                titleText[i] = 'Email: <a href="mailto:'+ text +'">'+ text +'</a>';
              }
            });
          titleText = titleText.join('<br />');
          thisTd.find('.iw')
              .append('<span class="info" style="display: none;"><b>For more information:</b> <br />'+ titleText +'</span>')
              .bind('mouseenter', function (e) {
                  isOver = true;
                  setTimeout(function(){
                      if (isOver)
                      {
                        thisTd
                            .addClass('active')
                            .find('.info').fadeIn('fast');
                      }
                    }, 250);
                })
              .bind('mouseleave', function (e) {
                  isOver = false;
                  setTimeout(function(){
                      if (!isOver)
                      {
                        thisTd
                            .removeClass('active')
                            .find('.info').fadeOut('fast');
                      }
                    }, 100);
                });
          if (href != '#')
          {
            thisTd.find('.info').append('<br/>').append(
                $('<a href="'+ href +'">Click for more information</a>').bind('click', function (e) {
                    matrix.find('.close').trigger('click');
                    $.get(
                         href,
                        'justPicPos=pgmain',
                        function(data) {
                          var moreinfo = $(data).find('.pgmain .boxbody');
                          moreinfo.find('.buttons').remove();
                          thisTd
                              .append('<div class="moreinfo" style="display: none;">'+ moreinfo.html() +'</div>')
                              .find('.moreinfo')
                                  .append(
                                      $('<a class="close" href="#" title="close">x</a>').bind('click', function (e) {
                                          thisTd.find('.moreinfo').fadeOut('fast', function() { $(this).remove(); });
                                          thisTd.find('.iw').trigger('mouseleave');
                                          return false;
                                        })
                                    )
                                  .fadeIn('fast');

                          moreinfo = thisTd.find('.moreinfo');
                          var mL = -parseInt(moreinfo.outerWidth()/2, 10);
                          moreinfo.css({ 'marginLeft': mL });
                          moreinfo.find(' img').bind('load readystatechange', function (e) {
                              mL = -parseInt(moreinfo.outerWidth()/2, 10);
                              moreinfo.css({ 'marginLeft': mL });
                            })[0].scr += '';
                          
                        }
                      );
                    return false;
                  })
              
            )

          }
        }
      });
      
    $(window).bind('keyup', function (e) {
        if (e.which == 27)
        {
          matrix.find('.close').trigger('click');
        }
      });
  }
  
  $('form').autoValidate();
  if ( !window.EPLICA_loggedin )
  {
    $('.netfang').mailtoEnabler();
  }
  $('.pagestyle').fontsizer();
  $('#qstr,#bookingno').labelizor();
  $('#noContentFlashHack').remove();

})(jQuery);



