// cookies
var cookies;

function getCookies () {
	cookies = document.cookie.split ('; ');
	for (i in cookies) {
		if (typeof cookies[i] == 'function')
			continue;
		cookies[i] = cookies[i].toString ().split ('=');
	}	
}
getCookies ();

function getCookie (name) {
	for (i in cookies) {
		if (typeof cookies[i] == 'function')
			continue;
	
		if (cookies[i][0] == name)
			return cookies[i][1];
	}			
	
	return null;
}

function setCookie (name, value) {
	var argv=setCookie.arguments;
	var argc=setCookie.arguments.length;
	document.cookie=name+"="+escape(value)+(argv[2] ? ("; expires="+argv[2].toGMTString()): '');

	for (i in cookies) {
		if (typeof cookies[i] == 'function')
			continue;
	
		if (cookies[i][0] == name)
			return cookies[i][1] = value;
	}			
}


function trace (message) {
	$('ajax_debug').innerHTML += message+'<br>';
}

function alertE (msg) {
	alert (msg);
	return false;
}

function showModalbox (url, options) {
	options = options || {};
	$('miniCaroussel_content').style.visibility = 'hidden';
	if ($('adv_content'))
		$('adv_content').style.visibility = 'hidden';
	for (var i = 0; i < ads.length; i++)
		$(ads[i].id).style.visibility = 'hidden';
	options.onShow = function () { 
			Resize0 ();
	}
	options.afterHide = function () { 
		$('miniCaroussel_content').style.visibility = '';
		if ($('adv_content'))
			$('adv_content').style.visibility = '';
		for (var i = 0; i < ads.length; i++)
			$(ads[i].id).style.visibility = '';
		setTimeout (Resize0, 100);
	}
		
	Modalbox.show("", url, options);	
}

function showMsgChat () {
	$('msgChat').style.visibility = '';
}


// ------------------------------------------------------------------------------------------------

function register (sex) {
	$('miniCaroussel_content').style.visibility = 'hidden';
	if ($('adv_content'))
		$('adv_content').style.visibility = 'hidden';
	showModalbox ("/register1.php?sex="+(sex == null ? -1 : sex), { width: 404, height: 423 });
}

function warn (id, type) {
	if (me_warn != 0)
		return;
	
	$('actions').hide ();
	lastActionsId = 0;
	
	if (me_sex == 0 && type == 1)
		new ajax ("/fajax_warnGirl.php?id="+id+"&type="+type);

	showModalbox ("/warning.php", { width: 365, height: 392, params: {id: id, type: type} });
}

function setWarning (value, type) {
	value = value==true ? 1 : 0;
	me_warn = value;
	new ajax ('fajax_setWarning.php?value='+value+'&type='+type);
}

function openAlbum (id, pict) {
	if (me_sex == '')
		return register ();
		
	if (pict == "X")
		return;

	showModalbox ("/fajax_album2.php", { width: 836, height: 522, params: {id: id, pict: pict} });
}


function charm(id) {
	if (me_sex == '')
		return register ();

	new autoAjax ("/fajax_addBasket.php?id="+id);
}

function noMoreFlashes (id) {
	$('miniCaroussel_content').style.visibility = 'hidden';
	if ($('adv_content'))
		$('adv_content').style.visibility = 'hidden';
	showModalbox ("/nomoreflashes.php?id="+id, { width: 507, height: 425 });
}

var __BasketCount = 2;
function addBasket(id) {
/*
	if (me_sex == '1') {
		var x = findPosX ($("logo"));
		var y = findPosY ($("logo"));
		$('icoBasket').style.left = (x-4)+"px";
		$('icoBasket').style.top  = (y-12)+"px";
		$('icoBasket').show ();
		document.getElementById("icoBasketSWF").SetVariable('_click',__BasketCount++);
	}
*/
	new autoAjax ("/fajax_addBasket.php?id="+id);
}

function updateBasket(entries, nbo, cnt, sex) {
	var so = new SWFObject("http://s.adopteunmec.com/swf/miniCaroussel18.swf", "miniCaroussel", "220", "100", "7", "#000000");
	so.addVariable("d", entries);
	so.addVariable("nbo", nbo);
	so.addVariable("sex", sex);
	so.addParam("AllowScriptAccess", "always");
	so.write("miniCaroussel_content");

	$('basketCount').innerHTML = cnt;
}

