/**
 * Squid Javascript Core
 * @author Eugene Song
 */
;(function($) {
    var lastSetTimeout;
	
	/**
	 * Delay
	 * 
	 * @author Eugene Song
	 * @param mixed time Milliseconds
	 * @param function func
	 * @return jQuery Object
	 */
    $.fn.delay = function(time,func){
        if (time == 'clear') {
            clearTimeout(lastSetTimeout);
            return this;
        }
        return this.each(function(){
            lastSetTimeout = setTimeout(func,time);
        });
    };
	
	/**
	 * Visor
	 * 
	 * @author Eugene Song
	 * @param function active
	 * @param function inactive
	 * @return jQuery Object
	 */
	$.fn.visor = function(active, inactive) {
		var elem = this;
		var check = function(posY) {
			if (posY < $(window).scrollTop()) {
				elem.parent("div").height(elem.height());
				elem.each(inactive);
			} else {
				elem.each(active);
			}
		}
		elem.wrap("<div></div>").parent("div").attr("id", elem.selector + "-anchor");
		check(elem.offset().top);
		$(document).scroll(function() {
			check(elem.parent("div").offset().top);
		});
		return this;
	};
	
	/**
	 * In Window Plugin
	 * Determines whether an element is within the window/viewport
	 * 
	 * @todo Need to support horizontal scrolling
	 * @author Eugene Song
	 * @param string mode
	 * @param function active
	 * @param function inactive
	 * @return jQuery object
	 */
	$.fn.inWindow = function(mode, active, inactive) {
		var elem = this;
		var calculate = function(){
			var inWindow = false;
			switch (mode) {
				case 'partial':
					if (elem.offset().top + elem.height() < $(window).scrollTop()) {
						inWindow = false;
					} else {
						inWindow = true;
					}
					break;
				case 'full':
					if (elem.offset().top < $(window).scrollTop()) {
						inWindow = false;
					} else {
						inWindow = true;
					}
					break;
				default:
					alert('Invalid mode selected');
					break;
			}
			
			if (inWindow) {
				elem.each(active);
			} else {
				elem.each(inactive);
			}
		}
		
		calculate();
		$(document).scroll(calculate);
		
		return this;
	};
	
    /**
     * Simple Recursive Dropdown Menus
     *
     * Suggested css:
     *   li { float: left; position: relative; }
     *   li ul { display: none; position: absolute; }
     * 
     * @author Eugene Song
     * @return jQuery Object
     */
    $.fn.dropDown = function() {
        $('li', this).hover(function() {
            $('ul:first', this).show().hover(function(){
                $(this).show();
            }, function() {
                $(this).hide();
            });
        }, function() {
            $('ul', this).hide();
        });
        return this;
    };
    
    
})(jQuery);


