//
// This should be in the global scope
function showOnlyTypeCustom(id) {
	roomListHandler.showOnlyType(id);
}
function RoomListHandler(queryUrl, refreshInterval, modelsPerScreen, modelsPerRow, ulang, isAuth) {
	//
	// Public properties
	this.queryUrl = queryUrl;
	this.refreshInterval = refreshInterval;
	this.modelsPerScreen = modelsPerScreen;
	this.modelsPerRow = modelsPerRow;
	
	this.translations = {
		NowLive				: '',
		NowNotLive			: '',
		NowInVip				: '',
		ModelsPage			: '',
		WriteMe				: '',
		MyPictures			: '',
		NoRoomsInCategory	: ''
	};
	
	//
	// Private fields
	this.cat = null;
	this.req = null;
	this.startIndex=0;
	this.userLanguage = ulang;
	this.isAuthenticated = isAuth;
	//
	// Public functions
	this.setTranslations = function(arr) {
		this.translations.NowLive				= arr[0];
		this.translations.NowNotLive			= arr[1];
		this.translations.ModelsPage			= arr[2];
		this.translations.WriteMe				= arr[3];
		this.translations.MyPictures			= arr[4];
		this.translations.NoRoomsInCategory	= arr[5];
		this.translations.NowInVip				= arr[6];
	}
	this.setUserLanguage = function(lang) {
		this.userLanguage = lang;
	}
	this.setAuthenticated = function(state) {
		this.isAuthenticated = state;
	}
	this.showOnlyType = function(catid) {
		this.startIndex = 0;
		this.getSelector(this.cat).className = '';
		this.getSelector(catid).className = 'selected';
		this.cat = catid;
		this.getRoomList(catid, false);
	}
	this.getRoomList = function(cat, scheduled) {
		var self = this;
		this.req = new XMLRequest(true, 'XML');
		if (scheduled) this.req.setEventHandler(function(state, response){self.onXMLStateWithSchedule(state, response)});
		else this.req.setEventHandler(function(state, response){self.onXMLState(state, response)});
		this.req.send(queryUrl + ((typeof cat!='undefined')&&(cat!=0)?('?cat=' + cat):''), '');
	}
	this.refreshList = function(newCat) {
		if (typeof newCat !== 'undefined') this.cat = newCat;
		this.getRoomList(this.cat, true);
	}
	this.getSelector = function(id) {
		return document.getElementById('catselector_'+((typeof id != 'undefined')&&(id!=0)?id:'all'));
	}
	this.showStartingFrom = function(idx) {
		this.startIndex = idx;
		this.getRoomList(this.cat, false);
	}
	this.onXMLStateWithSchedule = function(state, response) {
		if (state == 4) {
			var self = this;
			this.onXMLState(state, response);
			setTimeout(function(){self.refreshList()}, this.refreshInterval);
		}
	}
	this.createTable = function(padding, spacing, className) {
		var qwe = document.createElement("table");
		qwe.border = 0;
		qwe.cellPadding = padding;
		qwe.cellSpacing = spacing;
		if (typeof className != 'undefined') qwe.className = className;
		qwe.setAttribute("align","center");
		return qwe;
	}
	this.createTableBody = function(table) {
		var qwe = document.createElement("tbody");
		table.appendChild(qwe);
		return qwe;
	}
	this.createTableRow = function(tbody) {
		var qwe = document.createElement("tr");
		tbody.appendChild(qwe);
		return qwe;
	}
	this.createTableCell = function(row, className, colspan, valign) {
		var qwe = document.createElement("td");
		if (typeof className != 'undefined') qwe.className = className;
		if (typeof colspan != 'undefined') qwe.colSpan = colspan;
		if (typeof valign != 'undefined') qwe.vAlign = valign;
		row.appendChild(qwe);
		return qwe;
	}
	this.createImage = function(container, src, alt, className, width, height, isBlock) {
		var qwe = document.createElement("img");
		qwe.src = src;
		qwe.border = 0;
		qwe.alt = alt;
		if (typeof width != 'undefined') qwe.width = width;
		if (typeof height != 'undefined') qwe.height = height;
		if (typeof className != 'undefined') qwe.className = className;
		if (isBlock) qwe.style.display = "block";
		container.appendChild(qwe);
		return qwe;
	}
	this.createLink = function(container, href, className, text) {
		var qwe = document.createElement("a");
		if (typeof href == 'function') {
			qwe.href = '#';
			qwe.onclick=href;
		} else qwe.href = href;
		if (typeof className != 'undefined') qwe.className = className;
		qwe.appendChild(document.createTextNode(text));
		container.appendChild(qwe);
		return qwe;
	}
	this.createParagraph = function(className, text) {
		var qwe = document.createElement("p");
		if (typeof className != 'undefined') qwe.className = className;
		qwe.appendChild(document.createTextNode(text));
		return qwe;
	}
	this.createDIV = function(container, className, align, text) {
		var qwe = document.createElement("div");
		if (typeof className != 'undefined') qwe.className = className;
		if (typeof text != 'undefined') qwe.appendChild(document.createTextNode(text));
		if (typeof align != 'undefined') qwe.setAttribute("align", align);
		container.appendChild(qwe);
		return qwe;
	}
	this.getNamedElementNode = function(node, elemName) {
		var list =  node.getElementsByTagName(elemName);
		if (list.length == 1) {
			return list[0];
		} else return false;
	}
	this.showAward = function(container, name, readmore) {
		container.style.padding = "0px";
		var insideTable = this.createTable(0, 0, "modelsheet contest");
		//insideTable.style.width = '391px';
		var insideTableBody = this.createTableBody(insideTable);
		var insideRow = this.createTableRow(insideTableBody);
		var insideCell = this.createTableCell(insideRow, "left");
		embed = document.createElement("embed");
		embed.setAttribute("quality", "high");
		embed.setAttribute("menu", "false");
		if (name != '') {
			//insideCell.style.width = '205px';
			embed.setAttribute("width", 130);
			embed.setAttribute("src", "/swf/award2.swf");
		} else {
			embed.setAttribute("width", 315);
			embed.setAttribute("src", "/swf/award4.swf");
		}
		embed.setAttribute("height", 127);
		embed.setAttribute("wmode", "transparent");
		embed.setAttribute("type", "application/x-shockwave-flash");
		embed.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
		embed.setAttribute("flashvars", "kupaURL=" + encodeURIComponent(readmore));
		insideCell.appendChild(embed);
		if (name != '') {
			insideCell = this.createTableCell(insideRow, "center");
			this.createLink(insideCell, readmore, "", name);
			insideCell.onclick = function(){location=readmore};
		}
		container.appendChild(insideTable);
	}
	this.showHappyHour = function(container, name, readmore) {
		container.style.padding = "0px";
		var insideTable = this.createTable(0, 0, "modelsheet");
		var insideTableBody = this.createTableBody(insideTable);
		var insideRow = this.createTableRow(insideTableBody);
		var insideCell = this.createTableCell(insideRow, "center");
		embed = document.createElement("embed");
		embed.setAttribute("src", "/swf/" + name + ".swf");
		embed.setAttribute("quality", "high");
		embed.setAttribute("menu", "false");
		embed.setAttribute("width", 315);
		embed.setAttribute("height", 126);
		embed.setAttribute("wmode", "transparent");
		embed.setAttribute("type", "application/x-shockwave-flash");
		embed.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
		embed.setAttribute("flashvars", "happyURL=" + encodeURIComponent(readmore));
		insideCell.appendChild(embed);
		container.appendChild(insideTable);
	}
	this.onXMLState = function(state, response) {
		if (state == 4) {
			var cnt = document.getElementById('dynamicArea');
			while (cnt.childNodes.length > 0) cnt.removeChild(cnt.childNodes[0]);
			if ((response.nodeName == 'roomquery') && response.firstChild) {
				i=0;
				var table = this.createTable(0, 0);
				var tbody = this.createTableBody(table);
				var shownContest = false;
				var shownHappy = false;
				this.showPagingButtons(tbody, response);
				while (response.childNodes[i]) {
					if (false || ((i >= this.startIndex) && ((i - this.startIndex) < (this.modelsPerScreen - (shownContest?1:0) - (shownHappy?1:0))))) {
						var row = this.createTableRow(tbody);
						for(var j=0; j<this.modelsPerRow; j++) {
							var cell = this.createTableCell(row, "center", 1, 'top');
							if (!shownContest && ((response.childNodes[0] && (response.childNodes[0].nodeName == 'contest')) || (response.childNodes[1] && (response.childNodes[1].nodeName == 'contest')))) {
								shownContest = true;
								var contestName = '';
								if (response.childNodes[0].getAttribute('showname') == '1') {
									for(var qwe=0; qwe < response.childNodes[0].childNodes.length; qwe++) {
										if ((contestName == '') || (response.childNodes[0].childNodes[qwe].getAttribute('lang') == this.userLanguage))
											contestName = response.childNodes[0].childNodes[qwe].firstChild.nodeValue;
									}
								}
								this.showAward(cell, contestName, response.childNodes[0].getAttribute('readmore'));
								if (i == 0) {
									i++;
								} else if (shownHappy && (i == 1)) {
									i++;
								}
							} else if (!shownHappy && ((response.childNodes[0] && (response.childNodes[0].nodeName == 'happyhour')) || (response.childNodes[1] && (response.childNodes[1].nodeName == 'happyhour')))) {
								shownHappy = true;
								var happyHourNode = response.childNodes[0]&&(response.childNodes[0].nodeName == 'happyhour')?response.childNodes[0]:response.childNodes[1];
								this.showHappyHour(cell, happyHourNode.getAttribute('type'), happyHourNode.getAttribute('readmore'));
								if (i == 0) {
									i++;
								} else if (shownContest && (i == 1)) {
									i++;
								}
							} else if (response.childNodes[i]) {
								var name = this.getNamedElementNode(response.childNodes[i], 'modelname');
								var image = this.getNamedElementNode(response.childNodes[i], 'modelimage');
								var room = this.getNamedElementNode(response.childNodes[i], 'online');
								var id = response.childNodes[i].getAttribute('modelid');
								var languages = new Array();
								var nodeList = response.childNodes[i].getElementsByTagName('lang');
								for(var l=0;l<nodeList.length;l++) languages.push(nodeList[l].firstChild.nodeValue)
								var insideTable = this.createTable(5, 0, "modelsheet");
								var insideTableBody = this.createTableBody(insideTable);
								var insideRow = this.createTableRow(insideTableBody);
								var insideCell = this.createTableCell(insideRow);
								insideCell.width = "122";
								insideCell.height = "92";
								if (j==0) insideCell.style.paddingLeft = '0px';
								if (image.firstChild) {
									var img = this.createImage(insideCell, image.firstChild.nodeValue, name.firstChild?name.firstChild.nodeValue:'', "modelimg");
									img.align="left";
								} else {
									this.createDIV(insideCell, "noimage", "center", "");
								}
								var insideCell = this.createTableCell(insideRow);
								insideCell.width = "183";
								insideCell.height = "97";
								insideCell.style.paddingBottom = "0px";
								if (j == this.modelsPerRow - 1) insideCell.style.paddingRight = '0px';
								
								this.createDIV(insideCell, "modelname", "left", name.firstChild?name.firstChild.nodeValue:'');
								var div = this.createDIV(insideCell, "flags_stars_buttons", "left");
								if (languages.length > 0) {
									for(var l=0; l<Math.min(5,languages.length); l++)
										this.createImage(div, "/images/flags/" + languages[l] + ".gif", languages[l], "flag", 20, 14);
								} else {
									div.appendChild(document.createTextNode(' '));
								}
								this.createImage(div, "images/stars_" + response.childNodes[i].getAttribute('modelrank') + ".gif", "", "star");
								this.createLink(div, "about-"+id+".html", "", this.translations.ModelsPage);
								this.createLink(div, "uzenetek-write.html?to="+id, "", this.translations.WriteMe);
								this.createLink(div, "kepek-"+id+".html", "", this.translations.MyPictures);
								this.createDIV(div, "clear");
								var insideRow = this.createTableRow(insideTableBody);
								var insideCell = this.createTableCell(insideRow, "", 2);
								if (j==0) insideCell.style.paddingLeft = '0px';
								if (j == this.modelsPerRow - 1) insideCell.style.paddingRight = '0px';
								if (room.firstChild) {
									this.createLink(insideCell, "online-"+room.firstChild.nodeValue+".html", "liveButton isLive", (room.getAttribute('vip')==1?this.translations.NowInVip:this.translations.NowLive));
								} else {
									this.createLink(insideCell, this.isAuthenticated?("uzenetek-write.html?to=" + id):("about-" + id + ".html"), "liveButton notLive", this.translations.NowNotLive);
								}
								cell.appendChild(insideTable);
								i++;
							}
						}
					} else i++;
				}
				this.showPagingButtons(tbody, response);
				cnt.appendChild(table);
			} else {
				cnt.appendChild(this.createParagraph("hiba2", this.translations.NoRoomsInCategory));
			}
		}
	}
	this.showPagingButtons = function(container, roomList) {
		function createFunc(self,j){return function(){self.showStartingFrom(j)};}var roomCount=0;while(roomList.childNodes[roomCount])roomCount++;
		if(roomCount>this.modelsPerScreen){
			var cell=this.createTableCell(this.createTableRow(container),"pagenumbers",this.modelsPerRow);
			var hasContest=roomList.childNodes[0]&&(roomList.childNodes[0].nodeName=='contest');
			var modelsPerScreen=this.modelsPerScreen-(hasContest?1:0);
			if(this.startIndex!=0){
				var link=this.createLink(cell,createFunc(this,this.startIndex-modelsPerScreen),'','«');
				link.rel='prev';
			}
			for(var j=0;j<roomCount;j+=modelsPerScreen){
				if((j>0)&&hasContest){
					hasContest=false;
					j++;
				}
				if(this.startIndex!=j)this.createLink(cell,createFunc(this,j),'',Math.round(j/modelsPerScreen)+1);
				else{
					var span=document.createElement('SPAN');
					span.appendChild(document.createTextNode(Math.round(j/modelsPerScreen)+1));
					cell.appendChild(span);
				}
			}
			if((this.startIndex+modelsPerScreen)<=roomCount){
				var link=this.createLink(cell,createFunc(this,this.startIndex+modelsPerScreen),'','»');
				link.rel='next';
			}
		}
	}
}
