var group99 = null;
function sort_evs(a, b)
{
	return b.ID - a.ID
}

function sort_evs_back(a, b)
{
	return a.ID - b.ID
}

function Event_ev(id,ti,ty,uid,suid,mty,xid)
{
	var self=this;
	this.ID= id;
	this.TI= ti;
	this.TY= ty;
	this.UID= uid;
	this.SUID= suid;
	this.MTY= mty;
	this.XID= xid;
}

function Event_us(uid,ma1,ma2,ma3,ma4,ma5,sx,ol,un,ih,ip)
{
	this.UID=uid;
	this.MA1=ma1;
	this.MA2=ma2;
	this.MA3=ma3;
	this.MA4=ma4;
	this.MA5=ma5;
	this.MA1=((this.MA1 >= 0)?this.MA1:5)*1;
	this.MA2=((this.MA2 >= 0)?this.MA2:5)*1;
	this.MA3=((this.MA3 >= 0)?this.MA3:5)*1;
	this.MA4=((this.MA4 >= 0)?this.MA4:5)*1;
	this.MA5=((this.MA5 >= 0)?this.MA5:5)*1;
	this.SX=sx;
	this.OL=ol;
	this.UN=un;
	this.IH=ih;
	this.IP=ip;
}

var ev_ta = new Array
( 
	'',
	'hat gerade die Community betreten',
	'hat gerade die Community verlassen',
	'hat eine Blogseite besucht',
	'hat eine ID-Card besucht',
	'hat einen Kalender besucht',
	'hat eine Foto&uuml;bersicht besucht',
	'hat eine Fotoseite besucht',
	'','',
	'hat eine Blog&uuml;bersicht besucht',
	'hat eine Kleinanzeige besucht',
	'hat eine Kleinanzeigen&uuml;bersicht besucht',
	'hat eine Bookmark besucht',
	'hat eine Bookmark&uuml;bersicht besucht',
	'hat eine Videoseite besucht',
	'hat eine Video&uuml;bersicht besucht',
	'hat eine Pin-It-Seite besucht',
	'hat einen Group-Blog besucht',
	'hat ein Group-Foto eingestellt',
	'hat einen Blog geschrieben',
	'hat einen Kommentar verfa&szlig;t',
	'hat ein neues Foto eingestellt',
	'hat eine Kleinanzeige aufgegeben',
	'hat eine Bookmark eingestellt',
	'hat im Forum ein Thema erstellt',
	'hat im Forum einen Beitrag erstellt',
	'hat sein Pin-It ge&auml;ndert',
	'hat ein Post-It erstellt',
	'hat einen Group-Blog erstellt',
	'hat eine Nachricht verfa&szlig;t',
	'','','',''
)

var ev_tp = new Array
( 
	'', '', '',
	'hat eine Deiner Blogseiten besucht',
	'hat Deine ID-Card besucht',
	'hat Deinen Kalender besucht',
	'hat Deine Foto&uuml;bersicht besucht',
	'hat eine Deiner Fotoseiten besucht',
	'','',
	'hat Deine Blog&uuml;bersicht besucht',
	'hat eine Deiner Kleinanzeigen besucht',
	'hat Deine Kleinanzeigen&uuml;bersicht besucht',
	'hat eine Deiner Bookamrks besucht',
	'hat Deine Bookmark&uuml;bersicht besucht',
	'hat Deine Videoseite besucht',
	'hat Deine Video&uuml;bersicht besucht',
	'hat Deine Pin-It-Seite besucht',
	'hat Deinen Group-Blog besucht',
	'hat ein Group-Foto eingestellt', 
	'',
	'hat bei Deinen Inhalten einen Kommentar verfa&szlig;t',
	'', '', '', '',
	'hat ein Antwort auf Dein Thema geschrieben.',
	'', '', '',
	'hat Dir eine Nachricht gesendet',
	'hat Dich zum Chat eingeladen',
	'hat Deinen Chat angenommen',
	'hat Deinen Chat abgelehnt',
	'hat den Chat verlassen'
)

