/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/**
 * jQuery[a] - Animated scrolling of series
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 3/20/2008
 * @author Ariel Flesler
 * @version 1.2.1
 *
 * http://flesler.blogspot.com/2008/02/jqueryserialscroll.html
 */
;(function($){var a='serialScroll',b='.'+a,c='bind',C=$[a]=function(b){$.scrollTo.window()[a](b)};C.defaults={duration:1e3,axis:'x',event:'click',start:0,step:1,lock:1,cycle:1,constant:1};$.fn[a]=function(y){y=$.extend({},C.defaults,y);var z=y.event,A=y.step,B=y.lazy;return this.each(function(){var j=y.target?this:document,k=$(y.target||this,j),l=k[0],m=y.items,o=y.start,p=y.interval,q=y.navigation,r;if(!B)m=w();if(y.force)t({},o);$(y.prev||[],j)[c](z,-A,s);$(y.next||[],j)[c](z,A,s);if(!l.ssbound)k[c]('prev'+b,-A,s)[c]('next'+b,A,s)[c]('goto'+b,t);if(p)k[c]('start'+b,function(e){if(!p){v();p=1;u()}})[c]('stop'+b,function(){v();p=0});k[c]('notify'+b,function(e,a){var i=x(a);if(i>-1)o=i});l.ssbound=1;if(y.jump)(B?k:w())[c](z,function(e){t(e,x(e.target))});if(q)q=$(q,j)[c](z,function(e){e.data=Math.round(w().length/q.length)*q.index(this);t(e,this)});function s(e){e.data+=o;t(e,this)};function t(e,a){if(!isNaN(a)){e.data=a;a=l}var c=e.data,n,d=e.type,f=y.exclude?w().slice(0,-y.exclude):w(),g=f.length,h=f[c],i=y.duration;if(d)e.preventDefault();if(p){v();r=setTimeout(u,y.interval)}if(!h){n=c<0?0:n=g-1;if(o!=n)c=n;else if(!y.cycle)return;else c=g-n-1;h=f[c]}if(!h||d&&o==c||y.lock&&k.is(':animated')||d&&y.onBefore&&y.onBefore.call(a,e,h,k,w(),c)===!1)return;if(y.stop)k.queue('fx',[]).stop();if(y.constant)i=Math.abs(i/A*(o-c));k.scrollTo(h,i,y).trigger('notify'+b,[c])};function u(){k.trigger('next'+b)};function v(){clearTimeout(r)};function w(){return $(m,l)};function x(a){if(!isNaN(a))return a;var b=w(),i;while((i=b.index(a))==-1&&a!=l)a=a.parentNode;return i}})}})(jQuery);

// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
  return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){
  /**
   * Most jQuery.serialScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
   * @see http://flesler.demos.com/jquery/scrollTo/
   * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.serialScroll.
   */
  
  /**
   * The plugin binds 6 events to the container to allow external manipulation.
   * prev, next, goto, start, stop and notify
   * You use them like this: $(your_container).trigger('next'), $(your_container).trigger('goto', [5]) (0-based index).
   * If for some odd reason, the element already has any of these events bound, trigger it with the namespace.
   */    
  
  /**
   * IMPORTANT: this call to the plugin specifies ALL the settings (plus some of jQuery.ScrollTo)
   * This is done so you can see them. You DON'T need to specify the commented ones.
   * A 'target' is specified, that means that #screen is the context for target, prev, next and navigation.
   */
  $('#screen').serialScroll({
    target:'#sections',
    items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
    prev:'img.prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
    next:'img.next',// Selector to the 'next' button (absolute too)
    axis:'xy',// The default is 'y' scroll on both ways
    navigation:'#navigation li a',
    duration:700,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
    force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
    
    //queue:false,// We scroll on both axes, scroll both at the same time.
    //event:'click',// On which event to react (click is the default, you probably won't need to specify it)
    //stop:false,// Each click will stop any previous animations of the target. (false by default)
    //lock:true, // Ignore events if already animating (true by default)    
    start: -1, // On which element (index) to begin ( 0 is the default, redundant in this case )    
    //cycle:true,// Cycle endlessly ( constant velocity, true is the default )
    //step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
    //jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
    //lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
    //interval:1000, // It's the number of milliseconds to automatically go to the next
    //constant:true, // constant speed
    
    onBefore:function( e, elem, $pane, $items, pos ){
      /**
       * 'this' is the triggered element 
       * e is the event object
       * elem is the element we'll be scrolling to
       * $pane is the element being scrolled
       * $items is the items collection at this moment
       * pos is the position of elem in the collection
       * if it returns false, the event will be ignored
       */
       //those arguments with a $ are jqueryfied, elem isn't.
      e.preventDefault();
      if( this.blur )
        this.blur();
    },
    onAfter:function( elem ){
      //'this' is the element being scrolled ($pane) not jqueryfied
    }
  });
  
  /**
   * No need to have only one element in view, you can use it for slideshows or similar.
   * In this case, clicking the images, scrolls to them.
   * No target in this case, so the selectors are absolute.
   */
  
  $('#portfolio').serialScroll({
    items:'li',
    prev:'a.prev',
    next:'a.next',
    offset:-460, //when scrolling to photo, stop 230 before reaching it (from the left)
    start:1, //as we are centering it, start at the 2nd
    duration:1200,
    force:true,
    stop:true,
    lock:false,
    cycle:false, //don't pull back once you reach the end
    easing:'easeOutQuart', //use this easing equation for a funny effect
    jump: false //click on the images to scroll to them
  });
  
  /**
   * The call below, is just to show that you are not restricted to prev/next buttons
   * In this case, the plugin will react to a custom event on the container
   * You can trigger the event from the outside.
   */
  
  var $news = $('#news-ticker');//we'll re use it a lot, so better save it to a var.
  $news.serialScroll({
    items:'div',
    duration:2000,
    force:true,
    axis:'y',
    easing:'linear',
    lazy:true,// NOTE: it's set to true, meaning you can add/remove/reorder items and the changes are taken into account.
    interval:1, // yeah! I now added auto-scrolling
    step:2 // scroll 2 news each time
  });  
  
  /**
   * The following you don't need to see, is just for the "Add 2 Items" and "Shuffle"" buttons
   * These exemplify the use of the option 'lazy'.
   */
  $('#add-news').click(function(){
    var 
      $items = $news.find('div'),
      num = $items.length + 1;
      
    $items.slice(-2).clone().find('h4').each(function(i){
      $(this).text( 'News ' + (num + i) );
    }).end().appendTo($news);
  });
  $('#shuffle-news').click(function(){//don't shuffle the first, don't wanna deal with css
    var shuffled = $news.find('div').get().slice(1).sort(function(){
      return Math.round(Math.random())-0.5;//just a random number between -0.5 and 0.5
    });
    $(shuffled).appendTo($news);//add them all reordered
  });
});
