(function(){

	/* ASPX LOOKUP */
	var _controlCache = []
	jQuery.getAspxId = function(id){ 
		// check control cache
		if(_controlCache[id]){return _controlCache[id];}
		
		// loop placeholder and cache control id
		var controlId;
		for(var i=0,len = _placeHolders.length; i < len; i++){
			controlId = _placeHolders[i] + '_' + id;
			if(document.getElementById(controlId )){
				return _controlCache[id] = controlId ;
			}
		}
		
		// cache id
		return _controlCache[id] = id;
	}

	jQuery.aspx = function(id){ 
		if(typeof id == 'string'){
			id = '#' + jQuery.getAspxId(id); 
		} else {
			for(var i=0,len=id.length;i<len;i++){
				id[i] = '#' + jQuery.getAspxId(id[i]);
			}
			id = id.join(',');
		}
		return jQuery(id); 
	}

	/* TOOLS NAVIGATION */
	// private functions
	var toolsTimer; // nav timer is set whenever a user mouses out 
	function setToolsTimer(){
		clearToolsTimer(); 
		toolsTimer = setTimeout(hideTools, 500);
	}

	function clearToolsTimer(){
		clearTimeout(toolsTimer); 
	}

	function hideTools(){ 
		$('#header-tools ul').fadeOut('fast');	
	}

	$('#header-tools a').hover(
		function(){
			clearToolsTimer();
			$('#header-tools ul').fadeIn();	
			return false;
		},
		function(){
			setToolsTimer();
			return false;
		}
	);
	$('#header-tools b a').click( function(){return false;});
	
	/* TEXT-SIZE */
	function getFontSize(){
		return parseInt( $('html').css('font-size'), 10 );
	}	

	function setFontSize(size){
		$('html').css('font-size', size + 'px');
		$.cookie('TextSize', size);
	}	

	var fontIncrement = 2;
	var minFontSize = 14;
	var maxFontSize = minFontSize+(3*fontIncrement);

	$.aspx('TextSizeDecreaseButton').click( function() {
		var size = getFontSize()-fontIncrement;
		if (size >= minFontSize){setFontSize(size);}
		return false;
	});	

	$.aspx('TextSizeIncreaseButton').click( function() {
		var size = getFontSize()+fontIncrement;
		if(size <= maxFontSize){setFontSize(size);}
		return false;
	});


	/* ADVANCE DETAILS FORM */
	$('#' + $.getAspxId('AdvancedDetails') + ' input').click( function(){
		if(this.value == "1") {
			$.aspx('AdvancedDetailsForm').slideDown();
		} else {
			$.aspx('AdvancedDetailsForm').slideUp();
		}
	});
		

	/* TOP CLEAR AND CALCULATE LINK BUTTONS */
	$.aspx('TopClearLinkButton').click( function() {
		// Fire clear button
		$.aspx('ClearButton').click();
		return false;
	});

	$.aspx('TopCalculateLinkButton').click( function() {
		// Fire calculate button
		$.aspx('CalculateButton').click();
		return false;
	});

	/* PRINT */
	$.aspx('PrintButton').click( function(){
		window.print();
		return false;
	});

	/* RESULTS - CURRENT & HISTORICAL MODELS */
	$.aspx('HistoricalModelLink').click( function(){
		$.aspx('ModelState').val('Historical');
		$.aspx('CurrentModel').hide();
		$.aspx('HistoricalModel').show();
		return false;
	});
	
	$.aspx('CurrentModelLink').click( function(){
		$.aspx('ModelState').val('Current');
		$.aspx('CurrentModel').show();
		$.aspx('HistoricalModel').hide();
		return false;
	});
	

	/* RESULTS - DETAILS */
	$.localScroll();

	/* RESULTS - ERROR MESSAGES */
	var errorMessages = $('#results .error-message');

	$('#results .error td').mouseover( function(event){
		errorMessages.hide();

		var id = this.id.replace( /_\d+$/, ''); // remove row index. ex:{Nomogram}Row_01
		var errorMessage = $.aspx(id + 'Error');
		var offset = $(this).offset();

		var position = {
			top:(offset.top + ($(this).height()/2) - errorMessage.height())  + 'px', 
			left:(offset.left + 24 - errorMessage.width()) + 'px'
		}
		errorMessage.fadeIn('medium').css(position);
		return false;
	});

	$('#results .error').bind('mouseout', function(event){
		errorMessages.hide();
		return false;
	});

	/* DISCLAIMER */
	$('#disclaimer-accept').click( function(event){$.aspx('DisclaimerAcceptButton').click();});
	$('#disclaimer-do-not-accept').click( function(event){$.aspx('DisclaimerDoNotAcceptButton').click();});

	$.aspx('DisclaimerAcceptButton').click( function(event){event.stopPropagation();});
	$.aspx('DisclaimerDoNotAcceptButton').click( function(event){event.stopPropagation();});

	/* WINDOW MANAGER */
	$.WindowManager = (function(){
		var windows = [];
	
		// set onunload to close all open windows
		window.onunload = function() {$.WindowManager.closeAll();}
	
		return {
			open: function(URL, windowName, windowFeatures){
				windows[windowName] = window.open(URL, windowName, windowFeatures);
				windows[windowName].focus();
				return windows[windowName];
			},
			
			center: function(width, height){
				var leftPosition = Math.round( (screen.availWidth - width)/2 )
				var topPosition = Math.round( (screen.availHeight - height)/2 )
				return "left=" + leftPosition  + ",top=" + topPosition  + ",";
			},
			
			closeAll: function(){
				for(each in windows){
					if(!windows[each].closed){windows[each].close();}
				}
			}
		}
	})();

	/* GLOSSARY */
	var windowAttributes = $.WindowManager.center(620, 460) + 'width=620,height=460,resizable=1,status=1,scrollbars=1,menubar=0,location=0,toolbar=0';
	$('a[href^=/glossary=]').each( function(){ 
		this.tabIndex = 999;
		$(this).click(
			function(){ 
				$.WindowManager.open(this.href, 'Glossary', windowAttributes);
				return false;
		});
	})
})();


