/*
 * jQuery plugin for scrolling text inside a div like a dynamic carousel
 * @author  Dennis Nissen (post@dennisnissen.de)
 * @version    0.7 23.07.09
 *
 *
 */

jQuery.fn.scrollview = function(options){

   settings = jQuery.extend({
               distanceY:50,     //space between the texts
               easing:'',        //easing function
               duration:1000,     //duration of the animation
               staticAnimation:false,
               debug:0           //give some debug information
   },options);

   var ViewportCenterY = $(this).height()/2;
   var ViewportWidth = $(this).width();
   var ViewportHeight = $(this).height();
   var Viewport = this;
   var objects = $(this).children(".list").children();

   
   var log = function(text){
      if (settings.debug==1){
         console.log(text);
      }
   }

   $(objects).each(function(){
      this.maximumHeight = $(this).height();
   });

   initialArrange = function(){        //Place object and prepare for animation
         var pos=0-settings.distanceY;
         var cnt=0;
         var last = 0;
       if (settings.staticAnimation==false){
         $(objects).each(function(){ // place object around center and act on mouseover
           
               var height = $(this).height();
               var spacing = (pos == -1 ? 0 : 1);
               var sw = (cnt%2) == 0 ? 1:-1;
               pos = (pos == -1 ?  ViewportCenterY-height/2 : pos+sw*(cnt*(height+settings.distanceY))); //initial Position of Object
            
               $(this).css({"width":ViewportWidth});
               $(this).css({"text-align":"center"});
               $(this).css({"position":"absolute","top":pos});

               var relPosition = (100-Math.abs((100/(ViewportHeight/2))*(-1*(ViewportHeight/2-pos)))); //relative position according center of div
               $(this).css({"opacity":relPosition/100,"font-size":relPosition+"%"})                    //set initial opacity


               $(this).mouseenter(function(){                                                          //the animation
                  if (last != this){                                                                   //avoid double-computation if object is same element as last selected
                     var pos = parseInt($(this).css("top"));
                     var distanceToCenter = ViewportHeight/2-pos;                                      //distance to move
                     $(objects).each(function(){
                        var curPosition = parseInt($(this).css("top"));
                        var newPosition = curPosition+distanceToCenter;
                        var relativePosition = (100-Math.abs((100/(ViewportHeight/2))*(-1*(ViewportHeight/2-newPosition)))); //new relative position for opacity and font-size animation
                        log("Adjusting opacity for '"+this.innerHTML+"' (animating to "+(newPosition)+") to "+(relativePosition/100));
                        $(this).stop().animate({top:"+="+distanceToCenter,
                                             "opacity":(relativePosition/100),
                                             fontSize:relativePosition+"%"},settings.duration,settings.easing);
                     });
                     last = this;
                  }
               });
               log("moved '"+this.innerHTML+"' to "+pos);
            
            
            cnt++;
         });
         }
            else{          //place object on top of scroll-div and repeat not-treated animation
                log("starting static animation");
                $(objects).each(function(){
                   this.initialHeight = $(this).height();
                   this.initialFontSize = parseInt($(this).css("font-size"));
                   pos = pos - settings.distanceY - this.initialHeight;
                   log("starting at "+pos);
                   $(this).css({"position":"absolute","top":pos,"text-align":"center","width":ViewportWidth,"opacity":0});
                   staticAnimation(this);
                });
                
            }
   }

   staticAnimation = function(obj){
      var curPosition = parseInt($(obj).css("top"));

      if (curPosition >= (ViewportHeight)){
            var min = 0;
            $(objects).each(function(){
               var pos = parseInt($(this).css("top"));
               if (pos < min){
                 min = pos;
               }
            });
            curLastPosition = min;
            log("("+curPosition+") moving to TOP ("+min+")");
            var newPosition = min-obj.initialHeight-settings.distanceY;
            $(obj).css({top:newPosition});
      }

       var curPosition = parseInt($(obj).css("top"));
       log("("+curPosition+") moving to center");
       relDuration = (100/ViewportHeight) * (ViewportHeight + Math.abs(curPosition))/100; //Absolute Positioning - relative duration for movement
       var Duration = (settings.duration)*relDuration;
       log("position: "+curPosition+" - "+relDuration);
       var newPosition = ViewportHeight+obj.initialHeight;                                //move to end of viewport + secure space
       $(obj).animate({"top":newPosition},{queue:false,duration:Duration,
                                              complete:function(){new staticAnimation(obj);},
                                              step: function(now,fx){
                                                 if (now>0 && fx.prop == "top"){
                                                  var relativeOpacity = (100-Math.abs((100/(ViewportHeight/2))*((ViewportHeight/2-(now+this.initialHeight/2)))))/100;
                                                  $(this).css({"opacity":relativeOpacity/*,"font-size":relativeOpacity*this.initialFontSize+"px"*/});

                                                 }
                                               },
                                               easing:"linear"});
   }

   initialArrange();
};


