var Roomle = { 
	resize: function() { 
 
		if( typeof( window.innerWidth ) == 'number' ) {
		  //Non-IE
		  myWidth = window.innerWidth;
		  myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		  //IE 6+ in 'standards compliant mode'
		  myWidth = document.documentElement.clientWidth;
		  myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		  //IE 4 compatible
		  myWidth = document.body.clientWidth;
		  myHeight = document.body.clientHeight;
		}
		
		// alert("roomle2 eventhandler " + myHeight + "x" + myWidth);
		var new_height = myHeight-80;
		var new_width = myWidth-80;

		var roomle_frame = document.getElementById("frame");
		roomle_frame.style.height = new_height + "px";
		roomle_frame.style.width = new_width + "px"; 
 
		var roomle_bg = document.getElementById("sil-bg");
		roomle_bg.style.height = myHeight + "px";
		roomle_bg.style.width = myWidth + "px"; 
		 
		var roomle_dialog_closer = document.getElementById("roomle_dialog_closer"); 
		roomle_dialog_closer.style.left = (myWidth-87) + "px"; 
	}, 
	init: function() {
		tagNames = new Array("img", "input", "button", "a", "div");
		
		
		for (var i = 0; i < tagNames.length; i++) {
			var tagName = tagNames[i];
			var elems = document.getElementsByTagName(tagName);
			for (var j = 0; j < elems.length; j++) {
				if (elems[j].className.indexOf("roomle") >= 0) {
					if (elems[j].tagName.toLowerCase() == "img") {
						var img = elems[j];
						//check if there are more than one class name
						var cssClass = img.className.split(" ");
						var roomleClass;
						for (var k = 0; k < cssClass.length; k++) {
							if (cssClass[k].indexOf("roomle") >=0) {
								roomleClass = cssClass[k];
							}
						}
						var parts = roomleClass.split("-");
						if (parts.length == 3) {						
							var parent = img.parentNode;											
							var guid = parts[1];
							var objId = parts[2];
							var imgSrc = img.src;
							var id = j;
							//add button under img tag
							var url = "http://www.roomle.com/file/UploadExtern.action?objId=" + objId + "&url=" + imgSrc + "&extern=" + guid;					
							parent.appendChild(Roomle.getButton(id, url));
							parent.appendChild(Roomle.getPopup(id)); 
						}
					} else { //button
						var elem = elems[j];
						var cssClass = elem.className.split(" ");
						var roomleClass;
						for (var k = 0; k < cssClass.length; k++) {
							if (cssClass[k].indexOf("roomle") >=0) {
								roomleClass = cssClass[k];
							}
						}
						var parts = roomleClass.split("-");
						if (parts.length == 3) {						
							var parent = elem.parentNode;											
							var tenant = parts[1];
							var keyword = parts[2];
							if (keyword == "btn") {
                                if(tenant == "planungswelten") {
                                    elem.onclick = function() { top.Roomle.open("http://www.roomle.com/planungswelten/index.jsp?signupnow=true"); };
                                } else {
                                    elem.onclick = function() { top.Roomle.open("http://www.roomle.com/" + tenant + "/index.jsp"); };
                                }
							}
						}				
					}
				}
			}
			
		}
	},
	getButton: function(id, url) {
	var div = document.createElement("div");
		var popupId = id;
		var elem = document.createElement("button");
		elem.id = "roomle-button-" + id;
		elem.onclick = function() { top.Roomle.open(url); };
		elem.onmouseover = function() { document.getElementById("roomle-popup-" + popupId).style.display="block"; };
		elem.onmouseout = function() { document.getElementById("roomle-popup-" + popupId).style.display="none"; };
		elem.style.background = "url(http://www.roomle.com/assets/style/gfx/btn_wohnnet.jpg) no-repeat scroll left top";
		elem.style.width = "133px";
		elem.style.height = "38px";
		elem.style.border = "0";
		elem.value = "";
		elem.name = "roomle-button";
	div.appendChild(elem);
	return div;
		//return elem;
	},
	getPopup: function(id) {
	var div = document.createElement("div");
		var popup = document.createElement("div");
		var textNode = document.createTextNode("Diesen Plan k\u00F6nnen Sie auf Roomle.com einrichten, planen und an Freunde senden - jetzt Roomle starten!");
		popup.appendChild(textNode);
		popup.id = "roomle-popup-" + id;
		popup.style.background = "transparent url(http://www.roomle.com/assets/style/gfx/popup_wohnnet.png) no-repeat scroll left top";
		popup.style.width = "235px";
		popup.style.height = "177px";
		popup.style.margin = "-13px 0 0 47px";
		popup.style.padding = " 24px 15px 15px 15px";
		popup.style.color = "#fff";
		popup.style.zIndex = "100";
		popup.style.position = "absolute";
		popup.style.fontSize = "11px";
		popup.style.display = "none";
		
	div.appendChild(popup);
	return div;
		//<div id="popup" style="background: transparent url(http://www.roomle.com/assets/style/gfx/popup_wohnnet.jpg) no-repeat scroll left top; 
		//	width: 235px; height: 177px; margin:-15px 0 0 33px; z-index: 100; position: relative; padding: 24px 15px 15px 15px; color: #fff;
		//	font-family: Arial, Helvetica, sans-serif; font-size: 11px; display: none; ">
		//	Diesen Plan k�nnen Sie auf Roomle.com einrichten, planen und an Freunde senden - jetzt Roomle starten!</div>
		//return popup;
	},
	open: function(url) {
		if (!document.getElementById("sil-container")) {    	
			var topOffset = 32;
			var left = 32;
			
			//http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
			var myWidth = 0, myHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
			  //Non-IE
			  myWidth = window.innerWidth;
			  myHeight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			  //IE 6+ in 'standards compliant mode'
			  myWidth = document.documentElement.clientWidth;
			  myHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			  //IE 4 compatible
			  myWidth = document.body.clientWidth;
			  myHeight = document.body.clientHeight;
			}
			
			var docHeight = myHeight;
			var docWidth = myWidth;
			
			var height = docHeight - (topOffset * 2);
			var width = docWidth - (left * 2);
			
			//alert("docHeight: " + docHeight + "docWidth: " + docWidth + "height: " + height + "width: " + width);

		 
			var str = '<div id="sil-bg" style="background: #666; opacity: 0.6; filter: alpha(Opacity=60); ';
			str += 'position: absolute; left: 0; top: 0; width: 101.4%; height: ' + docHeight + 'px;">&nbsp;</div> ';
			//str += '<div id="sil-close" style="position: absolute; left: ' + (docWidth - left - 16) + 'px; top: ' + (topOffset - 13) + 'px; width: 30px; height: 30px; overflow: hidden; z-index: 100; background: transparent url(http://www.roomle.com/assets/style/gfx/button-close.png) no-repeat 0 0; color: #fff;">&nbsp;</div>';
			str += '<iframe id="frame" style="position: absolute; z-index: 50; background: #fff; overflow-x: hidden !important; ';
			str += 'left: ' + left + 'px; top: ' + topOffset + 'px; width: ' + width + 'px; height: ' + height + 'px; ';
			str += 'background: transparent url(http://cms2005.netural.com/silvm-proto/silvm_bg.png) no-repeat 0 0;" ';
			str += 'src="' + url + '" frameborder="0"></iframe>';
			str += '<div id="roomle_dialog_closer" style="position: absolute; top: 15px; left: ' + (myWidth - 70) + 'px; ">'+
						'<a href="#reopen" id="reopener"><img src="http://www.roomle.com/assets/style/gfx/reopener.png" style="border: none;"/></a>'+
						'<a href="#close" id="closer"><img src="http://www.roomle.com/assets/style/gfx/closer.png" style="border: none;"/></a>'+
					'</div> ';
			var elem = document.createElement("div");
			elem.id = "sil-container";
			elem.innerHTML = str;
			document.body.appendChild(elem);

			/*
			document.getElementById("sil-close").onclick = function() {
				document.getElementById("sil-container").style.display = "none";
				document.getElementById("sil-bg").style.display = "none";
			};
			document.getElementById("sil-close").onmouseover = function() {
				document.getElementById("sil-close").style["background-position"] = "0 -30px";
			}
			document.getElementById("sil-close").onmouseout = function() {
				document.getElementById("sil-close").style["background-position"] = "0 0";
			}
			*/
			document.getElementById("sil-bg").onclick = function() {
				document.getElementById("sil-container").style.display = "none";
				document.getElementById("sil-bg").style.display = "none";
			};
			
			// Reopener Event Handler
			document.getElementById("reopener").onclick = function() {
				// Close Layover before
				document.getElementById("sil-container").style.display = "none";
				document.getElementById("sil-bg").style.display = "none";
				
				// then reload content in a new window
				window.open( document.getElementById("frame").src );
			};
			// Closer Event Handler
			document.getElementById("closer").onclick = function() {
				// Close Layover before
				document.getElementById("sil-container").style.display = "none";
				document.getElementById("sil-bg").style.display = "none"; 
			};
				
		     // Tie our roomle resize handler to the window
		     window.onresize = this.resize;
		} else {
				document.getElementById("frame").src = url;
				document.getElementById("sil-container").style.display = "block";
				document.getElementById("sil-bg").style.display = "block";
		}
	}
}

 Roomle.init();
