var _Mails = new Array();
var _MailListHover = null;
var _SlideInterval = new Array();
var _Opacity = new Array();
var _MailListCount = new Array();
var _MailListCountGes = 0;

function blendInfo(id, add){
	_Opacity[id] += add;
	document.getElementById("mI" + id).style.opacity = _Opacity[id];
	document.getElementById("mIBody" + id).style.opacity = _Opacity[id];

	if( (add > 0 && _Opacity[id] >= 1) || (add < 0 && _Opacity[id] <= 0) ){
		window.clearInterval(_SlideInterval[id]);
		_SlideInterval[id] = null;
		if(_Opacity[id] <= 0){
			closew("mI" + id);
			closew("mIBody" + id);
		}
	}
}

function mailInfo(id, postfach, postfachId, betreff, von, showInfo){
	if(document.getElementById("mI" + id)) return;
	
	_Mails[id] = new Array(postfach, postfachId, betreff, von);
	
	if(!showInfo){
		var mailImgDiv = newDiv("mailInfoImg", "position:absolute; left:10px; top:10px; width:20px; height:13px; padding:0px;");
		mailImgDiv.innerHTML = "<a href=\"javascript:mailListShow(-1);\"><img src=\"/images/icons/mailInfo.png\" /></a>";
		document.getElementsByTagName("body")[0].appendChild(mailImgDiv);
		mailListAdd(id);
		
		mailListAddCount(postfachId, 1);
		
		showMailInfoWin(_MailListCountGes);
		return;
	}
	
	var bgImg = imgTransPNGBackground("/images/mail/bgInfoNew.png");
	if(_IE){
		bgImg = bgImg.substr(0, bgImg.length-1); 
		bgImg += " revealTrans(Duration=1,Transition=12);";
	}

	var div = newDiv("mI" + id, "position:absolute; width:300px; height:120px; " + bgImg + " top:250px; visibility:hidden; left:" + ((_innerWidth/2)-150) + "px; opacity:0;");
	var code = "&nbsp;"	
	div.innerHTML = code;
	document.getElementsByTagName("body")[0].appendChild(div);

	div = newDiv("mIBody" + id, "position:absolute; left:" + ((_innerWidth/2)-50) + "px; top:270px; width:190px; height:80px; visibility:hidden; filter:revealTrans(Duration=1,Transition=12);");
	code = "Du hast eine neue E-Mail im Postfach <div align=\"center\" style=\"margin-bottom:3px;\"><b>" + unescape(postfach) + "</b></div>";
	code += "Betreff: <b>" + unescape(betreff) + "</b>";
	code += "<div style=\"margin-bottom:10px;\">von " + unescape(von) + "</div>";
	code += "<div align=\"center\"><a href=\"javascript:top.mailShow(" + id + ");\" target=\"mainFrame\">[Anzeigen]</a> <a href=\"javascript:top.mailDel(" + id + ");\">[L&ouml;schen]</a> <a href=\"javascript:top.mailInfoClose(" + id + ", true);\">[Schlie&szlig;en]</a></div>";
	div.innerHTML = code;
	document.getElementsByTagName("body")[0].appendChild(div);

	if(!_IE){
		if(_SlideInterval[id] != null){
			window.clearInterval(_SlideInterval[id]);
			_SlideInterval[id] = null;
		}
		if(_Opacity[id] == null)
			_Opacity[id] = 0;

		document.getElementById("mI" + id).style.visibility = "visible";
		document.getElementById("mI" + id).style.opacity = 0;
		document.getElementById("mIBody" + id).style.visibility = "visible";
		document.getElementById("mIBody" + id).style.opacity = 0;

		_SlideInterval[id] = window.setInterval("blendInfo(" + id + ", 0.05)", 15);
	}else{
		document.all["mI" + id].filters.revealTrans.Apply();
		document.all["mI" + id].style.visibility = "visible";
		document.all["mIBody" + id].filters.revealTrans.Apply();
		document.all["mIBody" + id].style.visibility = "visible";
		document.all["mI" + id].filters.revealTrans.Play();
		document.all["mIBody" + id].filters.revealTrans.Play();
	}
}



