(function( $ ){
  $.fn.wSlide = function(options) {
    var $oList  = $(this), 
        $oListDesc = $('#flow_desc'),
        iInitImgSize = 225,
        iAmount = 40,
        iFocusedImageInit = 2,
        iResize, iClickIndex = 2,
        iFocusedImage = iFocusedImageInit,
        iMarginLeft = 40,
        aMargins = [],
        iMargin,
        oWait;
    function showDesc(){
      var oThisDesc = $oListDesc.children(':eq('+iClickIndex+')');
      var oThisButton = oThisDesc.find('.button');
      
      //show description
      $oListDesc.children().hide();
      oThisDesc.show();
      
      //center description
      if(oThisButton.css('marginLeft') == '0px'){
        $(function() {
          oThisButton.css('marginLeft',((oThisDesc.width()-oThisButton.outerWidth())/2)+'px');
        });
      }
      return false;
    };
    function getMargin(){
      if(iClickIndex == iFocusedImageInit){
        iMargin = 40;
      }else if(iClickIndex>iFocusedImageInit){
        if(iClickIndex == 3){
          iMargin = -65; 
        };
        if(iClickIndex == 4){
          iMargin = -130; 
        }
      }else{
        iMargin = aMargins[iClickIndex-1];
      };
      return iMargin;
    };
    function leftSlide(){
      resize(iClickIndex,getMargin());
      return false;
    };
    function rightSlide(){
      resize(iClickIndex,getMargin());
      return false;
    };
    function resize(piFocus,piMargin){
      if(!piMargin){
        piMargin = aMargins[1];
      };
      iMarginLeft = piMargin;  
      iResize = iAmount*piFocus;
      $oList.children().each(function(index){  
        if(index<piFocus){
          $(this).find('img').animate({
            'width':iInitImgSize-iResize+'px',
            'margin-top':iResize/2+'px'
          });
          $(this).animate({
            'margin-left':iMarginLeft+'px'
          });
          if(iResize>iAmount*2){
            $(this).fadeOut(500);
          }else{
            $(this).fadeIn(800);
          };
          iMarginLeft += iInitImgSize-iResize;
          iResize -= iAmount;
        };
        if(index==piFocus){
          $(this).find('img').animate({
            'width':iInitImgSize+'px',
            'margin-top':'0'
          });
          $(this).animate({
            'margin-left':iMarginLeft+'px'
          });
          iMarginLeft += iInitImgSize-iResize;
        };
        if(index>piFocus){
          iResize += iAmount;
          $(this).find('img').animate({
            'width':iInitImgSize-iResize+'px',
            'margin-top':iResize/2+'px'
          });
          $(this).animate({
            'margin-left':iMarginLeft+'px'
          });
          if(iResize>iAmount*2){
            $(this).fadeOut(500);
          }else{
            $(this).fadeIn(800);
          };
          iMarginLeft += iInitImgSize-iResize;
        };
        if(aMargins.length <= 4){
          aMargins.push(iMarginLeft);
        };
      });
      showDesc();
    };
    $(this).each(function() {
      //hide description
      $oListDesc.children().hide();
      //initial resize
      resize(iFocusedImage,iMarginLeft);
      //navigation buttons
      $('#wLeft').click(function(e){
        $oList.children(':eq('+(iFocusedImage-1)+')').click();
      });
      $('#wRight').click(function(e){
        e.preventDefault();
        $oList.children(':eq('+(iFocusedImage+1)+')').click();
      });
      // click on image
      $oList.children().each(function(){
        $(this).click(function(){
          iClickIndex = $(this).index();
          if(iClickIndex>iFocusedImage){
            oWait = setInterval(function() {
	            if( !$("#flow").children().is(":animated") ) {
		            clearInterval(oWait);
                leftSlide();    
	            }
            }, 100);
          }else if(iClickIndex<iFocusedImage){
            oWait = setInterval(function() {
	            if( !$("#flow").children().is(":animated") ) {
		            clearInterval(oWait);
                rightSlide();
              }
            }, 100);
          }else if(iClickIndex==iFocusedImage){
            window.location.href = $('#flow_desc').find('a:visible.button').attr('href');
          };
          iFocusedImage = iClickIndex;
        });
      });
    });  
  };
  return false;
})( jQuery );

