//  August Chat System
//  Copyright (c) 2000-2010 by August
//  www:  http://www.august4u.ru/
//  file: august.who.js

var TT=0

UserList = {
	$user: {
		NICK:		function () { return this.$u.a ? "<a class=n i=" + this.$u.id + " title='" + this.$nick (1) + "' onClick='return false' href=>" + this.$nick () + "</a>" : "<span class=n>" + this.$nick () + "</span>" },
		ICON:		function () { if (!this.$u.r) return ""; return this.$info (this.$u.ui ? this.$icon () : ((cfg.InfoPic [1].length && this.$u.s & 0x08000000) ? this.$img (1, this.$alt (cfg.InfoPic [1], this.$nick (1))) : (this.RANK_ICON () || this.$img (0, this.$alt (cfg.InfoPic [0], this.$nick (1)))))) },
		RANK:		function () { if (!this.$u.r) return ""; var r = this.$rank (); return r ? r.t : "" },
		RANK_ICON:	function () { if (!this.$u.r) return ""; var r = this.$rank (); return r ? r.p ? AUGUST_HTML.img ("", "/images/" + r.p, r.w, r.h, 0, "", "", "", r.t) : this.$img (0, r.t) : "" },
		RANK_ICON_INFO:	function () { var r = this.RANK_ICON (); return r ? this.$info (r) : "" },
		USER_ICON:	function () { return this.$u.ui ? this.$icon () : "" },
		USER_ICON_INFO:	function () { return this.$u.ui ? this.$info (this.$icon ()) : "" },
		STAT:		function () { if (this.$v) return this.$vote (); var s = (this.$u.s >> 12) & 0x3F; return this.$u.v == 7 ? "" : this.$u.us ? "<span class=s>" + this.$u.p3.replace (AUGUST_HTML.SMILE_RE, AUGUST_HTML.smile) + "</span>" : (s && isset (cfg.UserStatus [s])) ? "<span class=s>" + cfg.UserStatus [s].s [this.$u.x].replace (AUGUST_HTML.SMILE_RE, AUGUST_HTML.smile).toLowerCase () + "</span>" : "" },
		STAT_ICON:	function () { var s = (this.$u.s >> 12) & 0x3F; return (isset (cfg.UserStatus [s]) && isset (cfg.UserStatus [s].i)) ? AUGUST_HTML.img ("", "/images/" + cfg.UserStatus [s].i, cfg.UserStatus [s].w, cfg.UserStatus [s].h, 0, "", "", "", cfg.UserStatus [s].s [this.$u.x].replace (AUGUST_HTML.SMILE_RE, " ").trim ()) : "" },
		USER_STAT:	function () { if (this.$v) return this.$vote (); return (this.$u.v != 7 && this.$u.us) ? "<span class=s>" + this.$u.p3.replace (AUGUST_HTML.SMILE_RE, AUGUST_HTML.smile) + "</span>" : "" },
		TORT:		function () { return this.$u.s & 0x00800000 ? this.$img (4) : "" },
		INVISIBLE:	function () { return this.$u.s & 0x01000000 ? this.$img (5, "invisible") : "" },
		SEX:		function () { return this.$u.x == 1 ? this.$img (2) : this.$u.x == 2 ? this.$img (3) : "" },
		FLAG:		function () { var co = this.$co (); var name = this.$country (co); return co ? AUGUST_HTML.img ("", "/images/flags/" + co + ".gif", 29, 20, 0, "", "", "", name || co) : "" },
		FLAG_INFO:	function () { return this.$u.r ? this.$info (this.FLAG ()) : this.FLAG () },
		SMALL_FLAG:	function () { var co = this.$co (); var name = this.$country (co); return co ? AUGUST_HTML.img ("", "/images/small-flags/" + co + ".gif", 18, 12, 0, "", "", "", name || co) : "" },
		SMALL_FLAG_INFO:function () { return this.$u.r ? this.$info (this.SMALL_FLAG ()) : this.SMALL_FLAG () },
		CO:		function () { return this.$co () },
		COUNTRY:	function () { return this.$country (this.$co ()) },
		PHOTO:		function () { return this.$u.p & 0x0400 ? " " : "" },
		ODDEVEN:	function () { return this.$i & 1 },
		DESIGN:		function () { return root.Chat.Design },

		ENTER:		function () { return this.$u.d [0] },
		QUIT:		function () { return this.$u.d [1] },
		LONG_AGO:	function () { return this.$u.d [2] },
		HOW_LONG:	function () { return this.$u.d [3] },
		COUNT1:		function () { return this.$u.d [4] },
		COUNT2:		function () { return this.$u.d [5] },
		COUNT3:		function () { return this.$u.d [6] },

		$size:		function () { return this.$LIST.length },
		$set:		function ( i ) { this.$u = this.$LIST [i]; this.$i = i },
		$setList:	function ( i ) { this.$LIST = this.$DATA [i]; this.$v = i == 3 },
		$nick:		function ( t ) { return AUGUST_HTML.nick (this.$u.n, t) },
		$alt:		function ( t, n ) { return t.replace ("%NICK%", n) },
		$rank:		function () { return cfg.Ranks ? cfg.Ranks [this.$u.s & 0xff] : null },
		$icon:		function () { var ext = ["", ".gif", ".jpg", ".png"]; var r = this.$rank (); var p = this.$u.ui [0].toString (16); var i = this.$u.ui [1].toString (16); return AUGUST_HTML.img ("", "/people/icon/" + "000000".substr (p.length) + p + "00000".substr (i.length) + i + ext [(this.$u.s >> 18) & 3], (this.$u.ui [1] & 0x7F) + 3, ((this.$u.ui [1] >> 7) & 0x7F) + 3, 0, "", "", "", r ? r.t : this.$alt ((cfg.InfoPic [1].length && this.$u.s & 0x08000000) ? cfg.InfoPic [1] : cfg.InfoPic [0], this.$nick (1))) },
		$info:		function ( i ) { return "<a onClick='return UserList.userInfo (" + this.$u.id + ")' onMouseOver='status=\"" + this.$alt (cfg.InfoPic [0], this.$nick (1)) + "\"; return true' href=>" + i + "</a>" },
		$vote:		function () { var s = (this.$u.v - 1) & 3; var Stat = root.cfg.VoteMess [s]; if (s == 0) Stat = Stat.mreplace (["<*", "<*", "<*"], ["<a onClick='return root.August.vote (" + this.$u.id + ")' href=>", "<a onClick='return root.August.vote (" + this.$u.id + ", 1)' href=>", "<a onClick='return root.August.vote (" + this.$u.id + ", 2)' href=>"]); else if (s == 3) Stat = Stat.replace ("<*", "<a onClick='return root.August.ignore (" + this.$u.id + ", 4)' href=>"); return Stat.mreplace ([/\*>/g, "TIME", "VOTERS", "YES"], ["</a>", this.$u.p3, this.$u.p3 & 0xFFFF, this.$u.p3 >> 16]) },
		$img:		function ( i, title ) { return (this.$IMG [i].complete && this.$IMG [i].width) ? AUGUST_HTML.img ("", this.$IMG [i].src, this.$IMG [i].width, this.$IMG [i].height, 0, "absmiddle", "", "", title) : "" },
		$co:		function () { var a1 = (this.$u.p >> 5) & 0x1f; var a2 = this.$u.p & 0x1f; return (a1 || a2) ? String.fromCharCode (a1 + 0x61) + String.fromCharCode (a2 + 0x61) : "" },
		$country:	function ( co ) { return (window.CountryName && co) ? CountryName [co.toUpperCase ()] : "" },

		$u:		null,
		$LIST:		null
	},
	$list: {
		CAPTION:	function () { return AUGUST_HTML.html (cfg.Order [this.$i * 2 + 1].tpl ({ DESIGN: UserList.$user.DESIGN })) },

		$size:		function () { return cfg.Order.length >> 1 },
		$set:		function ( i ) { this.$i = i; this.$user.$setList (cfg.Order [i * 2]) },
		$i:		0,
		$user:		null
	},

	online: function ( Auth, List, Reset ) {
		if (window.wwList || !this.$list.$user)
			return
		if (Reset)
			clearTimeout (this.$tr)
		if (!List) {
			$(window, "who").innerHTML = this.$INNER ? cfg.UserText : ""
			return
		} else if (!List.length) {
			$(window, "who").innerHTML = cfg.None.tpl ({ DESIGN: UserList.$user.DESIGN })
			return
		}
		var Multi = cfg.Order.length >= 6
		var DATA = [[], [], [], []]
		var Total = 0
		for (var i = 0; i < List.length; ) {
			var s = List [i + 2]
			var x = (s >> 20) & 3
			var v = (s >> 29) & 0x07
			var us = s & 0x02000000 ? 1 : 0
			var ui = s & 0x000C0000
			var u = { n: "" + List [i] + "", id: List [i + 1] & 0xFFFF, p: List [i + 1] >> 16, s: s, x: x, v: v, us: us, p3: List [i + 3], ui: ui ? [List [i + 3 + us], List [i + 4 + us]] : 0, a: Auth, r: s & 0x00400000 }
			if (v != 4)
				DATA [Multi ? x : 0].push (u)
			if (Auth && v)
				DATA [3].push (u)
			i += (ui ? 5 : 3) + us
			Total++
		}
		this.$user.$DATA = DATA

do {
var hh = cfg.Table.pattern ([this.$list, this.$user])
var tt = hh.tpl ({ TOTAL: function () { return Total }, DESIGN: UserList.$user.DESIGN })
TT++
} while (TT< 17)

if (ChatID == "000001" && window.opera)
root.out (hh.replace (/</g, "&lt;") + "<hr>", 0)

		$(window, "who").innerHTML = tt
	},
	offline: function ( List ) {
		if (!List.length) {
			$(window, "who").innerHTML = wwNone
			return
		}
		if (wwOrder.length < 2)
			wwOrder = [0, ""]
cfg.Order = wwOrder
cfg.InfoPic = [wwInfoPic, wwKillerPic]
		var Multi = wwOrder.length == 6
		var DATA = [[], [], []]
		for (var i = 0; i < List.length; i++) {
			var Data = List [i]
			var n = Data.shift ()
			var p = Data.shift ()
			var s = Data.shift ()
			var x = (s >> 20) & 3
			var ui = s & 0x000C0000 ? [Data.shift (), Data.shift ()] : null
			DATA [Multi ? x : 0].push ({ n: "" + n + "", id: p & 0xFFFF, p: p >> 16, s: s, x: x, d: Data, ui: ui, r: s & 0x00400000 })
		}
		this.$user.$DATA = DATA
		$(window, "who").innerHTML = wwTable.pattern ([this.$list, this.$user]).tpl ({ TOTAL: function () { return List.length } })
	},
	read: function ( ex ) {
		var self = this
		var js = document.createElement ("SCRIPT")
		js.src = "/august?a=24&r=" + Chat.Room + "&id=" + root.User.ID + (ex ? "&" + ex : "") + "&" + august_now ()
		document.getElementsByTagName ("HEAD") [0].replaceChild (js, this.$query)
		this.$query = js
		this.$tr = setTimeout (function () { self.read () }, 30000)
	},
	roomMenuInit: function () {
		function setRoom () {
			if (!this.selectedIndex || this.value == Chat.Room)
				return
			$(window, "who").innerHTML = ""
			Chat.Room = cfg.Rooms [this.value].id
			clearTimeout (self.$tr)
			self.read ()
		}

		var Menu = $(window, "roomMenu")
		if (!Menu)
			return
		var self = this
		var rm = "<option>" + Menu.innerHTML
		for (var i = 0; i < cfg.Rooms.length; i++)
			rm += "<option value=" + i + ">" + cfg.Rooms [i].n
		var go = this.$INNER ? "<input " + STYLE_BTN + " name=go type=button value='>>>'>" : ""
		Menu.innerHTML = "<form name=room><nobr><select name=room " + STYLE_INP + ">" + rm + "</select>" + go + "</nobr></form>"
		document.room.room.value = Chat.Room
		document.room.room.onchange = setRoom
		if (this.$INNER)
			document.room.go.onclick = function () { root.goRoom (cfg.Rooms [document.room.room.value]); clearTimeout (this.$tr); setTimeout (function () { self.read () }, 500) }
	},
	userInfo: function ( ID ) {
		if (this.$INNER)
			return root.August.userInfo (ID, 0, 1)
		august_wo ("/people/info?id2=" + ID, "INFO_0_" + ChatID + "_" + Chat.Room + "_" + ID, cfg.pwinWidth, cfg.pwinHeight, 3)
		return false
	},
	init: function ( Run, List ) {
		function run ( run ) {
			function preLoad () {
				for (var i = 0, c = 0; i < IMG.length; i++)
					c += IMG [i].complete ? 1 : 0
				if (c == IMG.length || ++_tic == 150)
					self [run] (List)
				else
					setTimeout (preLoad, 100)
			}
			var _tic = 0
			preLoad ()
		}
		function click ( e ) {
			for (var el = e ? e.target : event.srcElement; el; el = el.parentNode) {
				if (el.tagName == "TR" && el.className.indexOf ("line") != -1) {
					if (Cur && Cur != el)
						Cur.id = ""
					Cur = el.id == "sel" ? 0 : el
					el.id = el.id == "sel" ? "" : "sel"
					return false
				}
			}
		}

		try {
			window.root = window.opener ? (opener.root ? opener : (opener.parent.root ? opener.parent : window)) : (parent.root ? parent : window)
		} catch ( e ) {
			window.root = window
		}
		this.$INNER = root != window

		if (this.$INNER) {
			this.cm = root.getCMenu (window, root.Chat)
			if (!opener)
				AugustNav = root.getNav (window)
			if (root.clickHandler)
				root.clickHandler (root.August, window, function ( el ) { return root.menuClick.call (el) })
		} else {
			window.User = { Set: 0, ID: "" }
		}

		if ($(window, "title"))
			document.title = $(window, "title").innerHTML.text ()
		this.roomMenuInit ()
		this.$query = document.createElement ("SCRIPT")
		document.getElementsByTagName ("HEAD") [0].appendChild (this.$query)
		august_noselect (document.body)

		var R = []
		for (var i = 0; i < cfg.Ranks.length; i++) {
			R [cfg.Ranks [i].id] = cfg.Ranks [i]
			if (cfg.Ranks [i].p)
				cfg.Pics [cfg.Pics.length] = cfg.Ranks [i].p
		}
		cfg.Ranks = R
		var IMG = []
		for (var i = 0; i < cfg.Pics.length; i++) {
			IMG [i] = new Image ()
			IMG [i].src = "/images/" + cfg.Pics [i]
		}
		this.$user.$IMG = IMG
		this.$list.$user = this.$user
		this.$tr = 0
		var self = this
		var Cur = 0
		$(window, "who").onclick = click
		run (Run)
	}
}

august_start (window, function () {
	UserList.init (window.wwList ? "offline" : "read", window.wwList)
})
