var box_url = document.getElementById('box_url');
var box_photo = document.getElementById('box_photo');
var box_video = document.getElementById('box_video');

var picto_url = document.getElementById('picto_url');
var picto_photo = document.getElementById('picto_photo');
var picto_video = document.getElementById('picto_video');

var check_url = document.getElementById('check_url');
var check_photo = document.getElementById('check_photo');
var check_video = document.getElementById('check_video');

var display_url = 'none';
var display_photo = 'none';
var display_video = 'none';

var url_input = document.getElementById('url_input');
var video_input = document.getElementById('video_input');
var url_details = document.getElementById('url_details');

var ajax_petites_annonces_url = '/ajax_url_details.php';
var ajax_petites_annonces_media = '/ajax_media_details.php';
var ajax_petites_annonces_comment = '/ajax_comment_details.php';
var ajax_profile_url = '/ajax_profile.php';

var pos = 1;
var array_image;

function open_profile(nickname) {
	var req = new Request({
		method: 'get',
		url: ajax_profile_url,
		data: { 'nickname' : nickname },
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) { open_greybox(response);}
	}).send();
}

function display_album_diffuse_form()
{
	var req = new Request({
		method: 'get',
		url: '/ajax_diffuse_album.php',
//		data: { 'nickname' : nickname },
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) {
			open_greybox(response);
		}
	}).send();
}

function get_url_details() {
	var req = new Request({
		method: 'post',
		url: ajax_petites_annonces_url,
		data: { 'url' : url_input.value },
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) { open_greybox(response);}
	}).send();
}

function get_media_details() {
	var req = new Request({
		method: 'post',
		url: ajax_petites_annonces_media,
		data: { 'url' : video_input.value },
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) { open_greybox(response);}
	}).send();
}

function display_all() {
	picto_photo.style.display = 'block';
	//picto_video.style.display = 'block';
	picto_url.style.display = 'block';
	
	box_photo.style.display = 'none';
	//box_video.style.display = 'none';
	box_url.style.display = 'none';
	
	//check_video.checked = false;
	check_photo.checked = false;
	check_url.checked = false;
	
	document.getElementById('url_preview_loading').style.display='none';
	document.getElementById('url_preview').style.display='none';
	document.getElementById('image').innerHTML='<table width="120" height="90" style="background:#fff"><tr><td valign="middle" align="center"><img src="/vzg_images/ajax_image.gif" border="0" /><br />chargement des images</td></tr></table>';
	document.getElementById('url_infos').innerHTML='<center><img src="/vzg_images/ajax_image.gif" border="0" /><br />chargement des informations du site</center>';
	pos = 1;
	
	//display_video = 'none';
	display_photo = 'none';
	display_url = 'none';

	url_input.Value = '';
	url_details.innerHTML = '';
}

function toggle_url() {
	if(display_url == 'block') {
		display_all();
	} else {
		display_url = 'block';
		check_url.checked = true;
		document.getElementById('url_preview').style.display='none';
		pos=1;
		picto_photo.style.display = 'none';
		//picto_video.style.display = 'none';
		picto_url.style.display = 'block';
		box_photo.style.display = 'none';
		//box_video.style.display = 'none';
		display_photo = 'none';
		//display_video = 'none';
		box_url.style.display = 'block';
	}
}

function toggle_photo() {
	if(display_photo == 'block') {
		display_all();
	} else {
		display_photo = 'block';
		check_photo.checked = true;
		//picto_video.style.display = 'none';
		picto_url.style.display = 'none';
		picto_photo.style.display = 'block';
		box_url.style.display = 'none';
		//box_video.style.display = 'none';
		display_url = 'none';
		//display_video = 'none';
		box_photo.style.display = 'block';
	}
}

function toggle_video() {
	//alert('Bientôt disponible');
	//return false;
	
	if(display_video == 'block') {
		display_all();
	} else {
		display_video = 'block';
		check_video.checked = true;
		picto_photo.style.display = 'none';
		picto_url.style.display = 'none';
		picto_video.style.display = 'block';
		box_photo.style.display = 'none';
		box_url.style.display = 'none';
		display_photo = 'none';
		display_url = 'none';
		box_video.style.display = 'block';
	}
}

