//  This is a set of scripts which run when the page loads. 
//
// 1. Navigation menu
//    Creates the sub-menu drop down menu
//
// 2. Print
//    Replaces the elements with the class print-this-page with print buttons
//
// 3. Text Size
//    Changes the style sheet to the large text if the user's cookie has it set. 
//    Replaces the elements with the class size-changer with text size toggle button
//

(function(){ // place inside of this to make it easier and not to clutter the namespace
	
	// First: the functions for each script. Second: setup the functions to run when the page loads 
	
	/*var menus = function(){
		jQuery('#navigation > ul > li.current-with-sub').each(function(){
			var submenu = jQuery('<div class="popup-menu"></div>').appendTo(this).hide();
			jQuery(this).children('ul').clone().appendTo(submenu);
			var linkOffset = jQuery(this).children('a').offset();
			
			
			jQuery(this).children('a').after(jQuery('<span class="drop-down-button">Show sub-navigation</span>').
			click(function(event){
				submenu.toggleFade('fast');
				jQuery(this).toggleClass('drop-down-button-hide');
				
				var that = this;
				var bodyClickAction = function(event){
					// don't do anything if the user clicks on the submenu
					if(event.target == submenu || jQuery.inArray(submenu.get(0), jQuery(event.target).parents()) != -1 ){
						return;
					}
					
					submenu.fadeOut('fast');
					jQuery(that).removeClass('drop-down-button-hide');
					jQuery(document).unbind('click', bodyClickAction);
				};
				// control the action if the user clicks anywhere else on the page to close the popup
				if(jQuery(this).hasClass('drop-down-button-hide')){
					jQuery(document).bind('click', bodyClickAction);
				} else {
					jQuery(document).unbind('click', bodyClickAction);
				}
				event.stopPropagation();
				return false;
			}));
		});
	};*/
	
	
	
	var print = function(){
		jQuery('.print-this-page').replaceWith(jQuery('<span class="print print-this-page triggerable" title="Print this page"><img alt="Print" align="bottom" src="../assets/images/design/print.gif"/></span>').click(function(){ window.print(); }));
	};
	
	var textSize = function(){
		var STYLE_TITLE = "AU Style Large Text Size";
		jQuery.styleswitch.load();

		jQuery('.size-changer').click(
			function(event){
				jQuery.styleswitch.toggle(STYLE_TITLE);
			}
		);
		
		var eventChange = function(event, title, disabled, prevDisabled){
			if(title == STYLE_TITLE && disabled != prevDisabled){
				elemDisplay(disabled);
			}
		};
		
		jQuery(document).bind('styleswitch', eventChange);
		
		var elemDisplay = function(disabled){
			if(disabled){
				jQuery('.size-changer').text('+A').attr('title', 'Increase text size');
			} else {
				jQuery('.size-changer').text('-A').attr('title', 'Decrease text size');
			}
		};
		
		elemDisplay(jQuery.styleswitch.isDisabled(STYLE_TITLE));
		jQuery('.size-changer').addClass('triggerable');
	}
	
	jQuery(document).ready(print);
	jQuery(document).ready(textSize);
	
})();
