var CoreWebsite = CoreWebsite || {};

jQuery.noConflict();

var plauditAnalytics = (function($) {
	
	var isPageTrackerLoaded = function() {
		return typeof(_gaq) != "undefined" && _gaq != null;
	};
	
	return {
		trackPage: function(url) {
			if ( url.substring(0,1) != '/' )
				url = '/' + url;
			if ( isPageTrackerLoaded() ) {
				_gaq.push(['_trackPageview', url]);
			} else if ( typeof(console) != "undefined" ) {
				console.log("Google Analytics not loaded. Trying to track page: " + url);
			}
		},
		trackEvent: function(category, action, opt_label, opt_value) {
			if ( isPageTrackerLoaded() ) {
				_gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
			} else if ( typeof(console) != "undefined" ) {
				console.log("Google Analytics not loaded. Trying to track event with category '" + category +"', action '" + action +"', opt_label '" + opt_label +"', and opt_value '" + opt_value +"'.");
			}
		},
		setupBinding: function() {
			$("a").filter("[href^='http://'], [href^='https://']").filter(":not([href*='://"+location.hostname+"'])").click(function(){
				plauditAnalytics.trackEvent("Outbound Link", "Click", $(this).attr("href"));
			});
			$("a").filter("[href^='mailto:']").click(function(){
				plauditAnalytics.trackEvent("Email Link", "Click", $(this).attr("href"));
			});
            
            // Document specific clicks e.g., pdf, doc, zip
			$("a").filter("[href$='pdf']").click(function(){
				plauditAnalytics.trackEvent("File Link", "Click", $(this).attr("href"));
			});
			$("a").filter("[href$='doc']").click(function(){
				plauditAnalytics.trackEvent("File Link", "Click", $(this).attr("href"));
			});
			$("a").filter("[href$='docx']").click(function(){
				plauditAnalytics.trackEvent("File Link", "Click", $(this).attr("href"));
			});
			$("a").filter("[href$='zip']").click(function(){
				plauditAnalytics.trackEvent("File Link", "Click", $(this).attr("href"));
			});

			$("form").filter("[action^='http://'], [action^='https://']").filter(":not([action*='://"+location.hostname+"'])").submit(function(){
				plauditAnalytics.trackEvent("Outbound Form Submissions", "Submit", $(this).attr("action"));
			});
		}
	};
})(jQuery);

jQuery(function(){
	plauditAnalytics.setupBinding();
});