function submit_comment(id) {
	var comment = document.getElementById('my_comment_'+id);
	var comment_box = document.getElementById('comment_box_'+id);

	if(comment.value != 'Ajouter un commentaire' && comment.value != '') {
		var req = new Request({
			method: 'post',
			url: ajax_petites_annonces_comment,
			data: {
				'action' : 'add_comment',
				'id' : id,
				'comment' : comment.value
			},
			onRequest: function() {
				comment_box.innerHTML = '<center><img src="/vzg_images/ajaxload.gif" /></center>';
			},
			onComplete: function(response) {
				var reg1=new RegExp("[OK]","g");
				if(response.match(reg1)) {
					comment.value = 'Ajouter un commentaire';
				} else {
					display_growl('',response);
				}
				close_comment_input();
				reload_comment(id);
			}
		}).send();
	}
	return false;
}

var last_comment_input_id = 0;
function open_comment_input(id) {
	if(last_comment_input_id!=0) {
		document.getElementById('btn-open-comment-'+last_comment_input_id).style.display='block';
		document.getElementById('comment_input_'+last_comment_input_id).style.display='none';
	}
	last_comment_input_id = id;
	document.getElementById('btn-open-comment-'+id).style.display='none';
	document.getElementById('comment_input_'+id).style.display='block';
}
function close_comment_input() {
	if(last_comment_input_id!=0) {
		document.getElementById('btn-open-comment-'+last_comment_input_id).style.display='block';
		document.getElementById('comment_input_'+last_comment_input_id).style.display='none';
		last_comment_input_id = 0;
	}
}

function submit_wall_comment(id) {
	var comment = document.getElementById('my_comment_'+id);
	var comment_box = document.getElementById('comment_box_'+id);

	if(comment.value != 'Ajouter un commentaire' && comment.value != '') {
		var req = new Request({
			method: 'post',
			url: ajax_petites_annonces_comment,
			data: {
				'action' : 'add_wall_comment',
				'id' : id,
				'comment' : comment.value
			},
			onRequest: function() {
				comment_box.innerHTML = '<center><img src="/vzg_images/ajaxload.gif" /></center>';
			},
			onComplete: function(response) {
				var reg1=new RegExp("[OK]","g");
				if(response.match(reg1)) {
					comment.value = 'Ajouter un commentaire';
				} else {
					display_growl('',response);
				}
				reload_wall_comment(id);
			}
		}).send();
	}
	return false;
}

function reload_comment(id) {
	var comment_box = document.getElementById('comment_box_'+id);
	var req = new Request({
		method: 'get',
		url: ajax_petites_annonces_comment,
		data: {
			'action' : 'retrieve_comment',
			'id' : id
		},
		onRequest: function() {
			comment_box.innerHTML = '<center><img src="/vzg_images/ajaxload.gif" /></center>';
		},
		onComplete: function(response) {
			comment_box.innerHTML = response;
		}
	}).send();
}

function reload_wall_comment(id) {
	var comment_box = document.getElementById('comment_box_'+id);
	var req = new Request({
		method: 'get',
		url: ajax_petites_annonces_comment,
		data: {
			'action' : 'retrieve_wall_comment',
			'id' : id
		},
		onRequest: function() {
			comment_box.innerHTML = '<center><img src="/vzg_images/ajaxload.gif" /></center>';
		},
		onComplete: function(response) {
			comment_box.innerHTML = response;
		}
	}).send();
}

var topic = document.getElementsByName('topic');
var content = document.getElementsByName('content');
var ajax_message_url = '/ajax_send_mail.php';
function open_message(id) {
	var req = new Request({
		method: 'get',
		url: ajax_message_url,
		data: {
			'action' : 'display',
			'id' : id
		},
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) {
			open_greybox(response);
		}
	}).send();
}

function submit_message(id) {
	var req = new Request({
		method: 'post',
		url: ajax_message_url,
		data: {
			'action' : 'process',
			'iddest' : id,
			'topic' : topic[0].value,
			'content' : content[0].value
		},
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) {
			if(response == 'OK')
				confirm_message();
			else
				open_message_error(response);
		}
	}).send();
}

function open_message_error(response) {
	var message_error = document.getElementById('message_error');
	message_error.style.display = 'block';
	message_error.innerHTML = response;
}

function confirm_message() {
	var req = new Request({
		method: 'post',
		url: ajax_message_url,
		data: {
			'action' : 'confirm'
		},
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) {
			open_greybox(response);
			setTimeout("close_greybox()",3000);
		}
	}).send();
}

