$(document).ready(function() {
	
	// Set starting text size if a cookie exists
	if ($.cookie('TEXT_SIZE')) {
		$('body').addClass($.cookie('TEXT_SIZE'));	
	}
	
	// Content control buttons
	// Descrease font size.
	$('.content_ctrls .small a').click(function() {
		var currTextSize = parseFloat($('body').css('font-size'));

		if (currTextSize > 6) {
			changeSize($('body'),"-");
		}
		return false;
	});
	// Increase font size.
	$('.content_ctrls .medium a').click(function() {
		var currTextSize = parseFloat($('body').css('font-size'));

		if (currTextSize < 24) {
			changeSize($('body'),"+");
		} 
		return false;
	});
	// Mail Page.
	$('.content_ctrls .mail_page a').fancybox({
		'zoomSpeedIn' 			: 300,
		'zoomSpeedOut' 			: 300,
		'overlayShow' 			: true,
		'hideOnOverlayClick' 	: false,
		'hideOnContentClick' 	: false,
		'width'					: 570,
		'height'				: 500,
		'autoScale'				: true,
		'transitionIn'			: 'none',
		'transitionOut'			: 'none',
		'type'					: 'iframe'
	});
	
	// Make the subscription links appear in a fancybox.
	$('a.register').fancybox({
		'zoomSpeedIn' 			: 300,
		'zoomSpeedOut' 			: 300,
		'overlayShow' 			: true,
		'hideOnOverlayClick' 	: false,
		'hideOnContentClick' 	: false,
		'width'					: 570,
		'height'				: 500,
		'autoScale'				: true,
		'transitionIn'			: 'none',
		'transitionOut'			: 'none',
		'type'					: 'iframe'
	});
	
	// Make horizontal navigation use drop down menus.
	$( ".horizontal_nav" ).jsddm();
	
	// Make vertical navigations into accordions.
	// $( ".vertical_nav" ).accordion({
	// 	autoHeight : false,
	// 	clearStyle : true
	// });
	
	$('.vertical_nav > li > a').click(function() {
		// Unset the cookie if any nav link is clicked.
		document.cookie = "vertical_nav_clicked=9999; path=/";
	});
	$('.vertical_nav .list_nav_sub_menu_header').click(function() {
		// Hide all other sections.
		$(this).parent().siblings().children('.list_nav_sub_menu_header').next().hide('fast');
		// Show this section.
		$(this).next().show('fast');
		// Save the currently viewed menu in a cookie.
		document.cookie = "vertical_nav_clicked=" + String($(this).parent().index()) + "; path=/";
		return false;
	});
	$('.vertical_nav .clickable_list_nav_sub_menu_header').click(function() {
		// Hide all other sections.
		$(this).parent().siblings().children('.clickable_list_nav_sub_menu_header').next().hide('fast');
		// Save the currently viewed menu in a cookie.
		document.cookie = "vertical_nav_clicked=" + String($(this).parent().index()) + "; path=/";
		return true;
	});
	var selectedVertNav = 0;
	// If the last viewed menu is stored in a cookie start by showing that section.
	// If there is no cookie start with the first item.
	var re = new RegExp("vertical_nav_clicked=[^;]+", "i");
	if (document.cookie.match(re)) {
		selectedVertNav = document.cookie.match(re)[0].split("=")[1];
	}
	// console.log(selectedVertNav);
	// Hide all but the viewed section.
	$('.vertical_nav > li').each(function(i) {
		if (selectedVertNav != i) {
			$(this).children('.list_nav_sub_menu_header').next().css('display', 'none');
			$(this).children('.clickable_list_nav_sub_menu_header').next().css('display', 'none');
		}
	});

});

function changeSize(ele,type) {
    if (ele && ele.children() && ele.children().size() > 0) {
        ele.children().each(function() {changeSize($(this),type);});
        var currSize = parseFloat(ele.css('font-size'));
        if (type == "+") {
            ele.css('font-size',currSize + 1);
        }
        else if (type == "-") {
            ele.css('font-size',currSize  - 1);
        }
        //ele.children().each(function() {alert(this);});
    }
    else {
        var currSize = parseFloat(ele.css('font-size'));
        if (type == "+") {
            ele.css('font-size',currSize + 1);
        }
        else if (type == "-") {
            ele.css('font-size',currSize  - 1);
        }
    }
}
