/* UNIVERSAL HELPER FUNCTION */
function myUnescape(str){
	return str?decodeURIComponent(str.replace(/\+/g," ")):(str==""?"":null) ;
//	return str?unescape(str.replace(/\+/g,"%20")):(str==""?"":null) ;
}

function isDefined(v){
	return !(v == undefined);
}
function isArrayEmpty(ar){
	var p ;
	for(p in ar)
		return false;
	return true;
}
/* END UNIVERSAL HELPER FUNCTIONS */

/* SYSTEM INFO */
function SystemInfo()
{
	this.mouseX = null;
	this.mouseY = null;
	this.type = navigator.appName;
	this.version = navigator.userAgent;
	this.platform = navigator.platform;
	this.javaEnabled = false;
	this.info = this.type + ', ' + this.version + ', ' + this.platform;
	this.browserCode = null;
		
	//make version info from navigator.userAgent
	var searchFor = null;
	var n = null;
	if (this.type == "Netscape")
	{
		//check for version 7+
		searchFor = "Netscape/7";
		n = this.version.indexOf(searchFor);
		if(n >= 0)
		{
			//this is version 7 so what sub-version
			this.version = this.version.substring(n + searchFor.length - 1, this.version.length);
		}

		//check for safari
		searchFor = "Safari/";
		n = this.version.indexOf(searchFor);
		if(n >= 0)
		{
			//this is safari so what version
			this.type = "Safari";
			this.version = this.version.substring(n + searchFor.length, this.version.length);
		}
		
		searchFor = "Firefox/";
		n = this.version.indexOf(searchFor);
		if(n >= 0)
		{
			//this is safari so what version
			this.type = "Firefox";
			this.version = this.version.substring(n + searchFor.length, this.version.length);
		}
		
		//check for version 6+
		searchFor = "Netscape6/";
		n = this.version.indexOf(searchFor);
		if(n >= 0)
		{
			//this is version 6 so what sub-version
			this.version = this.version.substring(n + searchFor.length, this.version.length);
		}
		
		//check for version 4.75
		searchFor = "Mozilla/4.7";
		n = this.version.indexOf(searchFor);
		if(n >= 0)
		{
			//this is version 4.7 so what sub-version
			this.version = this.version.substring(n + searchFor.length - 3, this.version.length);
			this.version = this.version.substring(0, this.version.indexOf(" "));
		}
		this.version = parseFloat(this.version);
	}	
	if(this.type == "Microsoft Internet Explorer")
	{
		//check for Opera
		searchFor = "Opera ";
		n = this.version.indexOf(searchFor);
		if(n >= 0)
		{
			//this is version 8+ so what sub-version
			this.version = this.version.substring(n + searchFor.length, this.version.length);
			this.type = "Opera";			 
		}

		//check for version 5+
		searchFor = "MSIE ";
		n = this.version.indexOf(searchFor);
		if(n >= 0)
		{
			//this is version 5+ so what sub-version
			this.version = this.version.substring(n + searchFor.length, this.version.length);
			this.version = this.version.substring(0, this.version.indexOf(";"));			 
		}
		this.version = parseFloat(this.version);
	}
	if(this.isIE())this.browserCode = (this.version >= 5.0 ? "IE5" : "IE4");
	if(this.isNS())this.browserCode = (this.version >= 6.1 ? "NS6" : "NS4");
	if(this.isSF())this.browserCode = (this.version >= 1.2 ? ((this.version/100) >= 2 ? "SF2" : "SF1") : "SF0");
	if(this.isFF())this.browserCode = (this.version >= 0.9 ? "FF1" : "FF0");
	if(this.isOP())this.browserCode = (this.version >= 7.0 ? "OP7" : "OP6");

	var p = 'X';
	if(this.isMac())p = 'M';
	if(this.isPC())p = 'P';
	this.browserCode += p;
	
	this.doHeightResize = (this.isIE() && this.version < 5.5) || (this.isOP() && this.version >= 8.5) || (this.isSF() && this.browserCode != 'SF2M');
}

