(function($){
	var 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('new_scripts.js')){
    				path = s.src.replace("/scripts/new_scripts.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();
    	}
    };
	
	homeSlideDeck = {
		activeAnimate: function( index ){
			var self = this;
			var leftVal = $(self.navLis[ (index - 1) ]).position().left;
			self.navLinks.removeClass('active');
			$(self.navLinks[( index - 1 )]).addClass('active');			
			$(self.activeElem).animate({
				left: leftVal + 'px'
			});
		},
		goTo: function( index ){
			var self = this;
			$('#slidedeck_home').slidedeck().goTo( index );
			self.activeAnimate( index );
		},
		bindNav: function( sdNav ){
			var self = this;
			self.activeElem = document.createElement('li');
			self.navCount = sdNav.length;
			self.navLis = sdNav.find('li.nav');
			self.navWidth = self.navLis.width();
			self.navLinks = sdNav.find('li.nav a');
			$(self.activeElem).attr( { id: 'active_state' } ).html( '&nbsp;' ).css( { width: self.navWidth + 'px' } );
			sdNav.append( self.activeElem );
			
			self.navLinks.click(function(e){
				e.preventDefault();
				var index = self.navLinks.index($(this)) + 1;
				self.goTo( index );
			});		
		},
		init: function(){
			var self = this;
			var sdNav = $('#slidedeck_nav');
			var sdNavWidth = ( $('#slidedeck_nav').find('li').outerWidth(true) ) * ( $('#slidedeck_nav').find('li').length );
			sdNav.css({
				width: sdNavWidth + 'px'
			});
			self.deck = $('#slidedeck_home').slidedeck();
			//assign mousenter and mouseleave for hover class
			$('#slidedeck_wrapper').mouseenter(function(){
				$(this).addClass('hover');
			}).mouseleave(function(){
				$(this).removeClass('hover');
			});			
			//bind clicks to Next/Prev buttons
			$('a#btn_deck_prev').click(function(e){
				e.preventDefault();
				$('#slidedeck_home').slidedeck().pauseAutoPlay = true;
				$('#slidedeck_home').slidedeck().prev();				
				self.activeAnimate( self.deck.current );
			});
			$('a#btn_deck_next').click(function(e){
				e.preventDefault();
				$('#slidedeck_home').slidedeck().pauseAutoPlay = true;
				$('#slidedeck_home').slidedeck().next();
				self.activeAnimate( self.deck.current );
			});
			self.bindNav(sdNav);
		}
	};
	
    $(document).ready(function(){
        DOMUtilities.initialize();		
		window.validateForms = [];
    });
	
})(jQuery);
