var URLUtil = function() {
    var public = {};
    var baseURL;
    
    public.setBaseURL = function( url ) {
            baseURL = url;
    };
    public.getBaseURL = function() {
            return baseURL;
    };
   
    return public;
}();

var plauditSite = (function($){
	
	var setup = function() {
		uiElements.init();
	};
	
	var uiElements = {
		
		init: function() {
			uiElements.utilMethods();
			//uiElements.externalPlugins.init();
			uiElements.customElements.init();
		},
		
		utilMethods: function() {
			$("body").addClass("jsEnabled"); // We use this to style non-js
			$("tr:odd").addClass("odd"); // Setup tables for zebra striping
			
			$(".searchForm .textField").focus(function(){
				$(this).select(); // Submit search form
			});
			
			$('input').keydown(function(event){
				var input = $(this);
			    if (event.keyCode == 13) {
				input.closest("form").find(":submit:first").click();
			        return false;
			    }
			});
			
			$(".moreInfo").live("click" , function(){
				$(this).next(".additionalInfo").toggle();
			});
			
			$('.submit').hover(
				function() { $(this).addClass('hover'); }, 
				function() { $(this).removeClass('hover'); }
			);
			
			$("li.file a", $(".documentList")).click(function(){
				pageTracker._trackPageview("/action/file-download.html");
			});
			
			$("#requestInformation .commandButton").click(function(){
				pageTracker._trackPageview("/action/request-information.html");
			});
			
			$("#reps a.email").live("click", function(){
				pageTracker._trackPageview("/action/email-clicked.html");
			});
		},
		
		customElements: {
			
			init: function() {
				this.homepageNavigation();
				this.documentLibrary();
				this.contactUpMap.init();
			},
			
			// START: Homepage Navigation Slide Elements
			
			homepageNavigation: function() {
				var contentHeight; // Height for Animated Area
				var distanceFromTop; 
				var animationLink; // Link for Animated content
				
				$('.animation', $("#animatedArea")).hover(
					function () {
						contentHeight = $(this).children(".contentArea").height();
						distanceFromTop = (273 - contentHeight - 30) + "px"
						$(this).children(".contentArea").stop().animate({top: distanceFromTop}, 500);
					},
					function () {
						$(this).children(".contentArea").stop().animate({top: "210px"}, 500);
					}	
				);
				
				$('.animation', $("#animatedArea")).click(function(){
					window.location = $(".contentArea a", $(this)).attr("href"); // Make click on .contentArea link you to ./h3/a/@href
				});
			}, 
			
			// END: Homepage Navigation Slide Elements
			
			// START: Contact Us Map
			
			contactUpMap: {
				
				init: function() {
					this.removeToolTip();
					this.contactMapProductClick();
					this.manufacturingFacilities();
				},
				
				addInitialActiveClass: function(){
					$("#contactProductType a:first").click();
				}, // addInitialActiveClass
				
				removeToolTip: function() {
					$("span.close").live("click" , function(){
						$(".tooltip").remove();
					});
				}, // removeToolTip
				
				contactMapProductClick: function() {
					$("#contactProductType a").click(function(){
						$("#contactProductType a.active").removeClass("active");
						$(this).addClass("active");
					});
				}, // contactMapProductClick
				
				manufacturingFacilities: function() {
					var facilityContacts = $(".contactArea .facilityContacts");
					var facilityContactHeader = $(".contactArea h3");
					
					facilityContacts.hide();
					facilityContactHeader.toggle(
						function(){
							$(this).next(facilityContacts).slideDown();
							$(this).children("span").addClass("active");
						},
						function(){
							$(this).next(facilityContacts).hide();
							$(this).children("span").removeClass("active");
						}
					);
				} // manufacturingFacilities
				
			},
			
			// END: Contact Us Map
			
			documentLibrary: function() {
				$('.documentList').each(function() {
					var $this = $(this);
					var root = '/';
					var ul = $("> ul", $this);
					var script = 'document-library/document-library-js.scriptJsf';
					
					if ( !$("body").hasClass("static") ) {
						
						if ( $this.hasClass("manual") ) {
							// Manually created library
							
							$("li", ul).each(function() {
								var $liThis = $(this);
								if ( !$liThis.hasClass("file") ) {
									$liThis.addClass("directory");
								}
							});
							
							ul.each(function() {
								var $ulThis = $(this);
								$ulThis.fileTree({ 
									root: root,
									script: script,
									expandSpeed: 350,
									collapseSpeed: 250
									}, function(file) {
										window.open( URLUtil.getBaseURL() + "assets/documents/library/" + file);
										return false;
								});
							});
						} else {
							if (ul.size() > 0 && ul.attr('title') != '') {
								root = ul.attr('title');
							}
							$this.fileTree({ 
								root: root,
								script: script,
								expandSpeed: 350,
								collapseSpeed: 250
								}, function(file) {
									window.open( URLUtil.getBaseURL() + "assets/documents/library/" + file);
									return false;
							});
						}
						
					}
				});
				
			} // END: Document Library

		} // customElements
	
	}; // uiElements
	
	return {
		setup: setup
	}
})(jQuery);

jQuery(function(){ plauditSite.setup(); });
