
// activate debugging true/false
var showDebug = false;

function loadAcceleratedWardrobe() {
	loadDesktop( "accWardrobe", null, -1 );
}

/******************** 	BEGIN: AvatarList Object	 ********************/

function AvatarList()
{
	this.users       = new Array();
	this.randUsers   = new Array();
	this.onlineUsers = new Array();
	this.roomList    = new Array();
	this.maxAvatars  = 8;
	this.sUserList   = "";
	//set these to output html
	this.swfUrl = "";
	this.profileUrl = "";
	this.roomHtmlObj = null;
	this.roomHtml = "";
	this.htmlObj = null;
	this.avImgHtml = "";
	this.avNameHtml = "";
	this.avImgWidth = 92;
	this.avImgHeight = 107;
	// connection info for getting online users from the service
	this.SvcConn = null;
	this.SvcCallback;
	this.SvcAction = "doView";
	this.SvcCompID = "CHSD";
	this.SvcMimeType = "XML";
	//var sUrl='getURL.jhtml?url='+escape(sOnlineUsers+"&p="+iOnlinePage);
	//this.SvcURL  = "http://profile.dev.zwinky.com/zwinkyprofile/component.htm";
	this.SvcURL;
	this.svcXmlObj = null;
}
//end function AvatarList

//begin methods for AvatarList
AvatarList.prototype = {
	//-- SETTERS --//
	setMaxAvatars: function(num){
		this.maxAvatars = num;
	},
	
	setSwfUrl: function(url){
		this.swfUrl = url;
	},
	
	setProfileUrl: function(url){
		this.profileUrl = url;
	},
	
	setHtmlObj: function(htmlID){
		var obj = document.getElementById(htmlID);
		if ( obj ){
			this.htmlObj = obj;
		}//end if
	},
	
	setRoomHtmlObj: function(htmlID){
		var obj = document.getElementById(htmlID);
		if ( obj ){
			this.roomHtmlObj = obj;
		}//end if
	},
	
	setSvcUrl: function(url){
		this.SvcURL = url;
	},
	
	setSvcCallback: function(callback){
		this.SvcCallback = (callback);
	},
	
	setXmlObject: function(xmlObj){
		this.svcXmlObj = xmlObj;
	},
	
	//-- GETTERS --//
	getOnlineUsers: function(random){
		//f,n,r,j,p,s
		
		var url = "getURL.jhtml";
		var params = "";

		if ( random ){
			params += "?ACTION=" + this.SvcAction
				   + "&COMPID=" + this.SvcCompID
				   + "&MIME=" + this.SvcMimeType
				   + "&f=0"
				   + "&r=" + this.maxAvatars
				   + "&nocache=" + Math.random();
		} else {
			params += "?ACTION=" + this.SvcAction
				   + "&COMPID=" + this.SvcCompID
				   + "&MIME=" + this.SvcMimeType
				   + "&f=0"
				   + "&n=" + this.userStr
				   + "&nocache=" + Math.random();
		}//end if
		//MODIFIED FOR HARDCODED USERS
		//----------------------------------------------
		//url += "?url=" + escape(this.SvcURL + params);
		url='topRandomUsers.jsp';
		
		this.SvcConn = new HttpConnect('users',url);
		this.SvcConn.create();
		this.SvcConn.setCallback(this.SvcCallback);
		this.SvcConn.execute();
		
	},
	getFriends: function(){		
	//http://profile.dev.zwinky.com/zwinkyprofile/component.htm?ACTION=doView&MODID=m3&MIME=HTML&v=viewwd&l=all&s=8&username='+sUserName+'&p=0
	var url = "getURL.jhtml";
		var params = "";		
		params += "?ACTION=" + this.SvcAction
			   + "&MODID=m3"
			   + "&MIME=HTML"
			   + "&v=viewxml"
			   + "&username=" + this.userStr
			   + "&s=8"
			   + "&p=0"
			   + "&nocache=" + Math.random();
			   
		url += "?url=" + escape(this.SvcURL + params);
		
		this.SvcConn = new HttpConnect('friends',url);
		this.SvcConn.create();
		this.SvcConn.setCallback(this.SvcCallback);
		this.SvcConn.execute();
		
	},
	getRooms: function(){
		//component.htm?ACTION=doView&COMPID=CHSD&MIME=XML&f=1&p=0&s=20
		var url = "getURL.jhtml";
		var params = "";
		
		params += "?ACTION=" + this.SvcAction
			   + "&COMPID=" + this.SvcCompID
			   + "&MIME=" + this.SvcMimeType
			   + "&f=1"
			   + "&p=0"
			   + "&s=10"
			   + "&nocache=" + Math.random();
		url += "?url=" + escape(this.SvcURL + params);
		
		this.SvcConn = new HttpConnect('rooms',url);
		this.SvcConn.create();
		this.SvcConn.setCallback(this.SvcCallback);
		this.SvcConn.execute();
		
	},
	
	//-- parse the returned XML from getRooms --//
	parseXMLRooms: function(){
		
		var firstNode = this.svcXmlObj.getElementsByTagName("chsdsvc_return")[0];
		var errNode   = this.svcXmlObj.getElementsByTagName("error")[0];
		var roomNodes = firstNode.getElementsByTagName("room");

		if ( errNode ){
			if ( showDebug ){ traceData("parseXMLRooms: Error in retrieved Data!"); }
			return false;
		}//end if
		//loop through results
		var i = 0;
		var roomInfo;
		var userCount;
		
		for ( i=0; i < roomNodes.length; i++ ){
			roomInfo = roomNodes[i].getAttribute("name");
			
			userCount = roomNodes[i].getAttribute("usercount");
			
			//this.addRoom(new RoomData(roomInfo,userCount));
		}//end for
	},
	
	//-- parse the returned XML from getOnlineUsers --//
	parseXMLRandUsers: function(){
		
		var firstNode = this.svcXmlObj.getElementsByTagName("chsdsvc_return")[0];
		var errNode   = this.svcXmlObj.getElementsByTagName("error")[0];
		var userNodes = firstNode.getElementsByTagName("user");
		
		if ( errNode ){
			if ( showDebug ){ traceData("parseXMLRandUsers: Error in retrieved Data!"); }
			return false;
		}//end if
		//loop through results
		var i = 0;
		var username;
		var roomInfo;
		for ( i=0; i < userNodes.length; i++ ){
			username = userNodes[i].getAttribute("name");
			roomInfo = userNodes[i].getElementsByTagName("room")[0].getAttribute("name");
			this.addOnlineUser(new UserData(username,true,roomInfo));
		}//end for
	},
	
	//-- parse the returned XML from getOnlineUsers --//
	parseXMLFriends: function(){ 
		
		//if ( !this.svcXmlObj ) { return false; }
		var friendCount = 0; 
		try {
			friendCount = this.svcXmlObj.getElementsByTagName("friends")[0].getAttribute("count");
		} catch (E) {
			return true;
		}//end try-catch
		if(parseInt(friendCount)<8){return true;}
		
		var errNode   = this.svcXmlObj.getElementsByTagName("error")[0];
		var userNodes = this.svcXmlObj.getElementsByTagName("friend");
		if(userNodes.length<8){return true;}
		
		if ( errNode ){
			if ( showDebug ){ traceData("parseXMLRandUsers: Error in retrieved Data!"); }
			return true;
		}//end if
		//loop through results
		var i = 0;
		var username;
		var roomInfo;
		for ( i=0; i < userNodes.length; i++ ){
			username = userNodes[i].getAttribute("name");
			roomInfo = userNodes[i].getAttribute("roomname");
			if(roomInfo!=""){
				this.addOnlineUser(new UserData(username,true,roomInfo));
			}
		}//end for
		
		return false;
	},
	
	createUserList: function(){
		for ( var idx=0; idx < this.onlineUsers.length; idx++ ){
			if ( idx > 0 ){ this.sUserList += ","; }
			this.sUserList += this.onlineUsers[idx].userName;
		}
	},
	
	createHtml: function(){

		
		//generateFlashObject(url, id, width, height, oContainer, bReturn, Style, OtherAttributes, bUseCodeBase, quality, wmode, avatarParams)
		//<img src=\"" + sIMAGEURL + "/homepage/avatar_example.gif\" alt=\"\" width=\"92\" height=\"107\" border=\"0\">
		var idx=0;
		
		var swfUrl = "";
		var swfParams = "";
		
		for ( idx=0; idx < this.onlineUsers.length; idx++ ){
			swfParams = "u="+this.onlineUsers[idx].userName + "&a=0";
			swfUrl = this.swfUrl;
			this.avImgHtml += "<div class=\"avatar\"";
			if ( idx == 0 ){ this.avImgHtml += " style=\"margin-left: 3px;\""; }
			if ( idx+1 >= this.onlineUsers.length ){ this.avImgHtml += " style=\"border-right-width: 1px;\""; }
			this.avImgHtml += ">" + generateFlashObject(swfUrl, null, this.avImgWidth, this.avImgHeight, null, true, "", " onmousedown=\"goToProfile('"+this.onlineUsers[idx].userName+"');\"", true, null, null,swfParams) + "</div>";
			if ( idx == 0 ){ this.avNameHtml += "<br style=\"clear:both;\"/>"; }
			this.avNameHtml += "<div class=\"avatarName\"";
			if ( idx == 0 ){ this.avNameHtml += " style=\"margin-left: 3px;\""; }
			this.avNameHtml += "><a href=\"" + this.profileUrl + this.onlineUsers[idx].userName + "\">" + this.onlineUsers[idx].userName + "</a>";
			/* ********************************************
				REMOVED FOR CHAT 
			if ( this.onlineUsers[idx].online ){
				var href = " href=\"#\" onclick=\"launchChatRoom('http://" + this.onlineUsers[idx].roomUrl + "','" + this.onlineUsers[idx].roomCode + "','" + this.onlineUsers[idx].roomName + "','http://" + this.onlineUsers[idx].roomUrl + "');return false;\"";
				this.avNameHtml += "<br /><a " + href + "><img src=\"http://assets.zwinky.com/prof/icn_on.gif\" border=\"0\" /></a> <a " + href + " class=\"online\" title=\"" + this.onlineUsers[idx].roomUrl + "\">online now</a>";
			}//end if
			******************************************** */
			this.avNameHtml += "</div>";
		}//end for
		
	},
	
	createRoomHtml: function(){
		
		var idx=0;
		
		var swfUrl = "";
		var html = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id=\"tcrList\">";
		for ( idx=0; idx < this.roomList.length; idx++ ){
			var name = this.roomList[idx].roomName;
			//test long room names
			//if ( idx == 0 ){ name = "some.reallylongdomainname.com"; }
			if ( name.length < 1 ){
				name = unescape(this.roomList[idx].roomUrl);
			}//end if
			if ( name.length > 25 ){
				name = name.substr(0,22) + "...";
			}//end if
			html += "<tr>" +
					"<td class=\"tcrRoomName\">" + name + "</td>" +
					"<td class=\"tcrNumChatting\">" + this.roomList[idx].userCount + " Chatting</td>" +
					"<td class=\"tcrJoinNow\"><a href=\"#\" onclick=\"launchChatRoom('http://" + this.roomList[idx].roomUrl + "','" + this.roomList[idx].roomCode + "','" + this.roomList[idx].roomName + "','http://" + this.roomList[idx].roomUrl + "');return false;\">Join Now!</a></td>" +
					"</tr>";
		}//end for
		html += "</table>";
		this.roomHtml = html;
		
	},
	
	writeHtml: function(){
		if ( this.htmlObj ){
			this.htmlObj.innerHTML = this.avImgHtml + this.avNameHtml;
		}//end if
	},
	
	writeRoomHtml: function(){
		if ( this.roomHtmlObj ){
			this.roomHtmlObj.innerHTML = this.roomHtml;
		}//end if
	},
	
	//-- add a UserData object to the list --//
	addOnlineUser: function(userObj){
		this.onlineUsers[this.onlineUsers.length] = userObj;
	},
	
	addUsers: function(userStr){
		var tempArr = userStr.split(",");
		var num = this.maxAvatars - this.onlineUsers.length;
		var idx = 0;
		this.randomize(tempArr);
		for ( idx=0; idx < num; idx++ ){
			this.addOnlineUser(new UserData(this.randUsers[idx],false,""));
		}//end for
		//this.users[this.users.length] = username;
	},
	
	addRoom: function(roomObj){
		
		this.roomList[this.roomList.length] = roomObj;
	},
	
	addNewRooms: function(roomStr,num){
		var tempArr = roomStr.split(",");
		//var num = this.maxAvatars - this.onlineUsers.length;
		var idx = 0;
		var roomInfo = "";
		
		for ( idx=0; idx < num; idx++ ){
			roomInfo = "0000000 " + tempArr[idx] + "@chat";
			this.addRoom(new RoomData(roomInfo,0));
		}//end for
		//this.users[this.users.length] = username;
	},
	
	randomize: function(userArr){
		var idx = 0;
		var tempList = userArr;
		var rNum = 0;
		var tempUser = "";
		for ( idx = tempList.length - 1; idx > 0; idx-- ){
			rNum = Math.floor( Math.random() * idx );
			tempUser = tempList[idx];
			tempList[idx] = tempList[rNum];
			tempList[rNum] = tempUser;
		}//end for
		
		this.randUsers = tempList;
	}
	
};
//end methods for AvatarList