SystemInfo.prototype.isIE = function(){ return (this.type == "Microsoft Internet Explorer"); };
SystemInfo.prototype.isSF = function(){ return (this.type == "Safari"); };
SystemInfo.prototype.isNS = function(){ return (this.type == "Netscape"); };
SystemInfo.prototype.isFF = function(){ return (this.type == "Firefox"); };
SystemInfo.prototype.isOP = function(){ return (this.type == "Opera"); };
SystemInfo.prototype.isPC = function(){ return (this.platform == "Win32"); } ;
SystemInfo.prototype.isMac = function(){ return (this.platform.indexOf("Mac") != -1); };
SystemInfo.prototype.isJava = function()
{
	if (!navigator.javaEnabled())
		return false ;	// No Java support at all
		
	try
	{
		if (this.isIE())
		{
			var obj = new ActiveXObject("JavaWebStart.isInstalled");
			if (obj)
				this.javaEnabled = true ;
			obj = null ;
		}
		else
		{
			// Check the plugins type
			var plg ;
			for (plg in navigator.plugins)
			{
				if (navigator.plugins[plg].name.indexOf("Java")==0)
				{
					this.javaEnabled = true ;
					break ;
				}
			}
		}
	}
	catch(x)
	{
	}
	return this.javaEnabled ;
}


SystemInfo.prototype.getWinWidth = function(hWin){
			if(!hWin)hWin = window;
			if(this.isOP() || this.isSF() || this.isFF())return hWin.innerWidth;
			if(this.isIE())return hWin.document.body.clientWidth;
		};
SystemInfo.prototype.getWinHeight = function(hWin){
			if(!hWin)hWin = window;
			if(this.isOP() || this.isSF() || this.isFF())return hWin.innerHeight;
			if(this.isIE())return hWin.document.body.clientHeight;
		};

var _si = new SystemInfo();
/* END SYSTEM INFO */

/* DOC ELEMENT HELPER STUFF */
function TableElement(hWin, id, rows, cols, cellClassName){
	this.hWin = hWin;
	this.id = id;
	this.rows = rows;
	this.cols = cols;
	this.table = this.hWin.document.createElement("TABLE");
	this.table.cellSpacing = "0";
	this.table.cellPadding = "0";
	this.body = this.hWin.document.createElement("TBODY");
	this.cells = new Array();
	for(var i = 0; i < this.rows; i++){
		var tr = this.hWin.document.createElement("TR");
		for(var j = 0; j < this.cols; j++){
			var idx = (i*this.cols) + j;
			var td = this.hWin.document.createElement("TD");
			td.className = cellClassName;
			tr.appendChild(td);
			this.cells[idx] = td;
		}
		this.body.appendChild(tr);
	}
	this.table.appendChild(this.body);
	
	this.getCell = function(i, j){
		var idx = (i*this.cols) + j;
		return this.cells[idx];	
	}
}
/* END DOC ELEMENT HELPER STUFF */

/* GENERAL FLIRTO SERVICE FUNCS */
function openPopup(url, name, width, height, offset){
	if(!offset){
		offset = new Array(0, 0);
	}
	
	var left = ((screen.width - width)/2) + offset[0];
	var top = ((screen.height - height)/2) + offset[1];
	var features = 'width=' + width + ', height=' + height + ',left=' + left + ',top=' + top;
	var hWin = stateWindow.openWindow(url, name, features);
	hWin.focus();
	return hWin;
}

function openAddPhotoPopup(session, mediaType, args){
	var url = 'uploadph.jsp?ghost=' + session + '&mediaType=' + mediaType + (args ? '&' + args : '');
	openPopup(url, '_new', 320, 250);
}

function openAgeVerifyPopup(session){
	openPopup('ageverify.jsp?ghost=' + session, 'ageverify', 350, 330);
}

