<PUBLIC:COMPONENT
    lightWeight? true
>

<PUBLIC:DEFAULTS
    contentEditable? false
    tabStop? true
/>

<PUBLIC:attach event="ondocumentready" onevent="initElement()" />
<PUBLIC:attach event="ondetach" onevent="cleanupElement()" />

<PUBLIC:method   name="createWindow" />
<PUBLIC:method   name="destroyWindow" />
<PUBLIC:method   name="sizeWindow" />
<PUBLIC:method   name="setWindowSource" />
<PUBLIC:method   name="setWindowVisibility" />
<PUBLIC:method   name="closeWindow" />
<PUBLIC:method   name="closeWindows" />
<PUBLIC:method   name="cascadeWindows" />
<PUBLIC:method   name="tileWindowsH" />
<PUBLIC:method   name="tileWindowsV" />
<PUBLIC:method   name="getWindowByCaption" />

<PUBLIC:property name="windowCount" value />
<PUBLIC:property name="topWindowzIndex" value />
<PUBLIC:property name="parentBorder" value="" />

<script language="jscript">

// ----------------------------------------------------
// 2k3Widgets: 2k3Windows
// 
// Copyright ?002-2004 Stedy Software and Systems     
// Please see http://www.stedy.com for terms of use.   
// ----------------------------------------------------

var isIE6 = ((window.navigator.appVersion).indexOf("MSIE 6.") != -1) ;

var windowCollection = null ;

var windowUniqueId = 0 ;
var windowURL = "" ;
var windowImage = "" ;
var windowTop ;
var windowLeft ;
var windowHeight ;
var windowWidth ;
var settingStates = false ;

function initElement() {
	windowCollection = new ActiveXObject("Scripting.Dictionary") ;
	with (element) {
		windowCount = 0 ;
		topWindowzIndex = 0 ;
		with (parentElement.currentStyle) {
			var strBorder = borderStyle + " " + borderWidth + " " + borderColor ;
		}
		parentBorder = strBorder ;
	}
	window.document.styleSheets.item(0).owningElement.attachEvent("onload", elementStyleChange) ;
}

function cleanupElement() {
	windowCollection = null ;
	return window.document.styleSheets.item(0).owningElement.detachEvent("onload", elementStyleChange) ;
}

function elementStyleChange() {
	with (element) {
		with (parentElement.currentStyle) {
			var strBorder = "solid 1px " + borderColor ;
		}
		parentBorder = strBorder ;
	}
}

function createWindow(url, img, iTop, iLeft, iHeight, iWidth) {
	var objKeys = (windowCollection.Keys()).toArray() ;
	for (var i = windowCollection.Count - 1; i >= 0; i--) {
		if (windowCollection.Exists(objKeys[i])) {
			var objWindow = windowCollection.Item(objKeys[i]) ;
			var newUrl = url ;
			var windowUrl = objWindow.src ;
			if (newUrl.indexOf("date=") != -1) {
				newUrl = newUrl.substr(0, newUrl.indexOf("date=")) ;
			}
			if (windowUrl.indexOf("date=") != -1) {
				windowUrl = windowUrl.substr(0, windowUrl.indexOf("date=")) ;
			}
			if (windowUrl == newUrl) {
				objWindow.setActive() ;
				return objWindow.id ;
			}
		}
	}
	var uId = getNewWindowUniqueId() ;
	windowURL = url ;
	if (img == null || img == "") {
		windowImage = element.currentStyle.defaultImageUrl ;
	}
	else {
		windowImage = img ;
	}
	windowTop = iTop ;
	windowLeft = iLeft ;
	windowHeight = iHeight ;
	windowWidth = iWidth ;
	var objWindow = window.document.createElement("<DIV>") ;
	with (objWindow) {
		id = "2k3Window_" + uId ;
		className = "2k3Window" ;
		style.visibility = "hidden" ;
		innerHTML = "<div class='windowCaption' id='capText_" + uId + "'></div><img class='captionImage' src='" + windowImage + "' width='16' height='16'>"
				  + "<span class='windowButtonMinimize' id='btnMin_" + uId + "'><img src='" + element.currentStyle.minimizeImageUrl + "' width='13' height='11'></span>"
				  + "<span class='windowButtonMaximize' id='btnMax_" + uId + "'><img src='" + element.currentStyle.maximizeImageUrl + "' width='13' height='11'></span>"
				  + "<span class='windowButtonClose' id='btnClose_" + uId + "'><img src='" + element.currentStyle.closeImageUrl + "' width='13' height='11'></span>"
				  + "<iframe class='windowContent' id='winContent_" + uId + "'></iframe>"
	}
	element.parentElement.insertAdjacentElement("beforeEnd", objWindow) ;
	addWindow(objWindow.id, objWindow, window.document.all("btnClose_" + uId)) ;
	return objWindow.id ;
}