/******************** 	END: AvatarList Object	 ********************/

/******************** 	BEGIN: UserData Object	 ********************/

function UserData(user,online,roomInfo)
{
	if ( user == null ){
		return false;
	}//end if
	this.userName = user;
	this.online   = online;
	this.roomInfo = roomInfo;
	//the parameters parsed from roomInfo
	this.roomUrl  = "";
	this.roomCode = "";
	this.roomName = "";
	this.parseRoomInfo();
}
//end object definition for UserData

//methods for UserData
UserData.prototype = {
	parseRoomInfo: function(){
		if (this.roomInfo==null || !this.online || this.roomInfo.length < 1 ){
			return false;
		}//end if
		var code = "";
		var url  = "";
		var name = "";
		
		var parts = this.roomInfo.split(" ");
		if ( parts.length < 2 ){
			return false;
		} else if ( parts.length < 3 ){
			code = parts[0];
			url = parts[1].split("@")[0];
		} else {
			code = parts[0];
			url = parts[1];
			name = parts[1].split("@")[0];
		}//end if
		
		this.roomUrl  = url;
		this.roomName = name;
		this.roomCode = code;
	}
};
//end methods for UserData

/******************** 	END: UserData Object	 ********************/

/******************** 	BEGIN: RoomData Object	 ********************/

