// keeping small plugins in here. They should be moved to their own file if they get too large

(function($) {
   
    $.fn.extend({
        
        // Alias functions to easier to remember names
        up : $(this).parents,
        upOne : $(this).parent,
        down : $(this).find,
        downOne : $(this).children,
        left : $(this).prevAll,
        leftOne : $(this).prev,
        right : $(this).nextAll,
        rightOne : $(this).next,

        textNodes : function() {
        	var text = [];
        	this.each(function() {
        		var children = this.childNodes;
        		for (var i = 0; i < children.length; i++) {
        			var child = children[i];
        			if (child.nodeType == 3) {
        			    text.push(child);
        			}
        		}
        	});
        	
        	return $(this.pushStack(text));
        },
        
        // Add animated scrolling to ancor tags
        // eg. $('a').smoothAnchor();
	    smoothAnchor : function(speed) {
	        if ('undefined' == typeof(speed))
	            speed = 400;
    		this.filter('a[href^="#"]').click(function () {	
    			var href = $(this).attr('href');
    			var destination = $(href).offset().top;
    			destination -= 70;
    			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, speed );
    		  	return false;
    		});
    	},

        // Make buttons into fg-buttons
        // eg. $('button').fgButtons();
        fgButtons : function() {
            this.filter(".fg-button:not(.ui-state-disabled)").hover(function() {
                    $(this).addClass("ui-state-hover");
                },
                function() {
                    $(this).removeClass("ui-state-hover");
                }
            ).mousedown(function() {
                $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
                if ( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
                else { $(this).addClass("ui-state-active"); }
            }).mouseup(function() {
                if (! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button, .fg-buttonset-multi .fg-button') ){
                    $(this).removeClass("ui-state-active");
                }
            });
        }
    });
        	        
    $.extend({
        
        // This is not really OOP. More like name spacing for static functions
        
        // form related functions
        Form : {
            initPhoneNumberInput : function() { 
                $(this).val($.String.phoneNumberFormat($(this).val())).attr('maxLength', 14); 
            }
        },
        
        // string related functions
        String : {
            subString : function(start, length) {
                if (start < 0) {
                    start = this.length + start;
                    if (start < 0) {
                        start = 0;
                    }
                }
            
                if ('undefined' == typeof(length)) {
                    end = this.length;
                }
                else {
                    var end = start + length;
                    if (end > this.length) {
                        end = this.length;
                    }
                }
            
                var substr = '';
                var i;
                for (i = start; i < end; i++)
                    substr += this.charAt(i);
                return substr;
            },
            
            numberFormat : function(string, decimals, dec_point, thousands_sep ) {
                var n = string, c = isNaN(decimals = Math.abs(decimals)) ? 0 : decimals;
                var d = dec_point == undefined ? "." : dec_point;
                var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
                var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
                
                return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
            },
            
            phoneNumberFormat : function(phone_number)
            {
                var numbers_only = phone_number.replace(/[^0-9]/g, '');
            	var length = numbers_only.length;
            	if (length == 0)
            		return '';
            
            	var formatted_phone_number = '(' + numbers_only.substr(0, 3);
            	if (length >= 4)
            		formatted_phone_number += ') ' + numbers_only.substr(3, 3);
            	if (length >= 7)
            		formatted_phone_number += '-' + numbers_only.substr(6, 4);	
            	return formatted_phone_number;
            }
    
    /*
        These are not yet in use anywhere. leaving commented until needed.   
        $.ltrim = function() {
            return this.replace(/^\s+/,'');
        };
        
        $.rtrim = function() {
        	return this.replace(/\s+$/,'');
        }
    */
        }
    });  
  
})(jQuery);

var $j = jQuery.noConflict();
var $e = function(el) {
    var $el = $j(el);
    return ($el.length) ? $el : false;
};
    
jQuery.Event.prototype.keyPressed = function() { return (this.which) ? this.which : this.keyCode; };
jQuery.Event.keys = {
    ENTER: 13
//    ATSIGN: 64,
//    PLUS:       43,
//    MINUS:      45,
//    PERIOD:     46,
//    OPENPAREN:  40,
//    CLOSEPAREN: 41,
//    SHIFT:      16,
//    CONTROL:    17,
//    CAPSLOCK:   20,
//    SPACE:      32
};
