addLoadEvent(FlexResizer);
window.onresize = FlexResizer;


function $(id) {
	return document.getElementById(id);
}

function addLoadEvent(func) {

	var oldonload = window.onload;
	if (browserID() == 'msie') {
		document.documentElement.onload = func;
		window.onload = func;
	}
	else if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function browserID() {
	var useragent = navigator.userAgent.toLowerCase();
	var knowBR = ['opera', 'msie', 'safari', 'gecko'];

	for (var i = 0; i < knowBR.length; i++)
		if (useragent.indexOf(knowBR[i]) != -1)
		return knowBR[i];
}

function FlexResizer() {

	ModuleResizerA(null, "FlexModule", 3);
	ModuleResizerA(null, "FlexModuleInner", 2);

	if (lastOpened != null) {
		ModuleResizerA(lastOpened, "content", 18);
		//ModuleResizerA(lastOpened, "treeViewUSR", 8);
		//ModuleResizerA(lastOpened, "treeView", 8);
		//ModuleResizerA(lastOpened, "ctl00_DefaultContent_ctrlStatistics_ctrlSelectionType_upSelectionType", 8);
	}

}

function ModuleResizerA(iframe, id, flags) {

	var object = null, windowWidth = 0, windowHeight = 0;

	if (iframe == null) {
		object = document.getElementById(id);
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else {
		if (iframe.contentDocument != null && iframe.contentDocument.documentElement != null)
			iDoc = iframe.contentDocument.documentElement;
		else if (iframe.contentWindow != null && iframe.contentWindow.document != null)
			iDoc = iframe.contentWindow.document.documentElement;
		else iDoc = null;

		object = (iframe.contentWindow != null && iframe.contentWindow.document != null) ? iframe.contentWindow.document.getElementById(id) : null;
		if (iDoc) {
			windowWidth = iDoc.clientWidth;
			windowHeight = iDoc.clientHeight;
		}
	}

	if (object != null) {
		var offsetTrail = object;
		var offsetLeft = 0;
		var offsetTop = 0;
		while (offsetTrail) {
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}

		var newObjectWidth = windowWidth - offsetLeft;
		var newObjectHeight = windowHeight - offsetTop;
		if (flags & 16) {
			if (iframe.paramSH == null) iframe.paramSH = object.scrollHeight;
			newObjectHeight = newObjectHeight > iframe.paramSH ? newObjectHeight : iframe.paramSH;
		}
		if (flags & 1)
			object.style.width = newObjectWidth + "px";
		if (flags & 2)
			object.style.height = newObjectHeight + "px";
		if (flags & 8) {
			object.style.minHeight = newObjectHeight + "px";
			object.style.maxHeight = newObjectHeight + "px";
		}
		if (flags & 32) {
			//if (iframe.paramSW == null) iframe.paramSW = object.scrollWidth;
			//newObjectHeight = newObjectHeight > iframe.paramSH ? newObjectHeight : iframe.paramSH;
			object.style.width = object.scrollWidth + "px";
		}
	}
}

function ShowHide(id) {
	var elm = document.getElementById(id);
	var dsp = (elm.style.display == 'none') ? '' : 'none';
	elm.style.display = dsp;
}


function ShowPopUpInfo(alt, destId) {
	var elm = document.getElementById(destId);
	window.MasterPopUpInfo = (window.MasterPopUpInfo) ? window.MasterPopUpInfo : elm.innerHTML;
	elm.innerHTML = alt;
}


function GetWindowScrollSize() {
	var heightScroll = 0;

	if (typeof (window.pageYOffset) == 'number') {
		//Netscape compliant
		heightScroll = window.pageYOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		heightScroll = document.body.scrollTop;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		heightScroll = document.documentElement.scrollTop;
	}

	return heightScroll;

}
function ABSposMidle(id) {

	var docHGT = document.documentElement.scrollHeight; // miezenie wysokosi ekranu wymaga testów

	if (document.getElementById('bigAPL')) {
		ShowHide('bigAPL');
	}
	else {
		var bigAPL = document.createElement("div");
		bigAPL.id = 'bigAPL';
		bigAPL.style.height = docHGT + "px";
		document.body.appendChild(bigAPL);
	}

	var elm = document.getElementById(id);
	document.body.insertBefore(elm, document.body.firstChild);
	var topPOS = GetWindowScrollSize() + document.documentElement.clientHeight / 2 - elm.clientHeight / 2;
	elm.style.top = topPOS + 'px';

}


function ABSposMidleNoInsertBefore(id) {

	var docHGT = document.documentElement.scrollHeight; // miezenie wysokosi ekranu wymaga testów

	if (document.getElementById('bigAPL')) {
		ShowHide('bigAPL');
	}
	else {
		var bigAPL = document.createElement("div");
		bigAPL.id = 'bigAPL';
		bigAPL.style.height = docHGT + "px";
		document.body.appendChild(bigAPL);
	}

	var elm = document.getElementById(id);

	var topPOS = GetWindowScrollSize() + document.documentElement.clientHeight / 2 - elm.clientHeight / 2;
	elm.style.top = topPOS + 'px';

}

function WebForm_FireDefaultButton(event, target) {
	try {
		//    var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
		//    if (ie7)
		//    {
		//        document.getElementById(target).focus();
		//        document.all[target].click();
		//        event.returnValue=false;
		//        return;
		//    }

		var element = event.target || event.srcElement;
		if (/*!__defaultFired &&*/event.keyCode == 13 && !(element && element.tagName.toLowerCase() == "textarea")) {
			if (element.tagName.toLowerCase() == "object") /// fix dla flasha osadzonego z wmode != window
				return false;

			var defaultButton;
			if (__nonMSDOMBrowser) {
				defaultButton = document.getElementById(target);
			}
			else {
				defaultButton = document.all[target];
			}
			if (!__nonMSDOMBrowser) {
				defaultButton.click();
				event.cancelBubble = true;
				if (event.stopPropagation) event.stopPropagation();
				return false;
			}
			else {
				defaultButton.focus();
				location.href = defaultButton.href;
				event.cancelBubble = true;
				event.stopPropagation();
				event.preventDefault();
				return false;
			}
			if (defaultButton && typeof (defaultButton.click) != "undefined") {
				__defaultFired = true;
				defaultButton.click();
				event.cancelBubble = true;
				if (event.stopPropagation) event.stopPropagation();
				return false;
			}
		}
		return true;
	}
	catch (err) {
		return false;
	}
}

var lastOpened = null;
var restorePlayerIFrame = false;
var isPlayerIFrameVisible = false;


function invokeFunctionOnChildren(parent, fname) {
	for (var i = 0; i < parent.children.length; i++) {
		var child = parent.children[i];
		if (child.tagName == "OBJECT" || child.tagName == "EMBED") {
			try { child[fname](); } catch (e) { /*alert(e + " " + child.id + "." + fname + "();");*/ }
			//alert(child.id + "." + fname + "();");
		}
	}
}

function onTabChange(from, target) {
	if (from) invokeFunctionOnChildren(from, "tabHidden");
	if (target) invokeFunctionOnChildren(target, "tabShown");
	//if (target) invokeFunctionOnChildren(target, "tabShown");
}


function loadFlex(sessionID, modulePath, moduleConfigPath, moduleName) {
	var moduleContainer = document.getElementById("FlexModule");

	if (moduleContainer) {
		var containerId = modulePath.replace(/[^a-z]*/gi, "");
		moduleName = "flex" + containerId;

		if (lastOpened && lastOpened.id == containerId) return;

		var localContainer = document.getElementById(containerId);
		var isPlayerTab = modulePath.indexOf("TabPlayer_new") >= 0;
		var isEventTab = modulePath.indexOf("TabEvent") >= 0;

		if (isPlayerTab && restorePlayerIFrame) {
			showIFrame();
			restorePlayerIFrame = false;
		}
		else if (!isPlayerTab && isPlayerIFrameVisible) {
			hideIFrame();
			restorePlayerIFrame = true;
		}


		if (!localContainer) {
			var wmode = (isPlayerTab || isEventTab) ? "opaque" : "window";
			var newdiv = document.createElement('div');
			newdiv.id = containerId;
			newdiv.style.width = '100%';
			newdiv.style.height = '100%';
			localContainer = newdiv;
			moduleContainer.appendChild(newdiv);

			AC_FL_RunContent(
                "src", "" + modulePath + "",
                "FlashVars", "sessionId=" + sessionID + '&moduleConfigPath=' + moduleConfigPath + "",
                "width", "100%",
                "height", "100%",
                "align", "middle",
                "id", "" + moduleName + "",
                "quality", "high",
                "bgcolor", "#777777",
                "wmode", wmode,
                "name", "" + moduleName + "",
                "allowScriptAccess", "always",
                "type", "application/x-shockwave-flash",
                "pluginspage", "http://www.adobe.com/go/getflashplayer",
                "parentId", containerId
            );
		} else {
			localContainer.style.width = '100%';
			localContainer.style.height = '100%';
		}

		if (lastOpened) {
			lastOpened.style.width = '0';
			lastOpened.style.height = '0';
		}

		onTabChange(lastOpened, localContainer);

		lastOpened = localContainer;
	}
}


function loadAspPage(modulePath) {

	function onHourglassLoaded() {

		newFrame.src = modulePath;
		if (newFrame.removeEventListener)
			newFrame.removeEventListener('load', onHourglassLoaded, false);
		else if (newFrame.detachEvent) // ie
			newFrame.detachEvent('onload', onHourglassLoaded);
		else // starocie
			newFrame.onload = null;

		newFrame.onload = function() { FlexResizer(); newFrame.onload = null; }
	}

	var moduleContainer = document.getElementById("FlexModule");

	if (moduleContainer) {
		var containerId = modulePath.replace(/[^a-z]*/gi, "");
		moduleName = "asp" + containerId;

		if (lastOpened && lastOpened.id == containerId)
			return;

		var localContainer = document.getElementById(containerId);

		if (isPlayerIFrameVisible) {
			hideIFrame();
			restorePlayerIFrame = true;
		}

		if (lastOpened) {
			lastOpened.style.width = '0';
			lastOpened.style.height = '0';
		}

		if (!localContainer) {
			var newFrame = document.createElement('iframe');
			newFrame.id = containerId;
			//newFrame.src = modulePath;
			newFrame.src = "Shared/hourglass.aspx";
			newFrame.frameborder = 0;
			newFrame.allowTransparency = "allowTransparency";
			newFrame.style.width = '100%';
			newFrame.style.height = '100%';
			newFrame.scrolling = 'auto';

			if (newFrame.addEventListener) // !ie
				newFrame.addEventListener('load', onHourglassLoaded, false);
			else if (newFrame.attachEvent) // ie
				newFrame.attachEvent('onload', onHourglassLoaded);
			else // starocie
				newFrame.onload = onHourglassLoaded;

			localContainer = newFrame;
			moduleContainer.appendChild(newFrame);

		} else {
			if (modulePath.toString().toLowerCase().indexOf("dashboard") >= 0)
				localContainer.src = modulePath;
			localContainer.frameborder = '0';
			localContainer.style.width = '100%';
			localContainer.style.height = '100%';
			FlexResizer();
		}

		onTabChange(lastOpened, localContainer);
		lastOpened = localContainer;
	}
}

function loadNothing() {
	if (lastOpened) {
		lastOpened.style.width = '0';
		lastOpened.style.height = '0';
	}
	lastOpened = null;
}


function moveIFrame(x, y, w, h) {
	var frameRef = document.getElementById("playerContent");

	frameRef.style.left = x + "px";
	frameRef.style.top = y + "px";
	frameRef.style.width = w + "px";
	frameRef.style.height = h + "px";
}

function setIFrameContent(contentSource) {
	document.getElementById("playerContent").src = "Flash/PlayerContainer.html?" + escape(contentSource);
}

function hideIFrame() {
	document.getElementById("playerContent").style.display = "none";
	//	document.getElementById("playerContent").style.visibility="hidden";
	isPlayerIFrameVisible = false;
}


function showIFrame() {
	document.getElementById("playerContent").style.display = "block";
	//	document.getElementById("playerContent").style.visibility="visible";
	isPlayerIFrameVisible = true;
}