var checkForFlash = function (version){
	var status = false, flash;
	if (window.ActiveXObject) {
		try {
			flash = new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version));
			status = true;
		} catch(e) {
			// Throws an error if the version isn't available			
		}
	} else if(navigator.plugins && navigator.mimeTypes.length > 0) {
		flash = navigator.plugins["Shockwave Flash"];
		if (flash) {
			var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*\s(\d+\.\d+).*/, "$1");
			if (flashVersion >= version) {
				status = true;
			}
		}
	}
	return status;
};

//load Chat Flash
var load_flash = function (show_onload) {
	var isIE = /*@cc_on!@*/false;
	var c = 'flash_container', swf = 'flashSwf', obj = 'flashObj';
	var f = {}, o =[], el = $(c);
	if (typeof show_onload != 'boolean') {
		show_onload = false;
	}
	//flash parameters
	f.width  = 1;
	f.height = 1;
	f.url    = 'flash/chat.swf?design=' + new Date().getTime();
	f.vars   = 'id_user=' + cs.id_user + '&langue=' + cs.lg + '&uid=' + cs.pyt  + '&show_onload=' + ( (show_onload === true) ? 1:0 ) + ((typeof cs.debug === 'string' && cs.debug.length>0) ? '&dev_name=' + cs.debug:'') + ((cs.wowza && cs.wowza > 0) ? '&wowza=' + cs.wowza:'');
	f.vars  += (cs.so == 1) ? '&so=1' : '&so=0';
	//building flash insert
	var header = (isIE) ? 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0"' : 'type="application/x-shockwave-flash" data="'+f.url+'"';
	o.push('<div id="'+obj+'"><object id="'+ swf +'" ' + header  + ' width="'+f.width+'" height="'+f.height+'">');
	if (isIE) {
		o.push('<param name="movie" value="'+f.url+'" \/>');
	}
	o.push('<param name="allowScriptAccess" value="always" \/>');
	o.push('<param name="allowFullScreen" value="true" \/>');
	o.push('<param name="flashvars" value="'+f.vars+'" \/>');
	o.push('<\/object><\/div>');
	el.innerHTML = o.join('\n');
	el.firstChild.style.width = (f.width+10) + 'px';
	$('shadow').style.display = 'none';
	document.getElementsByTagName('html')[0].style.overflow = 'auto';
	document.body.style.overflow = 'auto';
};

// showHideChat
var showHideChat = function (show) { 
	var o =  $('flashObj'), c = $('flashSwf');
	o.style.width  = c.style.width  = '960px';
	o.style.height = c.style.height = (show) ? '400px' : '30px';
	if (show) { 
		window.location.hash  = '#flash_container'; 
	}
};

//reload 
var cs_reload = function () {
	if (cs.id_user > 0) {
		$('flash_container').innerHTML = '';
		load_flash(true);
	}
};

//send chat request
var sendRequest = function(id_user) { 
	if (!cs.id_user || cs.id_user === 0) {
		open_popunder($('subscribe'));
	} else if (cs.id_user === id_user) {
		setLocation('module=profil');
	} else { 
		$('flashSwf').sendRequestFromJS(id_user); 
	} 
};

var play_vids = function (url) {		 
	var objFlash = $('flv_player');
	if(objFlash) {
		objFlash.play_video_JSCall(url);
	}
};

var videoAdded = function(){
	setLocation('module=profil&action=view_videos&status[add_video]=OK');
};

var alertUserByTitle = function (n) {
	setTitleContent( n + ' viens de te parler sur le tchat');
};

