﻿function mfnPopUpShow()
{
	if (top.document.readyState != 'complete')
	{
		return;
	}

	var oDIV = document.getElementById('div_' + this.Name);
	if (oDIV)
	{
		oDIV.parentNode.removeChild(oDIV);
	}

	var oIF = document.getElementById('if_' + this.Name);
	if (oIF)
	{
		oIF.parentNode.removeChild(oIF);
	}

	var iLeft;
	var iTop;

	if (this.Left == 0 && this.Top == 0)
	{
		iLeft = parseInt((document.body.clientWidth - this.Width) / 2);
		iTop = parseInt((screen.availHeight - this.Height) / 2) + document.documentElement.scrollTop + document.body.scrollTop;
	}
	else
	{
		iLeft = this.Left;
		iTop = this.Top;
	}

	var iPadding = 0;
	var iBorder = 0;

	if (this.Border != '' && /(\d+)px/.test(this.Border))
	{
		iBorder = parseInt(RegExp.$1);
	}

	// Creating the parent div element
	var sDIVHTML = document.createElement('div');
	sDIVHTML.setAttribute('id','div_' + this.Name);
	sDIVHTML.style['backgroundColor'] = this.BGColor;
	sDIVHTML.style['position'] = 'absolute';
	sDIVHTML.style['top'] = iTop + 'px';
	sDIVHTML.style['left'] = iLeft + 'px';
	sDIVHTML.style['width'] = this.Width + 'px';
	sDIVHTML.style['height'] = this.Height + 'px';
	sDIVHTML.style['zIndex'] = 10;
	sDIVHTML.style['visibility'] = 'hidden';
	
	// Creating the inner div
	var innerDiv = document.createElement('div');
	innerDiv.setAttribute('style',
						  'padding: ' + iPadding + 
						  ' height: ' + (this.Height - (iPadding + iBorder) * 2) + 'px;' + 
						  ((this.Border == '') ? '' : (' BORDER: ' + this.Border + ';')));
	
	// Creating the div of the image link
	var linkDiv = document.createElement('div');
	linkDiv.setAttribute('align','left');
	
	// Creating the link itself
	var linkElement = document.createElement('a');
	linkElement.setAttribute('href','#');
	linkElement.setAttribute('onclick',this.Name + '.Hide(); return false;');
	
	// Creating the image, that will serve as the link button
	var linkImage = document.createElement('img');
	linkImage.setAttribute('src','/App_Themes/MySayTheme/images/general/x.gif');
	linkImage.setAttribute('width','28');
	linkImage.setAttribute('height','28');
	linkImage.setAttribute('border','0');
	linkImage.setAttribute('alt','סגור חלון');
	
	// Accomodating the image inside the link
	linkElement.appendChild(linkImage);
	
	// Creating the text table
	var tableElement = document.createElement('table');
	tableElement.setAttribute('width','100%');
	tableElement.setAttribute('height',(this.Height - (iPadding + iBorder) * 2 - 56));
	tableElement.setAttribute('cellpadding','0');
	tableElement.setAttribute('cellspacing','0');
	
	// Creating a row
	var tableRow = document.createElement('tr');
	
	// Creating the cell, that will display the text
	var tableCell = document.createElement('td');
	tableCell.setAttribute('align','center');
	tableCell.setAttribute('style','color: ' + this.TextColor + '; FONT: 14px Arial;');
	tableCell.innerHTML = this.Text;
	
	// Inserting the cell into the row
	tableRow.appendChild(tableCell);
	
	// Inserting the row into the table
	tableElement.appendChild(tableRow);
	
	// Setting the link element into its div
	linkDiv.appendChild(linkElement);
	
	// Inserting the link and the table into the inner div
	innerDiv.appendChild(linkDiv);
	innerDiv.appendChild(tableElement);
	
	// Inserting the inner div into the parent div
	sDIVHTML.appendChild(innerDiv);
	
	// Creating the frame element 
	var sIFHTML = document.createElement('iframe');
	sIFHTML.setAttribute('id','if_' + this.Name);
	sIFHTML.setAttribute('frameBorder','0');
	sIFHTML.setAttribute('scrolling','no');
	sIFHTML.style['width'] = this.Width + 'px';
	sIFHTML.style['height'] = this.Height + 'px';
	sIFHTML.style['position'] = 'absolute';
	sIFHTML.style['top'] = iTop + 'px';
	sIFHTML.style['left'] = iLeft + 'px';
	sIFHTML.style['zIndex'] = -10;
	
	// Saving the values as strings because the object itself will dissapear
	// (this.Name will be null when called from onload)
	var name = this.Name.toString();
	var fitHeight = this.FitHeight.toString();
	
	if (this.URL != '')  
	{
        // Submitting the frame onload event	
	    eventPush(sIFHTML,'load',function () {frameOnloadFunction(name,fitHeight);});
	}

	/*var sDIVHTML = '<div id="div_' + this.Name + '" style="POSITION: absolute; TOP: ' + iTop + 'px; LEFT: ' + iLeft + 'px; WIDTH: ' + this.Width + 'px; HEIGHT: ' + this.Height + 'px; Z-INDEX: 10; VISIBILITY: hidden; BACKGROUND-COLOR: ' + this.BGColor + '"><div style="PADDING: ' + iPadding + 'px; HEIGHT: ' + (this.Height - (iPadding + iBorder) * 2) + 'px;' + ((this.Border == '') ? '' : (' BORDER: ' + this.Border + ';')) + '"><div align="left"><a href="#" onclick="' + this.Name + '.Hide(); return false;"><img src="/App_Themes/MySayTheme/images/general/x.gif" width="28" height="28" border="0" alt="סגור חלון"></a></div><table width="100%" height="' + (this.Height - (iPadding + iBorder) * 2 - 56) + '" cellpadding="0" cellspacing="0"><tr><td  style="COLOR: ' + this.TextColor + '; FONT: 14px Arial">' + this.Text + '</td></tr></table></div></div>';*/
	/*var sIFHTML = '<iframe id="if_' + this.Name + '" style="POSITION: absolute; TOP: ' + iTop + 'px; LEFT: ' + iLeft + 'px; WIDTH: ' + this.Width + 'px; HEIGHT: ' + this.Height + 'px; Z-INDEX: -10;" ' + ((this.URL != '') ? ('" onload="if (' + this.Name + ') {' + this.Name + '.RevealIF(); }"') : '') + '" frameBorder="0" scrolling="no" /></iframe>';*/

	// Proprietory IE method. isnt supported in any standard browser
	// document.body.insertAdjacentHTML('beforeEnd', sDIVHTML);
	document.body.appendChild(sDIVHTML); 
	oDIV = document.getElementById('div_' + this.Name);
	this.Div = oDIV;

	// Proprietory IE method. isnt supported in any standard browser
	// document.body.insertAdjacentHTML('beforeEnd', sIFHTML);
	document.body.appendChild(sIFHTML); 
	oIF = document.getElementById('if_' + this.Name);
	this.IF = oIF.contentWindow;

	if (this.URL)
	{
		oIF.contentWindow.location.replace(this.URL);
	}

	oDIV.style.visibility = 'visible';
}