function openSendFlirtogramAdminPopup(type, data){
	var url = 'sendflirtogram.jsp?ghost='+stateWindow.getSession()+'&id=' + stateWindow.getAdminId() +'&type=' + type + '&data=' + data;
	var hWin = openPopup(url, 'sendFlirtogram', 400, 194);	
}

function openSendFlirtogramPopup(sendToUserId, flirtname)
{
	var url = 'sendflirtogram.jsp?ghost='+stateWindow.getSession()+'&id=' + sendToUserId+'&flirtname=' + flirtname;
	var hWin = openPopup(url, 'sendFlirtogram', 400, 194);	
	return;
}

function openInfoPopup(infoType){
	var url = 'info.jsp?ghost=' + stateWindow.getSession() + '&type=' + infoType;
	openPopup(url, 'info', 300, 300);
}

function openEmailPopup(type, data){
	var url = 'email.jsp?ghost=' + stateWindow.getSession() + '&type=' + type + '&id=' + data;
	openPopup(url, 'email', 350, 220);
}

function changeFavouriteOnServer(add, id, type)
{
	if (add==0)
		id = -id ;
	var u = "xml/addfav.jsp?ghost="+stateWindow.getSession()+"&id="+id+"&type=" + type;
	stateWindow.loadXML(u) ;
	//stateWindow.openPopup(u, "ppopp", 400, 400);
}

function changeBlockOnServer(block, id)
{
	if (block==0)
		id = -id ;
	var u = "xml/doblock.jsp?ghost="+stateWindow.getSession()+"&id="+id ;
	stateWindow.loadXML(u) ;
}

function openUberProfile(id,flirtname)
{
	var url = 'uberprofile.jsp?ghost='+stateWindow.getSession()+'&id=' + id +'&flirtname=' + flirtname;
	stateWindow.contextArea.location.href = url ;
}

function openChat(uid, flirtname, phase)
{
	var invite = getInviteById(uid) ;
	if (invite)
		invite.close() ;

	// De-duplicate chats
	if (chatPopup["C"+uid])
	{
		chatWin = chatPopup["C"+uid].window ;
		if(chatWin.closed)
		{
			delete chatPopup["C"+uid];
		}
		else
		{
			chatWin.focus() ;
			return chatWin ;
		}
	}
	// Create a new chat
	var cp = new ChatPopup(uid,flirtname,phase) ;
	return cp.window ;
}

//WRITE AND SEND FLIRTOGRAM FUNCTIONS (INCLUDES MENU ICON CLASS)
function checkForReturn(e, elt, callBack)
{
	if(!e)e = event;
	if(e.keyCode == 13 && elt.value.length > 0)
	{
		if(elt.value.indexOf("\n") != 0 && elt.value.indexOf("\r") != 0)
		{
			if (callBack)
				callBack() ;
		}
	}
}

function sendFlirtogramToServer(fgList, userId, msgType, iconIdx, msg)
{
	if (!userId)
		return null ;

	msg = neutraliseTags(msg);
	var tempFg = fgList.addTempFlirtogram(userId, msgType, iconIdx, msg);
	var u = "xml/send.jsp?ghost="+getSession()+"&id="+userId+"&msg="+escape(msg)+ "&msgtype=" + escape(msgType) + "&icon=" + iconIdx + "&tempfgid=" + tempFg.id;
	loadXML(u);
	return tempFg ;
}

//MSG THREAD RESIZE FUNCTION (for msgthread and chat)
function threadResize(){
	var winHeight  = _si.getWinHeight();
	var newHeight = winHeight - 160;
	document.getElementById('profileCtn').style.height = newHeight;
	document.getElementById('flirtogramListCtn').style.height = newHeight;
	document.getElementById('flirtogramList').style.height = newHeight;
}
//END RESIZE

function contextAreaOnLoad(cArea){
	if(cArea == 'home' || cArea == 'mypeople' || cArea == 'mysettings' || cArea == 'mymobile' || cArea == 'toprated' || cArea == 'flirtshop'){
		stateWindow.navBtnUpdate('select', cArea);
	} else {
		stateWindow.navBtnUpdate('restore', stateWindow.currentNavBtn, true);
		stateWindow.currentNavBtn = null;
	}
	
	if(_si && _si.isIE()){
		var elt = self.top.document.getElementById('contextArea');
		elt.style.width = '99%';
		elt.style.width = '100%';
	}
}