//getting and caching connected users from the flash/SQL server
var get_connected_users = function(o) {
	if (getObjectType(o) !== 'Array') {
		return;
	}
	cs.users   = [];
	cs.geodata = [];
	if (getObjectType(cs.hasCBtld) != 'RegExp') {
		cs.hasCBtld = new RegExp ('^(' + cs.cb30list + ')$','i');
	}
	var hasCitytld = /^(FR|BE|IT|LU)$/;
	var actif     = (cs.cb30list.indexOf(cs.tld) > -1) ? true : false;
	var isHotesse = (inArray(cs.id_user, cs.hotesses)>-1) ? true : false;
	for (var i = 0, j = o.length, u, user, geo = [], content, hasImg, alist = [], blist = [], clist = [], dlist = [], sex = (cs.gender==='fem') ? 'mal' : 'fem'; i < j; i += 1) {
		u = o[i].split('|');
		user = {};
		user.id_user    = u[0];
		user.nick       = u[1];
		user.age        = u[2];
		user.country    = u[3];
		user.city       = u[4];
		hasImg          = (/150x150/.test(u[5])) ? true : false;
		user.avatar     = (hasImg) ? 'http://media.camsympa.com/members/' + u[5] : 'mb/default/images/avatar_' + sex + '.gif';
		user.cam        = (u[6] == 'y') ? true : false;
		user.sound      = (u[7] == 'y') ? true : false;
		user.fav        = (u[8] == 'y') ? true : false;
		user.tld        = u[9];
		user.d_name     = u[10];
		user.etat       = u[11];
		user.actif_type = u[12];
		//PATCH TEMPORAIRE 
		if (isHotesse) {
			if (cs.cb30list.indexOf(user.tld) == -1) {
				continue;
			}
		} else {
			if (inArray(user.id_user, cs.hotesses)>-1 && (cs.etat != 'ACTIF' || actif === false)) {
				continue;
			}
		}
		if (u[10] !== undefined && getObjectType(u[10]) == 'String') {
			user.isOK    = (u[10] == 'y') ? true : false;
		}
		var s = user.country.indexOf(',');
		if (s > -1) {
			user.country = user.country.substring(0, s);
		}
		user.l9n = (/^\s*$/.test(user.city)) ? user.country : user.city;
		if (/^Provence/.test(user.l9n)) {
			user.l9n = 'PACA';
		}
		if (user.l9n.length >= 22) {
			user.l9n = user.l9n.substring(0, 18) + '...';
		}
		user.l9n = ', ' + user.l9n;
		content = [];
		content.push('<span class="showdetail">\n');
		content.push('<img src="' + user.avatar + '" alt="" width="150" height="150" class="showdetail" \/><br\/>\n');
		if (isHotesse && user.etat == 'ACTIF') {
			var actif_type = (user.actif_type == 'TRIAL') ? 'pending' : ( (user.actif_type == 'REBILL') ? 'rebill' : 'actif');
			content.push('<img src="http://www.camsympa.com/mb/default/images/user-' + actif_type + '.gif" alt="" class="user-status" \/>\n');
		}
		content.push('<b>' + user.nick + '<\/b><br\/>\n');
		if (user.tld == 'null') {
			user.tld = user.country;
		}
		if (cs.gender == 'mal' && cs.etat == 'PENDING' && cs.hasCBtld.test(cs.tld) && !cs.hasCBtld.test(user.tld)) {
			user.l9n = '';
		} 
		user.geoinfos = user.age +' ans' + user.l9n;
		if (hasCitytld.test(user.tld) && inArray(user.tld, geo) === -1) {
			cs.geodata.push({'text': user.country, 'value' : user.tld});
			geo.push(user.tld);
		}
		content.push(user.geoinfos + '\n');
		content.push('<\/span>\n');
		content.push('<a href="#flash_container"><img src="mb/default/images/contacts_chat.gif" alt=""\/> Tchatte</a>&nbsp;<span class="mailto"><img src="mb/default/images/contacts_email.gif" alt="" \/> Ecrire<\/span>\n');
		user.content = content.join('');
		if (hasImg) {
			if (cs.hasCBtld.test(user.tld)) {
				alist.push(user);
			} else {
				blist.push(user);
			}
		} else {
			if (hasCitytld.test(user.tld)) {
				clist.push(user);
			} else {
				dlist.push(user);
			}
		}
	}
	cs.users = alist.concat(blist.concat(clist.concat(dlist)));
	cs.geodata.push({'text':'Tous les pays', 'value':''});
};

var get_user_infos = function (id_user) {
	var user = undefined;
	if (getObjectType(cs.users) == 'Array') {
		for (var i = 0, o; o = cs.users[i]; i++) {
			if (o.id_user === id_user) {
				user = o;
				break;
			}
		}
	}	
	return user;
};

var js_navigation = function (params) {
	var p = params || {};
	p.item_by_page = p.item_by_page || 15;
	p.interval     = p.interval || 3;
	p.index        = p.index || 1;
	p.maxPage   = Math.ceil(p.nb_items/p.item_by_page);
	if (p.index > p.maxPage || p.index < 1) {
		p.index = 1;
	} 
	p.firstPage = Math.max(p.index - p.interval, 1);
	p.lastPage  = Math.min(p.index + p.interval, p.maxPage);
	p.firstItem = (p.index - 1) * p.item_by_page;
	p.lastItem  = Math.min(p.firstItem + p.item_by_page, p.lastPage * p.item_by_page);
	return p;
};

