// Prevent console commands from throwing errors in IE
try { console.log('init console... done'); } catch(e) { console = { log: function() {} }; }

var biz = {
	setGlobals: function() {
		biz.hideTheseSelects = jQuery('#formSearch select[name="search_forum"], #advancedSearch select[name="category"], #findProductForm select[name="brand"]');
	},
	
	dropDownMenu: function() {
		var menuSpeed = 250; //menu animation speed in milliseconds
		var hideDelay = 100; //delay to hide menus in milliseconds

		jQuery('#main-nav > li').each(function(i) {
	
			if( (jQuery('ul', this).length) > 0 ) {
	
				var hideDelayTimer = null;
				var beingShown = false;
				var shown = false;
				var trigger = jQuery('>a', this);
				var menu = jQuery('>ul', this);			
	
				jQuery([trigger.get(0), menu.get(0)]).hover(
					function() {						
						if (hideDelayTimer) clearTimeout(hideDelayTimer);
						if (beingShown || shown) {
							// don't trigger the animation again
							return;
						} else {
							// reset position of info box
							beingShown = true;
	
							trigger.addClass('over');
							menu.slideDown(menuSpeed, function() {
								beingShown = false;
								shown = true;							
							});
						}
						biz.hideSelects();
						return false;
					},
					function(){						
						if (hideDelayTimer) clearTimeout(hideDelayTimer);
						hideDelayTimer = setTimeout(function () {
							hideDelayTimer = null;
							menu.slideUp( menuSpeed, function () {
								shown = false;
								trigger.removeClass('over');								
							});
	
							}, hideDelay);		
							biz.showSelects();
							return false;
						}
					);
				}
			});		
	},	
	
	collapseEmptyCaptions: function() {
		$('div.thumb-and-info p.caption').each(function(){
			var $this = $(this);
			if($this.html() === ''){
				$this.remove();
			}
		});
	},
	
	// Showing/hiding of search results
	collapseSearchResults: function() {
		if (jQuery('body.resultsAllCategories .searchSectionResults').length) {
			lib.listSlider($('.searchSectionResults'), $('.searchSectionHeader'), 1);	
		}
	},
	
	// Disable search results-per-page select if "All" categories is selected
	setResultsSelect: function() {
		function switchMe() {
			if (jQuery('select#collection').length) {
				if (jQuery('select#collection').val() == "DLT_all") {
					jQuery('select#num').attr('disabled', 'disabled');
				} else {
					jQuery('select#num').attr('disabled', '');
				}
			}
		}
		// Run function on page load
		switchMe();
		// Attach function to select
		jQuery('select#collection').bind('change', function() {
			switchMe();
		});
	},
	
	showNewsAlerts: function() {
		jQuery('#news-alert .news-alert-content').hide();
		jQuery('#hide-news-alert').hide();
		jQuery('a#show-news-alert').click(function() {
			jQuery('#news-alert .news-alert-content').slideToggle(400, function() {
				//jQuery('.changeBtn a').toggleClass('selected');
				jQuery('#show-news-alert').hide();
				jQuery('#hide-news-alert').show();
			});
			return false;
		});
		jQuery('a#hide-news-alert').click(function() {
			jQuery('#news-alert .news-alert-content').slideToggle(400, function() {
				jQuery('#hide-news-alert').hide();
				jQuery('#show-news-alert').show();
			});
			return false;
		});
	},
	
	loginNotice: function() {
		var $av = jQuery('#accountVerify');
		if ($av.length != 0 && jQuery('#accountVerify p').length != 0) {
			$av.hide();
			jQuery('#avLink').click();
		}
	},
	
	// Hide form selects on nav mouseover in IE6
	hideSelects: function(){
		if (typeof document.body.style.maxHeight === "undefined") {
			biz.hideTheseSelects.css({ visibility: 'hidden' });
		}
	},
	
	showSelects: function(){
		if(typeof document.body.style.maxHeight === "undefined") {
			biz.hideTheseSelects.css({ visibility: 'visible' });	
		}		
	}
};

// Initialize functions
$(document).ready(function(){
	biz.setGlobals();
	biz.dropDownMenu();
	lib.externalLinks();
	lib.newWindowLinks();
	lib.fileLinks();
	lib.addFormClasses();
	lib.linkFileTypes();
	biz.collapseSearchResults();
	biz.setResultsSelect();
	biz.showNewsAlerts();
	lib.inputDefault();
	biz.collapseEmptyCaptions();
	biz.loginNotice();
	lib.addTooltips();
});