function destroyWindow() {
	with (window.event) {
		if (button == 2) {
			return ;
		}
		var objCloseButton = srcElement ;
	}
	var objWindow = objCloseButton.parentElement ;
	if (!windowCollection.Exists(objWindow.id)) {
		objWindow = objWindow.parentElement ;
	}
	if (windowCollection.Exists(objWindow.id)) {
		var uId = getWindowUniqueId(objWindow.id) ;
		var objWindowMenuItem = window.document.getElementById("windowMenuItem_" + uId) ;
		objWindowMenuItem.detachEvent("onmouseup", windowMenuClick) ;
		objWindowMenuItem.parentElement.deleteRow(objWindowMenuItem.rowIndex - 1) ;
		objCloseButton.detachEvent("onmouseup", destroyWindow) ;
		objWindow.detachEvent("onsetwindowstate", windowStateSet) ;
		removeWindow(objWindow.id) ;
		objWindow.innerHTML = "" ;
		objWindow.removeNode(true) ;
	}
	if (element.windowCount == 0) {
		element.parentElement.style.border = element.parentBorder ;
	}
}

function addWindow(strName, objWindow, objCloseButton) {
	if (!windowCollection.Exists(strName)) {
		windowCount++ ;
		windowCollection.Add(strName, strName) ;
		windowCollection.Item(strName) = objWindow ;
		objCloseButton.attachEvent("onmouseup", destroyWindow) ;
		objWindow.attachEvent("onreadystatechange", windowReady) ;
	}
}

function removeWindow(strName) {
	if (windowCollection.Exists(strName)) {
		windowCount-- ;
		windowCollection.Item(strName) = null ;
		windowCollection.Remove(strName) ;
	}
}

function windowReady() {
	var objWindow = window.event.srcElement ;
	with (objWindow) {
		detachEvent("onreadystatechange", windowReady) ;
		var uId = getWindowUniqueId(id) ;
		with (parentElement) {
			var pHeight = offsetHeight ;
			var pWidth = offsetWidth ;
		}
	}
	var c = windowCollection.Count - 1 ;
	if (windowTop == null) { var winTop = (c * 25) ; } else { var winTop = windowTop ; }
	if (windowLeft == null) { var winLeft = (c * 10) ; } else { var winLeft = windowLeft ; }
//´°¿Ú´óÐ¡
//	if (windowHeight == null) { var winHeight = ((pHeight / 6) * 5.5) ; } else { var winHeight = windowHeight ; }
//	if (windowWidth == null) { var winWidth = ((pWidth / 6) * 5.5) ; } else { var winWidth = windowWidth ; }
	if (windowHeight == null) { var winHeight = ((pHeight / 6) * 5.9) ; } else { var winHeight = windowHeight ; }
	if (windowWidth == null) { var winWidth = ((pWidth / 6) * 5.9) ; } else { var winWidth = windowWidth ; }
	var sizeError = !sizeWindow(objWindow, winTop, winLeft, winHeight, winWidth) ;
	if (sizeError) {
		alert("An Error has occured creating a new Window!" +
		      "  Window readyState = " + objWindow.readyState +
		      "  contentWindow readyState = " + window.document.all("winContent_" + uId).readyState) ;
		removeWindow(objWindow.id) ;
		objWindow.removeNode(true) ;
		return ;
	}
	var objMenuItem = insertNewMenuItem(window.document.all("windowMenu"), "windowMenuItem_" + uId, windowImage, "New Window") ;
	setWindowSource(objWindow, windowURL) ;
	setWindowVisibility(objWindow, true) ;
	objWindow.parentId = element.id ;
	objMenuItem.attachEvent("onmouseup", windowMenuClick) ;
	objWindow.attachEvent("onsetwindowstate", windowStateSet) ;
}

function insertNewMenuItem(objMenu, mnuId, mnuImage, mnuCaption) {
	var objNewMenuItem = objMenu.tBodies[0].insertRow() ;
	with (objNewMenuItem) {
		id = mnuId ;
		className = "2k3MenuItem" ;
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerLeft" ;
		}
		with (insertCell()) {
			className = "2k3MenuImage" ;
			innerHTML = "<img src='" + mnuImage + "' height='16' width='16'>" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			innerHTML = mnuCaption ;
		}
		with (insertCell()) {
			className = "2k3MenuNoMore" ;
			innerHTML = "4" ;
		}
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerRight" ;
		}
	}
	return objNewMenuItem ;
}

