internavatar = null;

function Avatar(groupid, areaid, session, id) {
	this.loadid = parseInt(id,10);
	this.initid = this.loadid;
	this.disabled = false;
	if(parent && parent.frames && parent.frames.length > 0) {
		this.disabled = true;
	}
	this.url = "";

	this.urlparams = "g=" + groupid + "&a=" + areaid;
	if(session && session != "") {
		this.urlparams += "&r=1&s=" + session;
	}
	
	this.contentcontainer = null;
	this.balloon = null;
	this.flashobj = null;
	
	this.enabled = false;
	this.Dialogs = new Array();	
	
	internavatar = this;

	$(document).ready(function () {
		internavatar.Init();
	});

    this.Init = function () {
		if(this.disabled) return;
	
		this.contentcontainer = document.getElementById("avatarcontent");
		this.balloon = document.getElementById("avatarballoon");
		this.flashobj = findFlash("avatarflash");
		
		if(this.contentcontainer && this.balloon) {
			this.enabled = true;
		}
		
		if(this.loadid > 0) {
			this.Goto(this.loadid);
		}
	}

	this.Goto =	function(id) {
		if(this.enabled) {
			this.loadid = id;
			this.GetDialog();
		}
	}

	this.GetDialog = function() {
		var xmlHttp = GetHttpRequestObject();
		if (xmlHttp) {
				xmlHttp.open('GET', '/avatar/avatar.ashx?' + this.urlparams + '&id=' + this.loadid + "&rd=" + Math.floor(Math.random()*100000), true);
				xmlHttp.onreadystatechange = function () {
						if (xmlHttp.readyState == 4) {
							var d = new AvatarDialog();
							try {
									d.Id = xmlHttp.responseXML.getElementsByTagName("id")[0].firstChild.data;
									if(xmlHttp.responseXML.getElementsByTagName("animation")[0].firstChild) {
										d.Animation = xmlHttp.responseXML.getElementsByTagName("animation")[0].firstChild.data;
									}
									if(xmlHttp.responseXML.getElementsByTagName("title")[0].firstChild) {
										d.Title = xmlHttp.responseXML.getElementsByTagName("title")[0].firstChild.data;
									}
									d.Text = xmlHttp.responseXML.getElementsByTagName("text")[0].firstChild.data;
							} catch(e) {
								//alert(xmlHttp.responseText);
								d = null;
							}
							internavatar.OnDialogLoad(d);
						}
				};
				xmlHttp.send(null);
		}
	}

	this.OnDialogLoad = function(d) {
		if(d) {
			this.Dialogs[d.Id+""] = d;
			this.SetDialog(d.Id);
			this.loadid = 0;
		}
	}
	
	
	this.SetDialog = function(id) {
		//this.HideDialog();
		var d = this.Dialogs[id+""];
		this.contentcontainer.innerHTML = d.Text;
		this.ShowDialog();

		if(d.Animation != "") {
			this.flashobj.SetVariable("videodatei", "/dwd/_621/" + d.Animation);
			//this.flashobj.SetVariable("videodatei", "/avatar/avatar_startseite.flv");
			this.flashobj.GotoFrame(4);
			//this.flashobj.StopPlay();
			//this.HideDialog();
			// flash call: play animation -> flash needs to call showdialog;
		}
	}

	this.HideDialog = function() {
		this.balloon.style.display = "none";
	}

	this.ShowDialog = function() {
		this.balloon.style.display = "block";
	}
	
	this.OnClick = function() {
		if(this.initid > 0 && this.balloon.style.display != 'block') {
			this.Goto(this.initid);
		} else {
			if(this.url != "") {
				window.location.href = this.url;
			}
		}
	}
	
	this.Off = function() {
	  cookieManager.setCookie("avatar", "off", 1, "session");
	  cookieManager.setDocumentCookies();
 		window.location.reload();
	}

	this.On = function() {
		cookieManager.setCookie("avatar", "on", 1, "session");
	  cookieManager.setDocumentCookies();
 		window.location.reload();
	}
		
	this.WriteFlashObj = function(src, w, h) {
		var parr = new Array();
		parr[0] = {"Key":"swLiveConnect", "Value":"true"};
		parr[1] = {"Key":"wmode", "Value":"transparent"};
		ShowFlash("avatarflash", src, w, h, false, false, false, parr);
	}

	this.SetAvatarLink = function(_url) {
		this.url = _url;
	}
}



function GetHttpRequestObject() {
	var xmlHttp = null;
	// Mozilla, Opera, Safari sowie Internet Explorer 7
	if (typeof XMLHttpRequest != 'undefined') {
			xmlHttp = new XMLHttpRequest();
	}
	if (!xmlHttp) {
			// Internet Explorer 6 und älter
			try {
					xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
					try {
							xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
							xmlHttp  = null;
					}
			}
	}
	return xmlHttp;
}


function AvatarDialog() {
	this.Id = 0;
	this.Animation = "";
	this.Title = "";
	this.Text = "";
}

function gAvatarOnClick() {
	internavatar.OnClick();
}

