//Show overlay and popup window
function getGenericOverlayObject() {
	if (!popup_overlay) {
		popup_overlay = new OverlayBackground(new Object());
		popup_overlay.initialize();
	}
	return popup_overlay;
}

//  Overlay Window object - use for single-popup screens
function OverlayWindow(info){
	this.div_id = info.id;
	this.window_div = document.getElementById(info.id);
	this.overlay = getGenericOverlayObject();
	this.overlay.parent = this;
	this.parent = false;
	this.show = function(noOverlay) {
		if (this.window_div) {
			if (!noOverlay) this.overlay.show();
			this.window_div.style.display = "block";
			if (this.window_div.parentNode && this.window_div.parentNode.style.display != "block") {
				this.window_div.parentNode.style.display = "block";
			}
		}
	}
	this.close = function(noOverlay) {
		if (this.window_div) {
			this.window_div.style.display = "none";
			if (!noOverlay) {
				if (this.window_div.parentNode && this.window_div.parentNode.style.display != "none") {
					this.window_div.parentNode.style.display = "none";
				}
				this.overlay.close();
			}
		}
	}
	this.initializeDiv = function(info) {
		if (!info.width) info.width = 800;
		if (!info.height) info.height = 480;
		if (this.window_div && this.window_div.innerHTML.indexOf("http://images.shaklee.com/landing/banner/images/overlay-area-close.png") < 0) {
			var thisHeader = false;
			var thisBody = false;
			var thisFooter = false;
			for (var n=this.window_div.childNodes.length-1; n>=0; n--) {
				switch (this.window_div.childNodes[n].className) {
					case "popup_overlay_area_header":
						thisHeader = this.window_div.childNodes[n];
						break;
					case "popup_overlay_area_bodycontent":
						thisBody = this.window_div.childNodes[n];
						break;
					case "popup_overlay_area_footer":
						thisFooter = this.window_div.childNodes[n];
						break;
				}
				this.window_div.removeChild(this.window_div.childNodes[n]);
			}
			this.window_div.style.width = (info.width) + "px";
			this.window_div.style.height = (info.height) + "px";
			var divID = String(this.window_div.id);
			//build the top
			var newTop = document.createElement("div");
			newTop.className = "popup_overlay_area_top";
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_tl";
			tDiv.appendChild(document.createTextNode(" "));
			newTop.appendChild(tDiv);
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_t";
			tDiv.style.width = (info.width - 57) + "px";
			tDiv.appendChild(document.createTextNode(" "));
			newTop.appendChild(tDiv);
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_tr";
			var tA = document.createElement("a");
			tA.setAttribute("href", "javascript:;");
			tA.setAttribute("onmouseup", info.objectname + ".close();");
			tA.appendChild(document.createTextNode(" "));
			tDiv.appendChild(tA);
			newTop.appendChild(tDiv);
			this.window_div.appendChild(newTop);
			//build the body
			var newMid = document.createElement("div");
			newMid.className = "popup_overlay_area_mid";
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_l";
			tDiv.style.height = (info.height - 54) + "px";
			tDiv.appendChild(document.createTextNode(" "));
			newMid.appendChild(tDiv);
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_contents";
			tDiv.style.width = (info.width - 27) + "px";
			tDiv.style.height = (info.height - 54) + "px";
			//add header
			if (thisHeader || info.header) {
				if (!thisHeader) {
					var thisHeader = document.createElement("div");
					thisHeader.className = "popup_overlay_area_header";
					thisHeader.appendChild(document.createTextNode(info.header));
				}
				tDiv.appendChild(thisHeader);
			}
			//get the footer, but don't append it yet
			if (thisFooter || info.footer || (info.pages && info.pages.length)) {
				if (!thisFooter) {
					var thisFooter = document.createElement("div");
					thisFooter.className = "popup_overlay_area_footer";
					if (info.pages && info.pages.length) {
						var tUL = document.createElement("ul");
						for (var n=0; n<info.pages.length; n++) {
							var tClass = info.pages[n].id;
							var selClass = ( (info.pages[n].id == divID) ? "popup_overlay_footer_sel" : "");
							var liClass = ( (info.pages[n].id == divID) ? "popup_overlay_footer_li_sel" : "");
							var tLI = document.createElement("li");
							if (liClass) tLI.className = liClass;
							var tA = document.createElement("a");
							tA.setAttribute("href", "javascript:;");
							tA.setAttribute("onmouseup", info.objectname + ".show('" + info.pages[n].id + "');");
							tA.className = info.pages[n].id + ( (selClass) ? " " + selClass : "");
							tA.appendChild(document.createTextNode(" "));
							tLI.appendChild(tA);
							tUL.appendChild(tLI);
						}
						thisFooter.appendChild(tUL);
					}
					else {
						thisFooter.appendChild(document.createTextNode(info.footer));
					}
				}
			}
			//add body
			var newBody = document.createElement("div");
			newBody.className = "popup_overlay_area_body";
			tDiv.style.width = (info.width - 27) + "px";
			var bodyHeight = (info.height - 89);
			if (thisHeader) bodyHeight -= 36;
			if (thisFooter) bodyHeight -= 44;
			var arrowPadding = ((parseInt((info.height - 54) / 2) - 16) - ( (thisFooter) ? 44 : 0 ));
			//tDiv.style.height = (bodyHeight) + "px";
			if (info.pages && info.pages.length) {
				var arrowDiv = document.createElement("div");
				arrowDiv.className = "popup_overlay_area_leftarrow";
				arrowDiv.style.paddingTop = arrowPadding + "px";
				arrowDiv.style.paddingBottom = arrowPadding + "px";
				var tA = document.createElement("a");
				tA.setAttribute("href", "javascript:;");
				tA.setAttribute("onmouseup", info.objectname + ".prev();");
				tA.appendChild(document.createTextNode(" "));
				arrowDiv.appendChild(tA);
				newBody.appendChild(arrowDiv);
			}
			if (!thisBody) {
				thisBody = document.createElement("div");
				thisBody.className = "popup_overlay_area_bodycontent";
				thisBody.appendChild(document.createTextNode("No Contents Defined"));
			}
			newBody.appendChild(thisBody);
			if (info.pages && info.pages.length) {
				var arrowDiv = document.createElement("div");
				arrowDiv.className = "popup_overlay_area_rightarrow";
				arrowDiv.style.paddingTop = arrowPadding + "px";
				arrowDiv.style.paddingBottom = arrowPadding + "px";
				var tA = document.createElement("a");
				tA.setAttribute("href", "javascript:;");
				tA.setAttribute("onmouseup", info.objectname + ".next();");
				tA.appendChild(document.createTextNode(" "));
				arrowDiv.appendChild(tA);
				newBody.appendChild(arrowDiv);
			}
			tDiv.appendChild(newBody);
			//add the footer to the mid
			if (thisFooter) {
				tDiv.appendChild(thisFooter);
			}
			newMid.appendChild(tDiv);
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_r";
			tDiv.style.height = (info.height - 54) + "px";
			tDiv.appendChild(document.createTextNode(" "));
			newMid.appendChild(tDiv);
			this.window_div.appendChild(newMid);
			//build the bottom
			var newBot = document.createElement("div");
			newBot.className = "popup_overlay_area_bot";
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_bl";
			var tText = document.createTextNode(" ");
			tDiv.appendChild(document.createTextNode(" "));
			newBot.appendChild(tDiv);
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_b";
			tDiv.appendChild(document.createTextNode(" "));
			tDiv.style.width = (info.width - 47) + "px";
			newBot.appendChild(tDiv);
			var tDiv = document.createElement("div");
			tDiv.className = "popup_overlay_area_br";
			tDiv.appendChild(document.createTextNode(" "));
			newBot.appendChild(tDiv);
			this.window_div.appendChild(newBot);
			var tParent = this.window_div.parentNode;
			var tGParent = tParent.parentNode;
			if (!tGParent.tagName || tGParent.tagName.toLowerCase() != "body") {
				tGParent.removeChild(tParent);
				//tParent.setAttribute("onmouseup", info.objectname + ".close();");
				document.body.appendChild(tParent);
			}
		}
	}
	this.initializeDiv(info);
}

