// 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',
  'x/ifixpng',
  'imgpop',
  'listscroller',
  function(){
    var $ = jQuery;
    
    
    $('#noflickerCSS').remove();


    //cartstatus box
    $('.cartstatus').each(function() {
        var head = $('.boxhead', this),
            block = $('.boxbody', this),
            items = $('.contains span > b:first', block),
            open  = $('.itemadded2cart',this).size() > 0;

        $('.contains', this).remove();

        head
            .find('a')
                .append(items)
                .find('b')
                    .text(' ('+ items.text() +')')
                .end()
                .bind('click', function() {
                    if(open) {
                      block.fadeOut();
                      open = false;
                    } else {
                      block.fadeIn();
                      open = true;
                    }
                    return false;
                  });

          if (open)
          {
            block.show();
            /*
            // requested take out fade by Ragga @ m-design
            setTimeout(function(){
                block.fadeOut();
            },5000);*/
          }
          else
          {
            block.hide();
          }

          block
              .prepend( head.clone() )
              .append('<a class="act" href="#">Close</a>')
              .find('a.act')
                  .bind('click', function() {
                      head.find('a').trigger('click');
                      return false;
                    });
      });


    //collection scroller
    $('.collection .boxbody')
        .listscroller({
            item: 'li',
            aspect: 'horizontal',
            wrap:  'loop',
            paging: false,
            windowSize: 3,
            stepSize: 1,
            animation: 'carousel',
            autoScrollDelay: 3000
          })
        .find('a:has(img)')
            .each(function() {
              var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
              $(this).attr('href', imgsrc)
            })
            .imgPopper({
                curtainColor : '#000000',
                setContainerWidth : 1,
                curtainOpacity : '0.7'
              });



    //searchbox
    var searchBox = $('.qsearch');
    searchBox
        .find('.boxbody')
            .hide()
        .end()
        .find('.boxhead')
        .wrapInner('<a href="a" />')
            .find('a')
                .bind('click', function() {
                    $('.boxbody', searchBox).slideToggle(200, function() { if( $(this).is(':visible') ){ $('#qstr').focus() } });
                    return false;
                  });


    //set up products page
    $('.products .item').each(function() {
        var imgSpan = $(this).find('span.img');
        var imgSrc = $(imgSpan).find('img').attr('src');
        $(this).find('h3 a').prepend('<span class="img" style="background-image: url('+ imgSrc +')"></span>');
        imgSpan.remove();
      });


    if (!window.EPLICA_loggedin)
    {

      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').ifixpng();
      }


      //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 : '#ffffff',
              setContainerWidth : 1,
              curtainOpacity : '0.7'
            });


      var imagelist = $('div.imagelist'),
          setMarginTop = function (e) {
                              $(this).css({ 'margin-top' : ( $(this).parent().height() / 2 ) - ( $(this).height() / 2 ) });
                            };

      $('li a', imagelist)
          .each(function() {
              var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
              $(this).attr('href', imgsrc)
            })
          .imgPopper({
              curtainColor : '#ffffff',
              etContainerWidth : 1,
              curtainOpacity : '0.7'
            });
      $('img', imagelist)
          .each(function() {
              var elm = $(this).bind('load', setMarginTop);
              this.src += ''; //IE force readystate hack
            })

    } // end loggedin


    /* ***  product  *** */
    var productbox = $('div.product .boxbody'),
        productimgbox = productbox.find('.imagebox');
    
    productimgbox
        .find('.imagelist')
            .appendTo(productbox)
            .find('li a')
                .removeClass('img')
                .each(function(i){
                  
                    if(i == 0) {
                        $(this).clone().addClass('img').appendTo(productimgbox);
                    }
                  
                })
                .find('img')
                    .each(function(){
                        $(this).bind('load readystatechange', function (e) {
                            var liparentHeight = $(this).closest('li').height();
                            $(this).css('margin-top', parseInt((liparentHeight - $(this).height()) / 2, 10) + 'px' );
                          });
                          $(this)[0].src += '';
                    })
                .end()
                .bind('click', function(){
                  
                  var src = $(this).find('img').attr('src');
                  productimgbox.find('a.img').attr('href', $(this).attr('href'));
                  productimgbox.find('img').attr('src', src);
                  productimgbox.find('zoom-img').css('background-image', 'url(' + src + ')');
                  
                  return false;
                  
                });
                
        
    productbox.Req( Req.localPath + 'js/productpage.js' );



    /* ***  products  *** */
    $('div.products div.item')
        .each(function(){
            var ul = $('ul', this).hide();

            $('div.colors', this).hover(
              function() {
                $(this).addClass('colors-open');
                ul.fadeIn(300);
              },
              function() {
                $(this).removeClass('colors-open');
                ul.fadeOut(300);
              }
            )
        });


    $('form').autoValidate();

  }
);
// **** /jqreq *****

