var myFrameContainer;
var myFrameBg;
function toggleSelects( show ) {
	if( show ) {
		$( "select" ).css( "visibility", "visible" );
	} else {
		$( "select" ).css( "visibility", "hidden" );
	}
}
function waitBox( msg, isModal ) {
	var myWaitBox;
	if( msg == "" || !msg )
		msg = "Please Wait...";
		
	$( "<div id = \"waitBox\"><span>" + msg + "</span></div>" ).dialog( {
		autoOpen:true,
		modal:isModal,
		closeOnEscape:false,
		width:150,
		height:75,
		minHeight:75,
		minWidth:150,
		close:function( event, ui ) {
			toggleSelects( true );
		},
		open:function( event, ui ) {
			toggleSelects( false );
		}
	} );
	$( "#waitBox" ).dialog( "widget" ).parent().find( ".ui-dialog-titlebar" ).remove();
}
function removeWaitBox() {
	$( "#waitBox" ).dialog( "close" ).dialog( "destroy" );
	$( "#waitBox" ).empty().remove();
}
function removeToolTip() {
	$( "#toolTip" ).dialog( "close" ).dialog( "destroy" );
	$( "#toolTip" ).empty().remove();
}
function getModal( modalId ) {
	var returner			= "";
	if( $( modalId ).length ) {
		// modal exists; return its src
		return $( modalId ).attr( "src" );
	}
	return false;
}
function removeModal() {
	$( "#myFrame" ).dialog( "close" );
	clearModal();
}
function clearModal() {
	$( "#myFrame" ).dialog( "destroy" );
	$( "#myFrame" ).empty().remove();
	myFrameContainer 		= false;
}
function toolTypify() {
	var helpContents		= $( this ).attr( "title" );
	$( this ).bind( "click", function() {
		makeToolTip( helpContents );
		return false;
	} );
}
function makeToolTip( toolTipText ) {
	var myToolTip;
	// $( "<div id = \"toolTip\"><iframe></iframe></div>" ).dialog( {
	$( "<div id = \"toolTip\"><div class = \"pad\">" + toolTipText + "</div><div>" ).dialog( {
		autoOpen:true,
		modal:true,
		closeOnEscape:true,
		width:500,
		dialogClass:"toolTip",
		close:function( event, ui ) {
			toggleSelects( true );
		},
		open:function( event, ui ) {
			toggleSelects( false );
		}
		
	} );
}

function reposition( dir ) {
	if( myFrameContainer ) {
		var p			= myFrameContainer.position();
		var padding		= 25;
		var scrollbar	= 30;
		if( dir != "h" ) {
			if( myFrameContainer.height() + ( padding * 2 ) > $( window ).height() ) {
				myFrameContainer
					.css( "top", ( $( window ).scrollTop() + padding ) + "px" );
			} else {
				myFrameContainer
					.css( "top", $( window ).scrollTop() +( ( $( window ).height() - myFrameContainer.height() ) / 2 ) + "px" );
			}
		}
		if( dir != "v" ) {
			if( myFrameContainer.width() + ( padding * 2 ) > $( window ).width() - scrollbar ) {
				myFrameContainer
					.css( "left", $( window ).scrollLeft() + padding + "px" );
			} else {
				myFrameContainer
					.css( "left", $( window ).scrollLeft() + ( ( $( window ).width() - myFrameContainer.width() - scrollbar ) / 2 ) + "px" );
			}
		}
	}
	if( $( ".ui-widget-overlay" ).length ) {
		$( ".ui-widget-overlay" )
			.height( $( document ).height() )
			.width( $( document ).width() );
	}
}
function resize() {
	// console.log( $( "#myFrame" ) );
	var myFrame				= $( top ).find( "#myFrame" );
	if( myFrameContainer ) {
		var newHeight 		= $( "#myFrame" ).contents().find( "#container" ).outerHeight();
		var newWidth 		= $( "#myFrame" ).contents().find( "#container" ).outerWidth();
		myFrameContainer.height( newHeight );
		$( "#myFrame" ).height( newHeight );
		myFrameContainer.width( newWidth );
		$( "#myFrame" ).width( newWidth );
	}
}


$( document ).ready( function() {
	$( ".help" ).each( toolTypify );
	$( ".wait" ).bind( "click", function( e ) {
		waitBox( "Please Wait...", true );
	} );
	$( 'a[rel=overlay]' ).bind( "click", function( e ) {
		e.preventDefault();
		//
		if( getModal( "#myFrame" ) ) {
			if( getModal( "#myFrame" ).indexOf( e.currentTarget.href ) == 0 ) {
				myFrameContainer
					.before( myFrameBg )
				reposition();
				myFrameContainer
					.show();
				$( "#myFrame" ).contents().find( "form.custom input.text[type!=hidden]:first" ).focus();
				return false;
			} else {
				removeModal();
			}
		} 
			
		$( document.body ).append( "<iframe frameborder = \"0\" id = \"myFrame\" />" );
		
		$( "#myFrame" ).load(
			function() {
				removeWaitBox();
			
				/*var newHeight = $( this ).contents().find( "#container" ).outerHeight() + 5;
				var newWidth = $( this ).contents().find( "#container" ).outerWidth();
				myFrameContainer.height( newHeight );
				$( this ).height( newHeight );
				myFrameContainer.width( newWidth );
				$( this ).width( newWidth );
				myFrameContainer
					.css( "left", ( ( $( document ).width() - myFrameContainer.width() ) / 2 ) + "px" )
					.css( "top", $( window ).scrollTop() + 25 + "px" )
					
				$( ".ui-widget-overlay" ).height( $( document ).height() );*/
				
				resize();				
				reposition();
				
				myFrameContainer.css( "visibility", "visible" );
				$( "#myFrame" ).contents().find( "form.custom input.text[type!=hidden]:first" ).focus();
			}
		);
		$( "#myFrame" )
			.dialog( {
				autoOpen: false,
				modal: true,
				close: function( event, ui ) {
					// myFrameContainer.hide();
					top.clearModal();
					return false;
				},
				open: function( event, ui ) {
					waitBox( "Loading...", false );
					myFrameBg			= $( ".ui-widget-overlay" );
					
					var queryString		= "overlay=true";
					if( e.currentTarget.href.indexOf( "?" ) >= 0 ) {
						queryString = "&" + queryString;
					} else {
						queryString = "?" + queryString;
					}
					
					$( "#myFrame" ).attr( "src", e.currentTarget.href + queryString );
				},
				focus: function( event, ui ) {
					//console.log( "focus" );
				}
			} )
			.dialog( "moveToTop" );
		
		myFrameContainer = $( "#myFrame" ).parent();
		myFrameContainer.css( "visibility", "hidden" );
		$( "#myFrame" ).dialog( "open" );
		
		$( window ).bind( "resize", reposition );
	} );
	/*-----------------*/	
} );