
function nslog(s)
{
	if(console && console.log) 
		console.log(s);
}

/*
function OpenMyPopup(lang, profileid, staythere)
{
	window.open('/'+lang+'/mypopup/index/'+profileid+'/?staythere='+staythere, / * 'MYPROFILEPOPUP'+profileid * / '_blank', 'width=650,height=400,toolbar=no,scrollbars=yes,location=no,status=yes,resizable=yes');
}
*/

function OpenMyPopup(url, profileid)
{
	window.open(url, '_blank', 'width=700,height=430,toolbar=no,scrollbars=yes,location=no,status=yes,resizable=yes');
}

function OpenTheirPopup(url, profileid)
{
	window.open(url, '_blank', 'width=740,height=550,toolbar=no,scrollbars=yes,location=no,status=yes,resizable=yes');
}

function OpenMapPopup(lat, lng)
{
	w = 600;
	h = 500;
	url = 'http://www.webispink.com/p/googlemap?lat='+lat+'&lng='+lng+'&w='+w+'&h='+h;
	window.open(url, '_blank', 'width='+w+',height='+h+',toolbar=no,scrollbars=no,location=no,status=yes,resizable=no');
}

// XXX WHERE IS THAT USED?
function GetWindowHeight()
{
	//http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	if(typeof(window.innerWidth) == 'number' ) return window.innerHeight;
	if( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight) ) return document.documentElement.clientHeight;
	if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) return document.body.clientHeight;
}

