$(document).ready(function(){
	var d = document;
	if(d.getElementById('nav')){primaryNavAdjust(); setHover(); navCols();}
	if(d.getElementById('sliderWrap')){setSlider();}
//	if(d.getElementById('splashNav')){prepareSplash();}
	if(d.getElementById('modal')){setModal();}
	if(d.getElementById('overlay')){setMultiModal();} // ch 20100902
	if(d.getElementById('search')){setBlurFocus();}
	if(d.getElementById('searchAgain')){setBlurFocusAgain();}

    if(d.getElementById('homeSlide')){
        $('#homeSlide').css('z-index','100');
        $('#swapArea').css('visibility','hidden');
    }

	$(window).load(function(){
	    if(d.getElementById('splashNav')){
	        $('#homeSlide').appendTo('#splashWrap');
	        $('#homeSlide').css('z-index','9');
	        $('#swapArea').css('visibility','visible');
	        prepareSplash();
	    }
	 });
});

function setBlurFocus(){
	var searchBox = $('#search input')[0];
	searchBox.onfocus = function(){
		if(searchBox.value === 'Search'){
			searchBox.value = '';
		}
	};
	searchBox.onblur = function(){
		if(searchBox.value === ''){
			searchBox.value = 'Search';
		}
	};
}

function setBlurFocusAgain(){
	var searchBox = $('#searchAgain input')[0];
	searchBox.onfocus = function(){
		if(searchBox.value === 'Search Again'){
			searchBox.value = '';
		}
	};
	searchBox.onblur = function(){
		if(searchBox.value === ''){
			searchBox.value = 'Search Again';
		}
	};
}

function setHover(){
	var li = $('#nav>li');
	li.hover(
		function(){$(this).addClass('hover');},
		function(){$(this).removeClass('hover');}
	);
}

function setSlider(){
	var sliderWrap = $('#sliderWrap'),
		prev = sliderWrap.find('#prev'),
		next = sliderWrap.find('#next'),
		wind = sliderWrap.find('#window'),
		ul = wind.find('ul'),
		partners = wind.find('li'),
		numOfPartners = partners.length,
		liCopy = ul.html(), 
		itemWidth = 148, 
		animating = false,
		startPos, currentPos, newPos, maxRight, timer;
	
	start();
	function start(){
		timer = setInterval(function(){go();}, 5000);
	}
	
	function go(){
		next.click();
	}
	
	if(numOfPartners === 0){return;}
	if(numOfPartners < 7){
		var win = $('#window'),
			gap = 888 - (numOfPartners * 148),
			padPerLi = gap/numOfPartners,
			pad = padPerLi/2;
		partners.each(
			function(){
				this.style.paddingLeft = pad + 'px';
				this.style.paddingRight = pad + 'px';
			}
		);
		next.hide();
		prev.hide();
		win.css({'margin-left':'13px', 'margin-right':'13px'});
	}
	else {
		startPos = -(itemWidth * numOfPartners);
		maxRight = startPos * 2;
		currentPos = startPos;
		
		ul.css('left',startPos+'px');

		// Create copies on the beginning and end
		ul.find('li').eq(0).before(liCopy);	
		ul.find('li').eq(numOfPartners-1).after(liCopy);
			
		next.click(
			function(e){
				e.preventDefault();
				clearInterval(timer);
				if(!animating){
					if(currentPos === maxRight){
						ul.css('left',startPos+'px');
						currentPos = startPos;
					}
					newPos = currentPos - itemWidth;
					animating = true;
					ul.animate({left: newPos+'px'}, function(){animating = false;});
					currentPos = newPos;
				}
				start();
			}
		);
		
		prev.click(
			function(e){
				e.preventDefault();
				clearInterval(timer);
				if(!animating){
					if(currentPos === 0){
						ul.css('left',startPos+'px');
						currentPos = startPos;
					}
					newPos = currentPos + itemWidth;
					animating = true;
					ul.animate({left: newPos+'px'}, function(){animating = false;});
					currentPos = newPos;
				}
				start();
			}
		);
	}
}

