// Holder for flash instances to make less agressive loading
var flashInstances = new Array();

// DOM load
$(document).ready(function() {
	homepageRollovers();
	printLink();
	newWindowLinks();
	tabbedContent();
	quizEnhance();
	progressNumbers();
	progressToolTips();
});

// Window loaded
$(window).load(function() {
	for(flash in flashInstances) {
		eval(flashInstances[flash]).write(flash);
	}
});

/**
 */
function homepageRollovers() {
	
	if($('div.edit').length == 0){//only do this if not in cms edit mode.
		$("#homepageBlocks li").hover(
			function() {
				$(this).addClass("over").css({cursor:"pointer"});		
			},
			function() {
				$(this).removeClass("over");
			}
		).click(function(){
			
			href = $("a",this).attr("href");
			
			if($("a",this).attr("target") == "_blank"){
				window.open(href);
			}else{
				window.location = href;
			}
			return false;
		});
	}
}

/**
 */
function printLink() {
	
	if(typeof printText=="undefined"){
		printText ="Print this page";
	}
	
	$("#bottomLinks li").removeClass("last");
	$("#bottomLinks").append('<li class="last" id="print"><a href="#" onclick="window.print();return false">'+printText+'</a></li>');
	
}

/**
 */
function quizEnhance() {
	$("#quiz p input").hide();
	$("#submitButton").remove();
	$("#quiz p span").addClass("enhanced");
	$("#quiz[class!='disable'] p span").css("cursor","pointer").click(function(){
		$("input",this).attr("checked","true");
		$("#quiz form").submit();
	});
	$("#quiz p label em").each(function() {
		var text 	= $(this).text();
		$(this).hide();
		var target  = $("#" + $(this).parent().attr("for").toString()).parent();
		target.append(text);
	});
}

/**
 * Intercept links with rel="pop" and open them in new window
 */
function newWindowLinks() {
	$("a[rel='pop']").click(function() { window.open($(this).attr("href")); return false;});	
}

/**
 */
function tabbedContent() {
	if($('.tabContent div.edit a').length == 0){
		$(".tabContent:gt(0)").hide();
		$("#tabbedContent").before("<div id=\"tabs\" class=\"cfx\"></div>");
		$(".tabContent h2").each(function(i) {
			var sel = i==0 ? ' class="selected"' : '';
			$("#tabs").append("<a href=\"#\" onclick=\"showTab(" + i + ");return false\""+sel+">" + $(this).text() + "</a>");
			$(this).remove();
		});	
	}
}
function showTab(i) {
	$(".tabContent").each(function(j) {
		if(j==i) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
	$("#tabs a").each(function(j) {
		if(j==i) {
			$(this).addClass("selected");
		} else {
			$(this).removeClass("selected");
		}
	});
}
function progressNumbers(){
	if($("#progress").length){
		$("#progress ol li span") .each(function(i) {
			i++;
			$(this).before("<em>"+ i +"</em> ");
		});		
	}
}
function progressToolTips(){
	if($("#progress").length){	
							
		$("#progress ol li")
			.each(function(i) {				
				if(!$(this).hasClass('completed')&&!$(this).hasClass('nextStep')){
					$(this)
						.wrapInner('<a href="#" class="info"></a>')
						.find('span').hide();
				}				
				$(this).find('a')					
					.simpletip({			 	
				 		content: $(this).find("span").text(), 				 						 						 						 		
				 		boundryCheck: true,
				 		baseClass: 'tooltip hidden',
				 		onShow: function(){ 	
				 			this.getParent().find('.tooltip').removeClass('hidden');
			 		 	},
			 		 	onHide: function(){ 
				 		 	this.getParent().find('.tooltip').addClass('hidden');
			 		 	}
			 		})
			 		.find('span').addClass('offScreen'); 
			 		
			 	// keyboard accessible
			 	$(this).find('a')
			 		.bind('focus', function() {
			 			$(this).simpletip().show();
			 		})
			 		.bind('blur', function() {			 		
			 			$(this).simpletip().hide();
			 		})
			 	.end().find('a.info')
			 		.click(function() {					 				
				 		return false;			 			
				 	})
				 	/* 
				 	.hover(function() {
					 		$(this).parent().addClass("over");
					 	}, function() {
					 		$(this).parent().removeClass("over");
				 	})*/;
				 	 				 	
			 			
			});
	}
}
