/*
TimothyHumphrey.WebControls
Copyright (c) 2003 - 2004 Timothy Humphrey

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

//-------------------- thwcVisibleCH_IEWindowedCover --------------------\\
thwcVisibleCH_IEWindowedCover_CoverMethod = {
	Cover : 0
	,Hide : 1
}

thwcVisibleCH_IEWindowedCover = function() {
	this.base = Handler;
	this.base();
	delete this.base;

	this.CoverMethod;
	this.HideSave;
	this.IFrame = null;
	this.Windows = [];
	this.WindowsChecked = false;
}
thwcVisibleCH_IEWindowedCover.prototype = new Handler;
thwcVisibleCH_IEWindowedCover.prototype.constructor = thwcVisibleCH_IEWindowedCover;

thwcVisibleCH_IEWindowedCover.CoverSupport = parseFloat(/MSIE (\d+\.\d+)/.exec(navigator.userAgent)[1]) >= 5.5;
thwcVisibleCH_IEWindowedCover.IFrames = [];
thwcVisibleCH_IEWindowedCover.References = 0;

thwcVisibleCH_IEWindowedCover.CreateIFrame = function() {
	var id = "thwcVisibleCH_IEWindowedCoverIFrame" + thwcVisibleCH_IEWindowedCover.IFrames.length;
	var iframe = "<iframe" +
		" id='" + id + "'" +
		" src='javascript:\'\'' frameborder='0' scrolling='no' style='display:none; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);'" +
		"></iframe>";

	document.body.insertAdjacentHTML("afterBegin", iframe);
	return thwcVisibleCH_IEWindowedCover.IFrames[thwcVisibleCH_IEWindowedCover.IFrames.length] = Browser.GetElement(id);
}

thwcVisibleCH_IEWindowedCover.prototype.Load = function() {
	var data = this.Control.Data.thwcVisibleCH_IEWindowedCover;

	this.CoverMethod = data[0];
	this.HideSave = data[1];

	if(this.CoverMethod == thwcVisibleCH_IEWindowedCover_CoverMethod.Cover && !thwcVisibleCH_IEWindowedCover.CoverSupport)
		this.CoverMethod = thwcVisibleCH_IEWindowedCover_CoverMethod.Hide;

	if(thwcVisibleCH_IEWindowedCover.CoverSupport && this.CoverMethod == thwcVisibleCH_IEWindowedCover_CoverMethod.Cover) {
		while(thwcVisibleCH_IEWindowedCover.IFrames.length < 2)
			thwcVisibleCH_IEWindowedCover.CreateIFrame();
	}

	thwcVisibleCH_IEWindowedCover.References++;
}

thwcVisibleCH_IEWindowedCover.prototype.Play = function(initiator, visible) {
	var element = this.Control.Element;
	var iframe;
	var windows;
	var window;
	var location = this.Control.GetAlignmentPoint();
	var menuBounds;
	var i, iWindows, iElements;

	switch(this.CoverMethod) {
	case thwcVisibleCH_IEWindowedCover_CoverMethod.Cover:
		if(visible) {
			for(i = 0; i < thwcVisibleCH_IEWindowedCover.IFrames.length; i++) {
				if(thwcVisibleCH_IEWindowedCover.IFrames[i].style.display == "none") {
					iframe = thwcVisibleCH_IEWindowedCover.IFrames[i];
					break;
				}
			}

			if(!iframe)
				iframe = thwcVisibleCH_IEWindowedCover.CreateIFrame();

			this.IFrame = iframe;
			iframe.style.visibility = "hidden";
			iframe.style.display = "block";
			Browser.Move(iframe, location);
			iframe.style.width = element.offsetWidth + "px";
			iframe.style.height = element.offsetHeight + "px";
			iframe.style.zIndex = element.style.zIndex - 1;
			iframe.style.visibility = "";
		}
		else
			this.IFrame.style.display = "none";
		break;
	case thwcVisibleCH_IEWindowedCover_CoverMethod.Hide:
		if(visible) {
			if(!(this.WindowsChecked && this.HideSave)) {
				this.Windows.length = 0;
				menuBounds = new Rect(location.X, location.Y, element.offsetWidth, element.offsetHeight);
				windows = [].concat(document.body.all.tags("SELECT"), document.body.all.tags("OBJECT"));

				for(iWindows = 0; iWindows < windows.length; iWindows++) {
					for(iElements = 0; iElements < windows[iWindows].length; iElements++) {
						window = windows[iWindows][iElements];
						if(menuBounds.Intersects(Browser.GetDimensions(window)))
							this.Windows[this.Windows.length] = window;
					}
				}

				this.WindowsChecked = true;
			}

			for(i = 0; i < this.Windows.length; i++)
				this.Windows[i].style.visibility = "hidden";
		}
		else {
			for(i = 0; i < this.Windows.length; i++)
				this.Windows[i].style.visibility = "";
		}
		break;
	}

	return true;
}

thwcVisibleCH_IEWindowedCover.prototype.Unload = function() {
	if(document.body.removeChild) {
		thwcVisibleCH_IEWindowedCover.References--;
		if(thwcVisibleCH_IEWindowedCover.References == 0) {
			while(thwcVisibleCH_IEWindowedCover.IFrames.length > 0) {
				document.body.removeChild(thwcVisibleCH_IEWindowedCover.IFrames[thwcVisibleCH_IEWindowedCover.IFrames.length - 1]);
				thwcVisibleCH_IEWindowedCover.IFrames.length--;
			}
		}
	}
}

