//	Smaw.Common v 1.0
//
if ( typeof Smaw == 'undefined' ) { self.Smaw = {}; }
Smaw.Common = {	
	version: 1.0,
	
	hasCssClass: function(o, css_class) {
		return o.className.match(new RegExp("\\b" + css_class + "\\b")) == css_class;
	},
	addCssClass: function(o, css_class) {
        if (!o || typeof o != "object") {
            return false;
        }
        if (o.className == null) {
            return false;
        }
        if (this.hasCssClass(o, css_class)) {
            return false;
        }
        o.className = o.className.trim() + " " + css_class.trim();
        return true;
    },
    removeCssClass: function(o, css_class) {
        if (!o || typeof o != "object") {
            return false;
        }
        if (!o.className) {
			return false;
        }
        if (!this.hasCssClass(o, css_class)) {
            return false;
        }
        o.className = o.className.replace(css_class.trim(), "").trim();
        return true;
    },
    setCssClass: function(o, css_class) {
        if (!o || typeof o != "object") {
            return false;
        }
        if (!o.className) {
            return false;
        }
        o.className = css_class;
        return true;
    },
    getParentNodeByTagName: function(o, parent_tag) {
        if (!o || typeof o != "object") {
            return false;
        }
        while (o = o.parentNode) {
            if (o.nodeType != 1) {
                continue;
            }
            if (o.tagName.toLowerCase() == parent_tag.toLowerCase()) {
                return o;
            }
        }
        return null;
    },
    getElementsByTagNames: function(o, list) {
        if (!o || typeof o != "object") {
            return false;
        }
        var tag_names = list.split(",");
        var results = new Array();
        for (var i=0; i<tag_names.length; i++) {
            var elements = o.getElementsByTagName(tag_names[i]);
            for (var j=0, k=elements.length; j<k; j++) {
                results.push(elements[j]);
            }
        }
        return results;
    },
    getElementsByTagNameClass: function(o, tag_name, css_class) {
        if (!o || typeof o != "object") {
            return false;
        }
        var child_elements = new Array();
        var elements = o.getElementsByTagName( tag_name );
        for (var i = 0, j = elements.length; i<j; i++) {
            if (this.hasCssClass(elements[i], css_class) || css_class == "" || css_class == null) {
                child_elements.push(elements[i]);
            }
        }
        return child_elements;
    },
    getParentByStyleProperty: function(o, property, value) {
        if (!o || typeof o != "object") {
            return false;
        }
        while (o = o.parentNode) {
            if (this.getComputedStyle(o, property) == value) {
                return o;
            }
        }
        return null;
    },
    getComputedStyle: function(o, property) {
        if (!o || typeof o != "object") {
            return false;
        }
        var property_value = null;
        if (o.currentStyle) {
            property_value = o.currentStyle[property];
        } else if (window.getComputedStyle) {
            property_value = document.defaultView.getComputedStyle(o,null).getPropertyValue(property);
        }
        return property_value;
    },
	insertAfter: function(newElement,targetElement) {
		var parent = targetElement.parentNode;
		if ( parent.lastChild == targetElement ) {
			parent.appendChild(newElement);
		} else {
			parent.insertBefore(newElement,targetElement.nextSibling);
		}
	},
    removeAllChildren: function(o) {
        if (!o || o == null) {
            return false;
        }
        while (o.hasChildNodes()) {
            o.removeChild(o.firstChild);
        }
    },
	changeSrc: function(o, newSrc) {
		if (!o) {
			return false;
		}
		if (!o.src) {
	        return false;
	    }
	    o.src = newSrc;
	    return true;
	},
	
	// use add event where your fuction needs to work in IE but the function does not make
	// use of the 'this' object.
	addEvent: function(element, ev_type, fn) {
		if (element.addEventListener) {
			
			element.addEventListener(ev_type, fn, false);
	        return true;
	        
	    } else if (element.attachEvent) {
		    
		    // this is the key line for IE. Any function attached here will not be able to make
		    // use of 'this'. This is a design flaw in MSFTs attachEvent function.
	        var r = element.attachEvent('on' + ev_type, fn);
	        return r;
	        
	    } else {
	        element['on' + ev_type] = fn;
	    }
	},

	// use replace event where your fuction to pass *must* make use
	// of the 'this' object, and you want it to work in IE	
	// NOTE: despite being named replace event, for firefox this method
	// still adds the event to the object.
	replaceEvent: function(element, ev_type, fn) {
		if (element.addEventListener) {
			
			element.addEventListener(ev_type, fn, false);
	        return true;
	        
	    } else {
		    
		    // this is the key line for IE. A function given here *can* make use of 'this'
		    // because the attachment is a simple assignment. Unfortunately this means
		    // any previously attached functions will be deleted and overwritten.
		    // That's why this method is called 'replaceEvent'
	        element['on' + ev_type] = fn;
	        
	    }
	},

	/* Document Object Model ready function loader
	// Special thanks to Andrea Giammarchi of Web Reflection at http://webreflection.blogspot.com
	-----------------------------------------------------------------------------------------------*/
	domReady: function (loadFunction) {
		var f=loadFunction,a,b=navigator.userAgent,d=document,w=window,
		c="__onContent__",e="addEventListener",o="opera",r="readyState",
		s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,"()}'></scr","ipt>");
		w[c]=(function(o){return function(){w[c]=function(){};for(a=arguments.callee;!a.done;a.done=1)f(o?o():o)}})(w[c]);
		if(d[e])d[e]("DOMContentLoaded",w[c],false);
		if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))
		(function(){/loaded|complete/.test(d[r])?w[c]():setTimeout(arguments.callee,1)})();
		else if(/MSIE/i.test(b))d.write(s);
	},
	
	/* Input field text population
	// Takes current value of form INPUTS and handles clearing and repopulating on focus
	-------------------------------------------------------------------------------------*/
	dynamicInputText: function() {
		var inputs = Smaw.Common.getElementsByTagNameClass(document,"input","dyna-text");
		for(var i=0; i<inputs.length; i++){
			var target=inputs[i];
			target.savedText = target.value; // keep track of the original input value
			target.onfocus=function(){
				this.value = this.value.trim()
				if (this.value == this.savedText) {
					this.value = "";
				}
			}
			target.onblur=function(){
				this.value = this.value.trim()
				if (this.value == "") {
					this.value = this.savedText;
				}
			}
		}
	},
	
	/* Equal height box columns
	// Takes an array of elements and sets style to the maxiumum height
	-------------------------------------------------------------------------*/
	boxHeights: function(boxes) {
		var heights = new Array();
		for (var i=0;i<boxes.length;i++) {
			if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
				heights.push(boxes[i].scrollHeight);
			} else {
				heights.push(boxes[i].offsetHeight);
			}
		}
		heights.sort(this.sortNumeric);
		var maxh = heights[0];
		for (var j=0;j<boxes.length;j++) {
			boxes[j].style.height = maxh+"px";
		}
	},
	
	/* Returns DSC values
	--------------------------------*/
	sortNumeric: function(f,s) {
		return s-f;
	}
	
}/* /Smaw.Common */

String.prototype.trim = function () {
    return this.replace(/^\s*|\s*$/g,'');
}