/*
 * jQuery Scrolltastic plugin 1.1
 * 
 * Copyright (c) 2010 Stephen Coley, DK New Media
 *
 * Licensed under MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * http://www.opensource.org/licenses/mit-license.php
 *
 */


(function($){  
  $.fn.scrolltastic = function(options) {
    var anchors;
    var timer;
    var mouseWheelActive;
    var defaults = {
      up: null,
      down: null,
      duration: "auto"
    };  
    var options = $.extend(defaults, options);
      
    var elem = this;
    
    var content = $(elem);
    var wrapper = content.parent();
    if(wrapper.css("position") == "static") {
      wrapper.css("position", "relative");
    }
    var wrapperHeight = wrapper.height();
    var contentHeight = content.height();
  
    content.children(":last-child").each(function(){
      if($(this).css("margin-top") != "") {
        var margin = $(this).css("margin-top");
        var marginArr = margin.split("p");
        contentHeight = contentHeight + parseInt(marginArr[0]);
      }
    });

    if((options.up != null && options.down == null) || (options.up == null && options.down != null)) {
      alert('Scrolltastic: If you\'re going to assign anchors, you MUST asign both "up" and "down" anchors. Not just one of them.');
    } else if(options.up != null && options.down != null) {
      $("#" + options.up).mousedown(function() {
        upMouseDown();
      });

      $("#" + options.up).mouseup(function() {
        mouseUp();
      });
    
      $("#" + options.down).mousedown(function() {
        downMouseDown();
      });

      $("#" + options.down).mouseup(function() {
        mouseUp();
      });

      $("#" + options.down + ", #" + options.up).click(function(){
        return false;
      });
      anchors = true;
    } else {
      anchors = false;
    }

    content.mousewheel(function(event, delta, deltaX, deltaY) {
      if(mouseWheelActive == null) {
        $.clear(timer);
        if(delta > 0) {
          upMouseDown(content);
        } else {
          downMouseDown(content);
        }
        $.timeout(mouseUp, 100);
        mouseWheelActive = true;
      }
      return false;
    });

    function upMouseDown() {
      if(options.duration == "auto") {
        var dis = $(content).position().top * -1;
        var dur = dis * 3;
      }
      $(content).stop().animate({
        top: "0px"
      }, dur, "linear");
    }
    
    function downMouseDown() {
      if(options.duration == "auto") {
        var dis = ($(content).position().top + contentHeight) - wrapperHeight;
        var dur = dis * 3;
      }
      $(content).stop().animate({
        top: (contentHeight - wrapperHeight) * -1
      }, dur, "linear");
    }

    function mouseUp() {
      content.stop();
      var top = $(content).position().top;
      if(anchors) {
        if(top * -1 == contentHeight - wrapperHeight) {
          $("#" + options.down).hide();
        } else {
          $("#" + options.down).show();
        }
        if(top == 0) {
          $("#" + options.up).hide();
        } else {
          $("#" + options.up).show();
        }
      }
      mouseWheelActive = null;
    }

  };
})(jQuery);

/* Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 *
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 * Thanks to: Seamus Leahy for adding deltaX and deltaY
 *
 * Version: 3.0.4
 *
 * Requires: 1.2.2+
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(i){var g=i||window.event,f=[].slice.call(arguments,1),j=0,h=true,e=0,d=0;i=c.event.fix(g);i.type="mousewheel";if(i.wheelDelta){j=i.wheelDelta/120}if(i.detail){j=-i.detail/3}d=j;if(g.axis!==undefined&&g.axis===g.HORIZONTAL_AXIS){d=0;e=-1*j}if(g.wheelDeltaY!==undefined){d=g.wheelDeltaY/120}if(g.wheelDeltaX!==undefined){e=-1*g.wheelDeltaX/120}f.unshift(i,j,e,d);return c.event.handle.apply(this,f)}})(jQuery);

(function($) {

  if (typeof $.timeout != "undefined") return; 

  $.extend({
    timeout : function (func,delay) {
      // init
      if (typeof $.timeout.count == "undefined") $.timeout.count = 0;   
      if (typeof $.timeout.funcs == "undefined") $.timeout.funcs = new Array(); 
      // set timeout
      if (typeof func =='string') return setTimeout(func, delay); 
      if (typeof func =='function') {
        $.timeout.count++;
        $.timeout.funcs[$.timeout.count] = func;
        return setTimeout("$.timeout.funcs['"+$.timeout.count+"']();", delay);
      }
    },
    interval : function (func,delay) {
      // init
      if (typeof $.interval.count == "undefined") $.interval.count = 0;   
      if (typeof $.interval.funcs == "undefined") $.interval.funcs = new Array(); 
      // set interval
      if (typeof func =='string') return setInterval(func, delay); 
      if (typeof func =='function') {
        $.interval.count++;
        $.interval.funcs[$.interval.count] = func;
        return setInterval("$.interval.funcs['"+$.interval.count+"']();", delay);
      }
    },
    idle : function (func,delay) {
      // init
      if (typeof $.idle.lasttimeout == "undefined") $.idle.lasttimeout = null;
      if (typeof $.idle.lastfunc == "undefined") $.idle.lastfunc = null;
      // set idle timeout
      if ($.idle.timeout) { clearTimeout($.idle.timeout); $.idle.timeout = null; $.idle.lastfunc = null; }
      if (typeof(func)=='string') { 
        $.idle.timeout = setTimeout(func, delay); 
        return $.idle.timeout;
      }    
      if (typeof(func)=='function') { 
        $.idle.lastfunc = func;
        $.idle.timeout = setTimeout("$.idle.lastfunc();", delay);
        return $.idle.timeout;
      }
    },
    clear : function (countdown) {
    clearInterval(countdown);
    clearTimeout(countdown);
    }  
    
  });
  
  
})(jQuery);