//icon list stuff
function Icon(name, files){
	this.name = name;
	this.files = files;
	this.owner = null; //e.g used by IconMenu in build method
}
function IconList(){
	this.icons = new Array();
}	
IconList.prototype.addIcon = function(newIcon){
		var icon = this.getIconByName(newIcon.name);
		if(icon == null){
			this.icons[this.icons.length] = newIcon;
			return newIcon;
		} else {
			return icon;
		}
	}
		
IconList.prototype.getIconByName = function(name){
		for(var i = 0; i < this.icons.length; i++){
			if(this.icons[i].name == name){
				return this.icons[i];
			}
		}
		return null;
	}
	
IconList.prototype.getIconById = function(id){
		for(var i = 0; i < this.icons.length; i++){
			for(var j = 0; j < this.icons[i].files.length; j++){
				if(this.icons[i].files[j].id == id){
					return this.icons[i];
				}
			}
		}
		return null;
	}

var iconList = new IconList(); //used to associate ids from database with files on static server
var path = 'http://' + staticHost() + '/images/flirtograms/icons/compliments/';
iconList.addIcon(new Icon('heart', new Array({id: 41, file: path + 'heart.gif'})));
iconList.addIcon(new Icon('growl', new Array({id: 42, file: path + 'growl.gif'})));
iconList.addIcon(new Icon('temp_rise', new Array({id: 43, file: path + 'temp_rise.anim.gif'})));
iconList.addIcon(new Icon('love_bomb', new Array({id: 44, file: path + 'love_bomb.gif'}))); 
//iconList.addIcon(new Icon('love_bomb', new Array({id: 44, file: path + 'mistletoe.gif'})));  xmas icon
iconList.addIcon(new Icon('comeon', new Array({id: 45, file: path + 'comeon.anim.gif'})));
iconList.addIcon(new Icon('bat_lashes', new Array({id: 46, file: path + 'bat_lashes.anim.gif'})));
iconList.addIcon(new Icon('smile', new Array({id: 47, file: path + 'smile.gif'})));
iconList.addIcon(new Icon('fondle', new Array({id: 48, file: path + 'fondle.anim.gif'})));
iconList.addIcon(new Icon('tease', new Array({id: 49, file: path + 'tease.anim.gif'})));
//iconList.addIcon(new Icon('tease', new Array({id: 49, file: path + 'mrsclause_v2.gif'})));
iconList.addIcon(new Icon('kiss', new Array({id: 203, file: path + 'kiss.gif'})));
iconList.addIcon(new Icon('wink', new Array({id: 204, file: path + 'wink.anim.gif'})));
iconList.addIcon(new Icon('supersnog', new Array({id: 188, file: path + 'snowkiss.png'})));

path = 'http://' + staticHost() + '/images/flirtograms/icons/insults/';
iconList.addIcon(new Icon('slap', new Array({id: 202, file: path + 'slap.anim.gif'})));
iconList.addIcon(new Icon('drink', new Array({id: 65, file: path + 'drink.gif'})));
iconList.addIcon(new Icon('evileye', new Array({id: 66, file: path + 'evileye.gif'})));
iconList.addIcon(new Icon('bomb', new Array({id: 67, file: path + 'bomb.anim.gif'})));
iconList.addIcon(new Icon('burn', new Array({id: 68, file: path + 'burn.gif'})));
iconList.addIcon(new Icon('sick', new Array({id: 69, file: path + 'sick.gif'})));
iconList.addIcon(new Icon('tap_fingers', new Array({id: 70, file: path + 'tap_fingers.anim.gif'})));
iconList.addIcon(new Icon('dumped', new Array({id: 71, file: path + 'dumped.gif'})));
iconList.addIcon(new Icon('finger', new Array({id: 72, file: path + 'finger.gif'})));
iconList.addIcon(new Icon('yawn', new Array({id: 73, file: path + 'yawn.anim.gif'})));

