﻿var superbaner = function() {
	// Resource

	var resource = function() {
		var text = function(name) {
			var lang = {
				pl: {
					closepopup:	"zamknij okno"
				}
			};


			try {
				var elements = document.getElementsByTagName("html");

				if (elements.length == 1) {
					return lang[elements[0].lang][name];
				}
			}
			catch (error) {
				alert(error);
			}


			return false;
		};




		return {
			text: text
		};
	}();






	// DOM helper

	var dom = function() {
		var index = 0;




		var rewriteAttribute = function(instance, variable) {
			for (var key in variable) {
				switch (key) {
					case "style":

						rewriteAttribute(instance[key], variable[key]);
						break;


					default:

						instance[key] = variable[key];
						break;
				}
			}
		};




		var createElement = function(tagname, variable, anotherelements) {
			var element = document.createElement(tagname);


			if (variable) {
				rewriteAttribute(element, variable);
			}


			if (anotherelements) {
				for (var key in anotherelements) {
					element.appendChild(anotherelements[key]);
				}
			}


			return element;
		};




		var appendFirst = function(node, child) {
			if (node.hasChildNodes()) {
				node.insertBefore(child, node.firstChild);
			}
			else {
				node.appendChild(child);
			}
		};
		
		
		
		
		var removeAllChilds = function(node) {
			while (node.hasChildNodes()) {
				node.removeChild(node.lastChild);
			}
		};




		return {
			appendFirst: appendFirst,
			createElement: createElement,
			removeAllChilds: removeAllChilds
		};
	}();






	// Effect

	var effect = function() {
		var page = function() {
			// Page size

			var size = function() {
				// Scroll test

				var xScroll = 0;
				var yScroll = 0;


				if (window.innerHeight && window.scrollMaxY) {
					xScroll = window.innerWidth + window.scrollMaxX;
					yScroll = window.innerHeight + window.scrollMaxY;
				}
				else {
					if (document.body.scrollHeight > document.body.offsetHeight) {
						xScroll = document.body.scrollWidth;
						yScroll = document.body.scrollHeight;
					}
					else {
						xScroll = document.body.offsetWidth;
						yScroll = document.body.offsetHeight;
					}
				}




				// Window test

				var windowWidth = 0;
				var windowHeight = 0;


				if (self.innerHeight) {
					if (document.documentElement.clientWidth) {
						windowWidth = document.documentElement.clientWidth; 
					}
					else {
						windowWidth = self.innerWidth;
					}

					windowHeight = self.innerHeight;
				}
				else {
					if (document.documentElement && document.documentElement.clientHeight) {
						windowWidth = document.documentElement.clientWidth;
						windowHeight = document.documentElement.clientHeight;
					}
					else {
						if (document.body) {
							windowWidth = document.body.clientWidth;
							windowHeight = document.body.clientHeight;
						}
					}
				}




				// Page size

				var pageWidth = 0;
				var pageHeight = 0;


				if (yScroll < windowHeight) {
					pageHeight = windowHeight;
				}
				else { 
					pageHeight = yScroll;
				}


				if (xScroll < windowWidth) {
					pageWidth = xScroll;
				}
				else {
					pageWidth = windowWidth;
				}


				return {
					width: pageWidth,
					height: pageHeight
				};
			};






			// Page scroll

			var scroll = function() {
				var scroll = 0;


				if (self.pageYOffset) {
					scroll = self.pageYOffset;
				}
				else {
					if (document.documentElement && document.documentElement.scrollTop) {
						scroll = document.documentElement.scrollTop;
					}
					else {
						if (document.body) {
							scroll = document.body.scrollTop;
						}
					}
				}


				return {
					y: scroll
				};
			};






			return {
				size: size,
				scroll: scroll
			};
		}();






		// Popup window

		var popup = function() {
			var child = false;




			var close = function() {
				if (child) {
					document.getElementsByTagName("body")[0].removeChild(child);
				}


				child = false;


				return false;
			};
			
			
			
			
			var embed = function(source) {
				var sizecontent = {
					width: 710, height: 450
				};


				var size = {
					width: page.size().width, height: page.size().height
				};


				var position = {
					top: page.scroll().y + 100, left: (size.width - sizecontent.width) / 2
				};




				if (child) {
					return;
				}




				// Root

				child = dom.createElement("div", {style: {position: "absolute", width: size.width + "px", height: size.height + "px", textAlign: "left"}}, [
					]
				);


				// Container

				var container = dom.createElement("div", {style: {position: "absolute", left: position.left + "px", top: position.top + "px", width: sizecontent.width + "px"}}, [
					]
				);


				child.appendChild(
					container
				);


				// Popup

				var popup = dom.createElement("div", {style: {cssFloat: "left", width: sizecontent.width + "px", background: "rgb(255,255,255)"}}, [
					]
				);


				container.appendChild(
					popup
				);


				// Header

				var header = dom.createElement("div", {style: {cssFloat: "left", paddingLeft: (10) + "px", paddingRight: (10) + "px", width: (sizecontent.width - 20) + "px", textAlign: "right", background: "rgb(255,255,255)"}}, [
						dom.createElement("a", {href: "#", onclick: close}, [
										dom.createElement("img", {src: "Content/image/portal/icon/close.gif"}, [
									]
								)
							]
						)
					]
				);


				popup.appendChild(
					header
				);


				// Content

				var content = dom.createElement("div", {style: {cssFloat: "left", paddingLeft: (105) + "px", paddingRight: (105) + "px", width: (sizecontent.width - 210) + "px", background: "rgb(255,255,255)"}}, [
					]
				);


				popup.appendChild(
					content
				);


				// Footer

				var footer = dom.createElement("div", {style: {cssFloat: "left", paddingTop: "10px", paddingBottom: "10px", paddingLeft: (105) + "px", paddingRight: (105) + "px", width: (sizecontent.width - 210) + "px", background: "rgb(255,255,255)", textAlign: "center"}}, [
						dom.createElement("a", {href: "#", onclick: close, innerHTML: resource.text("closepopup")}, [
							]
						)
					]
				);


				popup.appendChild(
					footer
				);




				// If rame

				var rame = dom.createElement("iframe", {src: source, width: (sizecontent.width - 210) + "px", height: (sizecontent.height) + "px", style: {border: "0", overflowX: "hidden"}, scrolling: "no"}, [
					]
				);


				content.appendChild(
					rame
				);




				// Add child
				
				document.getElementsByTagName("body")[0].appendChild(
					child
				);


				return false;
			};




			return {
				embed: embed
			};
		}();






		return {
			popup: popup
		};
	}();






	return {
		effect: effect
	};
}();

