WUNDER.registerNameSpace("WUNDER.HTML.DYNAMIC");

WUNDER.HTML.DYNAMIC.PopIn = function(sInstanceName, sTargetId) {
    alert();
	var that = this;
	
	this.elements = {};
	
	this.sInstanceName = sInstanceName;
	this.sTargetId = sTargetId;
	this.sHolderId = sInstanceName + "Holder";
	this.sBackId = sInstanceName + "Back";
	this.sCloserId = sInstanceName + "Closer";
	this.sContentId = sInstanceName + "Content";
	this.sZIndex = "";
	this.sHolderBackColour = "#000000";
	this.isDestroyed = true;
	
	this.contentPad = 50;
	
	this.addElement = function(sParent, sType, aAttributes) {
		
		var newElement = document.createElement(sType);
		for (var i=0; i < aAttributes.length; i++) {
			if (aAttributes[i][0].indexOf("on") == 0) {
				
				if (document.all) {
					//Attach event in the IE way
					function makeEventFunc(sEvents) {
						return function() {
							eval(sEvents);
						}
					}
					newElement.attachEvent(aAttributes[i][0], makeEventFunc(aAttributes[i][1]), true);
					
				} else {
					//Attach event in the Moz way
					newElement.setAttribute(aAttributes[i][0], aAttributes[i][1]);
				}
				
			} if (aAttributes[i][0] == "class") {
				newElement.className = aAttributes[i][1];
			} else {
				newElement.setAttribute(aAttributes[i][0],aAttributes[i][1]);
			}
		}
		
		if (sParent != "") {
			document.getElementById(sParent).appendChild(newElement);	
		} else {
			document.body.appendChild(newElement);
		}
		
		return newElement;
	}
	
	
	this.changeUrl = function(sNewUrl) {
		trace("Destroying old url");
		this.destroy();
		trace("Opening " + sNewUrl);
		newPopIn.create(sNewUrl, 80);
	}
	
	
	this.create = function(sContentFile, iBackgroundOpacity) {
		trace("creating from " + sContentFile);
		that.elements = that.createElements(that.sTargetId);
		var aTargetOffsets = this.getRealOffsets(that.elements.eTarget);
		that.loadInfo(sContentFile);
		elements.eCloser.innerHTML = "<a href='javascript:" + that.sInstanceName + ".destroy();'><img src='images/close.gif' border='0' /></a>";

		that.pop;
	}
	
	
	this.createElements = function(sTargetId) {
	    var elements = that.elements;
	    
	    elements.eTarget = document.getElementById(sTargetId);
		elements.eHolder = that.addElement(that.sTargetId,'div', [
			['id', that.sHolderId]
		]);
		elements.eBack = that.addElement(that.sHolderId, 'div', [
			['id',that.sBackId]
		]);
		elements.eContent = that.addElement(that.sHolderId, 'div', [
			['id',that.sContentId]
		]);
		elements.eCloser = that.addElement(that.sHolderId, 'div', [
			['id',that.sCloserId]
		]);
		
		return elements;
	}
	
	
	this.destroy = function() {
		trace("removing div: " + that.eHolder.parentNode.id);
		that.eHolder.parentNode.removeChild(that.eHolder);
		that.isDestroyed = true;
		trace("removing done");
	}
	
	
	this.loadInfo = function(sContentFile) {
		var oAjax = new WUNDER.COMMUNICATION.AjaxPage();
		oAjax.request(sContentFile, that.sContentId);
	}
	
	
	this.pop = function() {
	    that.styleElements();
	    that.isDestroyed = false;
	}
	
	
	this.setOpacity = function(eElement, iValue) {
		eElement.style.opacity = iValue/100;
		eElement.style.filter = 'alpha(opacity=' + iValue + ')';
	}
	
	
	this.getRealOffsets = function(eElement) {
		var curleft = curtop = 0;
		if (eElement.offsetParent) {
			do {
				curleft += eElement.offsetLeft;
				curtop += eElement.offsetTop;
			} while (eElement = eElement.offsetParent);
			
			return [curleft,curtop];
		}
	}
	
	this.getScrollXY = function() {
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return [ scrOfX, scrOfY ];
	}
	
	
	this.styleElements = function(oElements) {
	    var eHolderStyle = that.elements.eHolder.style;
		eHolderStyle.position = 'absolute';
		eHolderStyle.top = aTargetOffsets[1] + 'px';
		eHolderStyle.left = aTargetOffsets[0] + 'px';
		eHolderStyle.width = that.eTarget.offsetWidth + 'px';
		eHolderStyle.height = that.eTarget.offsetHeight + 'px';
		
		if (that.sZIndex != "") {
			eHolderStyle.zIndex = that.sZIndex;
		}
		
		var eBackStyle = that.elements.eBack.style;
		eBackStyle.position = 'absolute';
		eBackStyle.top = '0';
		eBackStyle.left = '0';
		eBackStyle.width = that.eTarget.offsetWidth + 'px';
		eBackStyle.height = that.eTarget.offsetHeight + 'px';
		
		
		eBackStyle.backgroundColor = that.sHolderBackColour;
		that.setOpacity(that.elements.eBack, iBackgroundOpacity);
		
		var eContentStyle = that.elements.eContent.style;
		eContentStyle.position = 'absolute';
		//eContentStyle.border = '1px solid #DDDDDD';
		eContentStyle.top = that.contentPad + 'px';
		eContentStyle.left = that.contentPad + 'px';
		eContentStyle.width = (that.eTarget.offsetWidth - 2*that.contentPad) + 'px';
		eContentStyle.height = (that.eTarget.offsetHeight - 2*that.contentPad) + 'px';
		eContentStyle.overflow = 'auto';
		
		var eCloserStyle = that.elements.eCloser.style;
		eCloserStyle.position = 'absolute';
		eCloserStyle.right = '0px';
		eCloserStyle.padding = 5 + 'px';
		eCloserStyle.color = '#FFFFFF';
		eCloserStyle.textAlign = 'right';
	}
	
	
    this.toggle = function() {
        if (this.isDestroyed) {
            trace('showing ' + newPopIn.sInstanceName);
            this.create("test_data.aspx", 80);
        } else {
            trace('hiding ' + newPopIn.sInstanceName);
            this.destroy();
        }
    }
    
	
	this.xScrollTop = function(e, bWin) {
		var offset=0;
	  	if (typeof(e) == 'undefined' || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
			var w = window;
			if (bWin && e) w = e;
			if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
			else if(w.document.body && typeof(w.document.body.scrollTop) != 'undefined') offset=w.document.body.scrollTop;
	  	}  else {
			e = xGetElementById(e);
			if (e && xNum(e.scrollTop)) offset = e.scrollTop;
	  	}
	  	return offset;
	}
}