(function($){
	CoreWebsite = {
		init: function() {
			this.onAjaxQueueComplete();
			
			this.externalPlugins.init();
			this.plauditPlugins.init();
			
			/*
			  $folks.each(function(i){
				var $this = $(this);
				var $that = $('<div class="hoverBox"></div>');
				var $allPeople = $('#homeFeature .fullOp');
				$that.appendTo('#featureHovers');
				$that.mouseenter(function(){
					var tHeight = $this.height();
					if (!introRunning){
						textOutFast();
						$allPeople.stop().css({'float' : 'left'}).fadeTo("slow", .05);
						$this.stop().animate({
							'opacity' : 1
							}, 300).removeClass('fullOp');
						activePerson = i + 1;
						textIn();
						isHovered = true;
					}
				});
				$that.mouseleave(function(){
					$this.addClass('fullOp');
					isHovered = false;
				});
				
			});
			
			var $this = $(this);
			 */
			
			var $wjGal = $('.wj-partner');
			
			$wjGal.each(function(i){
				$('div .mainImage.mainImage' + (i+1)).cycle({
					 fx:     'fade', 
					    speed:  'fast', 
					    timeout: 0, 
					    pager:  'div .wj-photo' + (i+1), 
					    pagerAnchorBuilder: function(idx, slide) { 
					        return 'div .wj-photo' + (i+1) + ' li:eq(' + idx + ') a'; 
					    }     
				});
				$('div.wj-partner' + (i+1) + ' li a').click(function(ev){
					$('div .wj-photo' + (i+1) + ' li a.activeMini').removeClass('activeMini');
					$(this).addClass('activeMini');
					ev.preventDefault();
			 	   	ev.stopPropagation();
			 	   	
				});
			});
			
			$('#featureImages').cycle({
				random: true,
				timeout: 0
			});
//			$('#featureImages img').delay(2000).fadeIn('slow');
			
			$("#animationInner").before('<div id="controls"></div>');
			$("#animationInner").cycle({
				fx: 'uncover',
				speed: 2000,
				timeout: 6000,
				pause: true,
				pager: "#controls"
			});
			
			$("#mainMenu ul").children("li").hover(
				function (){
					$(this).children(".sub-menu").addClass("hover");
				}, function () {
					$(this).children(".sub-menu").removeClass("hover");
				}
			);
			
			$('a', $("#mainContent")).each(function(){
				var href = $(this).attr('href');
				if(href.indexOf('.pdf') != -1){
					$(this).attr("target", "_blank");
				}
			});
			
			$(".searchFormField:first, .searchInputFieldArea input").focus(function(event){
				this.select();
			});

			if ( $.browser.msie	&& parseInt($.browser.version) < 9 ) {
				// Some versions of ie do not submit the form using the first submit if the field only contains
				// one input field. We must have it use the first button so the correct JSF action runs.
				$('input').keypress(function(event){
				    var input = $(this);
				    if (event.keyCode == 13) {
				    	var submitButtons = input.closest("form").find(":submit:first, input.submit:first");
				    	if ( submitButtons.length > 0 ) {
				    		// handle the submit
				    		submitButtons.get(0).click();
				        	return false;
				    	}
				    	return true;
				    }
				});
			}
			
			$("#mediaViewer, img.thumbnail, td.thumb img, td.thumbnail img").bind("contextmenu",function(event){ return false; });
			$("#mediaViewer").bind("mousedown",function(event){ return false; });
		},
		
		onAjaxQueueComplete: function(request, event, data) {
			$(".jsLink").attr("href", "javascript: void(0);");
		},
		
		
		externalPlugins: {
			
			init: function() {
				this.overlay.init();
			},
		
			overlay: {
				
				options: {
				
					expose: { 
		        		color: '#000000', 
		        		loadSpeed: 200, 
		        		opacity: 0.9,
		        		maskId: 'overlayMask'
		    		}, 
		 
		    		closeOnClick: false,
		    		
		    		api: true,
		    		
		    		target: '#machineType'
		    		
				},
				
				init: function() {
					$('<div id="machineType" />').appendTo("#wrapper");
					var api = $("#dialineTypes a").overlay(this.options);
					$("#dialineTypes a").click(function(){
						$("#machineTypeBottom").remove();
						
						$.get($(this).attr("href"), function(data){
							var clearedResponse = data.replace(/<!DOCTYPE[^>]+>/m, "");
							var overlay =  $(clearedResponse);
							var overlayConfig = api.getConf();
							
							overlayConfig.target = '#machineType';
							
							overlay = $("#machineTypeBottom", overlay)
							overlay.appendTo("#machineType", function(){
								api.load();
							});
							$("#machineType .close").show();
						
						});
						return false;
					});
				}
				
			}
			
		},
		
		plauditPlugins: {
			
			init: function() {
				this.comparePumps.init();
				this.aitToolTip.setup();
			},
			
			comparePumps: {
			
				checkBoxId: null,
			
				checkBoxForm: $("#comparePumps"),
				
				initialColumnHidden: $(".AS-Ultra"),
				
				totalAvailableCheckBoxes: 5, 
				
				init: function() {
				
					if ( !this.parseQueryString() ) {
						this.initialColumnHidden.hide();
						this.checkBoxForm.attr("checked", true);
						this.checkBoxForm.eq(5).attr({
							disabled: "disabled",
							checked: false
						});
						$("td#pressure").attr("colspan", 10);
						this.enabledDisableCheckBoxes(this.totalAvailableCheckBoxes)
					} else if ( this.parseQueryString() ) {
						var checkedCount = $(":checkbox:checked").length;
						this.enabledDisableCheckBoxes(checkedCount);
					}
					this.attachClickEvent();
					
				},
				
				attachClickEvent: function() {
					
					var thisRef = this;
					
					$(":checkbox", this.checkBoxForm).click(function(){
						var checkBoxCount = thisRef.getCheckBoxCount();
						
						thisRef.setCheckBoxId( $(this) ); // get the id for this checkbox
						thisRef.enabledDisableCheckBoxes( checkBoxCount ); //enable and disable checkboxes based on count
						
						$("td#pressure").attr("colspan", checkBoxCount * 2);
						
						if ( $(this).is(":checked") ) {
							var classToShow = "." + $(this).attr("id");
							thisRef.showTableColumn(classToShow);
						}
	
						if ( $(this).is(':not(":checked")') ) {
							var classToHide = "." + $(this).attr("id");
							thisRef.hideTableColumn(classToHide);
						}
					});
				},
				
				setCheckBoxId: function( obj ) {
					this.checkBoxId = obj.attr("id");
				},
				
				getCheckBoxCount: function() {
					var checkBoxCount = $("input:checked", this.checkBoxForm).length;
					return checkBoxCount;
				},
				
				enabledDisableCheckBoxes: function(count) {
					if( count >= this.totalAvailableCheckBoxes ) {
						$(':checkbox:not(":checked")', $("#comparePumps")).attr("disabled", "disabled"); //Disable other checkbox if 5 have already been selected
					} else if ( count === 1 ) {
						$(':checkbox:checked', $("#comparePumps")).attr("disabled", "disabled"); //Disable last checkbox if there is only one left
					} else {
						$(":checkbox:disabled", $("#comparePumps")).removeAttr("disabled");  //Enable all disabled checkboxes
					}
				},
				
				hideTableColumn: function(className) {
					$(className).hide();
				},
				
				showTableColumn: function(className) {
					$(className).show();
				},
				
				parseQueryString: function() {
					var queryString = location.search;
					queryString = queryString.slice(1);
					
					if( queryString === "a-series") {
						$(".as-series", $("#comparison")).hide();
						$(".as-ultra", $("#comparison")).hide();
						$(".as-series", $("#comparePumps")).show().removeAttr("checked");
						$(".as-ultra", $("#comparePumps")).show().removeAttr("checked");
						return true;
					} else if ( queryString === "as-series") {
						$(".a-series", $("#comparison")).hide();
						$(".as-ultra", $("#comparison")).hide();
						$(".a-series", $("#comparePumps")).show().removeAttr("checked");
						$(".as-ultra", $("#comparePumps")).show().removeAttr("checked");
						return true;
					}
					return false;
				}
				
			}, // End Compare Table Plugin
			
			aitToolTip: {
				
				hoverItem: $("#aitDiagram > span"),
				
				toolTipHTML: $('<div id="aitToolTip" />'),
				
				setup: function() {
					var thisRef = this;
					thisRef.hoverItem.hover(
						function(){
							var toolTipArea = $(this).parent();
							thisRef.toolTipHTML.appendTo(toolTipArea);
							$(this).children("span").appendTo("#aitToolTip");
						},function(){
							$("#aitToolTip").children().appendTo( $(this) );
							$("#aitToolTip").remove();
						}
					);
				}
				
				
			}
			
		} // End Plaudit Plugin 
	
	}; // End CoreWebsite
	
	CoreWebsite.init();
	
})(jQuery);


