// Copyright 2010 Citrus Media Group
// by Spencer Steffen
// Drop down alert, to be uses globally rather than instantianiously. 
//
;var Alert = {
  version: "0.1.0",
  options: {
    overlay: true,
    speed: 'fast'
  },
  overlay_styles: {
    display: 'none',
    position: $.browser.msie ? 'absolute' : 'fixed',
    backgroundColor: '#000',
    width: '100%',
    height: '100%',
    top: 0,
    left: 0,
    opacity: 0,
    zIndex: 12000
  },
  init: function(id) {
    Alert.a = $(id || '#alert').hide().css('z-index', '12500').appendTo(document.body);
    Alert.b = Alert.a.find('div.message');
    return Alert.conceal();
  },
  show: function() {
    if (!(Alert.a || Alert.b)) Alert.init();
    var args = $.makeArray(arguments);
    var overlay = Alert.options.overlay;
    if (args[0] === false) {
      overlay = false;
      args.shift();
    }
    if (overlay) Alert.setupOverlay().showOverlay();
        
    if (Alert.visible) return Alert.collide(args);
    
    Alert.update(args).reveal();
    Alert.visible = true;
    
    return Alert;
  },
  hide: function() {
    if (!Alert.visible) return Alert;
    if (!Alert.a) return Alert;
    else Alert.a.animate({ top: -Alert.a.outerHeight() }, Alert.options.speed, Alert.hideOverlay);
    return Alert;
  },
  setupOverlay: function() {
    if (Alert.o) return Alert;
    $(document.body).append('<div id="overlay"></div>');
    Alert.o = $('#overlay').css(Alert.overlay_styles).hide();
    return Alert;
  },
  toggleOverlay: function() {
    if (!Alert.o) Alert.setupOverlay();
    if (Alert.o.css('display') === 'none') Alert.showOverlay();
    else Alert.hideOverlay();
    return Alert;
  },
  showOverlay: function() {
    if (0 < Alert.o.css('opacity')) return Alert;
    Alert.o.show().fadeTo(Alert.options.speed, 0.80, arguments[0] || function(){return;});
    return Alert;
  },
  hideOverlay: function() {
    if (!Alert.o) return Alert;
    Alert.o.fadeTo(Alert.options.speed, 0, function() {
      Alert.o.remove();
      Alert.visible = false;
      Alert.o = null;
    });
    return Alert;
  },
  update: function(args) {
    if (args.length) {
      var t,m,i = 0;
      Alert.b.empty();
      while(args[i]) {
        if ($.isArray(args[i])) {
          t = args[i][0];
          m = args[i][1];
        } else {
          t = i == 0 ? 'h3' : 'p';
          m = args[i];
        }
        Alert.b.append('<'+t+'>'+m+'</'+t+'>');
        i++;
      }
    }
    return Alert.conceal();
  },
  conceal: function() {
    if (!Alert.a) return Alert;
    Alert.a.hide().css('top', -Alert.a.outerHeight());
    return Alert;
  },
  reveal: function() {
    if (!Alert.a) return Alert;
    Alert.a.show().animate({top: "0px" }, Alert.options.speed, function() {
      Alert.visible = true;
      Alert.collision = false;
    });
    return Alert;
  },  
  collide: function(args) {
    if (!Alert.visible || Alert.collision) return;
    Alert.collision = true;
    Alert.a.animate({top: -Alert.a.outerHeight()},Alert.options.speed, function() {
      Alert.update(args).reveal();
    });
    return Alert;
  }
};