function prepareSplash(){
	var swapArea = $('#swapArea'),
		slides = swapArea.find('ul > li'),
		splashNav = $('#splashNav'),
		navLis = splashNav.find('ul > li'),
		details = $('li .rel .details'),
		tabCallOut = $('li .rel .tabCallOut'),
		boxes = $('li .rel .boxContain'),
		home_details = $('#homeSlide .rel .details'),
		home_tabCallOut = $('#homeSlide .rel .tabCallOut'),
		home_boxes = $('#homeSlide .rel .boxContain'),
		homeLink = $('#nav .home'),
		homeSlide = $('#homeSlide'),
		ie = false,
		current = 0, timer, wait, homeTimer, waitAgain;	
		
	if($.browser.msie && $.browser.version <= 6){ie = true;}	
		
	slides.css({'z-index':'1', 'display':'none'});
	slides.eq(current).css({'z-index':'10', 'display':'block'});
	
	if(!ie){slides.eq(current).css('opacity','1');}
	
	startHome();
	
	function startHome(){
		homeTimer = setTimeout(function(){
			homeSlide.animate({'opacity':'0'}, 500, function(){
				homeSlide.css({'display':'none','opacity':'1'});
				}
			);
			startTimer();
		}, 8000);	
	}
	
	function resetAll(){
		clearInterval(timer);
		clearTimeout(homeTimer);
		navLis.removeClass('selected');
		current = 0;
		slides.css({'z-index':'1','opacity':'1', 'display':'none'});
		slides.eq(current).css({'z-index':'10', 'display':'block'});
		if(!ie){slides.eq(current).css('opacity','1');}
	}
	
	function startTimer(){		
		navLis.eq(current).addClass('selected');
		
		timer = setInterval(function(){changeSlide();}, 6000);
	}
	
	homeLink.bind({
		mouseover: function(){
			slides.stop(true, true);
			clearInterval(timer);
			clearTimeout(homeTimer);
			resetAll();
			homeSlide.css({'display':'block', 'opacity':'1'});
		},
		mouseout: function(){
		//	resetAll();
			startHome();
		}
	});
	
	function changeSlide(){
		if(!ie){	
			slides.eq(current).animate({opacity: '0'}, 500,
				function(){
					navLis.eq(current).removeClass('selected');
					current++;
					if(current > slides.length-1){
						current = 0;
					}
					navLis.eq(current).addClass('selected');
					slides.eq(current).css({'opacity':'0','display':'block'});
					slides.eq(current).animate({opacity: '1'}, 500, 
						function(){
							slides.css({'z-index':'1', 'display':'none'});
							slides.eq(current).css({'z-index':'10', 'display':'block'});
						}
					);
				}
			);
		}
		else {
			navLis.eq(current).removeClass('selected');
			current++;
			if(current > slides.length-1){
				current = 0;
			}
			navLis.eq(current).addClass('selected');
			slides.eq(current).css({'display':'block'});
			slides.css({'z-index':'1', 'display':'none'});
			slides.eq(current).css({'z-index':'10', 'display':'block'});
		}
	}
	
	navLis.each(function(){
		$(this).bind({
			mouseover: function(){
				clearInterval(timer);
				clearTimeout(homeTimer);
				homeSlide.css({'display':'none','opacity':'1'});
				if(!ie){
					slides.eq(current).stop(true, true).css('opacity','0');
				}
				navLis.removeClass('selected');
				current = $.inArray(this, navLis);
				slides.css('z-index','1');
				slides.eq(current).css({'z-index':'10', 'display':'block'});
				if(!ie){
					slides.eq(current).css('opacity','1');
				}
				navLis.eq(current).addClass('selected');
			},
			mouseout: function(){
				clearInterval(timer);
				clearTimeout(homeTimer);
				startTimer();
			}
		});
	});

	details.bind({
		mouseover: function(){
			clearTimeout(homeTimer)
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			startTimer();
		}
	});	
	
	tabCallOut.bind({
		mouseover: function(){
			clearTimeout(homeTimer);
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			startTimer();
		}
	});	
	
	boxes.bind({
		mouseover: function(){
			clearTimeout(homeTimer);
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			startTimer();
		}
	});
	
	// treating the 'homeslide' details/callouts/boxes differently since they are not in the listing
	home_details.bind({
		mouseover: function(){
			clearTimeout(homeTimer)
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			homeTimer = setTimeout(function(){startTimer();}, 8000);	
		}
	});	
	
	home_tabCallOut.bind({
		mouseover: function(){
			clearTimeout(homeTimer);
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			homeTimer = setTimeout(function(){startTimer();}, 8000);	
		}
	});	
	
	home_boxes.bind({
		mouseover: function(){
			clearTimeout(homeTimer);
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			homeTimer = setTimeout(function(){startTimer();}, 8000);	
		}
	});
}

