//Global JavaScript Document

/********************************

	Notes:
		+ I rely on JQuery v1.2.6

********************************/

	
// Global variables
var menuSpeed = 150; //menu animation speed in milliseconds
var menuSpeedMedium = 350; //menu animation speed in milliseconds
var menuSpeedSlow = 550; //menu animation speed in milliseconds
var hideDelay = 500; //delay to hide menus in milliseconds


// Prevent console commands from throwing errors in IE
try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }

// Global variable assignment
function setGlobals() {

}

// Function to open PDFs in a new window
function pdfLinks() {
	$('a[href$=".pdf"]').click(function(){
		window.open(this.href);
		return false;
	})
}

// Add classes to form inputs for css use
function addFormClasses() {
	if(jQuery('input').length) {
		jQuery('input').each(function(){
			jQuery(this).addClass(this.type);
		})
	}
}

function initMenus() {

	$('#mainNav > li').each(function(){
		
		//set variables to reference main nav link and subnav menu so that we only traverse the DOM once
		var $trigger = $('> .mainNavItem', this);
		var $menu = $('> ul.subnav', this);
		
	
		//alert($menu.length);
		
		if($menu.length > 0) { //test if subnav menu exists for this item
		
			//declare variables to track menu status and timer for menu closing
			var menuHideTimer = null;
			var isAnimating = false;
			var isShown = false;
			
			var menuHeight = $menu.height();
			
			$menu.hide();
		
			$([$trigger.get(0), $menu.get(0)]).hover(
				function() {
				
					if (menuHideTimer) clearTimeout(menuHideTimer);
					
					if (isAnimating || isShown) {
						// don't trigger the animation again
						return;
					} else {
						// reset position of info box
						isAnimating = true;
						
						var delay = 100; // variable to stagger fade of menu links

						$trigger.addClass('hover');
						$('a', $menu).css({ opacity: 0 });
						$menu
						.css({
							height: '0px'
						}).show()
						.animate({
							height: menuHeight
						}, 250, 'easeOutQuad', function() {
							isAnimating = false;
							isShown = true;
							
						});

						$('a', $menu).each(function(){
							$(this).animate({
								opacity: 0
							}, delay);
							$(this).animate({
								opacity: 1
							}, 200);
							
							delay += 50;
						});

					}

					return false;
					
				},
				function(){
				
					if (menuHideTimer) clearTimeout(menuHideTimer);
					
					menuHideTimer = setTimeout(function () {
						menuHideTimer = null;
						$menu.animate(
							{ 
								height: '0px'
							}, 
							menuSpeed, 
							function () {
								$menu.hide();
								isShown = false;
								$trigger.removeClass('hover');							
							});
					}, hideDelay);

					return false;
				}
			
			);
		}
		
	});
	
	$('#mainNavProducts > a, #mainNavStory > a').click(function(){ return false; });
	
}

function initSharing() {

	var bookmarkHideTimer = null;
	var isAnimating = false;
	var isShown = false;
	
	var $trigger = $('#linkBookmark > a');
	var $menu = $('#bookmarkMenu');
	
	//set up bookmark and share menu	
	$menu.hide().css({ opacity: 1 })
	.hover(
		function() {
			if (bookmarkHideTimer) clearTimeout(bookmarkHideTimer);
		},
		function() {
			if (bookmarkHideTimer) clearTimeout(bookmarkHideTimer);
			
			bookmarkHideTimer = setTimeout( function () {
			
				bookmarkHideTimer = null;
		
				$menu.slideUp(menuSpeed, function () {
					isShown = false;						
		
				});
			
			}, hideDelay);
		}
	);
	
	$trigger.click(
		function() {
			if ((isAnimating == false) && (isShown == false)) {
				var delay = 50;
			
				if (bookmarkHideTimer) clearTimeout(bookmarkHideTimer);
				
				$("a", $menu).css({ opacity: 0 });
				
				$menu.slideDown(menuSpeed, function(){
					
					isShown = true;
					
				});
				
					
					$("a", $menu).each(function(){
						$(this).animate({
							opacity: 0
						}, delay);
						$(this).animate({
							opacity: 1
						}, 400);
						
						delay += 20;
					});
				
				return false;
			} else {
				if (bookmarkHideTimer) clearTimeout(bookmarkHideTimer);
				$("#bookmarkMenu").slideUp(menuSpeed, function(){
					isShown = false;
				});
				return false;
			}
		}
	);
}

