/*
 * jQuery Shop Teaser Plugin for Noeastro, v0.2
 * Requires jQuery 1.4.2
 *
 * Author: Martin Gamnitzer (kontakt@martin-gamnitzer.de)
 * Date: Aug 08 10:14:48 2011 +0100
 *
 */


(function($) {

        $.fn.produktbanderole = function(userOptions) {
            
            var parent = $(this);
            var defaults = {
                slidertime          : 600,
                sliderpause         : 5000,
                orientation         : 'horizontal',
                auto                : true,
                visible             : 3,
                scroll              : 3,
                displayproducttext  : false,
                withborder          : true
            };
            var options = $.extend(defaults,userOptions);
            // Validierung der Optionen:
            if(options.visible > 3)
                options.visible = 3;
            else if(options.visible < 1)
                options.visble = 1;

            if(options.scroll > options.visible)
                options.scroll = options.visible;
            else if(options.scroll < 1)
                options.scroll = 1;

            var current = 0;
            var slides = new Array();
            var contentWidth = 181;
            //var contentHeight = options.orientation == 'vertical' ? 215 : 200;
            var contentHeight = 210;
            var lock = false;
            var userMoved = 0;
            var hasMouseover = false;
            //var stepWidth = options.orientation == 'wide' ? 3 : 2;
            var nextButton = $('<div class="pb_nextButton png"></div>');
            var prevButton = $('<div class="pb_prevButton png"></div>')
            var ltCorner = $('<div class="pb_leftTopBorder png"></div>')
            var rtCorner = $('<div class="pb_rightTopBorder png"></div>')
            var lbCorner = $('<div class="pb_leftBottomBorder png"></div>')
            var rbCorner = $('<div class="pb_rightBottomBorder png"></div>')
            
            var contentList = parent.find('.pb_content');
            var amount = contentList.length;            
            var last = amount -1;


            parent.addClass('pb_'+options.orientation+options.visible);
            if(amount <= options.visible && options.orientation == 'vertical')
                parent.addClass('pb_noTopBorder');
            
            if(!options.withborder) {
                parent.addClass('pb_noBorder');
                parent.append(ltCorner).append(rtCorner).append(lbCorner).append(rbCorner);
            }

            if(!options.displayproducttext) {
                parent.addClass('pb_noText');
                contentHeight=175;
            }

            //anordnen der Elemente zu Beginn
            if (options.orientation == 'horizontal') {
                for(var i = 0; i < options.visible; i++) {
                    var l = i * contentWidth;
                    contentList.eq(i).css({left:l+'px',top:0});
                }
            } else {
                for(var i = 0; i < options.visible; i++) {
                    var t = (options.visible - i - 1) * contentHeight;
                    contentList.eq(i).css({left:0,top:t+'px'});
                }
            }
  
            // if scrolling is not possible (not enough elements), just stop here
            if(options.visible >= amount) {
                return;
            }

            parent.children('.pb_contentbox').append(prevButton).prepend(nextButton);
            /*
            if(options.orientation == 'vertical')
                parent.children('.pb_contentbox').prepend('<div class="pb_extraBorder"></div>');
            */
            //run!!
            run();
            

            //c = Startpos
            //n = Schrittweite
            function getNext(c,n) {
                if(c==null) c=current;
                if(n==null) n=options.scroll;
                c += n;
                while(c>last) c -= amount;
                return c;
            }

            function getPrev(c,n) {
                if(c==null) c=current;
                if(n==null) n=options.scroll;
                c -= n;
                while(c<0) c+=amount;
                return c;
            }


            parent.mouseenter(function() {
              hasMouseover = true;
            }).mouseleave(function() {
              hasMouseover = false;
            });

            nextButton.click(function() {
                sliderMoveTo(getNext());
                //block autorunner and start again
                userMoved++;
                run();
            });
            
            prevButton.click(function() {
                sliderMoveTo(getPrev(), -1);
                //block autorunner and start again
                userMoved++;
                run();
            });
            
            function sliderMoveTo(pos,dir) {
                if(lock) return;
                lock = true;
                if(dir != 1 && dir != -1) dir = 1;
                var posDiff = current - pos;
                var overflow = false;
                if((posDiff > 0 && dir == 1) || (posDiff < 0 && dir == -1)) {
                    posDiff -= dir * amount;
                    overflow = true;
                }
                var from = {property: 0};
                if(options.orientation=='horizontal') {
                    var to = {property: posDiff * contentWidth};
                    var range1 = -1 * contentWidth;
                    var range2 = (options.visible + 1) * contentWidth;
                 } else {
                    var to = {property: posDiff * contentHeight};
                    var range1 = -1 * contentHeight;
                    var range2 = (options.visible) * contentHeight + 20 ;
                 }
                $(from).animate(to, {
                    duration: options.slidertime,
                    step: function() {
                        var j = 0;
                        var prop = this.property;
                        if(dir == 1) {
                            for(var i=0; i< options.visible * 2; i++) {
                              var p = current+i > last ? current+i-amount : current+i;
                              if(options.orientation=='horizontal') {
                                  var n = i*contentWidth + prop;
                                  if(n >= range1 && n <= range2)
                                      contentList.eq(p).css({left:n+'px',top:0});
                              } else {
                                  var n = (options.visible-1-i)*contentHeight - prop;
                                  if(n >= range1 && n <= range2)
                                      contentList.eq(p).css({left:0, top: n+'px'});
                              }
                            }
                        } else {
                            for(var i=options.visible -1; i>= -1* options.visible; i--) {
                              var p = current+i > last ? current+i-amount : current+i;
                              if(p<0) p+=amount;
                              if(options.orientation=='horizontal') {
                                  var n = i*contentWidth + prop;
                                  if(n >= range1 && n <= range2)
                                      contentList.eq(p).css({left:n+'px',top:0});
                              } else {
                                  var n = (options.visible-1-i)*contentHeight - prop;
                                  if(n >= range1 && n <= range2)
                                      contentList.eq(p).css({left:0, top:n+'px'});
                             }
                           }
                        }

                    },
                    complete: function() {
                        //final position;
                        contentList.css({left:'-400px'});
                        current = pos;
                            for(var i=0; i<options.visible;i++) {
                                var p = current+i > last ? current+i-amount : current+i;
                                if(options.orientation=='horizontal') {
                                    var n = (i * contentWidth);
                                    contentList.eq(p).css({left:n+'px',top:0});
                                } else {
                                    var n = ((options.visible-1-i) * contentHeight);
                                    contentList.eq(p).css({left:0, top: n+'px'});
                                }
                            }
                        lock = false;
                    }
                }); //animate end


            }


            function run() {
                if(!options.auto)
                  return;

                window.setTimeout(function() {
                    if(userMoved > 0) {
                      userMoved--;
                      return;
                    }
                    if(!hasMouseover)
                      sliderMoveTo(getNext());
                    run();
                }, options.sliderpause);

            }

        };

})(jQuery);



