function $typeof(type)
	{
		switch(typeof(type))
		{
			case 'string': return String;
			case 'array': return Array;
			case 'object':return Object;
			case 'boolean':return Boolean;
			default: return false;
		}
	}
function each(tthis, handler) {
		if(typeof(tthis) === "array") {
			for(var i=0;i<tthis.length;i++)
			{
				handler(tthis[i], i);
			}
		} 
		else if(typeof(tthis) == "object") {
			if(typeof(handler) === "function") {
				for(attr in tthis)
				{
					if($typeof(tthis[attr])&&!$typeof(tthis[attr]).prototype[attr])
					{
						handler(tthis[attr], attr);
					}
				}
			}
		}	
	}

var CwChat = new function() {
	
	this.openChat = function(cID) {
		if(document.getElementById("CwChatIframe"+cID)) {
			document.getElementById("CwChatIframe"+cID).style.display = '';
			return;
		}
		var iframe = document.createElement("iframe");
		iframe.setAttribute("frameborder","0");
		iframe.setAttribute("border","0");
		iframe.setAttribute("marginwidth","0");
		iframe.setAttribute("marginheight","0");
		iframe.setAttribute("scrolling","no");
		//iframe.setAttribute("height","322px");
		iframe.className = "chatIFrame";
		
		iframe.id = "CwChatIframe"+cID;
		iframe.style.overflow = "hidden";
//		iframe.style.width = "100%";
//		iframe.style.height = "100%";
		iframe.style.paddingTop = "2px";
		iframe.style.border = "0px solid #FFF";
		iframe.setAttribute("src", "http://"+window.location.host+"/modules/CwChat/window.php?users="+cID);
		
		document.getElementById("CwChatContainer").appendChild(iframe);
		
		CwChat.adjustIframe();

		//iframe.onload = function() { CwChat.ID = cID; };
	};
	
	this.closeChat = function(id) {
		var iframe = document.getElementById("CwChatIframe"+id);
		document.getElementById("CwChatContainer").removeChild(iframe);
		
		CwChat.adjustIframe();
	};
	
	this.checkOpenChats = function(cUserId) {
		var settings = null;
	
		var nameEQ = "CwChatSettings=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) settings = eval("(" + decodeURIComponent(c.substring(nameEQ.length,c.length)) + ")");
		}
		
		if(settings != null) {
			each(settings, function(value, key) {
				//if(console) console.info(value);
				if (cUserId == value['cu']) {
					CwChat.openChat(key);
				}
			});
		}
		
		cwOffice.jS.queue.pA.set({"name":"CwChatGetLastMessages","php":["CwChat","getLatestMessages"],"getControls":[]});
		//cwOffice.jS.req();
	};
	this.adjustIframe = function() {
		//parent.window.document.getElementById("CwChatSystem").style.height = document.getElementById('CwChatContainer').offsetHeight + 'px';
	};
	this.changeShortIcon = function(icon) {
		try {
//			var si = parent.window.document.getElementById('shortcutIcon');
//			if (si) {
//				si.href = icon;
//			}
			addLink(icon);
		} catch(e) {}
	};
	
	var orgTitle = null;
	this.changeTitle = function(title, move) {
		try {
			if(orgTitle == null) {
				orgTitle = parent.window.document.title;
			}
			if (move == true) {
				parent.window.document.title = "CwCity.de";
				titleAppear(title);
			}
			else {
				parent.window.document.title = title;
			}
		} catch(e) {
			
		}
	};
	this.changeOrgTitle = function() {
		try {
			if (orgTitle != null) {
				parent.window.document.title = orgTitle;
				orgTitle = null;
			}
		} catch(e) {
			
		}
	};
	titleAppearCount = 0;
	function titleAppear(text){
		if (orgTitle != null) {
			if (titleAppearCount != text.length) {
				// Add next character...
				parent.document.title = text.charAt(text.length - (++titleAppearCount)) + parent.document.title;
				setTimeout(function() {
					titleAppear(text);
				}, 50);
			}
			else {
				titleAppearCount = 0;
				setTimeout(function(){
					titleDisappear(text);
				}, 3000);
			}
		}
	}
	function titleDisappear(text){
		if (orgTitle != null) {
			if (titleAppearCount != text.length) {
				// 'Remove' characters
				var len = text.length - (++titleAppearCount);
				var str = text.substr(titleAppearCount, len);
				
				parent.document.title = str + "Conup.de";
				setTimeout(function(){
					titleDisappear(text);
				}, 50);
				
			}
			else {
				parent.document.title = "Conup.de";
				titleAppearCount = 0;
				setTimeout(function(){
					titleAppear(text);
				}, 1000);
			}
		}
	}
	
	function addLink(iconURL) {
	  var link = parent.document.createElement("link");
	  link.type = "image/x-icon";
	  link.rel = "shortcut icon";
	  link.href = iconURL;
	  removeLinkIfExists();
	  parent.document.getElementsByTagName("head")[0].appendChild(link);
	}
	
	function removeLinkIfExists() {
	  var links = parent.document.getElementsByTagName("head")[0].getElementsByTagName("link");
	  for (var i=0; i<links.length; i++) {
	    var link = links[i];
	    if (link.type=="image/x-icon" && link.rel=="shortcut icon") {
	      parent.document.getElementsByTagName("head")[0].removeChild(link);
	      return; // Assuming only one match at most.
	    }
	  }
	}

};