var Event =
{
	ev_uid: 0, ev_ma1: 5, ev_ma2: 5, ev_ma3: 5, ev_ma4: 5, ev_ma5: 5,
	ev_ev_last: 0,
	ev_ev_ar: new Array(),
	ev_us_ar: new Array(),
	ev_ne_ar: new Array(),
	ev_msgc: 0,
	ev_polling: false, // true = Blockversion - false = Steamversion
	ev_aktive: false,
	ev_div: '',
	ev_lastid: 0,
	ev_path: 'http://community.pferde.de/event/index.html',
	ev_ppath: 'http://community.pferde.de/content/',
	ev_ipath: 'http://images.pferde.de/userfiles/',
	ev_first_cycle: 15000,
	ev_cycle: 45000,
	ev_move_height: 180,
	ev_min_time: 2,
	ev_max_time: 10,
	evo: '',
	opos: 0,
	opTime: 0,
	Timer: '',
	duration: 400,
	onMoveComplete: '',
	moving: false,
	next_block: 0,
	first_call: true,
	outspool: false,

	el: '',
	onOnComplete: '',
	onOffComplete: '',
	onFlushComplete: '',

	// init
	init: function (div)
	{
		var obj=this;
		this.ev_div=div;
		if(this.ev_polling==true && this.ev_aktive==false)
		{
			window.setTimeout(function(){obj.get();}, obj.ev_first_cycle);
			this.ev_aktive==true;
		}
		//this.get();
		this.isloaded();
	},

	// ERSETZEN
	get: function ()
	{
		if(this.ev_polling==true)
		{
			var obj=this;
			new ajax(obj.ev_path, {onComplete:function(transport){
				obj.isloaded(transport);
			},evalObj:'Event.evo'});
			window.setTimeout(function(){obj.get();}, obj.ev_cycle);
		}
	},

	url_encode_link: function (val)
	{
		var res='';
		var hex='0123456789ABCDEF';
		for(i=0;i<val.length;i++)
		{
			var c=val.charAt(i);
			if((c>='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z'))
			{
				res+=c;
			}
			else if(c==' ')
			{
				res+='-';
			}
			else
			{
				var a=c.charCodeAt(0);
				res+=':';
				res+=hex.charAt((a>>4)%16);
				res+=hex.charAt(a%16);
				res+=':';
			}
		}
		return res;
	},

	do_disp: function ()
	{
		var din;
		// sort eventlist
		this.ev_ne_ar.sort(sort_evs_back);
		if( this.ev_ne_ar.length>0 && (din=this.get_evsn(this.ev_ne_ar[0].ID))>=0)
		{
			this.write_udiv(din,'db');
			this.ev_ne_ar.splice(0, 1);
			if(this.ev_ne_ar.length>0)
			{
				var obj=this;
				var vt=parseInt(Math.random()*(this.ev_max_time-this.ev_min_time));
				vt+=this.ev_min_time;
				this.outspool=true;
				window.setTimeout(function(){obj.do_disp();}, vt*1000);
			}
			else
			{
				this.outspool=false;
			}
		}
	},

	do_disp_stream: function ()
	{
		var din;
		if( this.ev_ne_ar.length>0 && (din=this.get_evsn(this.ev_ne_ar[0].ID))>=0)
		{
			//alert(this.ev_ne_ar[0].ID);
			this.write_udiv(din,'stream');
			this.ev_ne_ar.splice(0, 1);
			var obj=this;
			this.outspool=true;
			window.setTimeout(function(){obj.do_disp_stream();}, 1800);
		}
	},

	copy_udiv: function (src,dest)
	{
		setInnerHTML('evub_'+dest,$('evub_'+src).innerHTML);
		var div = Element.find($('evub_'+dest), 'nextSibling');
	},

	write_udiv: function (ev_num,source)
	{
		var evu;
		var evv;
		var evsu;
		var ma;
		var evmsg;
		var imli = 'grafiken/user/user_default_m-small.jpg';
		var olc ='user_image-offline';
		
		if(ev_num<0 || ev_num>this.ev_ev_ar.length)
		{
			return false;
		}
		evv=this.ev_ev_ar[ev_num];
		evmsg='';
		if(ev_ta[evv.TY]!='')
		{
			evmsg=ev_ta[evv.TY];
		}
		if(evv.SUID>0 && evv.SUID==this.ev_uid && (evsu=this.get_us(evv.UID))!=null)
		{
			if(ev_tp[evv.TY]!='')
			{
				evmsg=ev_tp[evv.TY];
			}
		}
		if(evmsg!='')
		{
			evmsg='&hellip; '+evmsg;
		}
		else
		{
			return false;
		}

		evu=this.get_us(evv.UID);
		if(evu==null)
		{
			return false;
		}
		if(evu.IH=='')
		{
			if(evu.SX==1)
			{
				imli='grafiken/user/user_default_f-small.jpg';
			}
		}
		else
		{
			imli=this.ev_ipath+evu.IP+'/'+evu.IH+'_1.jpg';
		}
		if(evu.OL==1)
		{
			olc ='user_image-online';
		}
		if(this.next_block>5)
		{
			//Verschieben der Daten!!
			this.copy_udiv(3,0);
			this.copy_udiv(4,1);
			this.copy_udiv(5,2);
			this.opos=0;
			this.move_margin(this.opos);
			this.next_block=3;
		}
		var tl='';
		tl+='<div class="user" id="usm_'+evv.ID+'">\n';
		tl+='<div class="user_name"><a href="'+this.ev_ppath+'id/'+this.url_encode_link(evu.UN)+'.html">'+evu.UN+'</a></div>\n';
		tl+='<a href="'+this.ev_ppath+'id/'+this.url_encode_link(evu.UN)+'.html"><img src="'+imli+'" title="'+evu.UN+'" class="'+olc+'" /></a>\n';
		tl+='<div class="user_functions">\n';
		if(this.ev_uid!=evv.UID && this.ev_uid > 0)
		{
			tl+='<a href="javascript://" onclick="window.open(\''+this.ev_ppath+'nachrichten_neu.html?NAME='+URLEncode(evu.UN)+'\',\'nachrichten\',\'resizable=yes,width=750,height=550\'); return false;" class="send-message">&nbsp;</a>\n';
			if (evu.OL==1)
			{
				if(evv.TY==31)
				{
					//tl+='<a href="javascript://" onclick="ToolTip.get(\'chat_anfrage\',this,true,\'&EV_UID='+this.ev_uid+'&EVU_UID='+evu.UID+'&EVU_UN='+evu.UN+'\');" class="chat">&nbsp;</a>';
					tl+='<div class="chat-ia">&nbsp;</div>';
					if (source == 'stream') playmovie('chatanfrage');
					new ajax(PAJAX+'o2ochat_list.html', {postBody:'',update: 'o2ochat',onComplete: function(){o2o_chat_status();}})
				}
				else
				{
					if(evv.TY==34) new ajax(PAJAX+'o2ochat_list.html', {postBody:'',update: 'o2ochat',onComplete: function(){o2o_chat_status();}})
					tl+='<a href="javascript://" onclick="o2o_chat_popup('+this.ev_uid+','+evu.UID+'); return false;" class="chat">&nbsp;</a>';
				}
			}
			else
			{
				tl+='<div class="chat-ia">&nbsp;</div>';
			}
		}
		else
		{
			tl+='<div class="send-message-ia">&nbsp;</div>';
			tl+='<div class="chat-ia">&nbsp;</div>';
		}
		tl+='<a href="'+this.ev_ppath+'id/'+this.url_encode_link(evu.UN)+'.html" class="view-id-card">&nbsp;</a>\n';
		if(this.ev_uid!=evv.UID && this.ev_uid > 0)
		{
			ma=(50-(Math.abs(evu.MA1-this.ev_ma1) + Math.abs(evu.MA2-this.ev_ma2) +Math.abs(evu.MA3-this.ev_ma3) +Math.abs(evu.MA4-this.ev_ma4) +Math.abs(evu.MA5-this.ev_ma5)))*2;
			tl+='<div class="user_matchfield">'+ma+'%</div>\n';
		}
		else
		{
			tl+='<div class="user_matchfield-ia">&nbsp;</div>\n';
		}
		tl+='</div>\n';
		tl+='<div class="clear"></div>\n';
		tl+='</div>\n';
		tl+='<p id="msg_'+evv.ID+'">'+evmsg+'</p>\n';
		tl+='<div class="clear vspace"></div><div class="vspace"></div><div class="devider"></div>\n';
		setInnerHTML('evub_'+this.next_block,tl);
		if(this.next_block>2)
		{
			var toflush = 0;
			// post an mich
			if(evv.SUID>0 && evv.SUID==this.ev_uid)
			{
				toflush = evv.ID;
				if(evv.TY==30)
				{
					this.ev_msgc++;
					setInnerHTML('ev_msgc','&rsaquo; Neue Nachrichten: '+this.ev_msgc);
					setClass('ev_msgc','oranged');
				}
			}
			this.move_top(toflush);
		}
		this.next_block++;
		return true;
	},

	get_us: function (uid)
	{
		var i;
		var j=this.ev_us_ar.length;
		for (i=0;i<j;i++)
		{
			if(uid==this.ev_us_ar[i].UID)
			{
				return this.ev_us_ar[i];
			}
		}
		return null;
	},

	get_evsn: function (id)
	{
		var i;
		var j=this.ev_ev_ar.length;
		for (i=0;i<j;i++)
		{
			if(id==this.ev_ev_ar[i].ID)
			{
				return i;
			}
		}
		return -1;
	},

	ins_ev: function (id,ti,ty,uid,suid,mty,xid)
	{
		if(id <=this.ev_ev_last)
		{
			return false;
		}
		this.ev_ev_ar.push(new Event_ev(id,ti,ty,uid,suid,mty,xid));
		return true;
	},

	ins_us: function (uid,ma1,ma2,ma3,ma4,ma5,sx,ol,un,ih,ip)
	{
		var i;
		var j=this.ev_us_ar.length;
		for (i=0;i<j;i++)
		{
			if(uid==this.ev_us_ar[i].UID)
			{
				// update online-flag
				this.ev_us_ar[i].OL=ol;
				return;
			}
		}
		this.ev_us_ar.push(new Event_us(uid,ma1,ma2,ma3,ma4,ma5,sx,ol,un,ih,ip));
	},

	isloaded: function (transport)
	{
		var i;
		var evo=this.evo;
		var j=evo.EUN;
		for (i=0;i<j;i++)
		{
			if(evo.UUID[i]==this.ev_uid)
			{
				this.ev_ma1=evo.MA1[i];
				this.ev_ma2=evo.MA2[i];
				this.ev_ma3=evo.MA3[i];
				this.ev_ma4=evo.MA4[i];
				this.ev_ma5=evo.MA5[i];
					
				this.ev_ma1=((this.ev_ma1 >= 0)?this.ev_ma1:5)*1;
				this.ev_ma2=((this.ev_ma2 >= 0)?this.ev_ma2:5)*1;
				this.ev_ma3=((this.ev_ma3 >= 0)?this.ev_ma3:5)*1;
				this.ev_ma4=((this.ev_ma4 >= 0)?this.ev_ma4:5)*1;
				this.ev_ma5=((this.ev_ma5 >= 0)?this.ev_ma5:5)*1;
			}
			this.ins_us(evo.UUID[i],evo.MA1[i],evo.MA2[i],evo.MA3[i],evo.MA4[i],evo.MA5[i],evo.SX[i],evo.OL[i],evo.UN[i],evo.IH[i],evo.IP[i]);
		}

		var gr=0;
		j=evo.EVN;
		for (i=0;i<j;i++)
		{
			if(gr<evo.ID[i])
			{
				gr=evo.ID[i];
			}
			if(this.ins_ev(evo.ID[i],evo.TI[i],evo.TY[i],evo.UID[i],evo.SUID[i],evo.MTY[i],evo.XID[i])==false)
			{
				break;
			}
		}
		this.ev_ev_ar.sort(sort_evs);
//setInnerHTML('ev_xx',gr+ ' '+this.ev_ev_last);
		if(this.first_call==true)
		{
			// erste abfrage der daten... ausgabe der ersten 3
			this.first_call=false;
			var x=0;
			for(i=0;i<j;i++)
			{
				this.next_block=2-x;
				if(this.write_udiv(i)==true) x+=1;
				if(x>2) break;
			}
			this.next_block=3;
		}
		else if(this.ev_ev_last<gr)
		{
			// es sind neue events da; diese ausgeben
			j=this.ev_ev_ar.length;
			for(i=0;i<j;i++)
			{
				if(this.ev_ev_ar[i].ID>this.ev_ev_last)
				{
					if(this.ev_ev_ar[i].UID==this.ev_uid && this.ev_ev_ar[i].TY>2)
					{
						continue;
					}
					this.ev_ne_ar.push(this.ev_ev_ar[i]);
				}
				else
				{
					break;
				}
			}
			if(this.outspool==false && this.ev_ne_ar.length>0)
			{
				var obj=this;
				window.setTimeout(function(){obj.do_disp();}, 10);
			}
		}

		// store last managed event
		if(gr>this.ev_ev_last) this.ev_ev_last=gr;
	},

	move_top: function (toflush)
	{
		var obj=this;
		if(this.div!='' && this.moving==false)
		{
			var div_obj=$(this.div);
			this.opTime=(new Date).getTime();
			this.Timer = setInterval (function(){obj.move_time(toflush);}, 15);
			this.moving=true;
		}
	},

	move_margin: function(val)
	{
		$(this.ev_div).style.marginTop='-'+val+'px';
	},

	move_time: function(toflush)
	{
		if(this.Timer)
		{
			var time  = (new Date).getTime();
			if(time>=this.opTime+this.duration)
			{
				clearInterval(this.Timer);
				this.Timer=null;
				this.opos+=this.ev_move_height;
				this.move_margin(this.opos);
				if(toflush>0)
				{
					PlugIn.flush('usm_'+toflush,'#23982b');
				}
				if(this.onMoveComplete)
				{
					this.onMoveComplete();
				}
				this.moving=false;
			}
			else
			{
				var mh=(time - this.opTime) / this.duration *this.ev_move_height;
				this.move_margin(mh+this.opos);
			}
		}
	},

	move_end: function()
	{
	}
}

