$(document).ready(function(){

	replace_items = [
		{
			selector: ".newsgothic", 
			url: "fonts/newsgothic_h4_home.php"
		}, {
			selector: "#topnav > ul > li.active_section > span a", 
			url: "fonts/newsgothic_menu_active.php"
		}, {
			selector: "#topnav > ul > li.inactive_section > span a", 
			url: "fonts/newsgothic_menu.php"
		}, {
			selector: ".tier2_child_title a, .tier3_child_title a", 
			url: "fonts/newsgothic_h4.php"
		}, {
			selector: ".term_title_depth0, .tier4_title, .article_parent_title", 
			url: "fonts/newsgothic_h2.php"
		}, {
			selector: ".term_title_depth1", 
			url: "fonts/newsgothic_h2_italic.php"
		}, /* {
			selector: ".term_title_depth2, .term_title_depth3", 
			url: "fonts/newsgothic_h2_small.php"
		},*/ {
			selector: ".tier4_articles_heading", 
			url: "fonts/newsgothic_h4_light.php"
		}, {
			selector: ".tier2_right_column .block h4", 
			url: "fonts/newsgothic_h3.php"
		}
	];
	doReplacement();



	$("#topnav ul li ul li").wrap("<nobr></nobr>");

	$("#home_blocks .block:last").addClass('last');	

	$("ul").each(function() {
		$(this).children("li:last").addClass('last');
	});

	$(".autofill").focus(function() {
		if ($(this).val() == $(this).attr('title')) { $(this).val(''); }
	}).blur(function() {
		if ($(this).val() == '') { $(this).val($(this).attr('title')); }
	});


	$(".poll .bar").each(function() {
		percent = parseInt($('.percent_number', this).text());
		rgb = percentToRGB(percent);
		
		$('.foreground', this).css('background-color', 'rgb(' + rgb.join(',') + ')');
	});



	$("#topnav ul li").hover(function() {
		$(this).addClass('hover');	
		$(this).prev().addClass('hover_prev');
	}, function() {
		$(this).removeClass('hover');	
		$(this).prev().removeClass('hover_prev');
	});


	$("#font_size_normal").click(function() {
		$("body").css('font-size', $("html").css('font-size'));
	});

	$("#font_size_decrease").click(function() {
		fontsize = parseFloat($("body").css('font-size')) - 1;
		$("body").css('font-size', Math.max(8, fontsize));
	});

	$("#font_size_increase").click(function() {
		fontsize = parseFloat($("body").css('font-size')) + 1;
		$("body").css('font-size', Math.min(20, fontsize));
	});


	$("#printer_friendly").click(function() {
		$("#stanford_print_css").attr('media', 'all');
		
		$("<p id='printer_friendly_url'>").text(window.location.href).appendTo($('body'));
		
		$("<a id='printer_friendly_remove'>Close printer-friendly view &raquo;</a>").click(function() {
			$("#stanford_print_css").attr('media', 'print');
			
			$("#printer_friendly_remove").remove();
			$("#printer_friendly_url").remove();		
		}).appendTo($('body')).show();

	});

	
	$("#node-form").each(function() {
		form_class = $(this).find('input[name=form_id]').val();
		$(this).addClass(form_class)
	});

	$(".tier4_node_form #edit-taxonomy-2").parent().addClass('hidden_form_item');
	$(".directory_node_form #edit-taxonomy-4").parent().addClass('hidden_form_item');

	$("#edit-log").parent().addClass('hidden_form_item');

});

function doReplacement() {
	for (i in replace_items) {
		$(replace_items[i].selector).each(function() {
			background_src = ''
			background_image = $(this).parents('.tier2_top, .tier3_top').css('background-image');
			if (background_image) {
				background_src = background_image.replace('url(', '').replace(')', '').replace("'", '').replace('"', '');
			}

			var tokens = $(this).text().split(' '); // : [text] ;
			$(this).empty();
			for (k in tokens) {
				var url = replace_items[i].url + "?text=" + escape(tokens[k]+' ');
				if (background_src != 'none') {
					url += "&background_image=" + escape(background_src);
				}
				

				$("<img />").addClass('replacement').attr('alt', tokens[k]).attr('src', url).load(function() {
					linewidth = $(this).parents('li').width() + 'px';
					nav_hide_line = $("<img />").attr('src', 'sites/all/themes/stanford/images/blank.gif');
					nav_hide_line.addClass('nav_hide_border').css('width', linewidth).css('height', 1);
					$(this).parents('li').children('ul').prepend(nav_hide_line);
				}).appendTo($(this));
			}
		});
	}

}

function percentToRGB(pct) {
	blue = 0;
	red = 0;
	green = 0;
	if (pct < 50) {
		red = 255;
		green = Math.round(pct * 5.1);
	}	
	else if (pct == 50) {
		red = 255;
		green = 255;
	}
	else if (pct > 50) {
		red = 255 - Math.round((pct - 50) * 5.1);
		green = 255;
	}
	color = [red, green, blue];
	return color;
}