function trim(str) {
	if (str == null || str == undefined) return str;
	return str.replace(/^\s+|\s+$/g, '');	
}

function getParms(parmString) {
	return getParms(parmString, null);
}
function getParms(parmString, optionalToArray) {
    var ret = optionalToArray;
    if (ret == null || ret == undefined) {
    	ret = new Array();
    }
    if (parmString == null || (parmString = trim(parmString)) == '') {
    	return ret;
    }
    if (parmString.charAt(0) == '#' || parmString.charAt(0) == '?') parmString = trim(parmString.substring(1));
    if (parmString == '') {
    	return ret;
    }
    var args = parmString.split('&');
    var parts;
    var value;
    for(var i=0;i<args.length;i++) {
    	parts = args[i].split('=');
    	if (parts.length > 0 && (parts[0] = trim(parts[0])) != '') {
	    	value = null;
	    	if (parts.length > 1) {
	    		value = trim(parts[1]);
	    	}
	    	ret[parts[0]] = value;
    	}
    }
    return ret;
}
function makeParmString(parmArray) {
	var ret = '';
	var first = true;
	for(var i in parmArray) {
		if (parmArray[i] != undefined) {
			if (first) {
				first = false;
			} else {
				ret = ret + '&';
			}
			ret = ret + i + (parmArray[i] == null ? '' : '=' + parmArray[i]);
		}
	}
	return ret;
}
function mergeParms(parmString1, parmString2) {
	return makeParmString(getParms(parmString2, getParms(parmString1)));
}
function removeMatchingParms(parmString, removeString) {
	var parms = getParms(parmString);
	var remove = getParms(removeString);
	for(var i in remove) {
		if (trim(parms[i]) == trim(remove[i])) {
			parms[i] = undefined;
		}
	}
	return makeParmString(parms);
}

function searchFocus() {
	var $searchBox = $('#mainSearchBox');
	var searchValue = $.url.param("query");
	
	if(!searchValue)
		searchValue = 'Enter Keyword/SKU'; 
	else
		searchValue = decodeURIComponent(replacePluses(searchValue));
	$searchBox.attr('value',searchValue).css('color', '#999');
	$searchBox.blur(function() {
		searchValue = $searchBox.attr('value');
		searchHasValue(searchValue) == true ? $(this).css('color', '#525D58') : $(this).attr('value', 'Enter Keyword/SKU').css('color', '#999');
	});
	$searchBox.focus(function() {
		searchValue = $searchBox.attr('value');
		searchHasValue(searchValue) == false ? $(this).attr('value', '').css('color', '#525D58') : $(this).css('color', '#525D58');
	});
	
	$('#searchForm').submit(function(){
		
		//alert('start submit');
		
		var searchValue = $searchBox.val();
		
		//alert(searchValue);
		
		if( searchHasValue(searchValue) ) {
			
		} else {
			$searchBox.focus();
			return false;
		}	
	});	
}

function searchHasValue(myValue) {
	if (myValue == "" || myValue == "Enter Keyword/SKU") {
		return false;
	}else{
		return true;
	};
}

function setExtLinks() {
	$('.extLink').attr('target', '_blank');
}

function replacePluses(sIn){
	var sOut=sIn;
	while(sOut.indexOf("+")>=0)sOut=sOut.replace("+"," ");
	return sOut;
}

// Initialize JS functions
$(document).ready(function(){
	setGlobals()
	pdfLinks();
	addFormClasses();
	initMenus();
	initSharing();
	searchFocus();
	setExtLinks();
})