/*
function prepareSplash(){
	var swapArea = $('#swapArea'),
		slides = swapArea.find('ul > li'),
		splashNav = $('#splashNav'),
		navLis = splashNav.find('ul > li'),
		details = $('li .rel .details'),
		tabCallOut = $('li .rel .tabCallOut'),
		boxes = $('li .rel .boxContain'),
		home_details = $('#homeSlide .rel .details'),
		home_tabCallOut = $('#homeSlide .rel .tabCallOut'),
		home_boxes = $('#homeSlide .rel .boxContain'),
		homeLink = $('#nav .home'),
		homeSlide = $('#homeSlide'),
		ie = false,
		current = 0, timer, wait, homeTimer, waitAgain;	
		
	if($.browser.msie && $.browser.version <= 6){ie = true;}	
		
	slides.css({'z-index':'1', 'display':'none'});
	slides.eq(current).css({'z-index':'10', 'display':'block'});
	
	if(!ie){slides.eq(current).css('opacity','1');}
	
	startHome();
	
	function startHome(){
		homeTimer = setTimeout(function(){startTimer();}, 8000);	
	}
	
	function resetAll(){
		clearInterval(timer);
		clearTimeout(homeTimer);
		navLis.removeClass('selected');
		current = 0;
		slides.css({'z-index':'1','opacity':'1', 'display':'none'});
		slides.eq(current).css({'z-index':'10', 'display':'block'});
		if(!ie){slides.eq(current).css('opacity','1');}
	}
	
	function startTimer(){		
		navLis.eq(current).addClass('selected');
		homeSlide.animate({'opacity':'0'}, 500, function(){
			homeSlide.css({'display':'none','opacity':'1'});
			}
		);
		timer = setInterval(function(){changeSlide();}, 6000);
	}
	
	homeLink.bind({
		mouseover: function(){
			slides.stop(true, true);
			clearTimeout(waitAgain);
			clearInterval(timer);
			clearTimeout(homeTimer);
			resetAll();
			homeSlide.css({'display':'block', 'opacity':'1'});
		},
		mouseout: function(){
			resetAll();
			startHome();
		}
	});
	
	function changeSlide(){
		if(!ie){	
			slides.eq(current).animate({opacity: '0'}, 500,
				function(){
					navLis.eq(current).removeClass('selected');
					current++;
					if(current > slides.length-1){
						current = 0;
					}
					navLis.eq(current).addClass('selected');
					slides.eq(current).css({'opacity':'0','display':'block'});
					slides.eq(current).animate({opacity: '1'}, 500, 
						function(){
							slides.css({'z-index':'1', 'display':'none'});
							slides.eq(current).css({'z-index':'10', 'display':'block'});
						}
					);
				}
			);
		}
		else {
			navLis.eq(current).removeClass('selected');
			current++;
			if(current > slides.length-1){
				current = 0;
			}
			navLis.eq(current).addClass('selected');
			slides.eq(current).css({'display':'block'});
			slides.css({'z-index':'1', 'display':'none'});
			slides.eq(current).css({'z-index':'10', 'display':'block'});
		}
	}
	
	navLis.each(function(){
		$(this).bind({
			mouseover: function(){
				clearInterval(timer);
				clearTimeout(homeTimer);
				homeSlide.css({'display':'none','opacity':'1'});
				if(!ie){
					slides.eq(current).stop(true, true).css('opacity','0');
				}
				navLis.removeClass('selected');
				current = $.inArray(this, navLis);
				slides.css('z-index','1');
				slides.eq(current).css({'z-index':'10', 'display':'block'});
				if(!ie){
					slides.eq(current).css('opacity','1');
				}
				navLis.eq(current).addClass('selected');
			},
			mouseout: function(){
				clearInterval(timer);
				clearTimeout(homeTimer);
				startTimer();
			}	
		});
	});

	details.bind({
		mouseover: function(){
			clearTimeout(homeTimer)
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			startTimer();
		}
	});	
	
	tabCallOut.bind({
		mouseover: function(){
			clearTimeout(homeTimer);
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			startTimer();
		}
	});	
	
	boxes.bind({
		mouseover: function(){
			clearTimeout(homeTimer);
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			startTimer();
		}
	});
	
	// treating the 'homeslide' details/callouts/boxes differently since they are not in the listing
	home_details.bind({
		mouseover: function(){
			clearTimeout(homeTimer)
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			homeTimer = setTimeout(function(){startTimer();}, 8000);	
		}
	});	
	
	home_tabCallOut.bind({
		mouseover: function(){
			clearTimeout(homeTimer);
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			homeTimer = setTimeout(function(){startTimer();}, 8000);	
		}
	});	
	
	home_boxes.bind({
		mouseover: function(){
			clearTimeout(homeTimer);
			clearInterval(timer);
		},
		mouseleave: function(){
			clearInterval(timer);
			homeTimer = setTimeout(function(){startTimer();}, 8000);	
		}
	});
}
*/
function primaryNavAdjust(){
	var self = this,
		ul = document.getElementById('nav'),
		as = $('#nav>li:not(".home")>a'),
		areaWidth = 895, gap, growth, width;
	
	if($.browser.msie && $.browser.version <= 6){areaWidth = 892;}	
	
	this.getWidth = function() {
		width = 0;
		for(var x=0;x<as.length;x++) {
			as[x].style.paddingLeft = 0;
			as[x].style.paddingRight = 0;
			as[x].style.textAlign = "center";
			width += as[x].offsetWidth;
		}
		return width;
	};
	gap = areaWidth - as.length + 1 - this.getWidth();
	if(gap > 0) {
		growth = Math.ceil(gap / as.length);
		for(var x=0;x<as.length;x++) {
			
			$(as[x]).after('<span class="liShadow"></span>');
			
			gap = gap - growth;
			as[x].style.width = as[x].offsetWidth + growth + "px";
			if(gap < growth) {
				growth = gap;
			}
			if(gap === 0) {
				break;
			}
		}
	}
	$('#nav').css('visibility','visible');
}