var doNavigate = function (event) {
	var o = getEventTarget(event);
	if (o.nodeName == 'SPAN' && o.parentNode.id == 'navConnected') {
		var index = o.className.match(/^nav-goto-(\d+)$/i);
		if (index !== null) {
			show_connected_users(parseInt(index[1], 10));
		}
	}
};

var find_connected_users = function (event) {
	var d_name = '', o = getEventTarget(event);
	if (!/^\s*$/.test(o.value)) {
		d_name = new RegExp(o.value, 'i');
	}
	show_connected_users(cs.timer.page, d_name);
}

//general function to show the actual flash connected users
var show_connected_users = function () {
	var delay = 6E4, users_by_page = 15, no_match = false, page2show = 3, nav = [], n, p, i, j, user, div, script, users, f, navigation, container = $('connected_users'), s = $('search_shadow'), pagination = $('connected_navigation');
	cs.timer.page = (getObjectType(arguments[0]) == 'Number') ? arguments[0] : ((getObjectType(cs.timer.page) != 'Number') ? 0 : cs.timer.page);
	cs.conn.reg   = (getObjectType(arguments[1]) == 'RegExp') ? arguments[1] : ((getObjectType(cs.conn.reg) != 'RegExp') ? '' : cs.conn.reg);
	if (container && /module=home/.test(cs.qs_storage) && !/\&action=/.test(cs.qs_storage) && s.style.display !== 'block') {
		if (cs.users === undefined || getObjectType(cs.users) !== 'Array' || cs.users.length === 0) {
			cs.timer.connected = setTimeout(arguments.callee, 2E3);
			/* DO NOT ERASED <-- for rollback purpose 
			script = $('script_connected');
			if (!script) {
				script = document.createElement('script');
				script.id = 'script_connected';
				$('footer').appendChild(script);
			}
			script.src = 'index.php?module=home&js_array=1&timestamp=' + (new Date()).getTime();
			*/
			return;
		}
		container.innerHTML = '';
		if (cs.timer.connected !== undefined) {
			clearTimeout(cs.timer.connected);
		}
		users = cs.users;
		if (getObjectType(cs.conn.reg) == 'RegExp') {
			var tmp = users;
			for (var i = 0, j = tmp.length, users = []; i<j; i++) {
				if (cs.conn.reg.test(tmp[i].d_name)) {
					users.push(tmp[i]);
				}
			}
			if (users.length === 0) {
				cs.conn.reg = '';
				no_match = true;
				users = tmp;
			}
		}

		n = js_navigation({'nb_items' : users.length, 'item_by_page' : users_by_page, 'interval' : page2show, 'index' : cs.timer.page });
		if (n.maxPage > 1) {
			if (n.index !== 1) {
				nav.push('<span class="nav-goto-0" title="' + cs.wording.navigation.firstPage  + '">&laquo; ' + cs.wording.navigation.start  + '<\/span>\n...\n');
			}
			for (p = n.firstPage; p <= n.lastPage; p++) {
				if (p === n.index) {
					nav.push('<span class="selected">' + p + '<\/span>\n');
				} else {
					nav.push('<span class="nav-goto-' + p + '">' + p + '<\/span>\n');
				}
			}
			if (n.index !== n.maxPage) {
				nav.push('\n...\n<span title="' + cs.wording.navigation.lastPage + '" class="nav-goto-' + n.maxPage + '">' + cs.wording.navigation.end + ' &raquo;<\/span>\n');
			}
		}

		navigation = document.createElement('P');
		navigation.id = 'navConnected';
		navigation.className = 'navigation';
		navigation.innerHTML = nav.join('');
		pagination.innerHTML = '';
		pagination.appendChild(navigation);
		container.innerHTML = '';
		if (no_match===true) {
			tmp = document.createElement('h2');
			tmp.className = 'status status-ko';
			tmp.innerHTML = "Aucun profil n'est connect&eacute; dans cette localit&eacute;"; 
			container.appendChild(tmp);
		}

		for (f = document.createDocumentFragment(), i = n.firstItem, j = n.lastItem; i < j; i += 1) {
			user = users[i];
			if (user !== undefined) {
				div           = document.createElement('DIV');
				div.id        = 'memberid-' + user.id_user;
				div.className = 'contacts';
				div.innerHTML = user.content;
				f.appendChild(div);
			}
		}
		container.appendChild(f);
	}
	cs.timer.connected = setTimeout(arguments.callee, delay);
};