// Another variation of the on load function.
// It was made because of a bug in IE,
// that doesnt allow to set onload event to an iFrame by JS
function frameOnloadFunction(name, fitHeight)
{
    var oDIV = document.getElementById('div_' + name);
	var oIF = document.getElementById('if_' + name);

	if (!oDIV || !oIF) 
	{
		oPopUp = eval('top.' + name);
		if (oPopUp)
		{
			oPopUp.Hide();
		}
		return;
	}

	oDIV.style.visibility = 'hidden';
	oIF.style.zIndex = 10;

	if (fitHeight == 'true' && oIF.contentWindow.document.body)
	{
	    // Checking if the browser is IE to add the border height
	    if (navigator.appName == 'Microsoft Internet Explorer')
	    {
		    oIF.style.height = oIF.contentWindow.document.body.scrollHeight + 10 + 'px';
		}
		else
		{
		    oIF.style.height = oIF.contentWindow.document.body.offsetHeight + 'px';
		}
		
		oDIV.style.height = oIF.style.height;
	}
}

// A cross-browser method to submit an event
function eventPush(obj, event, handler) {
  if (obj.addEventListener) {
     obj.addEventListener(event, handler, false);
  } else if (obj.attachEvent) {
     obj.attachEvent('on'+event, handler);
  }
}