function setModal(){
	var modalLink = $('.openModal'),
		overlay = $('#overlay'),
		modal = $('#modal'),
		close = $('.closeModal');
		modalVideo = $('.modalVideo');
	
	modalLink.click(
		function(e){
			e.preventDefault();
			var modalVideoHdn = document.getElementById('modalVideoHdn');
			if(modalVideoHdn){
			    modalVideo.html(modalVideoHdn.innerHTML);
			    overlay.css('display','block');
			    modal.css('display','block');
			}
		}
	);
	close.click(
		function(e){
			e.preventDefault();
			overlay.css('display','none');
			modal.css('display','none');
			modalVideo.html('');
		}
	);
}
// ch 20100902 - enhancement: multiple modals possible with new detail layout.
function setMultiModal(){
    var modalLinks = $('a.openModal'),
        overlay = $('div#overlay'),
        modals = $('div.modal'),
        close = $('a.closeModal'),
        modalVideos = $('div.modalVideo');
        
    modalLinks.click(
		function(e){
			e.preventDefault();
			var modalVideoHdn = document.getElementById('modalVideoHdn' + this.rel);
			if (modalVideoHdn){
			    $('div#modalVideo' + this.rel).html(modalVideoHdn.innerHTML);
			    //modalVideo.html(modalVideoHdn.innerHTML);
			    overlay.css('display','block');
			    $('div#modal' + this.rel).css('display','block');
			    //modal.css('display','block');
			}
		}
	);
    close.click(
		function(e){
			e.preventDefault();
			overlay.css('display','none');
			modals.css('display','none');
			modalVideos.html('');
		}
	);
}

function navCols() {
	var colNames = [];
	colNames[0] = "oneCol";
	colNames[1] = "twoCol";
	colNames[2] = "threeCol";
	
	$('#nav div.dropDown').each(function(i) {
		if($(this).parents('li').hasClass('contact')){return;}
		var $this = $(this);
		$this.parent().parent().css({ display:'block',visibility:'hidden' });
		$this.append('<div class="col"><ul id="navCol'+i+'"></ul></div>');
		var $col = $('#navCol'+i);
		var count = 0;
		$this.find('>ul>li').each(function(j) {
			$col.append('<li>'+$(this).html()+'</li>');
			if($col.height() > 345) {
				var $last = $col.find('>li:last li');
				var num = 0;
				for(var x=$last.length-1;x>=0;x--) {
					$last.eq(x).remove();
					if($col.height() <= 345) {
						num = x;
						break;
					}
				}
				if(num === 0) {
					$col.find('>li:last').remove();
				}
				
				// If there are already 3 columns, don't continue.
				if(count > 1) {
					return;
				}
				if(num === 0) {
					$this.append('<div class="col"><ul id="navCol'+i+j+'"><li>'+$(this).html()+'</li></ul></div>');
				}
				else {
					var $a = $(this).find('>a');
					var continued = '<li><a href="'+$a.attr('href')+'" title="'+$a.attr('title')+'" target="'+$a.attr('target')+'">'+$a.html()+' cont&hellip;</a><ul>';
					$last = $(this).find('li');
					for(var x=num;x<$last.length;x++) {
						continued += "<li>"+$last.eq(x).html()+"</li>";
					}
					continued += "</ul></li>";
					$this.append('<div class="col"><ul id="navCol'+i+j+'">'+continued+'</ul></div>');
				}
				$col = $('#navCol'+i+j);
				count++;
			}
		}).parent().remove();
		$col.parent().addClass('last');
		$this.parents('div.dropDownWrap').addClass(colNames[count]);
		$this.parent().parent().removeAttr('style');
	});
}