// overlay window set object - use for lists of overlay windows
function OverlayWindowSet(info) {
	if (!info.height) info.height = 480;
	if (!info.width) info.width = 800;
	this.objectname = info.objectname;
	this.height = info.height;
	this.width = info.width;
	this.presentwindow = "";
	this.showing = false;
	this.windows = new Object();
	this.pages = info.pages;
	this.overlay = getGenericOverlayObject();
	this.overlay.parent = this;
	//methods
	this.initialize = function(info) {
		for (var n=0; n<this.pages.length; n++) {
			var tDiv = document.getElementById(this.pages[n].id);
			if (tDiv) {
				var myInfo = new Object();
				myInfo.objectname = info.objectname;
				myInfo.pages = info.pages;
				myInfo.id = this.pages[n].id;
				if (this.pages[n].width) myInfo.width = this.pages[n].width;
				else myInfo.width = info.width;
				if (this.pages[n].height) myInfo.height = this.pages[n].height;
				else myInfo.height = info.height;
				this.windows[this.pages[n].id] = new OverlayWindow(myInfo);
			}
		}
	}
	this.show = function(id) {
		if (!id && this.pages.length) id = this.pages[0];
		if (id && this.windows[id]) {
			if (!this.is_showing) this.overlay.show();
			if (this.presentwindow && this.windows[this.presentwindow]) {
				this.windows[this.presentwindow].close(true);
			}
			this.windows[id].show(true);
			this.presentwindow = id;
			this.showing = true;
		}
	}
	this.next = function() {
		for (var n=0; n<this.pages.length; n++) {
			if (this.pages[n].id == this.presentwindow) {
				n++;
				break;
			}
		}
		if (n == this.pages.length) n = 0;
		this.show(this.pages[n].id);
	}
	this.prev = function() {
		for (var n=0; n<this.pages.length; n++) {
			if (this.pages[n].id == this.presentwindow) {
				n--;
				break;
			}
		}
		if (n == -1) n = this.pages.length-1;
		this.show(this.pages[n].id);
	}
	this.close = function() {
		for (var n=0; n<this.pages.length; n++) {
			if (this.windows[this.pages[n].id]) {
				this.windows[this.pages[n].id].close(true);
				if (this.windows[this.pages[n].id].window_div && this.windows[this.pages[n].id].window_div.parentNode && this.windows[this.pages[n].id].window_div.parentNode.style.display != "none") {
					this.windows[this.pages[n].id].window_div.parentNode.style.display = "none";
				}
			}
		}
		this.overlay.close();
		this.showing = false;
	}
	this.initialize(info);
}