function mailInfoClose(id, addToList){
	if(!document.getElementById("mI" + id))
		return;
		
	var opacity = 1;
	var filter = "";
	var moveIconInterval = null;

	if(_IE){
		document.all["mI" + id].filters.revealTrans.Apply();
		document.all["mI" + id].style.visibility = "hidden";
		document.all["mIBody" + id].filters.revealTrans.Apply();
		document.all["mIBody" + id].style.visibility = "hidden";
		document.all["mI" + id].filters.revealTrans.Play();
		document.all["mIBody" + id].filters.revealTrans.Play();
	}else{
		if(_SlideInterval[id] != null){
			window.clearInterval(_SlideInterval[id]);
			_SlideInterval[id] = null;
		}
		if(_Opacity[id] == null)
			_Opacity[id] = 1;

		document.getElementById("mI" + id).style.visibility = "visible";
		document.getElementById("mI" + id).style.opacity = 0;
		document.getElementById("mIBody" + id).style.visibility = "visible";
		document.getElementById("mIBody" + id).style.opacity = 0;

		_SlideInterval[id] = window.setInterval("blendInfo(" + id + ", -0.05)", 15);
	}

	if(addToList){
		var postfachId = _Mails[id][1];
		mailListAddCount(postfachId, 1);
		
		if(!document.getElementById("mailInfoImg")){
			var mailImgDiv = newDiv("mailInfoImg", "position:absolute; left:10px; top:10px; width:20px; height:13px; padding:0px;");
			mailImgDiv.innerHTML = "<a href=\"javascript:mailListShow(-1);\"><img src=\"/images/icons/mailInfo.png\" /></a>";
			document.getElementsByTagName("body")[0].appendChild(mailImgDiv);	
		}
		mailListAdd(id);
		showMailInfoWin(_MailListCountGes);
	}

	axSend("/configs/ajax/functions.php", "a=6&id=" + id, "none");
}



function mailShow(id){
	mailInfoClose(id, false);
	if(document.getElementById("mailListMail" + id))
		mailListDel(id);
		
	window.open("/main/profil/mail/mails.php?sh=1&id=" + id + "&pf=" + _Mails[id][1], "mainFrame", "");
}

function mailDel(id){
	mailInfoClose(id);
	axSend("/main/profil/mail/ajax.php", "a=5&mid=" + id, "none");
}

function mailListAdd(mid){
	var mailList = null;

	if(!document.getElementById("mailList")){
		mailList = newDiv("mailList", "position:absolute; left:31px; top:10px; width:100px; overflow:hidden; padding:0px; display:none;");
		mailList.innerHTML = "<table width=\"100\" border=\"0\ cellspacing=\"0\" cellpadding=\"0\" style=\"border:1px solid #999999; background-color:#FFFFFF;\"><tbody style=\"padding:0px; margin:0px;\"></tbody></table>";
		document.getElementsByTagName("body")[0].appendChild(mailList);
	}

	mailList = document.getElementById("mailList");
	mailListTBody = mailList.getElementsByTagName("tbody")[0];

	var postfach = _Mails[mid][0];
	var postfachId = _Mails[mid][1];
	var betreff = unescape(_Mails[mid][2]);
	var von = unescape(_Mails[mid][3]);

	var tr = document.createElement("tr");
	var td = document.createElement("td");

	if(!document.getElementById("mailListPFTr" + postfachId)){
		tr.setAttribute("id", "mailListPFTr" + postfachId, 1);
		td.onmouseover = function() { mailListShow(postfachId); this.style.backgroundColor="#CCCCCC"; }
		td.onmouseout = function() { mailListHide(postfachId); this.style.backgroundColor="#FFFFFF"; }
		td.innerHTML = postfach + " (" + (_MailListCount[postfachId] == null ? "0" : _MailListCount[postfachId]) + ")";
		td = setElementStyle(td, "font-weight:bold; cursor:pointer;");
		tr.appendChild(td);
		mailListTBody.appendChild(tr);

		var newMailList = newDiv("mailList" + postfachId, "position:absolute; left:131px; top:10px; padding:0px; display:none;");
		newMailList.innerHTML = "<table border=\"0\ cellspacing=\"0\" onmouseover=\"javascript:_MailListHover=" + postfachId + ";\" onmouseout=\"javascript:_MailListHover=null; mailListHide(" + postfachId + ");\" style=\"border:1px solid #999999; background-color:#FFFFFF;\"><tbody style=\"padding:0px; margin:0px;\"></tbody></table>";
		document.getElementsByTagName("body")[0].appendChild(newMailList);
	}

	mailList = document.getElementById("mailList" + postfachId);
	mailListTBody = mailList.getElementsByTagName("tbody")[0];

	var border = "";
	if(mailListTBody.getElementsByTagName("tr").length > 0)
		border = "border-top:1px solid #999999;";

	tr = document.createElement("tr");
	tr.setAttribute("id", "mailListMailTr" + mid, 1);
	td = document.createElement("td");
	td.setAttribute("id", "mailListMail" + mid, 1);
	td.onclick = function() { mailShow(mid); }
	td.onmouseover = function() { this.style.backgroundColor = "#CCCCCC"; }
	td.onmouseout = function() { this.style.backgroundColor = "#FFFFFF"; }
	td = setElementStyle(td, "cursor:pointer; " + border);
	td.innerHTML = "<b>" + betreff + "</b><div style=\"padding-left:7px;\">von " + von + "</div>";
	tr.appendChild(td);
	mailListTBody.appendChild(tr);
}