;(function($) {

  var ver = '0.2.3';
    
  $.fn.sliders = function() {
    var args = arguments;
    
    if (!$.isReady) return $.fn.sliders.errors.dom();
    
    return this.each(function() {
      
      
      // =================================  
      // get vars or set defaults
      
      var slider, index;
      
      var $id        =  this.$id || $.fn.sliders.generateId();
      
      var container  =  $(this);
      var slider     =  this.slider || getSlider();
      var slides     =  getSlides();
      
      var index      =  this.index || 0;
      var options    =  this.options = $.extend(false, this.options || $.fn.sliders.defaults, args.constructor == Object ? args : {});
      var timeout    =  this.timeout || null;
      var keyEnabled =  this.keyEnabled || false;
      var locked     =  $(slider).data('locked') || false;
      
      var transitions = {
        slide: function() {
          if (locked) return;
          slider.animate({ left: -container.width() * index }, options.speed, options.ease);
        },
        lock: function(unlock) {
          if (slider.data('locked')) return;
          slider.data('locked', true);
          if (unlock) setTimeout(transitions.unlock, options.speed - 25);
        },
        unlock: function() {
          slider.data('locked', false);
        }  
      }
      
      if (!this.hasInit) {
  
        align();
        
        if (slides.length < $.fn.sliders.defaults.minimum) $.fn.sliders.errors.minimum(slides.length);
        if (options.speed < 300) options.speed = $.fn.sliders.errors.speed(options.speed);
        
        advance(Number(options.first));
        
        if (!args[0]) {
          if (options.play) play();
          if (options.keyboardEvents) enableKeyboard();
        }
        this.hasInit = true;
      }
      
      if (args[0] && args[0].constructor == String) control(args[0], args[1]);
      
      
      // save for laters!
      
      this.$id        = $id;
      this.slider     = slider;
      this.options    = options;
      this.index      = index;
      this.timeout    = timeout;
      this.keyEnabled = keyEnabled;
      this.locked     = locked;
      
      return this;
      
      
          
          
          
      // =================================  
      // SETUP SLIDER & SLIDES
      
      function getSlider() {
        if (slider) return slider;
        var w = container.width();    
        var h = container.height();
        var pos = container.css('position');
        if (!pos || !pos.match(/relative|absolute/)) container.css('position', 'relative');
        
        container.css({ display: 'block', overflow: 'hidden' }).wrapInner('<div class="slider"></div>');
        
        return $('div.slider', container).css($.fn.sliders.absTopLeft);
      }
      
      function getSlides() {
        return slider ? slider.children().get() : container.children().get();
      }
      
      
      function align() {
        var w = container.width();    
        var h = container.height();
        slider.css({ width: w * slides.length, height: h, left: -w * index });
        return $(slides).css({ display: 'block', position: 'absolute', top: 0, width: w }).each(function(i) { 
          $(this).css({ left: w * i, zIndex: slides.length - i });
        });
      }
      
      
      
      // =================================  
      // CONTROLS 
      
      function control(control, option) {
        switch(control) {
    		  case 'play':
    		    play();
    		    break;
          case 'stop':
            stop();
            break;
          case 'toggle':
            toggle();
            break;
          case 'prev':
          case 'previous':
            stop();
            prev();
            break;
          case 'next':
            stop();
            next();
            break;
          case 'goto':
          case 'go to':
            var idx;
            if (option.constructor == String) {
              switch(option) {
                case 'next':
                  idx = advance(1);
                  break;
                case 'prev':
                  idx = advance(-1);
                  break;
                case 'last':
                case 'end':
                case 'finish':
                  idx = slides.length - 1;
                  break;
                case 'first':
                case 'start':
                default:
                  idx = Number(option.replace(/[^0-9]/g, ''));
                  break;
              }
            } else {
              idx = Number(option);
            }
            stop();
            goto(idx);
            break;
          case 'advance':
            stop();
            advance(option || 1);
            transition();
            break;
          case 'enableKeyboard':
            enableKeyboard();
            break;
          case 'disableKeyboard':
            disableKeyboard();
            break;
          case 'toggleKeyboard':
            toggleKeyboard();
            break;
          case 'show':
            if (option === false) container.css('display', 'block');
            else container.fadeIn();            
            if (options.play) play();
            if (options.keyboardEvents) enableKeyboard();
            break;
          case 'hide':
            stop();
            disableKeyboard();
            if (option === false) container.css('display', 'none');
            else container.fadeOut();
            break;
          default:
            // nothing!
            break;
        }
      };
      
      
      
      // slideshow 
      
      function play() {
        if (timeout) return;
        timeout = setInterval(next, options.timeout);
      };
      function stop() {
        if(timeout) timeout = clearTimeout(timeout);
      };
      function toggle() {
        if(timeout) stop();
        else play();
      };
      
      
      
      // manual
      
      function next() {
        if (locked || slides.length < 2) return;
        advance(1);
        transition();
      };      
      function prev() {
        if (locked || slides.length < 2) return;
        advance(-1);
        transition();
      }; 
      function goto(n) {
        if (locked || slides.length < 2 || index == n) return;
        index = withinLimits(n);
        transition();
      };
      
      
      
      
      // =================================  
      // KEYBOARD 
      
      function enableKeyboard() {
        if (keyEnabled) return false;
        $(window).bind('keyup.' + $id, container, $.fn.sliders.handleKeyboard);
        keyEnabled = true;
      };
      function disableKeyboard() {
        if (!keyEnabled) return false;
        $(window).unbind('keyup.' + $id, $.fn.sliders.handleKeyboard);
        keyEnabled = false;
      };
      function toggleKeyboard() {
        if (keyEnabled) disableKeyboard();
        else enableKeyboard();
      }  
      
      
      
      
      // =================================  
      // TRANSITION 
      
      function transition() {
        if (locked) return;
        switch(options.transition) {
          case 'slide':
          default:
            transitions.slide();
            break;
        }
        if (!options.allowCue) transitions.lock(true);
      };
      
          
  
      // =================================  
      // LOGIC 
    
      function advance(count) {
        if (locked) return index;
        var l = slides.length; 
        var i = count < l ? count : l - (count % l);
        return index = withinLimits(index + i);
      };
      
      function withinLimits(n) {
        var l = slides.length;
        var i = n < l ? n : l - (n % l);
        return i < 0 ? l + i : l <= i ? i - l : i;
      };
      
      
            
    });
    
  };
  
  
  
  
  
  
  
  
  
  
  
  
  
  $.fn.sliders.handleKeyboard = function(evt) {
    switch(evt.keyCode) {
      case 39:
      case 40:
        $(evt.data).sliders('next');
        break;
      case 37:
      case 38:
        $(evt.data).sliders('prev');
        break;
      case 32:
        $(evt.data).sliders('toggle');
        break;
    }
  };
  
  
  
  
  
  
  
  
  
  // =================================  
  // ERRORS  

  $.fn.sliders.errors = {
    dom: function() {
      $.fn.sliders.errors.show('Sliders::DOM not ready, quiting slideshow');
    },
    minimum: function(count) {
      $.fn.sliders.errors.show('Sliders::Not enough slides. ' + count + '/' + $.fn.sliders.defaults.minimum);
    },
    speed: function(speed) {
      $.fn.sliders.errors.show('Sliders::Speed to slow. (' + speed + 'ms) minimum 300ms');
      return $.fn.sliders.minimums.speed;
    },
    show: function(err) {
      if (window.console && window.console.log) window.console.log(err);
      return this;
    } 
  }
  
  
  
  
  // =================================  
  // ID  

  $.fn.sliders.generateId = function() {
    return Math.round(new Date().getTime() * Math.random());
  }
  
  
  
    
  // =================================  
  // DEFAULTS 
  
  $.fn.sliders.absTopLeft = {
    display:     'block',
    position: 'absolute',
    left:              0,
    top:               0
  };
  
  $.fn.sliders.minimums = {
    speed:           300  
  };
        
  $.fn.sliders.defaults = {
    transistion: 'slide',
    allowCue:      false,
    timeout:        5000, 
    speed:           750,
    minimum:           2,
    first:             0,
    ease:        'swing',
    play:           true,
    keyboardEvents: true
  };
  
        
})(jQuery);



