﻿/*******                                                                         */ 
/*                     functions for HelpManager.js (InstructorViewer cues)      */
/*******                                                                         */


function Rect(Left,Top,Right,Bottom)
{
	// members.
	this.left		= 0;
	this.top  	= 0;
	this.right 	= 0;
	this.bottom = 0;
	this.width  = 0;
	this.height = 0;
	
	// Methods
	this.toString = function()
	{
		return "Rect[ L:" + this.left + " T:"  + this.top + " W:" + this.width + " H:" + this.height + " ]";
	} // Rect.toString

	this.SetRect = function(Left,Top,Right,Bottom)
	{
		this.left 	= Left;
		this.top  	= Top;
		this.right 	= Right;
		this.bottom = Bottom;
		this.width  = (Right - Left);
		this.height = (Bottom - Top);
		
		if(this.left<0 || this.top<0 || this.width<0 || this.height<0)
			throw new TegException(ET_InvalidArg, "Invalid Rectangular dimensions");
	} // Rect.SetRect

	this.Contains = function(nPointX,nPointY)
	{
		return ((nPointX >= this.left && nPointX <= this.right) &&
						(nPointY >= this.top	&& nPointY <= this.bottom));
	} // Rect.Contains
	
	this.Equals = function(oRect)
	{
		return this.left	 == oRect.left	&& 
					 this.top		 == oRect.top		&&
					 this.right	 == oRect.right	&& 
					 this.bottom == oRect.bottom;
	} // Rect.Equals 
	
	this.Shift = function(nX,nY)
	{
		if(isNaN(nX) || isNaN(nY))
			throw new TegException(ET_InvalidArg, "Invalid Shift arguments");
			
		this.SetRect(this.left+nX, this.top+nY, this.right+nX, this.bottom+nY);
	} // Rect.Shift

	// Initialize the Rect.
	this.SetRect(Left,Top,Right,Bottom);
} // Rect Class

//-----------------------------------------------------------------------
function Point(X,Y)
{
	this.x = X;
	this.y = Y;
} // Point.
//-----------------------------------------------------------------------

function IsString(str) // Non empty string.
{
	return str && typeof str == "string" && str.length;
} // IsString

//-----------------------------------------------------------------------
function GetMaxZOrder()
{
	return 1000;
} // GetMaxZorder

//-----------------------------------------------------------------------

function GetBodyRect(iFrame)
{
  if(iFrame!=null) //return rect of player iframe
    return new Rect(iFrame.offsetLeft,iFrame.offsetTop,iFrame.width,iFrame.height);
  else
    return new Rect(0,0,window.screen.width,window.screen.height);
} 
//-----------------------------------------------------------------------
    
function IsObject(obj)
{
	return obj && typeof obj == "object";
} // IsObject

//-----------------------------------------------------------------------

function GetDivRect(DivId, bHasBorder, bAbsolute) // if bAbsolute = true the returned Rect is in Body coordinations.
{
	var oDiv; 
	
	if(typeof DivId == "string")
		oDiv = document.getElementById(DivId);
	else 
		oDiv = DivId;
	
	if(oDiv == null)
		throw new TegException(ET_InvalidArg, "GetDivRect() failed with error: Invalid DivId - " + DivId);
	
	var Left		= parseInt(oDiv.offsetLeft);
	var Top			= parseInt(oDiv.offsetTop);
	var Right		= Left + parseInt(oDiv.offsetWidth);
	var Bottom	= Top	 + parseInt(oDiv.offsetHeight);	
	
	
	// In FireFox & Safari the div size doesn't include the border size.
	if(bHasBorder && GetBrowser() == "IExplorer")
	{
		if (oDiv.style.borderLeftWidth != "")
			Left   += parseInt(oDiv.style.borderLeftWidth);
		if (oDiv.style.borderTopWidth != "")
			Top		 += parseInt(oDiv.style.borderTopWidth);
		if (oDiv.style.borderRightWidth != "")
			Right  -= parseInt(oDiv.style.borderRightWidth);
		if (oDiv.style.borderBottomWidth != "")
			Bottom -= parseInt(oDiv.style.borderBottomWidth);
	}
	
	Right = (Right<0)? 0 : Right;
	Bottom = (Bottom<0)? 0 : Bottom;
	
	if(bAbsolute) // Include offsets.
	{
		var nOffsetLeft = 0;
		var nOffsetTop  = 0;
		var oParent = (GetBrowser() == "IExplorer") ? oDiv.parentElement : oDiv.parentNode;
		while(IsObject(oParent) && !isNaN(oParent.offsetLeft) && !isNaN(oParent.offsetTop))
		{
			//alert(oParent.tagName + "," + oParent.offsetTop);
			nOffsetLeft += oParent.offsetLeft;
			nOffsetTop  += oParent.offsetTop;
			oParent = (GetBrowser() == "IExplorer") ? oParent.parentElement : oParent.parentNode;
		}

		if (document.getElementById("frmPlayer") != null) {
		    //oDiv does not belong to player iframe
		    if (document.getElementById("frmPlayer").contentWindow.document.getElementById(oDiv.id) == null) {
		        if (GetBrowser() != "IExplorer") {
		            nOffsetTop -= 48;
		            nOffsetLeft -= 15;
		        }
		        else {
		            nOffsetTop -= 4;
		            nOffsetLeft -= 5;
		        }
		    }
		    else { //oDiv belongs to player iframe
		        nOffsetLeft += document.getElementById("frmPlayer").offsetLeft;
		        nOffsetTop += document.getElementById("frmPlayer").offsetTop;
		    }
		}
		
		Left	 += nOffsetLeft;
		Top		 += nOffsetTop;
		Right  += nOffsetLeft;
		Bottom += nOffsetTop;
	
	}
	return new Rect(Left,Top,Right,Bottom);
} // GetDivRect