function openChat (id) {
	if (me_sex == '')
		return register ();
	
	if ($('chatIco') != null) {
		var html = '<a href="javascript:openChat();" class=header2><img src="'+img_server+'icoChat.gif" align="absbottom">';
		html += (browser.name == "explorer" && (browser.version*1) < 7) ? "" : " CHAT";
		html += '</a>';
		$('chatIco').innerHTML = html;
		$('chatIco').onclick = function () { openChat () };
	}
	
//	if (chatWindow == null  || chatWindow.closed) {
		var url = 'chat.php';
		if (id != null)
			url += '?to='+id;
		var chatWindow = window.open (url,'chat','width=740,height=380');
//	}
	
	chatWindow.focus ();
}

function sendMail (id) {
	if (me_sex == '')
		return register ();

	window.location = "/thread.php?id="+id;
}

function deblock (id) {
	if (me_sex == '')
		return register ();

	new autoAjax ("/fajax_postMessage.php?action=deblock&to="+id);
}

var blockId = null;
var blockRnd = null;
var blockSex = null;
function blockMember (id, rnd, sex) {
	if (me_sex == '')
		return register ();
	
	blockId = id;
	blockRnd = rnd;
	blockSex = sex;
	
	new autoAjax ("/fajax_block.php?id="+id);
}

function blockMemberRes (status) {
	updateActions (blockId, blockRnd, blockSex, status);
	
	var blockStatus = $('status'+blockRnd);
	if (blockStatus)
		blockStatus.src = img_server+"smallCtc"+status+".png";

	blockId = null;
	blockRnd = null;
	blockSex = null;
}

var lastActionsId = 0;
function displayActions (table) {
	if ($('actionPict') == null)
		return;

	if (lastActionsId == table.id)
		return;
		
	lastActionsId = table.id;

	var id = table.id.split ('_')
	var rnd = "_"+id[1];
	var sex = id[2];
	id = id[0];

	$('actionPict').style.backgroundImage = "url("+img_server+"i/null.png)";
	$('actionPict').onclick = $('pict'+rnd).onclick;
	
	$('actions_view').onclick = $('pict'+rnd).onclick;

	var elem = $('status'+rnd);
	var status = elem.src.charAt (elem.src.indexOf (".png")-1);
	if (status == 'l')
		status = '';
	
	updateActions (id, rnd, sex, status);
	
	var x = findPosX (table);
	var y = findPosY (table);
	
	$('actions').style.left = (x-18)+'px';
	$('actions').style.top = y+'px';

	$('actions').show ();
}

function updateActions (id, rnd, sex, status) {
	var elem;
	var test;
//	var tag = getCookie ('display') == 2 ? '' : rnd;
	var tag = $('actions_flash'+rnd) ? rnd : '';

	if ($('actions_deblock'+tag)) {
	elem = $('actions_deblock'+tag);	
	test = me_sex == '' || (me_sex == 1 && sex != me_sex && status < 2);
	elem.style.cursor = test==true ? 'pointer' : '';
	elem.onclick = test==true ? ( function () { deblock (id); } ) : null;
	elem.setOpacity (test==true ? 1 : 0.5);
	}

	elem = $('actions_flash'+tag);	
	test = me_sex == '' || (sex != me_sex && status < 2);
	elem.style.cursor = test==true ? 'pointer' : '';
	elem.onclick = test==true ? ( sex==1 ? function () { charm (id); } : function () { addBasket (id); } ) : null;
	elem.setOpacity (test==true ? 1 : 0.5);

	elem = $('actions_chat'+tag);	
	test = $('online'+rnd).innerHTML!='' && (me_sex == '' || (status < 2 && (me_sex == '1' || status != '')));
if (debug_autoAjax == null || debug_autoAjax == false)
	test = false;
	elem.onclick = test==true ? function () { openChat (id); } : null;
	if (test == false && me_sex == '0' && me_warn==0) {
		test = true;
		elem.onclick = function () { warn (id, 1); };
	}
	elem.style.cursor = test==true ? 'pointer' : '';
	elem.setOpacity (test==true ? 1 : 0.5);

	elem = $('actions_mail'+tag);	
	test = me_sex == '' || (status < 2 && (me_sex == '1' || status != ''));
	elem.onclick = test==true ? function () { sendMail (id); } : null;
	if (test == false && me_sex == '0' && me_warn==0) {
		test = true;
		elem.onclick = function () { warn (id, 1); };
	}
	elem.style.cursor = test==true ? 'pointer' : '';
	elem.setOpacity (test==true ? 1 : 0.5);
/*
	elem = $('actions_block'+tag);	
	var b = [ 
		[0,0,0,1,1],
		[0,0,1,0,1],
	];
	elem.src = img_server+((me_sex == '' || status == '' || b[me_sex][status] == 0) ? 'actBlock.gif' : 'actDeblock.gif');
	test = me_sex == '1' || status != '';
	elem.style.cursor = test==true ? 'pointer' : '';
	elem.onclick = test==true ? function () { 
		blockMember (id, rnd, sex); 
	} : null;
	elem.setOpacity (test==true ? 1 : 0.5);
*/
}