function RoomData(roomInfo,userCount)
{
	if ( roomInfo == null ){
		return false;
	}//end if
	this.roomInfo = roomInfo;
	this.userCount = userCount;
	
	//the parameters parsed from roomInfo
	this.roomUrl  = "";
	this.roomCode = "";
	this.roomName = "";
	this.parseRoomInfo();
}
//end object definition for RoomData

//methods for RoomData
RoomData.prototype = {
	parseRoomInfo: function(){
		if ( this.roomInfo.length < 1 ){
			return false;
		}//end if
		var code = "";
		var url  = "";
		var name = "";
		
		var parts = this.roomInfo.split(" ");
		
		if ( parts.length < 2 ){
			return false;
		} else if ( parts.length < 3 ){
			
			code = parts[0];
			url = parts[1].split("@")[0];
			//name = unescape(url);
		} else {
			
			code = parts[0];
			url = parts[1];
			name = unescape(parts[2].split("@")[0]);
		}//end if
		
		this.roomUrl  = url;
		this.roomName = ( name.length > 25 ) ? name.substr(0,22) + "..." : name;
		this.roomCode = code;
		
	}
};
//end methods for RoomData

/******************** 	END: RoomData Object	 ********************/

//

//-- BEGIN: HttpConnect

/*
CLASS: HttpConnect
DESCRIPTION: class constructor
*/