function windowMenuClick() {
	var objWindowMenuItem = getRealItem(window.event.srcElement) ;
	with (getWindowByName((objWindowMenuItem.id).replace("windowMenuItem_", "2k3Window_"))) {
		if (anyWindowsMaximized()) {
			windowState = "MAXIMIZED" ;
		}
		else {
			if (windowState == "MINIMIZED") {
				windowState = "NORMAL" ;
			}
		}
		setActive() ;
	}
}

function windowStateSet() {
	if (settingStates) {
		return ;
	}
	settingStates = true ;
	var objWindow = window.event.srcElement ;
	var tmpWindowState = objWindow.windowState ;
	if (tmpWindowState != "MINIMIZED") {
		var objKeys = (windowCollection.Keys()).toArray() ;
		for (var i = windowCollection.Count - 1; i >= 0; i--) {
			if (windowCollection.Exists(objKeys[i])) {
				with (windowCollection.Item(objKeys[i])) {
					if ((tmpWindowState == "MAXIMIZED" && windowState == "NORMAL") ||
					    (tmpWindowState == "NORMAL" && windowState == "MAXIMIZED")) {
						windowsSettingState = true ;
						windowState = tmpWindowState ;
						windowsSettingState = false ;
					}
				}
			}
		}
	}
	fixMinimizedWindows() ;
	settingStates = false ;
}

function closeWindow(id) {
	var objWindow = window.document.getElementById(id) ;
	if (objWindow == null) {
		alert("2k3Window with id = '" + id + "' not found!") ;
		return false ;
	}
	try {
		var objWindowMenuItem = window.document.getElementById((objWindow.id).replace("2k3Window_", "windowMenuItem_")) ;
		objWindowMenuItem.detachEvent("onmouseup", windowMenuClick) ;
		objWindowMenuItem.parentElement.deleteRow(objWindowMenuItem.rowIndex - 1) ;
		window.document.getElementById((objWindow.id).replace("2k3Window_", "btnClose_")).detachEvent("onmouseup", destroyWindow) ;
		objWindow.detachEvent("onsetwindowstate", windowStateSet) ;
		objWindow.innerHTML = "" ;
		removeWindow(objWindow.id) ;
		objWindow.removeNode(true) ;
		return true ;
	}
	catch(e) {
		alert("Error closing window!\n" + e.message) ;
		return false ;
	} 
}

function closeWindows() {
	while (windowCollection.Count > 0) {
		var objKeys = (windowCollection.Keys()).toArray() ;
		if (windowCollection.Exists(objKeys[windowCollection.Count - 1])) {
			var objWindow = windowCollection.Item(objKeys[windowCollection.Count - 1])
			var objWindowMenuItem = window.document.getElementById((objWindow.id).replace("2k3Window_", "windowMenuItem_")) ;
			objWindowMenuItem.detachEvent("onmouseup", windowMenuClick) ;
			objWindowMenuItem.parentElement.deleteRow(objWindowMenuItem.rowIndex - 1) ;
			window.document.getElementById((objWindow.id).replace("2k3Window_", "btnClose_")).detachEvent("onmouseup", destroyWindow) ;
			objWindow.detachEvent("onsetwindowstate", windowStateSet) ;
			objWindow.innerHTML = "" ;
			removeWindow(objWindow.id) ;
			objWindow.removeNode(true) ;
		}
	}
}

function fixMinimizedWindows() {
	var minPos = 0 ;
	var minCount = 0 ;
	var minCount2 = 0 ;
	var minAcross = 0 ;
	var minStacked = 0 ;
	var objKeys = (windowCollection.Keys()).toArray() ;
	for (var i = 0; i < windowCollection.Count; i++) {
		if (windowCollection.Exists(objKeys[i])) {
			if (windowCollection.Item(objKeys[i]).windowState == "MINIMIZED") {
				if (minAcross == 0) {
					minHeight = windowCollection.Item(objKeys[i]).offsetHeight ;
					minWidth = windowCollection.Item(objKeys[i]).offsetWidth ;
					minAcross = Math.round(windowCollection.Item(objKeys[i]).parentElement.offsetWidth / minWidth)
				}
				if (minAcross == minPos) {
					minPos = 0 ;
				}
				minPos++ ;
				minCount++ ;
				if (minCount2 == minAcross) {
					minStacked++ ;
					minCount2 = 0 ;
				}
				minCount2++ ;
				windowCollection.Item(objKeys[i]).style.top = windowCollection.Item(objKeys[i]).parentElement.offsetHeight - ((minStacked + 1) * minHeight) - 5 ;
				windowCollection.Item(objKeys[i]).style.left = windowCollection.Item(objKeys[i]).parentElement.offsetLeft + ((minPos - 1) * minWidth) ;
			}
		}
	}
}