function dispNumber (amount) {
	amount = amount || 0;
	amount = amount.toString ();
	var ret = "";
	var len = amount.length;
	var i;
	for (i = 0; i < len; i++) {
		if (i == 3 || i == 6 || i == 9 || i == 12)
			ret = "." + ret;
		ret = amount.charAt((len -i) -1)+ret;
	}
	
	return ret;
}

var popuToTimer = null;
function popuTo(value) 
{
	if(me_popu == value)
		return;

	var i = Math.ceil(Math.abs(value-me_popu)/10);

	if(((me_popu+i) > value) && ((me_popu-i) < value))
		me_popu = value;

	if(me_popu < value)
		me_popu += i;
	if(me_popu > value)
		me_popu -= i;

	$('popScore').innerHTML = dispNumber (me_popu);
//	setJauge (me_popu/popuMax);

	if (popuToTimer)
		clearTimeout (popuToTimer);
		
	popuToTimer = setTimeout ("popuTo (" + value + ")", 50);
}

go_visibility = new Array;
function goblink() {
	if(document.getElementById && document.all) {
		blink_tab = document.getElementsByTagName('blink');
		for(a=0;a<blink_tab.length;a++)
			blink_tab[a].style.visibility=blink_tab[a].style.visibility != "visible" ? "visible" : "hidden";
	} else {
		blink_tab = document.getElementsByTagName('blnk');
		for(a=0;a<blink_tab.length;a++)
			blink_tab[a].style.visibility=blink_tab[a].style.visibility != "visible" ? "visible" : "hidden";
	}
}
setInterval ("goblink()", 500);

var particlesPts;
function showParticlesFrom (id, pts) {
	var x = findPosX ($(id));
	var y = findPosY ($(id));
	
	return showParticles (x, y, pts);
}
function showParticles (x, y, pts) {
	var ax = Math.floor ((screenSizeX-$('top').width)/2);
	var ay = Math.max (0, findPosY ($("top"))-32);
	
	$('particles').style.left = ax+'px';
	$('particles').style.top  = ay+'px';
	x -= ax;
	y -= ay;
	
	var dx = findPosX ($('popScore'));
	var dy = findPosY ($('popScore'));
	dx -= ax;
	dy -= ay;
	
	$('particles').innerHTML = '';
	$('particles').show ();
//	if ($('miniCaroussel_content'))
//		$('miniCaroussel_content').style.visibility = 'hidden';

	var so = new SWFObject(swf_server+"particles.swf", "particles0", "776", y+150, "8", "#336699");
	so.addVariable("largeur", 776);
	so.addVariable("hauteur", y+150);
	so.addVariable("x_depart", x);
	so.addVariable("y_depart", y);
	so.addVariable("x_arrivee", dx);
	so.addVariable("y_arrivee", dy+70);
	so.addVariable("points", "+"+pts+"pts");
	so.addParam ("scale", "noscale");
	so.addParam("wmode", "transparent");
	so.write("particles");
	
//	alert (x+" "+y+" "+dx+" "+dy);
	
	particlesPts = pts;
}

function updatePopuScore (pts) {
	pts = me_popu+(pts || particlesPts);

//	setJauge (pts/popuMax);

	popuTo (pts);
}

function hideParticles () {
	$('particles').hide ();
	$('particles').innerHTML = '';
//	if ($('miniCaroussel_content'))
//		$('miniCaroussel_content').style.visibility = 'visible';
}

var alertCount = 0;
function dispAlert (a) {
	var div = document.createElement("DIV");
	
	setTimeout (function () {
		if (div) 
		new Effect.Move (div,{ 
			x: 211, 
			y: 0, 
			mode: 'relative',
			afterFinish: function (elem) {
				if (div)
					Element.remove (div);
			}
		});
	}, 5000);

	div.innerHTML = a;
		
	$('alerts').appendChild (div);
}

