//main script
jQuery.each( $("a"), function(){
	if(this.rel=="external"){
		this.target = "_blank";
	}
});

$(window).ready(function(d){
		if($('#withPanel').length > 0){
			$('#withPanel .mainCopy').each(function(w){
					$(this).css({"position":"absolute","display":"none"});
					//$(this).hide();
				}
			);
			$('#withPanel .mainCopy:first-child').css({"display": "block"}).addClass("active");
			$('.glossbutton').css({"margin-top":"110px"});
			
			//this is a hack to ensure that the footer doesn't
			//move around
			$("#footer").css({"position":"relative"});
		}
		if($('#lifePanel').length > 0){
			lifeTips();
		}
		if($("#langSelector").length > 0){
			langSelector();
		}
		$('a.trackLink').each(function(){
			$(this).click(function(){
				trackLink = this.href.replace(window.location.protocol + "//" + window.location.hostname,"");
				pageTracker._trackPageview(trackLink);
			});
		});
	}
);

var hide_it = 0;
var sel_height = 0;
function langSelector(){
	var langDiv = $('#langSelector');
	sel_height = langDiv.css("height");
	$('#langSelector ul').css({"visibility":"hidden"});
	langDiv.css({"height":"100px"});
	$('#langSelector h2').each(function(){
		$(this).mouseover(function(){
			var langSel = $('#langSelector ul');
			if(langSel.css("visibility") != "visible"){
				langSel.css({"visibility":"visible"});
				langDiv.css({"height":sel_height});
				
			} else {
				clearTimeout(hide_it);
			}
		});
		$(this).mouseout(function(){
			hide_it = setTimeout(function(){
				$('#langSelector ul').css({"visibility":"hidden"});
				$("#langSelector").css({"height":"100px"});
			},300);
		});
	});
	$('#langSelector a').each(function(){
		$(this).mouseover(function(){
			clearTimeout(hide_it);
		});
		$(this).mouseout(function(){
			hide_it = setTimeout(function(){
				$('#langSelector ul').css({"visibility":"hidden"});
				$("#langSelector").css({"height":"100px"});
			},300);
		});
	});
}

function showServingSuggestion(val){
	var active = $('.active');
	if(active.length > 0){
		active.each(function(a){
			$(this).fadeOut(
				250, function(){
				$(this).removeClass("active");
			});
		});
	}
	var item = $('#' + val);
	//item.css({"display":"block"});
	$(item).fadeIn(
		250, function(){
		$(this).addClass("active");
		}
	);
	item = null;
}


function lifeTips(){
	jQuery.each( $(".tip"), function(){
		$(this).hide();
	});
	
	$("#tip_1").show();
	
	var totalTips = $(".tipCopy").length
	var tipState = 1;
	var shiftVal = 220;
	var timing = 500;
	var selItems = $(".imgSel");
	var tipImgs = $('#lifeWrapper img');
	var animating = false;
	
	$('.tipArrow').click(function(event) {
		event.preventDefault();
		if(animating == false){
			//$("#tip_"+tipState).fadeOut(timing/2);
			var dir = event.target.rel;
			var tipState1 = tipState;
			
			if(dir=="prev"){
				
				//if we're on the first one, and we need to go back,
				//go to the end
				if( tipState==1 ){
					tipState = totalTips;
				//otherwise, just go back one
				}else{
					tipState--;
				}
			}else if(dir=="next"){
				
				//if we're on the end, and we need to go forward
				//go to the beginning
				if( tipState==totalTips ){
					tipState = 1;
				
				//otherwise, just go forward one
				}else{
					tipState++;
				}
			}
		
			//switch content
			animating = true;
			$("#tip_"+tipState1).fadeOut(timing/2, function(){$("#tip_"+tipState).fadeIn(timing/2,function(){ animating = false; });});
			//switch selected states
			$(selItems[(tipState1-1)]).removeClass('selected');
			$(selItems[(tipState-1)]).addClass('selected');
		}
	});
}