//-----------------------------------------------------------------------
function GetBrowserVer(){
 
	var nIndex1,nIndex2;
	var sIE="MSIE",sFF="Firefox",sSF="Safari",sNS="Netscape",sSemiColon=";",sSlash="/";
	if( ((nIndex1 = navigator.userAgent.indexOf(sIE)) != -1) &&
			((nIndex2 = navigator.userAgent.indexOf(sSemiColon, nIndex1)) != -1) )
	{
		return navigator.userAgent.substr(nIndex1+sIE.length+1,nIndex2-nIndex1-sIE.length-1);
	}
	else if( ((nIndex1 = navigator.userAgent.indexOf(sFF)) != -1) &&
					 ((nIndex2 = navigator.userAgent.indexOf(sSlash, nIndex1)) != -1) )
	{
		return navigator.userAgent.substr(nIndex2+1, navigator.userAgent.length-nIndex2-1);
	}
	else if( ((nIndex1 = navigator.userAgent.indexOf(sSF)) != -1) &&
					 ((nIndex2 = navigator.userAgent.indexOf(sSlash, nIndex1)) != -1) )
	{
		return navigator.userAgent.substr(nIndex2+1, navigator.userAgent.length-nIndex2-1);
	}
	else if( ((nIndex1 = navigator.userAgent.indexOf(sNS)) != -1) &&
					 ((nIndex2 = navigator.userAgent.indexOf(sSlash, nIndex1)) != -1) )
	{
		return navigator.userAgent.substr(nIndex2+1, navigator.userAgent.length-nIndex2-1);
	}
	else
	{
		return "0.0";
	}	
}

//-----------------------------------------------------------------------

function AppendNewDiv(oParent, DivId, nPixelLeft,nPixelTop,nzIndex,nPixelHeight, nPixelWidth)
{
	var Div = document.createElement('div'); // Create dynamically div tag.
	
	Div.id								= DivId;
	Div.style.position		= "absolute";
	Div.style.pixelLeft		= nPixelLeft + "px";
	Div.style.pixelTop		= nPixelTop + "px";
	Div.style.zIndex			= nzIndex;
	Div.style.pixelHeight	= nPixelHeight + "px";
	Div.style.pixelWidth	= nPixelWidth + "px";
	
	Div.style.width				= nPixelWidth + "px";
	Div.style.left				= nPixelLeft + "px";
	Div.style.top					= nPixelTop + "px";
	Div.style.height			= nPixelHeight + "px";
		
	oParent.appendChild(Div);
	return Div;
} // AppendNewDiv

//-----------------------------------------------------------------------

function SafeDeleteElement(oHtmlElement)

{
	// Support Element id as string. 
	if(IsString(oHtmlElement))
		oHtmlElement = document.getElementById(oHtmlElement);
	// Validate that it's an HTML element.	
	if(!IsObject(oHtmlElement) || !IsString(oHtmlElement.tagName))
		return;
		
	// Remove it.
	if(IsObject(oHtmlElement.parentElement))
		oHtmlElement.parentElement.removeChild(oHtmlElement);
	else if(IsObject(oHtmlElement.parentNode))
		oHtmlElement.parentNode.removeChild(oHtmlElement);

} // SafeDeleteElement

//-----------------------------------------------------------------------