function cascadeWindows() {
	var winCount = -1 ;
	var objKeys = (windowCollection.Keys()).toArray() ;
	for (var i = 0; i < windowCollection.Count; i++) {
		if (windowCollection.Exists(objKeys[i])) {
			with (windowCollection.Item(objKeys[i])) {
				windowsSettingState = true ;
				if (windowState == "MAXIMIZED") {
					windowState = "NORMAL" ;
				}
				windowsSettingState = false ;
			}
		}
	}
	for (var i = 0; i < windowCollection.Count; i++) {
		if (windowCollection.Exists(objKeys[i])) {
			if (windowCollection.Item(objKeys[i]).windowState == "NORMAL") {
				winCount++ ;
				sizeWindow(windowCollection.Item(objKeys[i]), (winCount * 25), (winCount * 10), ((windowCollection.Item(objKeys[i]).parentElement.offsetHeight / 6) * 5), ((windowCollection.Item(objKeys[i]).parentElement.offsetWidth / 6) * 5))
				element.topWindowzIndex++ ;
				element.topWindowzIndex++ ;
				windowCollection.Item(objKeys[i]).style.zIndex = element.topWindowzIndex ;
			}
		}
	}
	fixMinimizedWindows() ;
}

function tileWindowsH() {
	var tileCount = getWindowsTileCount() ;
	var objKeys = (windowCollection.Keys()).toArray() ;
	for (var i = 0; i < windowCollection.Count; i++) {
		if (windowCollection.Exists(objKeys[i])) {
			with (windowCollection.Item(objKeys[i])) {
				windowsSettingState = true ;
				if (windowState == "MAXIMIZED") {
					windowState = "NORMAL" ;
				}
				windowsSettingState = false ;
			}
		}
	}
	if (tileCount > 1) {
		var winCount = -1 ;
		with (windowCollection.Item(objKeys[0]).parentElement) {
			var winHeight = (clientHeight / tileCount) ;
			var winWidth = clientWidth ;
			var winLeftOver = clientHeight - (winHeight * tileCount) ;
		}
		for (var i = 0; i < windowCollection.Count; i++) {
			if (windowCollection.Exists(objKeys[i])) {
				with (windowCollection.Item(objKeys[i])) {
				}
				if (windowCollection.Item(objKeys[i]).windowState == "NORMAL") {
					winCount++ ;
					sizeWindow(windowCollection.Item(objKeys[i]), winCount * winHeight, 0, winHeight + (Math.abs((i == (windowCollection.Count - 1))) * winLeftOver), winWidth) ;
					element.topWindowzIndex++ ;
					element.topWindowzIndex++ ;
					windowCollection.Item(objKeys[i]).style.zIndex = element.topWindowzIndex ;
				}
			}
		}
	}
	else {
		if (tileCount == 1) {
			for (var i = 0; i < windowCollection.Count; i++) {
				if (windowCollection.Exists(objKeys[i])) {
					var winItem = windowCollection.Item(objKeys[i]) ;
					with (winItem) {
						if (windowState == "NORMAL") {
							sizeWindow(winItem, 0, 0, parentElement.clientHeight, parentElement.clientWidth) ;
							element.topWindowzIndex++ ;
							element.topWindowzIndex++ ;
							style.zIndex = element.topWindowzIndex ;
							break ;
						}
					}
				}
			}
		}
	}
	fixMinimizedWindows() ;
}

