var CoreWebsite = CoreWebsite || {};

jQuery.noConflict();

(function($){
	CoreWebsite = {
		init: function() {
			this.onAjaxQueueComplete();
			
			this.externalPlugins.init();
			this.plauditPlugins.init();
			
			$("body").addClass("jsEnabled");
			
			$("#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();
			});
			$(".searchFormField:first").keypress(function(event){
				if (event.keyCode == 13) {
					$(this).parent().find("searchFormField:first").click();
				}
			});
			
			$("#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);

