$(function() {
	
	
	function alignColumns() {
		
		var mainHeight = $(".main").height();
		var sideHeight = $(".side").height();
	
		if(sideHeight>mainHeight) {$(".main").height(sideHeight)}
	}
	
	alignColumns()
	
	$("#topSection .container").append('<ul class="accessibility"/>').find(".accessibility").each(function() {
																									  
		var ul = $(this)
		
		$("<a/>",{
			href:"#"
			,title:"Increase Font Size"
			,text:"A+"
		}).click(function() {
			changeTextScale(textScale*1.2)
		}).appendTo(ul).wrap("<li/>")
		
		$("<a/>",{
			href:"#"
			,title:"Decrease Font Size"
			,text:"A-"
		}).click(function() {
			changeTextScale(textScale/1.2)
		}).appendTo(ul).wrap("<li/>")
		
		$("<a/>",{
			href:"#"
			,title:"Reset Font Size"
			,text:"A="
		}).click(function() {
			changeTextScale(1)
		}).appendTo(ul).wrap("<li/>")
		
		$("<a/>",{
			href:"#"
			,title:"Toggle High Contrast"
			,text:"AA"
		}).click(function() {
			toggleHighContrast()
		}).appendTo(ul).wrap('<li class="high-contrast"/>')
																									  
	})
	
	
	$(".notice-board").each(function() {
	
		var items = $(this).find("h3")
		
		var toTop = $(".top-of-page")
		
		var idCount = 0
		
		
		if(items.size()>0)
		{
			
			$(".news-contents").append("<ul/>").css("display","block");
			
			
			items.each(function(i) {
				
				
				
				
				var title = $(this).text()
				var anch = $("<a/>",{
						name:"notice_board_item"+(idCount++)
						,"class":"bookmark"
					})
				
				$(this).before(anch)
				
				var newLink = $("<a/>",{
						href:"#"+anch.attr("name")
						,text:title
					}).wrap("<li/>")
					
					$(".news-contents ul").append(newLink.parent())
				
								
			})
			
			
		}
		
		items = $(this).find("hr")
		
		items.each(function() {
			$(this).before(toTop.clone())
		})
		
		
	})
	
	
	var textScale = 1;
	
	function changeTextScale(scale,noReload) {
		
		noReload = noReload ? true : !$.browser.msie
		
		$.cookies.set("textScale",scale+"")
		
		if(!noReload) {
		
			location.reload();
			
		}
		else {
		
			var localScale = scale / textScale
			textScale = scale
		
			
			$("#content").find(".main li,.side li a,p,h2,h3,h4,h5,h6,blockquote").each(function() {
				
				var props = ["font-size","line-height"]
				
				var element = $(this)
				var parent = element.parent()
				
				$.each(props,function(i,prop) {
					
					var val = parseFloat(element.css(prop))
					
					if(val!=0) {
						element.css(prop,(val*localScale)+"px")					  
					}
				})
				
				
			})
			
			$("#content").find(".main li,.main .padding div, .side li a,p,h2,h3,h4,h5,h6,blockquote,ul,ol").each(function() {
				
				var props = ["margin-top","margin-bottom","padding-top","padding-bottom"]
				
				var element = $(this)
				var parent = element.parent()
				
				$.each(props,function(i,prop) {
					
					var val = parseFloat(element.css(prop))
					
					if(val!=0) {
						element.css(prop,(val*localScale)+"px")					  
					}
				})
				
				
			})
			
			alignColumns()
		}
	}
	
	var highContrast = false;
	
	function toggleHighContrast(noReload) {
			
		noReload = noReload ? true : !$.browser.msie
		
		highContrast = !highContrast
		
		$.cookies.set("highContrast",highContrast ? "yes" : "no")
		
		if(!noReload) {location.reload()}
		else {
			$("body").toggleClass("high-contrast")	
		}
	}
	
	window.onload = function() {
		var textScaleCookie = parseFloat($.cookies.get("textScale"))
		if(textScaleCookie && textScaleCookie!=1)
		{
			changeTextScale(parseFloat(textScaleCookie),true)
		}
		
		
		
		
		
		var highContrastCookie = $.cookies.get("highContrast")=="yes"
		if(highContrastCookie)
		{
			toggleHighContrast(true)
		}
		
		alignColumns()
	}
	
	//alignColumns()
	
	
})