function extractIFrameBody(iFrameEl) 
{
	var doc = null;
	if (iFrameEl.contentDocument)
	{ // For NS6
		doc = iFrameEl.contentDocument; 
	} 
	else if (iFrameEl.contentWindow) 
	{ // For IE5.5 and IE6
		doc = iFrameEl.contentWindow.document;
	} 
	else if (iFrameEl.document) 
	{ // For IE5
		doc = iFrameEl.document;
	} 
	else 
	{
		alert("Error: could not find sumiFrame document");
		return null;
	}
	return doc.body;
}

// VERTEILER
function controller()
{
	window.setTimeout(function()
	{
		doc=extractIFrameBody($('bb'));
		
		temp = doc.innerHTML;
		if(temp.length>0 && temp.length == doc.innerHTML.length)
		{
			doc.innerHTML='';
		}
		
		temp = temp.replace(/\n/g,"");
		var str_len = temp.lastIndexOf("|||");
		
		if (str_len > 0)
		{
		
			var checkarray = new Array();

			var strarray = temp.split('|||');

			for (i=0; i<strarray.length; i++){
	
				if (strarray[i].substr(0,4) == '<br>' || strarray[i].substr(0,4) == '<BR>')
				{
					strarray[i] = strarray[i].substr(4);
				}

				var zuweiser = strarray[i].substr(0,1);
				strarraysplit = strarray[i].split(" ");
		
				if (zuweiser == 'C')
				{
					//if (eval('!'+strarraysplit[2])) eval(strarraysplit[2]+';');
					/*if (eval('typeof '+strarraysplit[2]+'.chat_splitter') == 'function' || eval('typeof '+strarraysplit[2]+'.chat_splitter') == 'object')
					{
						eval((strarraysplit[2])+".chat_splitter(strarray[i])");
					}*/
					//if (eval('!'+strarraysplit[2])) eval(strarraysplit[2]+';');

					/*if (group99!=null && (typeof group99.chat_splitter == 'function' || typeof group99.chat_splitter == 'object'))
					{
						group99.chat_splitter(strarray[i]);
					}*/
					
					if (opengroups.inArray(strarraysplit[2]))
					{
						eval((strarraysplit[2])+".chat_splitter(strarray[i])");
					}
					
				}
				else if (zuweiser == 'E')
				{
					strarraysplit = strarray[i].split(" ");					
					checkarray[3] = strarray[i].substr(2,1);
		
					UUID = strarraysplit[2]; // User-ID
					MA1 = strarraysplit[11]; // Match1
					MA2 = strarraysplit[12]; // Match2
					MA3 = strarraysplit[13]; // Match3
					MA4 = strarraysplit[14]; // Match4
					MA5 = strarraysplit[15]; // Match5
					SX = strarraysplit[8]; // Geschlecht
					OL = ((checkarray[3] == 'O')?'1':'0'); // Onlinestatus
					UN = strarraysplit[3]; // Username
					IH = strarraysplit[9]; // Bild
					IP = strarraysplit[10]; // Pfad
					ID = strarraysplit[16]; // fortlaufende ID
					TI = strarraysplit[2]; // Timestamp - MSG-ID
					TY = strarraysplit[5]; // Eventtyp
					UID = strarraysplit[2]; // User-ID
					SUID = strarraysplit[4]; // User-ID des Events
					MTY = strarraysplit[6]; // Medientyp
					XID = strarraysplit[7]; // Medienid
					
					MA1=((MA1 >= 0)?MA1:5)*1;
					MA2=((MA2 >= 0)?MA2:5)*1;
					MA3=((MA3 >= 0)?MA3:5)*1;
					MA4=((MA4 >= 0)?MA4:5)*1;
					MA5=((MA5 >= 0)?MA5:5)*1;
					
					Event.ins_us(UUID,MA1,MA2,MA3,MA4,MA5,SX,OL,UN,IH,IP);
					Event.ins_ev(ID,TI,TY,UID,SUID,MTY,XID);
					var this_last_id = Event.ev_ev_ar.length-1;
					var ev_last_id = Event.ev_ne_ar.length;
					//alert(ID + '-' + Event.ev_ev_ar[0].ID);
					if (Event.ev_ev_ar[0].ID != null && ID > Event.ev_ev_ar[0].ID && UID != Event.ev_uid)
					{
						Event.ev_ne_ar[ev_last_id] = (Event.ev_ev_ar[this_last_id]);
						Event.do_disp_stream();
					}
				}
			}
		}

		controller();
	},500);
}
controller();