function GetElementPosition(obj) //http://www.quirksmode.org/js/findpos.html
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while(obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function GetElementSize(obj)
{
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	return [w,h];
}	

function GetWindowSize()
{
	if(window.innerWidth) return [window.innerWidth,window.innerHeight];
	if(document.documentElement) return [document.documentElement.clientWidth,document.documentElement.clientHeight];
	if(document.body) return [document.body.clientWidth,document.body.clientHeight];
}

/*****************************************************************************************************************************************/
/** FAVICON ******************************************************************************************************************************/
/*****************************************************************************************************************************************/

// http://softwareas.com/dynamic-favicons
function ChangePageFavicon(iconURL) 
{
	$("link[@rel='shortcut icon']").remove();
	$('head').append('<link rel="shortcut icon" href="'+iconURL+'" />').append('<link rel="icon" href="'+iconURL+'" />');
}

function ChangePageTitle(title)
{
	/*
	prev = $('head title').text();
	$('head title').text(title);
	return prev;
	*/
	prev = document.title;
	document.title = title;
	return prev;
}

/*****************************************************************************************************************************************/
/** VOID LINKS ***************************************************************************************************************************/
/*****************************************************************************************************************************************/

var voidLinkPointer = 0;
var voidLinkObjects = new Array();
var voidLinkHrefs = new Array();
var voidLinkTargets = new Array();

function VoidLink(a)
{
	if(!a) return;
	
	voidLinkObjects[voidLinkPointer] = a;
	voidLinkHrefs[voidLinkPointer] = a.href;
	voidLinkTargets[voidLinkPointer] = a.target;
	
	a.href = 'javascript:void(0);';
	a.target = '_self';
	
	setTimeout('UnvoidLink('+voidLinkPointer+')', 100);
	voidLinkPointer++;
	
	return void(0);
}
function UnvoidLink(pointer)
{
	if(!(voidLinkObjects[pointer] && voidLinkHrefs[pointer])) return;
	voidLinkObjects[pointer].href = voidLinkHrefs[pointer];
	voidLinkObjects[pointer].target = voidLinkTargets[pointer];
}

/*****************************************************************************************************************************************/
/** AVATAR MENUS *************************************************************************************************************************/
/*****************************************************************************************************************************************/

// XXX HeaderAvatarIsOver may be unnecessary because the timeout is cleared on mouseover

var HeaderAvatarIsOver = false;
var HeaderAvatarTimer = null;
var HeaderAvatarTimerDelay = 200;
function HeaderAvatarMouseOver(id)
{
	if(HeaderAvatarTimer) clearTimeout(HeaderAvatarTimer);
	HeaderAvatarIsOver = true;
	$('#HEADERAVATARMENU'+id).show();
	$('.HEADERAVATARMENU:not(#HEADERAVATARMENU'+id+')').hide();
}
function HeaderAvatarMouseOut(id)
{
	if(HeaderAvatarTimer) clearTimeout(HeaderAvatarTimer);
	HeaderAvatarIsOver = false;
	HeaderAvatarTimer = setTimeout(function() { if(!HeaderAvatarIsOver) $('#HEADERAVATARMENU'+id).hide(); HeaderAvatarTimer = null; }, HeaderAvatarTimerDelay);
}
function HeaderAvatarMenuMouseOver(id)
{
	if(HeaderAvatarTimer) clearTimeout(HeaderAvatarTimer);
	HeaderAvatarIsOver = true;
}
function HeaderAvatarMenuMouseOut(id)
{
	if(HeaderAvatarTimer) clearTimeout(HeaderAvatarTimer);
	HeaderAvatarIsOver = false;
	HeaderAvatarTimer = setTimeout(function() { if(!HeaderAvatarIsOver) $('#HEADERAVATARMENU'+id).hide(); HeaderAvatarTimer = null; }, HeaderAvatarTimerDelay);
}

function HeaderAvatarSetField(pos, profileid, field, value, showid, hideid, checksum)
{
	if(!(pic = new Image())) return false;
	if(!(date = new Date())) return false;
	pic.src = url = 'http://www.webispink.com/p/setprofiledata.php?profileid='+profileid+'&field='+field+'&value='+value+'&sessionupdate=1&c='+checksum+'&'+date.getHours()+''+date.getMinutes()+''+date.getSeconds();
	//window.open(url);

	$(showid).show();
	$(hideid).hide();
	
	if(field == 'LOOK4MEET' || field == 'LOOK4SEX')
	{
		if(value == '1') $('#headeravatarmenumoods2'+pos).slideDown();
		else if($('#headeravatar'+pos+'look4meet0').is(':visible') && $('#headeravatar'+pos+'look4sex0').is(':visible')) 
		{
			$('#headeravatar'+pos+'look4now1, #headeravatar'+pos+'look4myplace1, #headeravatar'+pos+'look4yourplace1').hide();
			$('#headeravatar'+pos+'look4now0, #headeravatar'+pos+'look4myplace0, #headeravatar'+pos+'look4yourplace0').show();
			$('#headeravatarmenumoods2'+pos).slideUp();
		}
	}
	if(field == 'ONLINE')
	{
		img = (value == '1' ? 'online' : 'offline');
		$('#HEADERAVATAR'+pos+' .headeravatarstatus img').attr('src', 'http://static.webispink.com/w/img/status7x7/'+img+'-70.png');
	}
}

var PreviousHeaderAvatarStatus = "";
function HeaderAvatarEditStatus(profileid, divid, checksum)
{
	PreviousHeaderAvatarStatus = $(divid).html();
	
	//$(divid).html('<form><textarea name="headeravatarstatusedit" cols="20" rows="3" NOonblur="HeaderAvatarEditStatusDone(\''+profileid+'\', \''+divid+'\', \''+checksum+'\');"></textarea></form>');
	$(divid).html($(divid+'form').html());
	$(divid+' textarea').focus();
}
function HeaderAvatarEditStatusCancel(profileid, divid)
{
	$(divid).html(PreviousHeaderAvatarStatus);
}
function HeaderAvatarEditStatusSave(profileid, divid, checksum)
{
	value = $(divid+' textarea').val();
	
	if(!(pic = new Image())) return false;
	if(!(date = new Date())) return false;
	pic.src = url = 'http://www.webispink.com/p/setstatus.php?profileid='+profileid+'&value='+encodeURIComponent(value)+'&sessionupdate=1&c='+checksum+'&'+date.getHours()+''+date.getMinutes()+''+date.getSeconds();
	
	if(value)
	{
		value = (value + '').replace(/</g, '&lt;');
		value = (value + '').replace(/>/g, '&gt;');
		value = (value + '').replace(/([^>]?)\n/g, '$1<br>');
		$(divid).html('<a href="javascript:HeaderAvatarEditStatus(\''+profileid+'\', \''+divid+'\', \''+checksum+'\');">'+value+'</a>');
	}
	else $(divid).html($(divid+'default').html());
}



var NumCheckForMessages = 0;
function HeaderCheckForMessages(id, pos)
{
	NumCheckForMessages++;
	if(NumCheckForMessages > 1000) return;

	date = new Date();
	$.ajax({
		type: "GET",
		url: '/r/messages/'+id+'.txt?'+date.getHours()+''+date.getMinutes()+''+date.getSeconds(),
		dataType: 'text',
		success: function(msg) 
		{
			if(msg*1)
			{
				$('#headeravatarbubble'+pos).show();
				$('#headeravatarbubbleanim'+pos).show();
				$('#myaccount_index_message_for_'+id).show();
				setTimeout("HeaderCheckForMessages("+id+", "+pos+");", 60000);
				YouHaveMessages(pos, 1);
			}
			else
			{
				$('#headeravatarbubble'+pos).hide();
				$('#headeravatarbubbleanim'+pos).hide();
				$('#myaccount_index_message_for_'+id).hide();
				setTimeout("HeaderCheckForMessages("+id+", "+pos+");", 35000);
				YouHaveMessages(pos, 0);
			}
			//alert("HeaderCheckForMessages('"+id+"', '"+pos+"');");
		}
	});
}

var NumCheckForNotices = 0;
var CheckForNoticesId = '';
function HeaderCheckForNotices(id, pos)
{
	NumCheckForNotices++;
	if(NumCheckForNotices > 1000) return;
	
	if(id) CheckForNoticesId = id;
	else id = CheckForNoticesId;

	date = new Date();
	$.ajax({
		type: "GET",
		url: '/r/notices/'+id+'.txt?'+date.getHours()+''+date.getMinutes()+''+date.getSeconds(),
		dataType: 'text',
		success: function(msg) 
		{
			if(msg && msg.length)
			{
				$('#headernoticeimage'+pos).show();
				$('#headernoticetext a').html($('#HEADER_NEW_NOTICE_'+msg).val());
				//setTimeout("HeaderCheckForNotices("+id+", "+pos+");", 240000);
				
				if(id == '1-4-13-2473') $('body').append('DEBUG:['+msg+']');
			}
			else
			{
				//$('#headernoticeimage'+pos).hide();
				setTimeout("HeaderCheckForNotices(0, "+pos+");", 60000);
				// can't resend id because somehow it's interpreted as a calculation
			}
		}
	});
}

/*****************************************************************************************************************************************/
/** IMAGE DISPLAY ************************************************************************************************************************/
/*****************************************************************************************************************************************/

function ShowImageInPopup(path)
{
	window.open('/p/imagepopup.php?path='+escape(path), '_blank', 'width=300,height=300,scrollbars=no,status=yes,location=no,resizable=yes');
}

function ShowImageOverlay(img, w, h, a)
{
	if(!document.getElementById) return;
	if(!(div = document.getElementById('imageoverlayfloat'))) return;

	if(img == 'x')
	{
		w = 400;				
		h = 200;
		div.style.display = 'block';
		div.style.visibility = 'visible';
		div.style.left = 0;
		div.style.top = 0;
		div.style.width = '100%';
		div.style.height= '100%';
		div.onclick = 'ShowImageOverlay(null,null,null);';
		div.onmousedown = 'ShowImageOverlay(null,null,null);';
		div.oncontextmenu = 'ShowImageOverlay(null,null,null);';

		padding = (GetWindowHeight() - h) / 2;
		if(padding < 0) padding = 0;
		div.innerHTML = '<div id="imageoverlay" style="padding-top: '+padding+'px;" onclick="ShowImageOverlay(null,null,null);" onmousedown="ShowImageOverlay(null,null,null);" oncontextmenu="ShowImageOverlay(null,null,null);return false;">'+
		                '<p class="x">XXX.</p>'+
		                '</div>';
	}
	else if(img)
	{
		div.style.display = 'block';
		div.style.visibility = 'visible';
		div.style.left = 0;
		div.style.top = 0;
		div.style.width = '100%';
		div.style.height= '100%';
		div.onclick = 'ShowImageOverlay(null,null,null);';
		div.onmousedown = 'ShowImageOverlay(null,null,null);';
		div.oncontextmenu = 'ShowImageOverlay(null,null,null);';
		
		/*
		size = '';
		if(w && h) size = 'width="'+w+'" height="'+h;

		padding = (GetWindowHeight() - h) / 2;
		if(padding < 0) padding = 0;
		div.innerHTML = '<div id="imageoverlay" style="padding-top: '+padding+'px;" onclick="PortraitPhoto(null,null);" onmousedown="ShowImageOverlay(null,null,null);" oncontextmenu="ShowImageOverlay(null,null,null);return false;">'+
		                '<a href="javascript:ShowImageOverlay(null,null,null);void(0);"><img src="'+img+'" '+size+'" /></a>'+
		                '</div>';
		*/
		div.innerHTML = '<div id="imageoverlay" align="center" onclick="ShowImageOverlay(null,null,null);" onmousedown="ShowImageOverlay(null,null,null);" oncontextmenu="ShowImageOverlay(null,null,null);return false;">'+
		                '<table height="'+(GetWindowHeight()-10)+'" summary="" border="0" cellpadding="0" cellspacing="0"><tr><td valign="middle">'+
		                '<a href="javascript:ShowImageOverlay(null,null,null);void(0);"><img src="'+img+'" /></a>'+
		                '</td></tr></table>'+
		                '</div>';
		
		// correction du bug bizarre de MSIE
		if(document.getElementById)
			setTimeout("document.getElementById('PORTRAITPHOTOIMG').src = '"+img+"'", 100);
	}
	else
	{
		div.innerHTML = '';
		div.style.display = 'none';
		div.style.visibility = 'hidden';
		div.style.left = '0';
		//div.style.top = '0';
		div.style.width = '0';
		div.style.margin = '0';
	}
	
	VoidLink(a);
}


/*****************************************************************************************************************************************/
/** SCROLLTO *****************************************************************************************************************************/
/*****************************************************************************************************************************************/

/*

function GetElementYPos(obj)
{
	var top = obj.offsetTop;
	var op = obj.offsetParent;
	while(obj.parentNode && obj.parentNode != document.body)
	{
		obj = obj.parentNode;
		top -= obj.scrollTop;
		if(op == obj) 
		{ 
			if(ua.safari() < 500 && obj.tagName == 'TR') top += obj.firstChild.offsetTop;
			else top += obj.offsetTop;
			op=obj.offsetParent;
		}
	}
	return top;
}

function ScrollTo(id)
{
	var obj = document.getElementById(id);
	var windowHeight = GetWindowHeight();
	var dh = GetElementYPos(obj) + obj.offsetHeight;
	dh = 300;
	if(document.documentElement) document.documentElement.scrollTop = dh - 20;
	document.body.scrollTop = dh - 20;
}

*/

/*****************************************************************************************************************************************/
/** FORM MANAGEMENT **********************************************************************************************************************/
/*****************************************************************************************************************************************/

var form_shiftenter = null;
var form_shiftescape = null;
function FormShortcuts(e)
{
	if(!form_shiftenter) return true;
	if(e.shiftKey && e.keyCode == 13)
	{
		e.preventDefault();
		FormSaved();
		if(typeof FormSending != 'undefined') FormSending();
		form_shiftenter.submit();
	}
	else if(e.shiftKey && e.keyCode == 27)
	{
		nslog('shift-escape');
		if(form_shiftescape)
		{
			e.preventDefault();
			FormSaved();
			if(typeof FormSending != 'undefined') FormSending();
			form_shiftescape.submit();
		}
	}
	return true;
}

function FormShortcutsRegister(id, escapeid)
{
	if(!id) return;
	if(!window) return;
	if(!document) return;
	if(!document.getElementById) return;
	if(!(form_shiftenter = document.getElementById(id))) return;
	if(typeof(escapeid) != 'undefined') form_shiftescape = document.getElementById(escapeid);
	window.onkeydown = FormShortcuts;
}

function FormSetFocus(id)
{
	if(!document) return;
	if(!document.getElementById) return;
	if(!(t = document.getElementById(id))) return;
	t.focus();
}


var form_unsaved = false;
function FormSaved()
{
	form_unsaved = false;
}
function FormUnsaved()
{
	form_unsaved = true;
}
function FormUnsavedCheck()
{
	return form_unsaved;
}
function FormUnsavedMessage(lang)
{
	switch(lang)
	{
		case 'fr': return 'Vos modifications seront perdues.';
		default: return 'Your changes will be lost.';
	}
}

function FormConfirm(form, msg)
{
	if(!form) return true;
	if(!form.action) return true;

	if(confirm(msg)) { form.action += '&confirm=1'; return true; }
	else return false;
}

function SubmitForm(id)
{
	form = getElementById(id);
	if(!form) return true;
	form.submit();
}

/*****************************************************************************************************************************************/
/** PING *********************************************************************************************************************************/
/*****************************************************************************************************************************************/

function PingAway(id)
{
	if(!(pic = new Image())) return false;
	if(!(date = new Date())) return false;
	pic.src = url = 'http://www.webispink.com/r/pingaway/'+id+'.txt?'+date.getHours()+''+date.getMinutes()+''+date.getSeconds();
	//alert(url);

	setTimeout("PingAway('"+id+"');", 7 * 60 * 1000);
}

function StartPingAway(id)
{
	setTimeout("PingAway('"+id+"');", 7 * 60 * 1000);
}

