/**
 * calendar.js
 * @projectDescription	Script for controlling calendar dialogs.
 * 
 * @author R.A. Ray
 * @version 1.0.3
 * @date 05/01/08
 * 
 * @requires /scripts/jquery/core.js
 * @requires /scripts/textResizeDetector.js
 */

var isEvent = false; //The variable "stop" controls whether or not the window containing the full day's events should be popped-up

var calendar = function() 
{
	$( 'a.eventLink' ).mouseover( function ()
		{ mouseOverLink(); }
	)
		.mouseout( function ()
			{ mouseOutLink(); }
		)
			.click( function ()
				{
					var viewer = document.createElement( 'iframe' );
					var destination = $( this ).attr( 'href' );
					
					overlay( 'attach' );

					$( viewer ).attr(
						{
							src: destination,
							id: 'calendarViewer',
							className: 'admin'
						}
					)
						.appendTo( '#centeringContainer' )
							.dialog(
								{
									resize: false,
									width: 502, 
									height: 380,
									position: 'center'
								}
							);
							
					return false;
				}
			);
	
	$( 'td.event' ).mouseover( function ()
		{ $( this ).addClass( 'eventOver' ); }
	)
		.mouseout( function ()
			{ $( this ).removeClass( 'eventOver' ); }
		)
			.click( function ()
				{
					if( !isEvent )
					{
						var viewer = document.createElement( 'iframe' );
						var destination = $( this ).find( 'a:eq( 0 )' ).attr( 'href' );
						
						overlay( 'attach' );
	
						$( viewer ).attr(
							{
								src: destination,
								id: 'calendarViewer',
								className: 'admin'
							}
						)
							.appendTo( '#centeringContainer' )
								.dialog(
									{
										resize: false,
										width: 502, 
										height: 380,
										position: 'center'
									}
								);
					}
					
					return false;
				}
			);			
};

var mouseOverLink = function() 
{
	isEvent = true;
};

var mouseOutLink = function() 
{
	isEvent = false;
};

var overlay = function ( action )
{
	if( action == 'attach' )
	{
		var dHeight = getDocHeight();
		
		var overlay = document.createElement( 'div' );
		
		$( overlay ).attr (
			{
				id: "overlay"
			}
		)
			.css (
				{
					height: dHeight + "px"
				}
			)
				.appendTo( 'body' );
		
			TextResizeDetector.TARGET_ELEMENT_ID = 'centeringContainer';
			TextResizeDetector.USER_INIT_FUNC = null;
			var iBase = TextResizeDetector.addEventListener( resizeOverlay, null );	
			TextResizeDetector.startDetector();
	}
	else if ( action == 'remove' )
	{
		$( '#overlay' ).remove();
		TextResizeDetector.stopDetector();
	}
};

var getDocHeight = function ()
{
	var docHeight = document.documentElement.clientHeight;

	if ( document.documentElement.clientHeight && ( document.body.parentNode.scrollHeight > document.documentElement.clientHeight) )
	{
		docHeight = document.body.parentNode.scrollHeight;
	}
	else if ( window.innerHeight && ( window.innerHeight > document.documentElement.clientHeight ) )
	{
		docHeight = window.innerHeight;
	}
	
	return docHeight;
};

var resizeOverlay = function ()
{	
	dHeight = getDocHeight();
	
	$( '#overlay' ).css (
		{
			height: dHeight + "px"
		}
	);
};

$( document ).ready(
	function ()
	{
		calendar();
	}
);