function Callout( oWizard ) {
	this.LEADER_OFFSET = 14;

	this.Destroy = Callout_Destructor;

	this.Control = null;
	
	this.Tooltip = oWizard.GlobalStrings.Lookup( "id_CalloutTooltip" );
	
	this.Task = null;
	this.OutlineObject = null;
	this.DetailsField = null;
	this.Reactivate = Callout_Reactivate;
	this.Reactivation = false;
	this.Clear = Callout_ClearCurrentTask;	

	this.OutlineDisplay = Callout_Outline;
	this.DetailsDisplay = Callout_Details;
	this.ToolbarDisplay = Callout_Toolbar;
	this.StatusbarDisplay = Callout_Statusbar;
	this.TabDisplay = Callout_Tab;
	this.TaskDisplay = Callout_Task;
	
	this.Close = Callout_Close;
	this.Display = Callout_Display;
	this.Hide = Callout_Hide;
	this.GetVisibility = Callout_GetVisibility;	
}
function Callout_Destructor() {
    if (this.Control != null) {
	this.Control.Destroy();
    }
	this.Close();
}

function Callout_Outline( oTask, sMessage ) {
	this.Task = oTask;
	this.OutlineObject = g_Wizard.App.Outline.SelectedItem;
	this.DetailsField = null;

	if( g_Wizard.App.Outline.SelectedItem != null ) {
		g_Wizard.App.Outline.SelectedItem.EnsureVisible();
		var rect = g_Wizard.App.GetOutlineObjectRect( g_Wizard.App.Outline.SelectedItem );
		if( rect != null )
			if( this.Display( sMessage, "left up", rect.left + rect.width, rect.top + rect.height, g_Wizard.FlashCallouts )) return true;
	}
	return false;
}
function Callout_Details( oTask, sMessage ) {
	this.Task = oTask;
	this.OutlineObject = g_Wizard.App.Outline.SelectedItem;
	this.DetailsField = g_Wizard.App.Details.SelectedItem;

	if( g_Wizard.App.Details.SelectedItem != null ) {
		var rect = g_Wizard.App.GetDetailsFieldRect( g_Wizard.App.Details.SelectedItem );
		if( rect != null )
			if( this.Display( sMessage, "left down", rect.left + g_Wizard.App.Details.NameWidth, rect.top, g_Wizard.FlashCallouts )) return true;
	}
	return false;
}
function Callout_Toolbar( oTask, sToolbarKey, sButtonKey, sMessage ) {
	this.Task = oTask;
	this.OutlineObject = g_Wizard.App.Outline.SelectedItem;
	this.DetailsField = null;

	var rect = g_Wizard.App.GetToolbarButtonRect( sToolbarKey, sButtonKey );
	if( rect != null )
		if( this.Display( sMessage, "left up", rect.left + rect.width / 2, rect.top + rect.height - 3, g_Wizard.FlashCallouts )) return true;
	return false;
}
function Callout_Statusbar( oTask, iPanelIndex, sMessage ) {
	this.Task = oTask;
	this.OutlineObject = g_Wizard.App.Outline.SelectedItem;
	this.DetailsField = null;

	var rect = g_Wizard.App.GetStatusBarPaneRect( iPanelIndex );
	if( rect != null ) 
		if( this.Display( sMessage, "left down", rect.left + rect.width / 2, rect.top + 3, g_Wizard.FlashCallouts )) return true;
	return false;
}
function Callout_Tab( oTask, sTabKey, sMessage ) {
	this.Task = oTask;
	this.OutlineObject = null;
	this.DetailsField = null;

	var rect = g_Wizard.App.GetTabRect( sTabKey )
	if( rect != null ) 
		if( this.Display( sMessage, "left down", rect.left + rect.width / 2, rect.top + 3, g_Wizard.FlashCallouts )) return true;
	return false;	
}
function Callout_Task( oTask, sMessage ) {
	this.Task = null;
	this.OutlineObject = null;
	this.DetailsField = null;
	
	if( this.Display( sMessage, "left down", GetTaskLeft( oTask ), GetTaskTop( oTask ) + 4 )) return true;
	return false;
}
function Callout_Display( sHTML, sOrientation, X, Y, bFlash, width, fCallback ) {
	try {
        if (this.Control == null) {
            //CHANGE IE7BETA2
            this.Control = window.external.AppletList.Applet("DSApplet").App.Script.DSMainScript_GetCallOutObject();

            this.Control.BorderWidth = 2;
            this.Control.CalloutOffset = this.LEADER_OFFSET;
            this.Control.CalloutWidth = this.LEADER_OFFSET;
        }
        
		this.Control.Show( false );
		if( this.Task != null ) this.Task.className = "Active";
		
		if( sOrientation == "left up" ) this.Control.DisplayMode = 2;
		else if( sOrientation == "left down" ) this.Control.DisplayMode = 1;

		var oDocument = this.Control.Control.document;
		oDocument.onclick = null;
		oDocument.body.title = "";
		oDocument.body.style.cursor = "default";
		oDocument.body.innerHTML = "";
		
		if( fCallback != null ) fCallback( oDocument );
		else {
			oDocument.onclick = CloseCallout;
			var body = oDocument.body;
			body.title = this.Tooltip;
			body.style.cursor = "hand";
			body.innerHTML = sHTML;
			
//			Images appearing in callouts must reside in the same folder as the wizard XML file
//			Generate an absolute path based on the location of the wizard XML file and the image filename
			var c = body.all.tags("IMG");
			for(var i=0; i<c.length; i++)
			{
				var img = c.item(i);
				var re = /(.*[\\\/])(.*)?$/;
				img.src = g_Wizard.DOM.url.match(re)[1]+img.src.match(re)[2];
			}
		}
		
//		SMART SIZING
		var oTR = oDocument.body.createTextRange();
		this.Control.DisplayWidth = ( width == null ) ? 200 : width;
		width = 2 * oTR.boundingLeft + oTR.boundingWidth + 4;
		var XCoordinate = X % window.screen.width;		
		if (XCoordinate + width - this.LEADER_OFFSET + 2 > window.screen.width) width = window.screen.width - XCoordinate + this.LEADER_OFFSET - 2;
		if( width < 3 * this.LEADER_OFFSET ) width = 3 * this.LEADER_OFFSET;
		this.Control.DisplayWidth = width;
		width = 2 * oTR.boundingLeft + oTR.boundingWidth + 4;
		if( width < 3 * this.LEADER_OFFSET ) width = 3 * this.LEADER_OFFSET;
		this.Control.DisplayWidth = width;
		this.Control.DisplayHeight = 2 * oTR.boundingTop + oTR.boundingHeight + this.Control.CalloutWidth + 2;
		
		this.Control.Move( X, Y );
		this.Control.Show( true );
		this.Reactivation = false;
		
		oDocument.SetFlash( ( bFlash != null ) ? ( bFlash && !this.Reactivation ) : false );
		
		return true; 
	}
	catch(e) { return false }
}
function Callout_Hide() {
    if (this.Control != null) {
	this.Control.Show( false );
}
}
function Callout_Close() {
	this.Hide();
	if( this.Task != null ) this.Task.className = "";
	this.Clear();
}
function Callout_GetVisibility() {
    if (this.Control != null) {
	return this.Control.IsVisible;
}
    return false;
}
function Callout_Reactivate() {
	try {
		if( this.Task != null && ( this.OutlineObject == null || this.OutlineObject == g_Wizard.App.Outline.SelectedItem )
			&& ( this.DetailsField == null || this.DetailsField == g_Wizard.App.Details.SelectedItem ) ) {
			g_Wizard.App.EventSink.Interactive = false;
			this.Reactivation = true;
			this.Task.Activate();
			g_Wizard.App.EventSink.Interactive = true;
			return true;
		}
		return false;
	}
	catch(e) { return false }
}
function Callout_ClearCurrentTask() {
	this.Task = null;
	this.OutlineObject = null;
	this.DetailsField = null;
}

function GetTaskLeft( oTask ) {
	var eTD = oTask.firstChild;
	var sPaddingLeft = eTD.currentStyle.paddingLeft;
	var iPaddingLeft = ( sPaddingLeft.indexOf( "px" ) > -1 ) ? parseInt( sPaddingLeft ) : 0;
	with( document.body ) 
	return window.screenLeft + offsetLeft - scrollLeft + eTD.offsetParent.offsetLeft + eTD.offsetLeft + iPaddingLeft;
}
function GetTaskTop( oTask ) {
	var eTD = oTask.firstChild;
	with( document.body )
	return window.screenTop + offsetTop - scrollTop + eTD.offsetParent.offsetTop + eTD.offsetTop;
}

function CloseCallout() {
	g_Wizard.Callout.Close();
}