function mfnPopUpHide()
{
	var oDIV = document.getElementById('div_' + this.Name);

	if (oDIV)
	{
		// Removing the picture element from its father
		oDIV.parentNode.removeChild(oDIV);
	}

	var oIF = document.getElementById('if_' + this.Name);

	if (oIF)
	{
		// Removing the frame element from its father
		oIF.parentNode.removeChild(oIF);
	}

	setTimeout('if (top.' + this.Name + '){ top.' + this.Name + ' = null; }', 10);
}

/*function mfnRevealIF()
{
alert('entered');
	var oDIV = document.getElementById('div_' + this.Name);
	var oIF = document.getElementById('if_' + this.Name);

	if (!oDIV || !oIF)
	{
		this.Hide();
		return;
	}

	oDIV.style.visibility = 'hidden';
	oIF.style.zIndex = 10;
	alert(oIF.contentWindow.document.body.scrollHeight);

	if (this.FitHeight && oIF.contentWindow.document.body)
	{
		oIF.style.height = oIF.contentWindow.document.body.scrollHeight + 'px';
		oDIV.style.height = oIF.style.height;
	}
}*/

function mfnFit()
{
	var oDIV = document.getElementById('div_' + this.Name);
	var oIF = document.getElementById('if_' + this.Name);

	oIF.style.height = oIF.contentWindow.document.body.scrollHeight + 10 + 'px';
	oDIV.style.height = oIF.style.height;
}

function mfnResizeBy(iWidth, iHeight)
{
	var oDIV = document.getElementById('div_' + this.Name);
	var oIF = document.getElementById('if_' + this.Name);

	// Switched pixelWidth with simple width
	oIF.style.width =  parseInt(oIF.style.width) + iWidth  + 'px';
	oDIV.style.width = oIF.style.width;

	// Switched pixelHeight with simple height
	oIF.style.height = parseInt(oIF.style.height) + iHeight + 'px';
	oDIV.style.height = oIF.style.height;

	if (this.FitHeight)
	{
		// Switched pixelHeight with simple height
		this.IF.document.body.style.height =  parseInt(this.IF.document.body.style.height) + iHeight + 'px';
	}
}

function mfnAdjustPosition(iPosEnum, iOffsetX, iOffsetY, oFrame)
{
	var oEPos = fnGetElemPosition(this.SrcElement);

	this.Left = ((iPosEnum == I_POS_REL_RTL) ? (oEPos.x + this.SrcElement.offsetWidth - this.Width) : oEPos.x) + iOffsetX;
	this.Top = oEPos.y + this.SrcElement.offsetHeight + iOffsetY;

	if (oFrame)
	{
		this.Left += oFrame.offsetLeft;
		this.Top += oFrame.offsetTop;
	}
	
	if (this.Left < 5)
	{
		this.Left = 5;
	}
}

function ctPopUp(sName)
{
	this.Name = sName;
	this.Text = '';
	this.BGColor = '#990000';
	this.TextColor = '#FFFFFF';
	this.URL = '';
	this.Border = '';
	this.Width = 265;
	this.Height = 180;
	this.FitHeight = false;
	this.Left = 0;
	this.Top = 0;
	this.IF = null;
	this.Div = null;
	this.SrcElement = null;
	this.Show = mfnPopUpShow;
	this.Hide = mfnPopUpHide;
	//this.RevealIF = mfnRevealIF;
	this.Fit = mfnFit;
	this.ResizeBy = mfnResizeBy;
	this.AdjustPosition = mfnAdjustPosition;
}
