// JavaScript Document
jQuery(function(){
	//Text Field Interactivity
	$(':text').each(function(){
		$(this).focus(function(){
			$(this).addClass('focus');
			if($(this).val() == $(this).attr('alt'))	{
				$(this).val('');
			}
		}).blur(function(){
			$(this).removeClass('focus');
			if($(this).val() == '' || $(this).val() == ' ') {
				$(this).val($(this).attr('alt'));
			}
		});
	});
	//site search basic validation
	$('#site_search').submit(function(){
		return $('#key_word').val() != $('#key_word').attr('alt')
	});

	//"fly-out" menu navigation
	$('#secondary>ul>li').each(function(){
		$(this).hover(function(){
			//mouseover
			if(!$.browser.msie){$('#product_magnify .selected div').addClass('inactive').siblings('.mm_hover').show();}//hide magigmagnify
			$(this).addClass('hover');
		}, function(){
			//mouseout
			if(!$.browser.msie){$('#product_magnify .selected div').removeClass('inactive').siblings('.mm_hover').hide();}
			$(this).removeClass('hover');
		}).children('.dropdown').append('<img src="http://www.demarini.com/nav-arrow.gif" class="indicator" />');//add html for arrow, reduces markup
	});

	//sprite navigation
	$('.sprite_nav').children('li:first').addClass('selected').siblings('li').andSelf().each(function(){
		$(this).hover(function(){
			//mouseover
			if(!$(this).hasClass('selected')){
				$(this).children('a').addClass('hover');
			}
		}, function(){
			//mouseout
			$(this).children('a').removeClass('hover');
		}).click(function(){
			if(!$(this).hasClass('selected')){
				$(this).addClass('selected').siblings('li').removeClass('selected');
				$('#'+$(this).children('a:first').attr('href').split('#')[1]).show().siblings('.panel').hide();
			}
			// three tabs
			if ($('.sprite_nav').children('li').length == 3) {
				if ($('.sprite_nav').children('li:last').hasClass('selected')) {
					$($('.sprite_nav li')[1]).addClass("alternate");
				} else {
					$($('.sprite_nav li')[1]).removeClass("alternate");
				}
			}
			return false;
		});
	});

	//video player slideshow
	window.slideshow = {//attach to window for closure
		current : 1,
		itemCount : $('#video_rotator .video_item').length,
		init : function(){
			//$('#video_rotator_choose').width(this.itemCount * 14);//*14* is the width of the A (9px) + the padding (5px)
            $('#video_rotator_choose').width(this.itemCount * 17);//*14* wasn't wide enough for stupid IE.  My disdain for IE will never die!!!
			if(this.itemCount > 1){
				this.start();
				$(window).unload(function(){
					slideshow.pause();
				});
			}else{
				//return false;//do nothing
			}
		},
		start : function(){
			//determine interactivity
			this.intervalVar = window.setInterval("slideshow.advance()", 4000);
		},
		pause : function(){
			clearInterval(this.intervalVar);
		},
		advance : function(advanceTo){
			var nextSlide = 0;
			if(arguments.length == 1){//check for arguments passed
				if(advanceTo == this.current){
					return;
				}else{
					nextSlide = advanceTo;
				}
			}else if (this.current == this.itemCount ) {//check for order/position
				nextSlide = 1;
			} else {
				nextSlide = this.current + 1;
			}
			//transition
			$("#vi_" + this.current).fadeOut(750);
			$("#vi_" + nextSlide).fadeIn(750);
			//update counter
			this.current = nextSlide;
			//change selectors' classes
			$('#video_rotator_choose a').removeClass('current');
			$('#vi_select_'+nextSlide).addClass('current');
		}
	};
	slideshow.init();//initialize slideshow

	//set translucency of the caption overlay
	$('.video_caption').css('opacity',.66);

	// add feature heading mouseover event
	$(".video_item").each(function(){
		$(this).hover(function(){
			//mouseover
			slideshow.pause();
			$(this).children(".video_caption").fadeIn(150);
		}, function(){
			//mouseout
			$(this).children(".video_caption").fadeOut(150);
			slideshow.start();
		}).click(function(){
			window.location = $('#vi_select_'+$(this).attr('id').split('_')[1]).attr('href');
		});

		// feature navigation
		$("#video_rotator_choose a").click(function() {
			slideshow.pause();
			slideshow.advance(parseInt($(this).attr('rel')));
			slideshow.start();
			return false;
		});
	});

	//equalize product_category columns
	$(window).load(function(){
		var colMaxHeight = 0;
		$('.product_category').each(function(){
			colMaxHeight = ($(this).height()> colMaxHeight) ? $(this).height() : colMaxHeight;
		}).height(colMaxHeight);

        $('#vi_2').css('display',"none"); //added fix to initialize video slideshow feature - CKS
        $('#vi_3').css('display',"none"); //added fix to initialize video slideshow feature - CKS
	});
});



/*
** Determine relative element position: used for popups
*/
function getRealPos(element, relativeTo){
	var thisPos = jQuery(element).offset();
	var relativePos = jQuery(relativeTo).offset();
	var realX = thisPos.left - relativePos.left;
	var realY = thisPos.top - relativePos.top;
	return {x: realX, y: realY};
}

/* Begin Wilson pop up function */
function popWindow(href,title,parameters) {
 window.open(href,title,parameters);
}
/* End Wilson pop up function */
