// 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(
  'fontsizer',
  'autovalidate',
  'labelizor',
  'eutils',
  'imgpop',
  'equalizeheights',
  'fickle',
  'curtain',
  'listscroller',
  function(){
    var $ = jQuery;

    // labelize search input
    $('#qstr').labelizor();

    if (!window.EPLICA_loggedin)
    {
      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').Req('x/ifixpng', function(){
            $(this).ifixpng();
          });
      }
    // Add "send to facebook" link to articles
    $('p.buttons')
        .append(
            $('<a class="btnfacebook" href="#">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;
                  })
          );

      //zebra tables
      $('tbody tr:nth-child(2n-1)').addClass('odd');
      //$('tbody tr:nth-child(2n)').addClass('even');


      //popup in articles
      $('div.article div.imagebox a.img')
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
              curtainColor : '#000000',
              curtainOpacity : '0.75'
            });
    }

    //remove flicker trick
    $('#noflickerCSS').remove();

    // fontsizer
    $('.pagestyle').fontsizer();

    // validate all forms
    $('form').autoValidate();
    
    // Equal Height Columns
    $('.home .pgmain .box').equalizeHeights();
    $('.home .pgextra3 .travel').equalizeHeights();
    
    // Listscroller
    $('div.slidebox > .boxbody:has(.item)')
        .find('h3')
            .append('<a class="focustarget" href="#">.</a>')  // For accessibility
        .end()
        .listscroller({
            item: '.item',
            paging: true,
            windowSize: 1,
            stepSize: 1,
            autoScrollDelay: 5000,
            animation: 'crossfade',
            jumpLabel: 'Vert að sjá ',
            titleNext: 'Fletta í næstu frétt',
            labelNext: 'Næsta frétt',
            titlePrev: 'Fletta í fyrri frétt',
            labelPrev: 'Fyrri frétt',
            statusLabel: 'Frétt: '
          });
    
    
    //popup windows
      $.fn.popup = function ( cfg ) {
          var popupElm = this;
          if ( popupElm.length) { 
            var curtainElm = $.curtain({
                        className: 'popup-curtain',
                        bg:        '#fff',
                        opacity:   .80
                      })
                remove = cfg && cfg.remove || false;
            popupElm
                .hide()
                .appendTo('body')
                .addClass('popupbox')
                .before(curtainElm)
                .css({
                      //top:        150 + $(window).scrollTop(),
                      //marginLeft: - $.toInt( popupElm.outerWidth() ) / 2
                    })
                .fickle({
                    fadein: 350,
                    onOpen:   function (e) { $(this).prev().fadeIn(100);  },
                    onClose:  function (e) { popupElm.fadeOut(200, function() { $(this).prev().fadeOut(100); }); },
                    onClosed: function (e) { if (remove) { popupElm.prev().remove(); popupElm.remove(); } }
                  })
                .append('<a class="close" href="#">Loka</a>')
                .find('a.close')
                    .bind('click', function (e) {
                        popupElm.fickle('close');
                        return false;
                      })
                .end()
                .fickle('open');
            popupElm.prev().bind('click', function (e) { popupElm.fickle('close'); });
          }
          return popupElm;
      };  
            
            
    //Ajax - For products popups
         var popper = $('.rsspopper');
          popper.click(function() {
                //      alert($(this).text());
           var thisItem = $(this).parents('.rsslinks'),
                toLoad = $(this).attr('href')+' .rsslinks .boxbody'; 
                thisItem.append('<div class="container" />');
                thisRss = thisItem.children('.container');
            thisRss.hide().load(toLoad, function() { $(this).popup(); })
            return false;
            });
            
    //Remove pgrwap-inner2 if it's empty
    var wraptoremove = $('.pgwrap-inner2'),
           text = $.trim( wraptoremove.text() );
          //;;;alert( text.length );
          if ( text.length == 0) {
            wraptoremove.remove();
          }
  }
);
// **** /jqreq *****
