
/*
  
  (c) modx-shopkeeper.ru
  
*/


$.fn.playSlides = function(options){
  
  var thisParent = $(this)
  var moveBlock = $('div.move-block:first',thisParent);
  var opt = $.extend({'sp_width':223,'n_frames':4,'delay':150},options);
  var timer = new Array;
  var frame = new Array;
  var timer2;
  var navLi = $('div.nav li',thisParent);
  var allItems =  $('div.item',moveBlock);
  var itemWidth = moveBlock.parent().width();
  var itemHeight = allItems.first().height();
  var pages = $('div.pages:first',thisParent);
  var curItemInd = 0;
  
  frame = [1,1,1,1];
  
  function play(elem,ind,first,last){
    if(ind == 'undefined')
      var ind = elem.prevAll('li').size();
    var curPos = elem.get(0).style.backgroundPosition=='' ? ['0','0'] : elem.get(0).style.backgroundPosition.split(' ');
    if(elem.is('.active'))
      curPos[0] = '100%';
    var curPosX = curPos[0]!='100%' ? parseInt(curPos[0]) : -opt.sp_width*(opt.n_frames-1);
    if(curPos[0]=='100%')
      elem.css('background-position',curPosX+'px 0');
    frame[ind] = (Math.abs(curPosX)/opt.sp_width)+1;
    if(frame[ind]!=last){
      elem.removeClass('active');
      clearTimeout(timer[ind]);
      timer[ind] = setTimeout(function(){
        var step = first<last ? curPosX-opt.sp_width : curPosX+opt.sp_width;
        elem.css({'background-position':step+'px 0'});
        play(elem,ind,first,last);
      },opt.delay);
    }else{
      if(first<last){
        elem.siblings().removeClass('active').css('background-position','0 0');
        elem.addClass('active');
      }else{
        elem.removeClass('active');//.css('background-position','0 0');
      }
    }
  }
  
  function moveSlide(ind){
    curItemInd = ind;
    var curEl = navLi.eq(ind);
    if(curEl.is('.active')==false){
      play(curEl,ind,1,4);
      curEl.siblings().each(function(){
        var index = $(this).prevAll().size();
        if(ind!=index)
          play($(this),index,4,1);
      });
    }
    var newMarg = -(ind*itemHeight);
    var thisEasing = parseInt(moveBlock.css('marginTop'))>newMarg ? 'easeInOutBack' : 'easeOutBack';//'easeOutBounce';
    clearTimeout(timer2);
    timer2 = setTimeout(function(){
      pages.fadeOut(400,function(){
        $(this).empty();
      });
      moveBlock
      .animate({
          'marginTop': newMarg
        }, 800, thisEasing,function(){
          $('ul',allItems).css('marginLeft',0);
          var numPages = allItems.eq(ind).children().children('li').size();
          if(numPages>1){
            for(var i=1;i<=numPages;i++){
              var act = i==1 ? ' active' : '';
              pages.append('<a class="page'+i+act+'" href="#">'+i+'</a>');
            }
            pages.fadeIn(400);
          }
        });
    },600);
  }
  
  navLi.each(function(i){
    $('a',this).click(function(){
      moveSlide(i);
      return false;
    });
  });
  
  $('a',pages).live('click',function(event){
    var index = $(event.currentTarget).prevAll('a').size();
    var item = allItems.eq(curItemInd);
    var itemUl = item.children('ul');
    $(event.currentTarget).siblings().removeClass('active');
    $(event.currentTarget).addClass('active');
    var newMarg = -(index*itemWidth);
    var thisEasing = parseInt(itemUl.css('marginLeft'))>newMarg ? 'easeInOutBack' : 'easeOutBack';
    itemUl.animate({
      'marginLeft': newMarg
    }, 1000, thisEasing);
    
    return false;
  });
  
};



function showHide(selector){
  if($(selector).is(':hidden'))
    $(selector).show();
  else
    $(selector).hide();
}




$(document).ready(function(){
  
  $('#slideBlock').playSlides({'sp_width':223});
  $('a.lightbox').wlightbox({'spinner_img':'assets/templates/modx-shopkeeper/img/spinner.gif'});
  
});