;(function($) {
  
  var version = "0.0.1";
   
  $.fn.extend({  
  
     bubbles: function(info, options) {
  	   
  	    options = options || $.fn.bubbles.defaults;
  	 
  			var self = $(this);
  			var info = $(info).css({ display: 'none', position: 'absolute', top: 'auto', right: 'auto', bottom: 'auto', left: 'auto' }).appendTo(document.body);
  	     		
  	     		
  	    var distance = 10;
        var time = 250;
        var hideDelay = 300;
        var hideDelayTimer = null;
        var beingShown = false;
        var shown = false;
        
  	    self.click(function(evt) {
  	      evt.preventDefault();
  	      return false;
  	    });
  	     		
  	    $([self.get(0), info.get(0)]).mouseover(function(evt){
  	      // info.show();
  	      if (hideDelayTimer) clearTimeout(hideDelayTimer);
          if (beingShown || shown) {
              // don't trigger the animation again
              return;
          } else {
              // reset position of info box
              beingShown = true;
  
              info.css({                
                left: evt.pageX - (info.width() * 0.33),
                top: evt.pageY - info.height(),
                opacity: 0,
                display: 'block'
              }).animate({
                top: '-=' + distance + 'px',
                opacity: 1
              }, time, 'swing', function() {
                beingShown = false;
                shown = true;
              });
          }  
          return false;
  	     
  	    });
  	    
  	    
  	    $([self.get(0), info.get(0)]).mouseout(function(){  	      
  	      if (hideDelayTimer) clearTimeout(hideDelayTimer);
          hideDelayTimer = setTimeout(function () {
              hideDelayTimer = null;
              info.animate({
                  top: '-=' + distance + 'px',
                  opacity: 0
              }, time, 'swing', function () {
                  shown = false;
                  info.css('display', 'none');
              });
  
          }, hideDelay);
  
          return false;
  	      
  	    });  	     
  	    
      } 
  });
  
  $.fn.bubbles.defaults = {
    speed: 100
  }
  
})(jQuery);

