$(document).ready(function($) {
	$(".contact-listing .email").each(function() {
		var newemail = $(this).html().replace(/\[dot\]/g,".");
		newemail = newemail.replace(/\[at\]/g,"@");
		$(this).html(newemail);
	});
	$("#books-tabs").tabs();
	$("#language-tabs").tabs();
	/*
	$(".alignright").each(function() {
		$(this).attr("align","right");
	});
	$(".alignleft").each(function() {
		$(this).attr("align","left");
	});
	*/
	$(".book-download .book-download-title").click(function(evt) {
		evt.preventDefault();
		if($(this).parent().hasClass("closed")) {
			$(this).parent().removeClass("closed");
			$(this).parent().addClass("open");
		} else {
			$(this).parent().removeClass("open");
			$(this).parent().addClass("closed");
		}
	});
	$(".book-download-link").click(function(evt) {
		evt.preventDefault();
		$("#language-tabs").tabs("select",1);
		$("#books-tabs").tabs("select",1);
		link_number = $(this).attr("id").replace("book-download-link-","");
		if($("#book-download-"+link_number).hasClass("closed")) $("#book-download-"+link_number+" .book-download-title").trigger('click');
		setTimeout(goToDownloadLink,100,link_number);
	});
	
	$("#language-letter-list a").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt;
		(parts.length>1) ? trgt = parts[1] : trgt = parts[0];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
		
		//get the distance so that we can make a consistent velocity
		var target_distance = Math.abs(target_offset.top - $(this).offset().top);

		//goto that anchor by setting the body scroll top to anchor top
		//$('html, body').animate({scrollTop:target_top}, target_distance/1.6);
		$('html, body').animate({scrollTop:target_top}, 500);
	});
	$(".ui-tabs-nav li a").each(function() {
		if($(this).html().length>13) {
			if($(this).html().toLowerCase().search("<br>")==-1) { $(this).html($(this).html().replace(" ","<br>")); }
		} else {
			$(this).addClass("short-name");
		}
	});
	$("#news-sidebar li").each(function() {
		$(this).html($(this).html().replace("?cat=3",""));
	});
	fixBreadcrubs();
});

function goToDownloadLink(link_number) {
	var offsetY = $("#book-download-"+link_number).offset().top-20;
	$('html,body').animate({
		scrollTop: offsetY
	}, 200);
	//window.scrollTo(0,offsetY);
}

function gotoPageID(pageID) {
	$('html, body').animate({scrollTop:$("#"+pageID).offset().top}, 500);
}

function fixBreadcrubs() {
	$(".breadcrumbs a").each(function() {
		if($(this).text()=="News") {
			if($(this).prev().prev().text()!="About Hesperian") $(this).before('<a title="Go to About Hesperian." href="/about">About Hesperian</a><span class="breadcrumb-separator"> &gt; </span>');
			$(this).attr('href','/about/news');
		}
	});
}
function skipDownloadForm() {
	$.cookie('email_capture_submitted','true',{path:'/'});
	$("#downloads-content").show();
	$("#downloads-form").hide();
}