iconList.addIcon(new Icon('egoboost', new Array({id: 206, file: 'http://'+staticHost()+'/images/flirtograms/icons/egoboost.gif'})));	// egoboost

//class for building icon menus
function IconMenu(hWin, id, icns, cols, onClick){
	this.hWin = hWin;
	this.id = id;
	this.elt = null;
	this.cols = cols;
	this.icons = new Array();
	if(icns){
		for(var i = 0; i < icns.length; i++){
			this.icons[this.icons.length] = new Icon(icns[i].name, icns[i].files);
		}
	}
	
	if(hWin){
		this.elt = this.hWin.document.createElement('DIV');
		this.elt.id = this.id;
		this.elt.className = 'IconMenuCtn';
	}
	
	this.cellClass = 'IconCell';
	this.imageClass = 'IconImage';
	this.visible = false;
	this.onClick = onClick;
}

IconMenu.prototype.build = function(useIconNames) { //so it can be called after document element is created
		var rows = Math.ceil(this.icons.length/this.cols);
		var table = this.hWin.document.createElement('TABLE');
		table.className = 'IconMenuTable';
		var tbody = this.hWin.document.createElement('TBODY');
		for(var i = 0; i < rows; i++){
			var tr = this.hWin.document.createElement('TR');
			for(var j = 0; j < this.cols; j++){
				var n = (i * this.cols) + j;
				var td = this.hWin.document.createElement('TD');
				td.className = this.cellClass;
				td.menu = this ;
				td.iconNumber = n ;
			    td.onmouseover = function(e)
			    {
			    	this.style.border = '1px solid #C0C0C0'; 
			    	if (this.menu.hover) this.menu.hover(true,this,e) ;
			    };
				td.onmouseout = function(e)
				{
					this.style.border = '1px solid #FFFFFF'; 
			    	if (this.menu.hover) this.menu.hover(false,this,e) ;
				};

				var ctnElt = null;
				if(n > this.icons.length - 1){
					ctnElt = this.hWin.document.createTextNode('')
				} else {
					var img = this.hWin.document.createElement('IMG');
					var icon = this.icons[n];
					icon.owner = this;
					img.src = icon.files[0].file;
					img.className = this.imageClass;
					img.altText = 'Select icon';
					ctnElt = this.hWin.document.createElement('DIV');
					ctnElt.className = 'Icon';
					ctnElt.id = 'icon' + n;
					ctnElt.value = icon;
					ctnElt.onclick = this.onClick;
					ctnElt.appendChild(img);
					if(useIconNames){
						var labelDiv = this.hWin.document.createElement('DIV');
						labelDiv.className = "IconLabel" ;
						var labelElt = this.hWin.document.createTextNode(icon.name);
						labelDiv.appendChild(labelElt);
						ctnElt.appendChild(labelDiv);
//						img.align = "absmiddle";
					}
				}
				td.appendChild(ctnElt);
				tr.appendChild(td);
			}
			tbody.appendChild(tr);
		}
		table.appendChild(tbody);
		this.elt.appendChild(table);
		this.hWin.document.body.appendChild(this.elt);
	}
	
IconMenu.prototype.show = function(x, y){
			this.elt.style.left = x;
			this.elt.style.top = y;
			this.elt.style.visibility = 'visible';
			this.elt.style.display = 'block';
			this.visible = true;
		}
IconMenu.prototype.hide = function(){
			this.elt.style.visibility = 'hidden';
			this.elt.style.display = 'none';
			this.visible = false;
		}
//end class for building icon menus