function tileWindowsV() {
	var tileCount = getWindowsTileCount() ;
	var objKeys = (windowCollection.Keys()).toArray() ;
	for (var i = 0; i < windowCollection.Count; i++) {
		if (windowCollection.Exists(objKeys[i])) {
			with (windowCollection.Item(objKeys[i])) {
				windowsSettingState = true ;
				if (windowState == "MAXIMIZED") {
					windowState = "NORMAL" ;
				}
				windowsSettingState = false ;
			}
		}
	}
	if (tileCount > 1) {
		var winCount = -1 ;
		with (windowCollection.Item(objKeys[0]).parentElement) {
			var winHeight = clientHeight ;
			var winWidth = (clientWidth / tileCount) ;
			var winLeftOver = clientWidth - (winWidth * tileCount) ;
		}
		for (var i = 0; i < windowCollection.Count; i++) {
			if (windowCollection.Exists(objKeys[i])) {
				with (windowCollection.Item(objKeys[i])) {
				}
				if (windowCollection.Item(objKeys[i]).windowState == "NORMAL") {
					winCount++ ;
					sizeWindow(windowCollection.Item(objKeys[i]), 0, winCount * winWidth, winHeight, winWidth + (Math.abs((i == (windowCollection.Count - 1))) * winLeftOver)) ;
					element.topWindowzIndex++ ;
					element.topWindowzIndex++ ;
					windowCollection.Item(objKeys[i]).style.zIndex = element.topWindowzIndex ;
				}
			}
		}
	}
	else {
		if (tileCount == 1) {
			for (var i = 0; i < windowCollection.Count; i++) {
				if (windowCollection.Exists(objKeys[i])) {
					var winItem = windowCollection.Item(objKeys[i]) ;
					with (winItem) {
						if (windowState == "NORMAL") {
							sizeWindow(winItem, 0, 0, parentElement.clientHeight, parentElement.clientWidth) ;
							element.topWindowzIndex++ ;
							element.topWindowzIndex++ ;
							style.zIndex = element.topWindowzIndex ;
							break ;
						}
					}
				}
			}
		}
	}
	fixMinimizedWindows() ;
}

function getWindowByName(strName) {
	return windowCollection.Item(strName) ;
}

function getWindowByCaption(strCaption) {
	var objKeys = (windowCollection.Keys()).toArray() ;
	for (var i = 0; i < windowCollection.Count; i++) {
		var objWindow = windowCollection.Item(objKeys[i]) ;
		if (objWindow.caption == strCaption) {
			return objWindow ;
		}
	}
	return null ;
}

function getNewWindowUniqueId() {
	windowUniqueId++ ;
	return windowUniqueId ;
}

function getWindowUniqueId(windowId) {
	return windowId.replace("2k3Window_", "") ;
}

function getWindowsTileCount() {
	var winCount = 0 ;
	var objKeys = (windowCollection.Keys()).toArray() ;
	for (var i = windowCollection.Count - 1; i >= 0; i--) {
		if (windowCollection.Exists(objKeys[i])) {
			if (windowCollection.Item(objKeys[i]).windowState != "MINIMIZED") {
				winCount++ ;
			}
		}
	}
	return winCount ;
}

function anyWindowsMaximized() {
	var objKeys = (windowCollection.Keys()).toArray() ;
	for (var i = windowCollection.Count - 1; i >= 0; i--) {
		if (windowCollection.Exists(objKeys[i])) {
			if (windowCollection.Item(objKeys[i]).windowState == "MAXIMIZED") {
				return true ;
			}
		}
	}
	return false ;
}

function sizeWindow(objWindow, t, l, h, w) {
	with (objWindow) {
		top = t ;
		left = l ;
		height = h ;
		width = w ;
//		if (anyWindowsMaximized()) { 
			windowState = "MAXIMIZED" ;
//		}
	}
	return true ;
}

function setWindowSource(objWindow, url) {
	objWindow.src = url ;
}

function setWindowVisibility(objWindow, bVisible) {
	if (bVisible) {
		visString = "visible" ;
	}
	else {
		visString = "hidden" ;
	}
	with (objWindow) {
		element.topWindowzIndex++ ;
		element.topWindowzIndex++ ;
		style.visibility = visString ;
		style.zIndex = element.topWindowzIndex ;
		setActive() ;
	}
}

function getRealItem(el) {
	if (el != null) {
		if (el.tagName == "IMG") {
			return el.parentElement.parentElement.parentElement ;
		}
		if (el.tagName == "B") {
			el = el.parentElement ;
		}
		switch (el.className) {
			case "2k3MenuVerticalSpacerLeft":
				return el.parentElement ;
				break ;
			case "2k3MenuImage":
				return el.parentElement ;
				break ;
			case "2k3MenuCaption":
				return el.parentElement ;
				break ;
			case "2k3MenuMore":
				return el.parentElement ;
				break ;
			case "2k3MenuNoMore":
				return el.parentElement ;
				break ;
			case "2k3MenuVerticalSpacerRight":
				return el.parentElement ;
				break ;
			default:
				return el ;
				break ;
		}
	}
	else {
		return el ;
	}
}

</script>

</PUBLIC:COMPONENT>