// 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(
  'eutils',
  'curtain',
  'fickle',
  'fontsizer',
  'autovalidate',
  'labelizor',
  'tabswitcher',
  'mailtoenabler',
  'roundcorners',
  'imgpop',
  'virtualbrowser',


  function(){
    var $ = jQuery;
    
    
    //Two variables necessary for Virtual Browser
    var MSIEver = parseFloat($.browser.verson);
    var isMSIE = $.browser.msie && MSIEver<7;


    if (!window.EPLICA_loggedin) {

      $('ul.tabs, ul.index').tabSwitcher();
      
      if ($.browser.msie && $.browser.version < 7) {
        $('div.mnav li')
            .bind('mouseenter', function() {
                $(this).addClass('mnavli')
              })
            .bind('mouseleave', function() {
                $(this).removeClass('mnavli')
              });
        $('img[src$=".png"]').Req('x/ifixpng', function(){
            $(this).ifixpng();
          });
      }
      
      //popup in articles
      $('.article .imgbox a.zoomimg')
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
              curtainColor : '#ffffff',
              curtainOpacity : '0.75',
              yOffset: 15
            });
      
      if ($.browser.msie && $.browser.version < 9) {
        $('a.btntempl').roundCorners();
      }

      $('div.article table tr:even, div.people table tr:even').addClass('alt');
      
      $('div.stafflist table a').mailtoEnabler();

      
      // Add "send to facebook" link to articles
      $('p.buttons')
          .prepend(
              $('<a class="facebook" href="#">Senda á facebook</a>')
                  .bind('click', function()  {
                      window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(document.location.href)+'&t='+encodeURIComponent($('h1').text()),'sharer','toolbar=0,status=0,width=626,height=436');
                      return false;
                    })
            );


    }

    $('div.pagestyle').fontsizer();
    $('form').autoValidate();
    $('#noflickerCSS').remove();
    
    
    
    
      // Virtual Browser
      var popupCont,
        f = isMSIE ? 0 : 1,
        nothingfoundMsg,
        cacheBust = 0, // cache busting (needed for IE6)
        getPopup = function () {
            if (!popupCont)
            {
              popupCont = $('<div class="virtualiframe-container" />')
                                .curtain({ className: '' })
                                .fickle({
                                    fadein: 500*f,
                                    fadeout: 500*f,
                                    onOpen: function(){
                                        popupCont.appendTo('body');
                                        //elm.css( 'top', $(window).scrollTop() );
                                      },
                                    onClosed: function(){
                                        popupCont.detach();
                                        var elms = popupCont.data('elms')
                                        elms.head.empty();
                                        elms.body.empty();
                                      },
                                    focusTarget: ''
                                  })
                                .bind('click', function (e) { $(this).fickle('close'); });


                var elm =  $('<div class="virtualiframe">' +
                              '<a href="#" class="focustarget">.</a>' +
                              '<div class="title" />' +
                              '<div class="wrap" />' +
                            '</div>')
                              .bind('click', function (e) { e.stopPropagation(); })
                              .appendTo(popupCont),

                  body =  elm.find('.wrap')
                              .virtualBrowser({
                                  params:       'justPicPos=pgmain',
                                  loadmsgMode:  'replace',
                                  onBeforeload: function (e, request) {
                                                    var url = request.url;
                                                    // Open .xml links in a new window
                                                    if (/\.xml$/.test(url))
                                                    {
                                                      request.elm.target = request.elm.target || 'feedwindow';
                                                      e.passThrough = true;
                                                    }
                                                  },
                                  onLoad:       function (e, request) {
                                                    var linkId = $(this).data('virtualBrowser').cfg.linkId,
                                                        box =  $.getResultBody(request.result)
                                                                   .find( (linkId  &&  ajaxSelectors[linkId]) || ajaxSelectors._default );
                                                    if (box.length)
                                                    {
                                                      box.wrap('<div />').find('form').autoValidate();
                                                      request.resultDOM = box;
                                                    }
                                                    else
                                                    {
                                                      request.resultDOM = nothingfoundMsg || (nothingfoundMsg = $('<div class="nothingfound">Nothing found...</div>'));
                                                      setTimeout(function(){ popupCont.fickle('close'); }, 1000);
                                                    }
                                                  }
                                }),

                  closeBtn = $('<a href="#" class="closebtn" title="Loka">Loka</a>')
                                .bind('click', function (e) {
                                    popupCont.fickle('close');
                                    return false;
                                  })
                                .appendTo(elm);

              popupCont.data('elms', {
                    head:     elm.find('.title'),
                    body:     body,
                    closeBtn: closeBtn
                  }
                );
            }
            return popupCont;
          },

        ajaxSelectors = {
            'rsslinks-window': '.rsslinks',
            _default:          '.pgmain .wrap > *'
            //_default: '.pgmain *:has(>.boxhead):first'
          };

          
     $('.vpopper').each(function(i, link){
          //var linkId = $(link).closest('li')[0].className.split(' ')[0] +'-window';
          var linkId = $(this).attr('class')+'-window';

          // find all links on the page that link to the same URL
          //$('a').filter(function(){ return this.href == link.href; })
              $(this).bind('click', function (e, obj) {
                  getPopup() // <-- returns popupCont
                           .addClass(linkId)
                           .addClass('hastitle')
                           .one('fickleclosed', function(){ $(this).removeClass(linkId); }) // remove-a classa nafn ef fickle er lokað?
                      .fickle('open', { opener: (obj&&obj.opener)||this }); // opener?
                  var elms = popupCont.data('elms'); //?
                  

                  //elms.head.append( link.title );
                  elms.body
                      .one('VBbeforeload', function (e) {
                          $(this).data('virtualBrowser').cfg.linkId = linkId; //cfg?
                        })
                      .virtualBrowser('load', this);
                  return false;
                });
        });


  }
);
// **** /jqreq *****