var getAlertsStartTime = 15000;
var getAlertsTime = getAlertsStartTime;
var getAlertsTimeInc = 30000;
function getAlerts () {
	new autoAjax ("/fajax_getAlerts.php"+(getAlertsTime==getAlertsStartTime?"?ft=true":""));
	
	getAlertsTime = Math.min (getAlertsTime+getAlertsTimeInc, 10*60*1000);

	setTimeout (getAlerts, getAlertsTime);
}

function setJauge (to) {
	var j1 = Math.round (to*200);
	$('jauge1').style.width = j1+'px';

	if ($('jauge0')) {
		var j0 = 200-j1;
		$('jauge0').style.width = j0+'px';
	}
}

function loginBox(loginform){
	loginform.login.onfocus = 
	loginform.password.onfocus = function() {};
	loginform.login.value = "";
	loginform.login.style.color="#000000";
	
	var input = document.createElement('input');
	input.type = 
	input.name = 'password';
	input.size = loginform.password.size;
	input.value = '';
	input.style.color="#000000";
	input.style.height="15px";
	
	loginform.password.parentNode.replaceChild(input, loginform.password);
}

function nextImage (path, images){
	var p = $('picture');

	showImage (path, images[(images.indexOf (p.name)+1)%images.length]);
}

function showImage (path, pict){
	if (pict == null)
		return;

	var p = $('picture');

	Element.show ($('cache_'+p.name));
	p.src = path+'image'+pict;
	p.name = pict;
	Element.hide ($('cache_'+pict));
}


function updateBoard (what) {
	switch (what) {
	case 1: // mail 
		var count = $('mailsCounter').innerHTML.replace(/(<([^>]+)>)/ig,"");
		count = count*1;
		$('mailsCounter').innerHTML = "<blink>"+(count+1)+"</blink>"; 
		break;
	case 2: // flash/panier 
		$('flashsCounter').innerHTML ++;
		break;
	case 3: // visite
		$('visitesCounter').innerHTML ++;
		break;
	}
}

function cleanPseudo (pseudo) {
	var i;
	var temp;
	
	var kills = ".-_/\\(){}#&><%$µŁ!?:;,˛~»’][@€+=*";
	
	for (i = 0, temp = ''; i < pseudo.length; i++)
		if (kills.indexOf (pseudo.charAt (i)) < 0)
			temp += pseudo.charAt (i);
	pseudo = temp;

	pseudo = pseudo.toLowerCase().replace (/[^a-zéčŕęâůďüë\40\']/g, '');
	
	var words = pseudo.split (' ');
	
	for (var i = 0; i < words.length; i++)
		words[i] = words[i].capitalize ();
		
	pseudo = words.join (' ');

	var count = 0;
	for (i = 1, temp = pseudo.charAt (0); i < pseudo.length; i++) {
		if (pseudo.charAt (i) != pseudo.charAt (i-1))
			count = 1;
		else
			if (count++ > 1)
				continue;
				
		temp += pseudo.charAt (i);
	}
	pseudo = temp;
	
	var p = pseudo.toLowerCase ();
	
	words = [
		'bite',			'bitte',		'sex',			'xxx',			'vagin',
		'chatt',		'chate',		'beaugos',		'bogos',		'verge',
		'penis',		'pinneu',		'susseuse',		'susseuz',		'suce',
		'queu',			'keu',			'zob',			'lecheur',		'lecheuz',
		'lecheuse',		'lecher',		'lecheus',		'outil',		'kiki',
		'chaud',		'encul',		'sodomi',		'défonc',		'anal',
		'anus',			'sperm',		'ejac',			'facial',		'clito',
		'penétration',	'fellation',	'cunni',		'salop',		'pute',
		'prosti',		'trottoir',		'gode',			'vibro',		'pétass',
		'poufia',		'grognas',		'salop',		'connar',		'conasse',
		'boncoup',		'chaulapin',	'chaud',		'gigolo',		'circoncis',
		'venal',		'grocul',		'soumi',		'domina',		'chien',
		'string',		'culott',		'biatch',		'bitch',		'pervers',
		'gangbang',		'echange',		'échange',		'levrette',		'tournante',
		'capotte',		'enfant',		'gamin',		'durex',		'manix',
		'gros',			'cousin'
	];
	
	for (var i = 0; i < words.length; i++) {
		if (p.indexOf (words[i]) >= 0) {
			pseudo = "";
			break;
		}
	}
	
	return pseudo.substr (0, 12);
}