//view profil detail from Flash
var viewDetail = function () {

	//view profil
	var show_profil = function (id_user)  {
		var shadow = $('search_shadow');
		var dList = $('main_bottom').getElementsByTagName('div');
		var d = $('detail-' + id_user);
		var dis = d.style.display;
		for (var i=0,o;o=dList[i];i+=1) {
			if (/search/i.test(o.className)) {
				o.style.display='none';
			}
		}
		var objList = $('main_content').getElementsByTagName('object');
		var selectList = $('main_content').getElementsByTagName('select');
		shadow.style.display = 'none';
		if (dis == 'none') {
			var content = $('content');
			d.style.display = '';
			d.style.left = ((content.offsetWidth-d.offsetWidth)/2)+'px';
			d.style.top = ((content.offsetHeight-d.offsetHeight)/4)+'px';
			if( content.offsetHeight < d.offsetHeight ){
				content.style.height = (d.offsetHeight + 20) + 'px';
			}
			shadow.style.display='block';
			shadow.style.left=0;
			shadow.style.top=0;
			shadow.style.width=content.offsetWidth+'px';
			shadow.style.height=content.offsetHeight+'px';
			for(var i = 0, o; o=objList[i]; i += 1){
				o.style.visibility = 'hidden';	
			}
			for(var i = 0, o; o=selectList[i]; i += 1){
				o.style.visibility = 'hidden';	
			}
		} else {
			if (objList) {
				for(var i = 0, o; o=objList[i]; i += 1){
					o.style.visibility ='';	
				}
			}
			if (selectList) {
				for(var i = 0, o; o=selectList[i]; i += 1){
					o.style.visibility ='';	
				}
			}
		}
	};

	var load_profil = function(e) { 
		if (e === cs.HR.location && cs.HR.location.status === 200) { 
			var m = $('main_bottom'), id_user = e.responseText.match(/detail-(\d+)/); 
			if (id_user !== null) {
				id_user = id_user[1];
				var c = e.responseText, o = get_user_infos(id_user);
				if (o !== undefined && getObjectType(o) == 'Object') {
					c = c.replace(/((\d+)\sans\s,(.*)<br)/i, o.geoinfos + '<br');
				}
				m.innerHTML += c; 
				show_profil(id_user); 
			}
		} 
	};

	var get_profil = function (id_user) { 
		if (cs.HR.location !== null) {
			cs.HR.location.abort();
		}
		cs.HR.location = sAjax(window.location.pathname, 'module=home&action=detail&ajax=1&id_user=' + id_user, load_profil , {'mime':'plain','method':'post'}); 
	};

	return function (id_user) { 
		if (cs.id_user === 0) {
			open_popunder($('subscribe'));
		} else if (cs.id_user === id_user) {
			setLocation('module=profil');
		} else {
			window.location.hash='#content'; 
			if($('detail-'+id_user)) { 
				show_profil(id_user); 
			} else { 
				$('search_shadow').style.display = 'block'; 
				var objList = $('main_content').getElementsByTagName('object');
				if (objList) {
					for(var i = 0, o; o=objList[i]; i += 1){
						o.style.visibility = 'hidden';	
					}
				}
				get_profil(id_user); 
			}
		}
	};
}();

var view_videolist = function (id_user, yes){
	var divList = $('detail-' + id_user).getElementsByTagName('div');
	for (var i=0, o; o = divList[i]; i += 1){
		if (/cam-carrousel/.test(o.className)) {
			o.style.display = 'none';
			if(yes===true){
				if(/videolist/.test(o.className)){
					o.style.display='';
				}
			} else {
				if(!/videolist/.test(o.className)){
					o.style.display='block';
				}
			}
		} else if (/user_description/.test(o.className)) {
			if (yes === true){
				if(/videolist/.test(o.className)){
					o.style.height='260px';
					o.style.width='349px';
					o.style.visibility='visible';
				} else {
					o.style.display='none';
				}
			} else {
				if(/videolist/.test(o.className)){
					o.style.height='1px';
					o.style.width='1px';
					o.style.visibility='hidden';
				} else {
					o.style.display='';
				}
			}
		}
	}
	return false;
};