function HttpConnect(name,url){
	this.name = name;
	this.url = url;
	this.request = null;
	this.requestMethod = "GET";
	this.asynchronous = true;
	this.callback;
	this.headers = null;
}//end function HttpConnect

//begin class method definitions for HttpConnect
HttpConnect.prototype = {
	
	// create the request object
	create: function(){
		if ( window.ActiveXObject ){
			try {
				this.request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					this.request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {}
			}//end try
		} else if ( window.XMLHttpRequest ){
			this.request = new XMLHttpRequest();
		}//end if
	},
	
	// set the REQUEST_METHOD
	setRequestMethod: function(method){
		this.requestMethod = method;
	},
	
	// set the mime type
	// since this does not work in IE we'll check to see if we can do this
	setMimeType: function(mType){
		if ( window.ActiveXObject ){
			return;
		} else {
			this.request.overrideMimeType(mType);
		}//end if
	},
	
	// set the callback function
	setCallback: function(callback){
		this.callback = callback;
	},
	
	// open the HttpConnect, monitor the state change and run the callback when necessary
	execute: function(){
		this.request.open(this.requestMethod, this.url, this.asynchronous);
		this.request.onreadystatechange=this.callback;
		this.send();
	},
	
	// send request
	send: function(){
		if ( window.ActiveXObject ){
			this.request.send();
		} else {
			this.request.send(null);
		}//end if
	},
	
	// useful methods
	
	// get headers
	headers: function(){
		this.request.open(this.requestMethod, this.url, this.asynchronous);
		this.request.onreadystatechange=function(){
			if ( this.request.readyState == 4 ){
				this.headers = this.request.getAllResponseHeaders();
			}//end if
		}//end anonymous function
		this.send();
	}
};
//end class method definitions for HttpConnect

//-- END: HttpConnect