//Overlay Background
function OverlayBackground(info) {
	this.info = new Object();
	this.info.id = ( (info.id) ? info.id : "popup_overlay" );
	this.info.color = ( (info.color) ? info.color : "#fff" );
	this.info.opacity = ( (info.opacity) ? info.opacity : 0.5 );
	this.info.zIndex = ( (info.zIndex) ? info.zIndex : "5000" );
	this.parent = false;
	
	this.initialize = function(container) {
		this.container = ( (container) ? document.getElementById(container) : document.getElementById(this.info.id) );
		if (!this.container) {
			//create overlay container
			var newContainer = document.createElement("div");
			newContainer.id = this.info.id;
			newContainer.setAttribute("style", "filter: alpha(opacity=" + (this.info.opacity * 100) + "); -ms-filter: \"progid:DXImageTransform.Microsoft.Alpha(Opacity=" + (this.info.opacity * 100) + ")\"; filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=" + (this.info.opacity * 100) + ");"); 
			newContainer.style.opacity = this.info.opacity;
			newContainer.style.backgroundColor = this.info.color;
			newContainer.style.zIndex = this.info.zIndex;
			newContainer.setAttribute("onmouseup", this.info.id + ".click();");
			document.body.appendChild(newContainer);
			this.container = ( (container) ? document.getElementById(container) : document.getElementById(this.info.id) );
		}
	},
	this.show = function() {
		if (this.container) {
			this.container.style.display = "block";
			this.container.style.height = $(document).height() + "px";
			this.container.style.width = $(document).width() + "px";
		}
		return true;
	}
	this.click = function() {
		if (this.parent) this.parent.close();
		this.close();
	}
	this.close = function() {
		if (this.container) this.container.style.display = "none";
		return true;
	}
}
var popup_overlay = false;
