//functions to be executed on page load
$(function(){
	
	//Open all external/pdf links in new window
	$(function(){
		$('a').filter(function(){
			return (!this.target && (this.href.indexOf(window.location.hostname) == -1 || this.href.match(/\.pdf$/i)));
		}).attr('target', '_blank');
	});
	

	//Clear value in search input box
	$(function() {
		swapValues = [];
		$("#search").each(function(i){
			swapValues[i] = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == swapValues[i]) {
					$(this).val("");
				}
			}).blur(function(){
				if ($.trim($(this).val()) == "") {
					$(this).val(swapValues[i]);
				}
			});
		});
	});	
});

//Set Cookie
function set_cookie (name,value,exp_y,exp_m,exp_d,path,domain,secure)
{
	var cookie_string = name + "=" + escape ( value );
	if( exp_y )
	{
	var expires = new Date ( exp_y, exp_m, exp_d );
	cookie_string += "; expires=" + expires.toGMTString();
	}
	if( path )
	cookie_string += "; path=" + escape ( path );
	if ( domain )
	cookie_string += "; domain=" + escape ( domain );
	if ( secure )
	cookie_string += "; secure";
	document.cookie = cookie_string;
};

//Delete Cookie
function delete_cookie (cookie_name)
{
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

//Get Cookie
function get_cookie (cookie_name)
{
	var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
	if (results) 
	{
	return (unescape (results[2]));
	} 
	else
	{
	return null;
	}
};


//functions to be executed inline
var mylib =
{
	carousel :
	{
		init : function()
		{
			$(".carousel .jCarouselLite").jCarouselLite({
				btnNext: ".carousel .next",
				btnPrev: ".carousel .prev",
				speed: 1000,
				easing: "easeinout",
				circular: false
			});
			$(".carousel li").click(function(e){
				window.location=$(this).find("a").attr("href");
				e.preventDefault();
				return false;
			});
			$('.carousel li').hover(function(e) {
					$(this).addClass('hover');
				}, function() {
					$(this).removeClass('hover');
			});
		}
	},
	tooltip :
	{
		init : function()
		{
			$("a.tooltipLink").tooltip(); 
		}
	},
	apprenticeReveal :
	{
		init : function()
		{
			
	
			var username = get_cookie("username");
			$('#expanded').prepend('<span id="close-expanded"></span>');
			$('#close-expanded').css('cursor','pointer');
			
			if(!get_cookie("username")) {
				$("#collapsed").hide();
				$("#close-expanded").click(function() {
					$("#expanded").hide();
					$("#collapsed").show();
					set_cookie("username","John Smith");
					return false;
				});
				$("#open-expanded").click(function() {
					$("#collapsed").hide();
					$("#expanded").show();
					delete_cookie("username","John Smith");
					return false;
				});
			}
			else
			{
				$("#expanded").hide();
				$("#open-expanded").click(function() {
					$("#collapsed").hide();
					$("#expanded").show();
					delete_cookie("username","John Smith");
					return false;
				});
				$("#close-expanded").click(function() {
					$("#expanded").hide();
					$("#collapsed").show();
					set_cookie("username","John Smith");
					return false;
				});
			}	
			
			if($('#secondary-content .box:nth-child(2)').attr("class") == "box current")
			{	
				$("#expanded").hide();
				$("#collapsed").show();
			};
			if($('#secondary-content .box:nth-child(3)').attr("class") == "box current")
			{	
				$("#expanded").hide();
				$("#collapsed").show();
			};
			
		}
	},
	secondaryArrow :
	{
		init : function()
		{
			$('#secondary-content').find('a.selected').append('<span></span>');
		}
	}
	,
	removeBorders :
	{
		init : function()
		{
			$('#secondary-content li:last-child a:not(.selected)').css({background: 'none'});
		}
	}
}