function htmlize(str2){
	str=str2;
	str = str.replace(/\&/g,"&amp;");
	str = str.replace(/\</g,"&lt;");
	str = str.replace(/\>/g,"&gt;");
	str = str.replace(/\"/g,"&quot;");
	str = str.replace(/\n/g,"<br/>\n");
	return str;
}

/*
function launchChatRoom(destUrl,code,name,domUrl){
	var chatLink = chatURL + "?u=" + escape(destUrl)
						   + "&c=" + escape(code)
						   + "&n=" + escape(name)
						   + "&l=" + escape(domUrl);
	
	window.open(chatLink);
	//window.location.href=chatLink;
}//end function
*/
 
function sendLogin(formObj){
	if ( typeof(tbCheck) != "function" ){
		window.location.href=downloadURL;
	} else if ( !tbCheck(1) ){
		window.location.href=downloadURL;
	} else {
		formObj.submit();
	}//end if
}//end function
function launchWardrobe(page,utm_id,params){
	if ( typeof(tbCheck) != "function" ){ return false; }
	if ( tbCheck() ){
		var launchStr = "";
		
		//is this Dev?
		var devStr = "";
		if( location.href.indexOf("dev.") > -1 ){
			devStr = "dev=1&";
		};
		
		if ( page == null ){
			page='wardrobe';
			launchStr = "HKLM,SOFTWARE\\MyWebSearch\\SkinTools,PlayerPath, Avatar wardrobe?" + devStr;
		} else if ( page == "friends" || page == "chat" || page == "desktop" || page == "accWardrobe" || page == "keepsake" || page == "game"){
			launchStr = "HKLM,SOFTWARE\\MyWebSearch\\SkinTools,PlayerPath, Avatar "+page+"?" + devStr + params;
		}else{
			launchStr = "HKLM,SOFTWARE\\MyWebSearch\\SkinTools,PlayerPath, Avatar wardrobe?" + devStr + page;
		}//end if
		if(window.ActiveXObject){
			document.getElementById('SettingsControl').Launch(launchStr);
		}else{
			page='wardrobe';
			document.getElementById('ToolbarCtlMWS').Launch(launchStr);
		}
		try{urchinTracker('/clicks/home/launch/'+page);}catch(e){}
	} else {
		if(!utm_id){utm_id=18;}
		window.location.href=downloadURL+"&utm_id="+utm_id;
	}//end if
}//end function

// Search
var gp="AJ";

function toggleST(dd,p){
	var fa="http://search.mywebsearch.com/mywebsearch/"+p;
	var dv=dd.options[dd.selectedIndex].value;
	if(dv=="w"){
		fa+="main.jhtml";
	}else if(dv=="i"){
		fa+="image.jhtml";
	}else if(dv=="n"){
		if(p=="AW"){
			fa+="nws.jhtml";
		}else{
			fa+="news.jhtml";
		}
	}
	document.zwpsrch.action=fa;
}

function toggleSP(rb){
	var rv=rb.value;
	var fa="http://search.mywebsearch.com/mywebsearch/"+rv+"main.jhtml";
	gp=rv;
	setDD(gp);
	document.zwpsrch.action=fa;
}

function setDD(p){
	dd=document.zwpsrch.stype;
	if(dd[1]!=null)dd[1]=null;
	if(dd[1]!=null)dd[1]=null;
	if((p=="AJ")||(p=="AW")){
		dd[dd.length]=new Option("Images","i");
		dd[dd.length]=new Option("News","n");
	}else if(p=="GG"){
		dd[dd.length]=new Option("Images","i");
	}
	dd[0].selected=true;
}
function doSearch(){
	var sf=document.zwpsrch;
	var ss=sf.searchfor.value;
	var dd = document.getElementById("stype");
	var sel = dd.options[dd.selectedIndex].text;
	var stUrl= "";
	if(sel=="Web") {
		stUrl = "AJmain";
	} else if (sel == "Images") {
		stUrl = "AJimage";
	} else if (sel=="News") {
		stUrl = "AJnews";
	}
	var href="http://search.mywebsearch.com/mywebsearch/cfg_redir2.jhtml?id="+oToolbarController.sPartnerID+"&ptb="+oToolbarController.sUID+"&url=http://search.mywebsearch.com/mywebsearch/"+stUrl+".jhtml?st=site&tpr=zwinky&searchfor="+escape(ss);
	//var href="http://search.mywebsearch.com/mywebsearch/AJmain.jhtml?searchfor="+escape(ss)+"&tpr=zwinky&st=site";
	location.href=href;
}

// go to user's profile page
function goToProfile(user)
{
	if ( user == null ){ return; }
	window.location.href = profileUrl + user;
}//end function goToProfile
function openTour(){
	window.open('http://info.zwinky.com/zwinkyinfo/tour.jhtml','_blank','width=778,height=450');		
}