var DOMUtilities;
(function($){
    DOMUtilities = {
    	path: "",
    	images: [],
    	getPath: function(){
    		for(var i=0,path,scripts=$('head script'); i<scripts.length; i++){
    			var s = scripts[i];
    			if(s.src.match('application.js')){
    				path = s.src.replace("/javascripts/application.js","");
    			}
    		}
    		return path;
    	},
    	linkTargets: function(c){
    		this.setContext(c).context.find('a[rel*="external"]').attr('target','_blank');
    		return this;
    	},
    	rollOvers: function(c){
    		this.setContext(c).context.find('img.rollOver, input[type="image"].rollOver').hover(function(e){
    			if (!this.className.match(/active/)) {
    				this.src = this.src.replace("_i.", "_o.");
    			}
    		},function(e){
    			if (!this.className.match(/active/)) {
    				if (this.src.indexOf("_o.") != -1) {
    					this.src = this.src.replace("_o.", "_i.");
    				}
    				else 
    					if (this.src.indexOf("_a.") != -1) {
    						this.src = this.src.replace("_a.", "_i.");
    					}
    			}
    		}).filter('input[type="image"]').mousedown(function(e){
    			this.src = this.src.replace("_o.","_a.");
    		});
    		return this;
    	},
    	autoReplaceInputs: function(c){
    		this.setContext(c).context.find('input.autoReplace, textarea.autoReplace').addClass('empty').focus(function(e){
    			if(this.value == this.defaultValue){
    				this.value = "";
    			}
    			$(this).addClass('focus').removeClass('empty');
    		}).blur(function(e){
    			if(jQuery.trim(this.value) === ""){
    				this.value = this.defaultValue;
    				$(this).addClass('empty');
    			}
    			$(this).removeClass('focus');
    		});
    		return this;
    	},
    	setContext: function(c){
    		if(typeof(c) != "undefined"){
    			this.context = $(c);
    		}
    		return this;
    	},
    	imgPreload: function(){
    		var preloader=new Image();
    		for(var i=0; i<this.images.length; i++){
    			preloader.src = this.path + this.images[i];
    		}
    		return this;
    	},
    	initialize: function(c){
    		this.path = this.getPath();
    		if(typeof(c) == "undefined"){
    			c = $(document.body);
    		}
    		this.setContext(c).linkTargets().rollOvers().autoReplaceInputs();
    	}
    };
    
    $(document).ready(function(){
        DOMUtilities.initialize();
    });
})(jQuery);