//rating menu
var ratingCategories = ["", "freak", "left me cold", "doesn't float my boat", "in reserve squad", "worth a look", "nice fling", "good time", "top flirt", "hot", "off the radar", "ego boost"];
function RatingMenu(hWin, onClick){
	this.hWin = hWin;
	this.onClick = onClick;
	this.id = 'ratingMenu';
	var path = 'http://' + staticHost() + '/images/profile/rating/';
	this.icons = new Array();
	this.icons[this.icons.length] = new Icon('ego boost (150 FP)', new Array({id: 11, file: path + 'r11.gif'}));
	this.icons[this.icons.length] = new Icon('off the radar', new Array({id: 10, file: path + 'r10.gif'}));
	this.icons[this.icons.length] = new Icon('hot', new Array({id: 9, file: path + 'r9.gif'}));
	this.icons[this.icons.length] = new Icon('top flirt', new Array({id: 8, file: path + 'r8.gif'}));
	this.icons[this.icons.length] = new Icon('good time', new Array({id: 7, file: path + 'r7.gif'}));
	this.icons[this.icons.length] = new Icon('nice fling', new Array({id: 6, file: path + 'r6.gif'}));
	this.icons[this.icons.length] = new Icon('worth a look', new Array({id: 5, file: path + 'r5.gif'}));
	this.icons[this.icons.length] = new Icon('in reserve squad', new Array({id: 4, file: path + 'r4.gif'}));
	this.icons[this.icons.length] = new Icon("doesn't float my boat", new Array({id: 3, file: path + 'r3.gif'}));
	this.icons[this.icons.length] = new Icon('left me cold', new Array({id: 2, file: path + 'r2.gif'}));
	this.icons[this.icons.length] = new Icon('freak', new Array({id: 1, file: path + 'r1.gif'}));
	
	this.elt = this.hWin.document.createElement('DIV');
	this.elt.id = this.id;
	this.elt.className = 'IconMenuCtn';
	this.cols = 1;
	this.imageClass = 'RatingIconImage';
	this.cellClass = 'RatingIconCell';
}
RatingMenu.prototype = new IconMenu;
RatingMenu.prototype.build = IconMenu.prototype.build;
//end rating menu

function GiftMenu(hWin, onClick){
	this.hWin = hWin;
	this.onClick = onClick;
	this.id = 'giftMenu';
	this.lastUserId = 0 ; 
	var path = 'http://' + staticHost() + '/images/gifts/' ;
	this.icons = new Array();
	for (var g in stateWindow.gift)
	{
		if (gift[g].rd.stock>0 && (gift[g].rd.flags&2)==0)
			this.icons[this.icons.length] = new Icon(stateWindow.gift[g].name+" ("+stateWindow.gift[g].rd.cost+" fp)", new Array({id: g, file: stateWindow.gift[g].iconImageUrl()}));
	}
	this.icons[this.icons.length] = new Icon('Ego boost (150 fp)', new Array({id: "EgoBoost", file: 'http://' + staticHost() + '/images/profile/rating/r11.gif'}));
	
	this.elt = this.hWin.document.createElement('DIV');
	this.elt.id = this.id;
	this.elt.className = 'IconMenuCtn';
	this.cols = 1;
	this.imageClass = 'GiftIconImage';
	this.cellClass = 'GiftIconCell';
	this.cols = 3;

	var prompt = this.hWin.document.createElement('H3') ;
	prompt.innerHTML = "<IMG style='vertical-align: middle' src='http://"+stateWindow.staticHost()+"/images/profile/fp.gif'> Use your Flirt Points to make someone\'s day!" ;
	this.elt.appendChild(prompt);
	
	this.zoom = null ;
	this.zoomElt = null ;
}
GiftMenu.prototype = new IconMenu;
GiftMenu.prototype.build = IconMenu.prototype.build;
GiftMenu.prototype.hover = function(flag,icon,e) 
{
	if (flag==false || !this.icons[icon.iconNumber] || !stateWindow.gift[this.icons[icon.iconNumber].files[0].id])
	{
		if (this.zoomElt && this.zoomElt.parentNode)
			this.zoomElt.parentNode.removeChild(this.zoomElt) ;
		this.zoomElt = null ;
		this.zoom = null ;
	}
	else
	{
		var giftImg = stateWindow.gift[this.icons[icon.iconNumber].files[0].id].rd.image ;
		if (giftImg!=this.zoom)
		{
			if (this.zoomElt && this.zoomElt.parentNode)
				this.zoomElt.parentNode.removeChild(this.zoomElt) ;
			this.zoom = giftImg ;
//			Now add a new popup with the image in		
			this.zoomElt = this.hWin.document.createElement('DIV') ;
			this.zoomElt.className = "InfoPopup" ;
			this.zoomElt.style.width = 175;
			this.zoomElt.style.height = 138;
			
			this.zoomElt.style.left = icon.offsetLeft+16 ; //mouseX+16;
			var compare = icon.offsetTop+138+48; 
			if(compare > this.elt.clientHeight)
				this.zoomElt.style.top = icon.offsetTop-138;			
			else
				this.zoomElt.style.top = icon.offsetTop+48;
			
			this.zoomElt.innerHTML = "<IMG src='"+ stateWindow.gift[this.icons[icon.iconNumber].files[0].id].mediumImageUrl() +"'/>";
			this.elt.appendChild(this.zoomElt);
		}
	}
}