// jQuery Snow. 
// Copyright 2010 Citrus Media Group

;(function($) {
  
  var version = "0.1.1";
   
  $.fn.snow = function() {  
  
  	 var args = $.makeArray(arguments);
      
     
     return this.each(function() {
        var count = this.count || count;
	      var self = $(this).css('overflow', 'hidden');
        var images = this.images || ( typeof(args[0]) === 'object' && $.isArray(args[0]) ? args.shift() : null );
        var parent = self.parent()
        var int = this.int || 0;
        
        if (!this.hasInit) {
          if ($.fn.snow.defaults.play) control('play');
          this.hasInit = true;
        }
      
        if (args[0] && args[0].constructor == String) control(args[0], args[1]);
        
        this.int = int;
        this.images = images;
        return this;
      
        function control(control, option) {
          switch(control) {
    		    case 'play':
    		      int = window.setInterval(function() { animate(); count += 1; }, 500)
    		      break;
            case 'stop':
              int = window.clearInterval(int);
              $('div.snowflake').stop().fadeOut();
              break;
          }
        }
        
        function animate() {
          var a = addElement();
          var p = a.position();
          var x = p.left + $.fn.snow.math.randomRange(100, 250);
          var y = screen.height + a.height();
          a.animate({
            left: x,
            top: y
          }, $.fn.snow.math.randomRange(20000, 22500), 'swing', function() { 
            a.remove();        
          });
        }
        
        function addElement() {
          var size = $.fn.snow.math.randomRange(20, 130);
          var div = $($.fn.snow.elements.div(count));
          
          if (images) {
            var img;
            img = $($.fn.snow.elements.image(images[$.fn.snow.math.randomRange(0,images.length-1)]));
            if (img) {
              img.css({
                opacity: $.fn.snow.math.randomRange(10,40) * 0.01
              });
              div.append(img);
            }
          } else {
            div.text($.fn.snow.defaults.snowflake);
          }
          
          div.css({
            display: 'block',
            position: 'absolute',
            width: 'auto',
            height: 'auto',
            fontSize: size,
            zIndex: -1
          }).hide().appendTo(parent);
          
          var w = div.width();
          var h = div.height();
                    
          div.css({
            left: $.fn.snow.math.randomRange(-w, self.width()),
            top: -h
            //top: $.fn.snow.math.randomRange(-h, screen.width + h)
          }).show();
          
          return div;
        }
    });
  };
  
  $.fn.snow.elements = {
    div: function(id) {
      return '<div class="' + $.fn.snow.defaults.class_name + '">&nbsp;</div>'
    },
    image: function(img) {
      return '<img src="' + img + '" alt="" />'
    }
  }
  
  $.fn.snow.math = {
    random: function(i) {
      return Math.floor(i*(Math.random()%1));
    },
    randomRange: function(i,j) {
      return i + $.fn.snow.math.random(j-i+1);
    },
    eitherOr: function() {
      return 0.5 - Math.random();
    }
  }  
  
  $.fn.snow.defaults = {
    play: true,
    images: [],
    class_name: 'snowflake',
    speed: 100,
    snowflake: "*"
  }
  
})(jQuery);
