// 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(
  'autovalidate',
  'eutils',  // use $.fn.detach()
  'imgpop',
  'listscroller',
  function(){
    var $ = jQuery;

    $('body').addClass('js-active');
    
    if (!window.EPLICA_loggedin)
    {
      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').Req('x/ifixpng', function(){
            $(this).ifixpng();
          });
      }

      //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'
            });
            
            
      $('a.imgpopup')
          .imgPopper({
                      curtainColor : '#000000',
                      curtainOpacity : '0.75'
                    });

            
    }
    
    // cartstatus
    $('div.cartstatus .item').pause(5000).slideUp(500);

  
    
    // products
    $('div.products .item')
        .each(function(){
          
            $(this).find('h3').append($(this).find('.meta'));
          
            var button = $(this).find('.fi_btn input');
          
            if(button.length)
            {
              $('<a class="buy" href="#">Setja í körfu</a>')
                  .bind('click', function (e) {
                      button.trigger('click');
                      return false;
                    })
                  .appendTo( $(this).find('.more') );
            }
        });
        
    // product
    var product = $('.product .boxbody');

    product.find('p.meta')
        .find('.source')
            .appendTo( product.find('h1') )
        .end()
        .remove();

      product
        .find('.extras .moreolder span')
            .mouseenter(function(e){
                product.find('.moreolder ul')
                    .show('fast')
            });
      
      product
        .find('.moreolder ul')
            .mouseleave(function(e){
                $(this).hide('fast')
      });

    $('div.topbox')
      /*
        .find('div.item img')
            .wrap('<div class="img" />')
        .end()
      */
        .listscroller({
            item            : 'div.item',
            windowSize      : 1,
            stepSize        : 1,
            paging          : true,
            speed           : 500,
            autoScrollDelay : 7000,
            controls        : 'below',
            hideClass       : 'overflow',
            wrap            : 'both',
            animation       : 'accordion'
        });
        
        
        
    $('div.hbox img')
        .each(function(){
            $(this).closest('.item').find('a').eq(0).css('background-image', 'url(' + $(this).attr('src') + ')');
        });
        
    
    // prodlist  

    $('.prodlist .item')
        .each(function(){
          
            var headlength = $(this).find('h3 a').text().length;
            var summarylength = 120;
            var headmaxlength = 25;
          
            if(headlength > headmaxlength) {
              summarylength = summarylength - (headlength - headmaxlength);
            }
        
            var summary = $(this).find('.summary');
          
            var moreLink = summary.find('.more').detach(),
                txt = summary.text();
                
            summary.html( $.cropText(txt, summarylength) ).append(moreLink);
        
        });
    
     

    //remove flicker trick
    $('#noflickerCSS').remove();

    // validate all forms
    $('form').autoValidate();

  }
);
// **** /jqreq *****