//FUNCTION WRAPPERS TO CREATE OBJECT COS IE AND SAFARI DON'T LIKE USING new hWin.Object() AS A CONSTRUCTION METHOD
function createIconMenu(hWin, id, icons, cols, onClick){
	return new IconMenu(hWin, id, icons, cols, onClick);
}
function selectMessageIcon(){
	var elt = this.value.owner.hWin.document.getElementById('messageIcon');
	if(elt){
		var icon = this.value;
		elt.src = icon.files[0].file;
		elt.value = icon.files[0].id;
		icon.owner.hide();
	}
}

function createRatingMenu(hWin, onClick){
	RatingMenu.prototype.hWin = hWin;
	return new RatingMenu(hWin, onClick);
}

function createGiftMenu(hWin, onClick){
	GiftMenu.prototype.hWin = hWin;
	return new GiftMenu(hWin, onClick);
}

// Standard function used to send gifts
function sendGift()
{ 
	if (this.value.files[0].id=='EgoBoost')
	{
			var u = 'xml/rateflirt.jsp?ghost=' + stateWindow.getSession() + '&id=' + this.value.owner.lastUserId + '&r=11';
			stateWindow.loadXML(u);
	}
	else
	{
		stateWindow.sendFlirtogram(window, stateWindow.flirtogramList, this.value.owner.lastUserId, 'PLEAREPLY', this.value.files[0].id, 'Sent a gift');
	}
}


//COOKIE STUFF
function createCookie(name,value,days,path) {
	var expires = "" ;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	if (!path) path = "/" ;
	document.cookie = name+"="+value+expires+"; path="+path+"; domain=flirtomatic.com";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name,path) {
	createCookie(name,"",-1,path);
}

function restoreTags(text){
	return text.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">");
}

function neutraliseTags(text){
	return restoreTags(text).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
}

/** Accessory Stuff **/
function AccessoryCollection(ctn,u,h,w)
{
	this.user = u ;
	
	this.lastAcc = null ;
	this.infoHeight  = h?h:168 ;
	this.infoWidth = w?w:180 ;
	
	var doc = ctn.ownerDocument ;
	var e = doc.getElementById("infoPopup") ;
	if (!e) e = doc.createElement("DIV") ;

	this.eltInfo = e ;
	this.eltInfo.className = "InfoPopup" ;
	this.eltInfo.style.width = this.infoWidth ;
	this.eltInfo.style.height = this.infoHeight;
	this.eltInfo.style.visibility = "hidden" ;
	this.eltInfo.id = "infoPopup" ;
	doc.body.appendChild(this.eltInfo) ;
	
	this.marginY = 8 ;
	this.mouseX = 200 ;
	this.mouseY = 120 ;
	this.ctn = ctn ;
	ctn.value = this ;
	ctn.onmouseout = function() { this.value.hideAccessory() ; }
	ctn.onmousemove = function(e)   
	{
//		alert(stateWindow.contextArea.event) ;
		this.value.handleMouseMove(e?e:stateWindow.contextArea.event) ; 
	} ;
	this.drawCollection() ;
}