function ApplyIeFilter(oImage)
{
	if(typeof oImage == "string")
		oImage = document.getElementById(oImage);
	
	// Validate that  it's of an Image type.
	if(!IsObject(oImage) || !IsString(oImage.tagName) || oImage.tagName != "IMG" )
		return;
	
	try
	{
		var sImgID		 = (oImage.id) ? "id='" + oImage.id + "' " : "";
		var sImgClass	 = (oImage.className) ? "class='" + oImage.className + "' " : "";
		var sImgTiltle = (oImage.title) ? "title='" + oImage.title + "' " : "title='" + oImage.alt + "' ";
		var sImgStyle  = "display:inline-block;" + oImage.style.cssText;
		if(oImage.align == "left")		sImgStyle = "float:left;"  + sImgStyle;
		if(oImage.align == "right")   sImgStyle = "float:right;" + sImgStyle;
		if(oImage.parentElement.href) sImgStyle = "cursor:hand;" + sImgStyle;
		var sHtml = "<span " + sImgID + sImgClass + sImgTiltle;
		sHtml += " style=\"" + "width:" + oImage.width + "px; height:" + oImage.height + "px;" + sImgStyle + ";";
		sHtml += " filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";
		sHtml += "(src=\'" + oImage.src + "\', sizingMethod='scale');\"></span>";
		oImage.outerHTML = sHtml;
	}
	catch(e)
	{
		g_oLogger.Error("Error in ApplyIeFilter("+oImage+"): " + e.message);
	}
} // ApplyIeFilter
//-----------------------------------------------------------------------
function FadeOut(sDivId,nOpacity,nFTime,fnHide) // Fade out 100%,90%,...,10% and then call the fnHide callback function.
{
	var oDiv = document.getElementById(sDivId);
	if(!IsObject(oDiv))
		return;

	// Validate IN params and set defaults.
	nOpacity = !isNaN(nFTime)	? (nOpacity > 100) ? 100 : nOpacity : 0;

	// Set the div new opacite
	oDiv.style.opacity			= (nOpacity/100);
	oDiv.style.MozOpacity		= (nOpacity/100);
	oDiv.style.KhtmlOpacity = (nOpacity/100);
	
	// IE Filters.
	if(IsObject(oDiv.filters))
	{
		if(IsObject(oDiv.filters["DXImageTransform.Microsoft.Alpha"]))
			oDiv.filters["DXImageTransform.Microsoft.Alpha"].Opacity = nOpacity;
		else
			oDiv.style.filter += " progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";
	}	

	// Set the next iteration.
	nOpacity -= 10;
	
	if(nOpacity <= 0 && typeof fnHide == "function")
		setTimeout(function(){fnHide(false);},nFTime/10);
	else
		setTimeout(function(){FadeOut(sDivId,nOpacity,nFTime,fnHide);}, nFTime/10);
	
} // FadeOut

//-----------------------------------------------------------------------
function SetDivRect(DivId, oRect)
{
	var oDiv; 
	
	if(typeof DivId == "string")
		oDiv = document.getElementById(DivId);
	else 
		oDiv = DivId;
	
	if(!IsObject(oDiv) || !IsObject(oRect))
		throw new TegException(ET_InvalidArg, "SetDivRect(" + DivId +","+ oRect + ") failed with error - Invalid in parameters.");
	
	var nLeft		=	oRect.left;
	var nTop		=	oRect.top;
	var nWidth	=	oRect.width;
	var nHeight	=	oRect.height;
	
	if (GetBrowser() != "IExplorer" )
	{
		var nLRBorderWidth = 0;
		var nTBRBorderWidth = 0;
			
		if (oDiv.style.borderLeftWidth != "")
			nLRBorderWidth   += parseInt(oDiv.style.borderLeftWidth);
		if (oDiv.style.borderRightWidth != "")
			nLRBorderWidth  += parseInt(oDiv.style.borderRightWidth);
		if (oDiv.style.borderTopWidth != "")
			nTBRBorderWidth		 += parseInt(oDiv.style.borderTopWidth);
		if (oDiv.style.borderBottomWidth != "")
			nTBRBorderWidth += parseInt(oDiv.style.borderBottomWidth);
    	
		nWidth  -= nLRBorderWidth;
		nHeight -= nTBRBorderWidth;
	}	
		
	oDiv.style.left		= nLeft;
	oDiv.style.top		= nTop;
	oDiv.style.width	= ((nWidth  <= 1) ? 1 : nWidth) + "px";
	oDiv.style.height   = ((nHeight <= 1) ? 1 : nHeight) + "px";
	    
} // SetDivRect
//-----------------------------------------------------------------------