var search = document.getElementsByName('search');
var time = document.getElementsByName('time');
var user = document.getElementsByName('user');
function mysearch_submit(connected) {
	var url = '/petites-annonces/';
	var new_url = '';
	if(search[0].value!='')
	{
		if(new_url!='')
			new_url = new_url + '/';
		
		new_url = new_url + 'search/' + search[0].value;
	}
	
	if(user[0].value!='')
	{
		if(new_url!='')
			new_url = new_url + '/';
		
		new_url = new_url + 'user/' + user[0].value;
	}
	
	if(time[0].value!='' && time[0].value != 'tous')
	{
		if(new_url!='')
			new_url = new_url + '/';
		
		new_url = new_url + 'region/' + time[0].value;
	}
	
	url = url + new_url;
	
	if(time[0].value=='ma-region' && connected!='OK')
	{
		open_profile_registration(4);
		return false;
	}

	url = url + '/page/1.htm';
	document.location.href=url;
}

var ajax_album_url = '/user-online/album/';
function open_album(id) {
	var req = new Request({
		method: 'get',
		url: ajax_album_url+id,
		data: {
			'id':id
		},
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) {
			open_greybox(response);
		}
	}).send();
}

var ajax_chatalbum_url = '/user-online/chatalbum/';
function open_album_popup(id,site)
{
	if(site=='VIZIO')
	{url = 'http://www.viziogay.com'+ajax_chatalbum_url+id;}
	else if(site=='LP')
	{url = 'http://www.lespompeurs.com'+ajax_chatalbum_url+id;}
	
	window.open(url, 'Galerie', 'width=680, height=660, location=no, menubar=no, status=no, scrollbars=no, menubar=no');
}

function user_online_submit(connected) {
	var url = '/user-online/'+search[0].value;

	if(time[0].value=='ma-region' && connected!='OK')
	{
		open_profile_registration(4);
		return false;
	}
	
	if(time[0].value != '' && time[0].value != 'tous') {
		url = url + '/'+time[0].value;
		url = url + '/page/1.htm';
		document.location.href=url;
	} else {
		url = url + '/page/1.htm';
		document.location.href=url;
	}
}

var ajax_photo_url = '/ajax_photo.php';
function open_greybox_image(image_path) {
	image_obj = new Image();
	image_obj.src = '.'+image_path;
	var req = new Request({
		method: 'get',
		url: ajax_photo_url,
		data: {
			'action' : 'display_photo',
			'path' : image_path,
			'width' : image_obj.width,
			'height' : image_obj.height
		},
		//onRequest: function() { alert('Request made. Please wait...'); },
		onComplete: function(response) {
			open_greybox(response);
		}
	}).send();
}

function extract_url_details() {
	var req = new Request({
		method: 'post',
		url: '/ajax_extract_url_details.php',
		data: { 'url' : url_input.value },
		onRequest: function() {
			document.getElementById('url_preview_loading').style.display='none';
			document.getElementById('url_preview').style.display='none';
			document.getElementById('image').innerHTML='<table width="120" height="90" style="background:#fff"><tr><td valign="middle" align="center"><img src="/vzg_images/ajax_image.gif" border="0" /><br />chargement des images</td></tr></table>';
			document.getElementById('url_infos').innerHTML='<center><img src="/vzg_images/ajax_image.gif" border="0" /><br />chargement des informations du site</center>';
			pos = 1;
		},
		onComplete: function(response) {
			document.getElementById('url_infos').innerHTML=response;
			extract_url_image();
		}
	}).send();
}

function extract_url_image() {
	var req = new Request({
		method: 'post',
		url: '/ajax_extract_url_image.php',
		data: { 'url' : url_input.value },
		onRequest: function() {
			document.getElementById('url_preview_loading').style.display='none';
			document.getElementById('url_preview').style.display='block';
		},
		onComplete: function(response) {
			var reg = new RegExp('[,]','g');
			array_image = response.split(reg);
			previous_image();
			document.getElementById('item_max').innerHTML=array_image.length;
			document.getElementById('item_numb').innerHTML=1;
		}
	}).send();
}

function previous_image() {
	if((pos-1) >= 0) {
		pos = pos - 1;
		disp_image(array_image[pos]);
		document.getElementById('navig').style.display='block';
	}
}
function next_image() {
	if((pos+1) != array_image.length) {
		pos = pos + 1;
		disp_image(array_image[pos]);
	}
}
function disp_image(thumb_image) {
	document.getElementById('item_numb').innerHTML=pos+1;
	if((pos+1)>1)
		img = '/view_image.php?file='+thumb_image+'&w=120&h=90';
	else
		img = thumb_image;
	
	document.getElementById('image').innerHTML = '<img src="'+img+'" />';
	document.getElementById('thumb_image').value = thumb_image;
}