AccessoryCollection.prototype.handleMouseMove = function(e)
{
//	if(!e)e = window.event;
//alert(e) ;	
	if(e)
	{
		this.mouseX = e.clientX ? e.clientX : e.pageX;
		this.mouseY = e.clientY ? e.clientY : e.pageY;
		if (this.eltInfo)
		{
			var doc = this.eltInfo.ownerDocument ;
			if (!this.mouseX || this.mouseX <1)
				this.mouseX = 1 ;
			if (!this.mouseY || this.mouseY<1)
				this.mouseY = 1 ;
	
			var y = (this.mouseY+doc.body.scrollTop)-(this.infoHeight+32) ;
			if (y<doc.body.scrollTop)
				y = doc.body.scrollTop ;
			this.eltInfo.style.top = y ;
			this.eltInfo.style.left = this.mouseX-this.infoWidth-24;
		}
	}
}

AccessoryCollection.prototype.showAccessory = function(acc)
{
	if (!this.user || !stateWindow.gift[acc] || acc<1)
	{
		hideAccessory() ;
		return ;
	}
	
	if (this.lastAcc!=acc)
	{
		this.lastAcc = acc ;

		var html = "" 
		html += "<H2>"+stateWindow.gift[acc].name+"</H2>" ;
		html += '<img class="InfoImage" src="'+stateWindow.gift[acc].mediumImageUrl()+'"><br><P>' ;
		html += "Received: "+this.user.accessories[acc] ;
		html += "</P>" ;
		this.eltInfo.style.left = this.mouseX-this.infoWidth-24;
		this.eltInfo.style.top = this.mouseY+this.marginY+this.eltInfo.ownerDocument.body.scrollTop ;
		this.eltInfo.innerHTML = html;
		this.eltInfo.style.visibility = "" ;
	}
}

AccessoryCollection.prototype.hideAccessory = function()
{
	this.lastAcc = null ;
	if (this.eltInfo)
		this.eltInfo.style.visibility = "hidden" ;
}

AccessoryCollection.prototype.drawCollection = function()
{
	var collection  ; 
	if (this.user.id==stateWindow.getUserID())
		collection = '<h2>My Collection</h2>&nbsp;<h3><a href="./shop.jsp?ghost='+stateWindow.getSession()+'">Buy accessories...</a></h3>' ;
	else
		collection = '<h2>'+this.user.flirtname+'\'s Collection</h2>&nbsp;<h3><a href="javascript:showGiftMenu()">Send gift...</a></h3>' ;

	var n = 0 ;
	var sortedAcc = new Array() ;
	for (var acc in this.user.accessories)
		sortedAcc[sortedAcc.length] = acc ;
	sortedAcc.sort(function(a,b) {return b-a; }) ;

	for (var accIndex in sortedAcc)
	{
		acc = sortedAcc[accIndex] ;
		if (!acc || !stateWindow.gift[acc] || !this.user.accessories[acc])
			collection += '<div class="collectionDiv"><img class="collection" src="http://' + stateWindow.staticHost() + '/images/gifts/none/icon.gif"><br>&nbsp;</div>' ;
		else
			collection += '<div class="collectionDiv"><img onmouseout="this.parentNode.parentNode.value.hideAccessory()" onmouseover="this.parentNode.parentNode.value.showAccessory('+acc+')" class="collection" src="'+stateWindow.gift[acc].iconImageUrl()+'"><br>'+this.user.accessories[acc]+'</div>' ;
		n += 1 ;
		if (n%8==7)
			collection += '<br>' ;
	}

	this.ctn.innerHTML = collection ;
}

function createAccessoryCollection(ctn,u,h,w)
{
	return new stateWindow.AccessoryCollection(ctn,u,h,w) ;
}

