$(document).ready(function(){
	
	// homepage slide hover
	homeSlideHover();
	
	// navigation
	mainNav('#nav > li');
	
	// search focus and blur
	$('.search input').focus(function() {
		$('.search').addClass('focus');
	})
	$('.search input').blur(function() {
		$('.search').removeClass('focus');
	})
	
	// detail tabs
	$('.descNav li').click(function(e){
		$('.descNav li').removeClass('active');
		$(this).addClass('active');
			if($(this).hasClass('description')) {
				$('#description').show();
				$('#details').hide();
				$('#certificates').hide();
			}
			if($(this).hasClass('details')) {
				$('#details').show();
				$('#description').hide();
				$('#certificates').hide();
			}
			if($(this).hasClass('certificates')) {
				$('#certificates').show();
				$('#description').hide();
				$('#details').hide();
			}
		e.preventDefault();
	})
	
	// clear input on focus
	$('input[type="text"]').focus(function(){
	if($(this).val()==this.defaultValue){
	$(this).val("");
	}
	});

	// if field is empty afterward, add text again
	$('input[type="text"]').blur(function(){
		if($(this).val()==""){
			$(this).val(this.defaultValue);
		}
	});
});

function mainNav(el) {
	var currentLi = $('#nav li.active');
	var currentA = $('#nav li.active a.active');
	$(el).hover(
		function(){
			$(currentLi).removeClass('active');
			$(currentA).removeClass('active');
			$(this).addClass('hover');
		},
		function(){
			var thisEl = $(this);
			window.setTimeout(function()
			{
				thisEl.removeClass('hover');
				$(currentLi).addClass('active');
				$(currentA).addClass('active');
			},0);

		}
	)
}

// homepage slide hover
var easingMethod = 'easeInOutQuad';

var homeSlideHover = function(){
	var an = $('#homeSlideHover');
	var lis = an.find('li');
	var animVals = [];
	//the order of the values is liWidth, imgMarg, cpWidth
	var vals1 = [];
	var vals2 = [];
	var vals3 = [];
	var dur = 400;
	
	var init = function(){
		lis.bind('mouseenter', function(){ hoverOn($(this)); });
		an.bind('mouseleave', function(){hoverOff();});
	}
	
	var hoverOn = function(t){
		var cl = t.attr('class');
		var ind = cl.charAt(cl.length - 1);
		switch(ind){
			case '1':     
			vals1 = [587, 90, 460];
			vals2 = [250, 0, null];
			vals3 = [360, -120, 205];
			break;
			
			case '2':
			vals1 = [352, -120, null];
			vals2 = [485, 0, null];
			vals3 = [360, -120, 205];
			break;
			
			case '3':
			vals1 = [352, -120, null];
			vals2 = [235, 0, null];
			vals3 = [590, 0, 450];
			break;
			
		}
		animVals = [vals1, vals2, vals3];
		moveEm(ind);
	}
	
	var hoverOff = function(){
		vals1 = [437, 0, null];
		vals2 = [326, 0, null];
		vals3 = [427, -110, 277];
		
		animVals = [vals1, vals2, vals3];
		moveEm();
	}
	
	var moveEm = function(ind){
		var curr = ind != null ? ind : 0;
		
		for(var i = 0;i<lis.length;i++){
			var l = $(lis[i]);
			var img = $(l.find('.img'));
			var cp = $(l.find('.copy'));
			if(animVals[i][0] != null){
				l.stop().animate({width:animVals[i][0]}, {duration:dur+12, easing:easingMethod, queue:false});
			}
			if(animVals[i][1] != null){
				img.stop().animate({marginLeft:animVals[i][1]}, {duration:dur, easing:easingMethod, queue:false});
			}
			if(animVals[i][2] != null){
				cp.stop().animate({width:animVals[i][2]}, {duration:dur, easing:easingMethod, queue:false});
			}
			
			if(i != curr-1 && curr != 0 ){
				l.fadeTo(dur, .8);
			}
			else{
				l.fadeTo(dur, 1);
			}
		}
	}
	init();
}