function mailListDel(id){
	var postfachId = _Mails[id][1];
	var mailList = document.getElementById("mailList" + postfachId);
	var mailListTBody = mailList.getElementsByTagName("tbody")[0];

	document.getElementById("mailListMailTr" + id).parentNode.removeChild(document.getElementById("mailListMailTr" + id));
	
	if(mailListTBody.getElementsByTagName("tr").length < 1){
		document.getElementById("mailList" + postfachId).parentNode.removeChild(document.getElementById("mailList" + postfachId));
		document.getElementById("mailListPFTr" + postfachId).parentNode.removeChild(document.getElementById("mailListPFTr" + postfachId));

		mailList = document.getElementById("mailList");
		mailListTBody = mailList.getElementsByTagName("tbody")[0];

		if(mailListTBody.getElementsByTagName("tr").length < 1){
			document.getElementById("mailList").parentNode.removeChild(document.getElementById("mailList"));
			document.getElementById("mailInfoImg").parentNode.removeChild(document.getElementById("mailInfoImg"));
		}
	}
	
	mailListAddCount(postfachId, -1);
}


function mailListShow(postfach){
	if(postfach == -1 && document.getElementById("mailList")){
		document.getElementById("mailList").style.display = document.getElementById("mailList").style.display == "none" ? "" : "none";
		if(_MailListHover != null)
			document.getElementById("mailList" + _MailListHover).style.display = "none";
		return;
	}

	document.getElementById("mailList" + postfach).style.display = "";
}


function mailListHide(postfach){
	this.hide = function(){
		if(_MailListHover == null)
			document.getElementById("mailList" + postfach).style.display = "none";
	}
	
	window.setTimeout("this.hide()", 25);
}

function showMailInfoWin(anzahl){
	if(document.getElementById("mailInfoWin")){
		document.getElementById("mailInfoWinContent").innerHTML = "Du hast " + anzahl + "<br /> neue E-Mail" + (anzahl == 1 ? "" : "s");
	}else{
		var div = newDiv("mailInfoWin", "width:130px; height:80px; position:absolute; left:-5px; top:15px; visibility:hidden; filter:blendTrans(Duration=0.5) Alpha(opacity:80);");
		var contentDiv = newDiv("mailInfoWinContent", "width:130px; height:50px; padding-top:30px; font-weight:bold; text-align:center; " + imgTransPNGBackground("/images/mail_infowin.png"));
	
		contentDiv.innerHTML = "Du hast " + anzahl + "<br /> neue E-Mail" + (anzahl == 1 ? "" : "s");
		
		div.onmouseover = function(){ hideMailInfoWin(); }
		div.appendChild(contentDiv);
		
		document.getElementsByTagName("body")[0].appendChild(div);
		
		_MaxBlendObjectOpacity = 80;
		_SetLoadingOnBlend = false;
		blendObject(15, "mailInfoWin");
		_SetLoadingOnBlend = true;
	}
}

function hideMailInfoWin(){
	_SetLoadingOnBlend = false;
	_BlendObjectPostFunction = "document.getElementById('mailInfoWin').parentNode.removeChild(document.getElementById('mailInfoWin'));";
	blendObject(-20, "mailInfoWin");
	_SetLoadingOnBlend = true;	
}

function mailListAddCount(postfachId, anzahl){
	if(_MailListCount[postfachId] == null)
		_MailListCount[postfachId] = 0;
		
	_MailListCount[postfachId] += anzahl;
	_MailListCountGes += anzahl;
	
	if(document.getElementById("mailListPFTr" + postfachId)){
		var mailListPfTr = document.getElementById("mailListPFTr" + postfachId);
		var mailListPfTd = mailListPfTr.getElementsByTagName("td")[0];
		if(mailListPfTd){
			var titel = mailListPfTd.innerHTML;
			titel = titel.substring(0, titel.lastIndexOf("("));
			mailListPfTd.innerHTML = titel + "(" + _MailListCount[postfachId] + ")";
		}
	}
}
