/*
* dialog : nom du module à appeller
* argument suivants ex : open_dialog('module_name', 'uid=5', 'limit=3', ...)
*/
var http_root2 = '';
if (typeof(HTTP_ROOT) !='undefined')
	http_root2 = HTTP_ROOT;

var opened_dialog = '';
var loading_displayed = 0;
var default_width = 400;
var default_height = 300;
var border_size = 10;

var is_local = false;
var default_div_class = 'dialog_default_style';

function dialog_open(dialog)
{
//alert('opened : ' + opened_dialog + '\n dialog : ' + dialog  + '\n exist : ' + $('dialog_border_confirm'));
	if (opened_dialog != '' &&  opened_dialog != dialog)
		dialog_close(opened_dialog);
	if (opened_dialog == dialog)
	{
		dialog_close(opened_dialog);
		return;
	}
	
	var div_id = dialog; 
	var request = 'dialog=' + dialog;
	var width = 0;
	var height = 0;
	var has_width = 0;
	var has_height = 0;
	var div_class = '';
	var has_div_class = 0;
	
	for( var i = 1; i < arguments.length; i++ )
	{
		request += '&' + arguments[i];
		
		var arg = arguments[i].split('=');
		if (arg[0] == 'width')
		{
			width = parseInt(arg[1]);
			has_width = 1;
		}
		if (arg[0] == 'height')
		{
			height = parseInt(arg[1]);
			has_height = 1;
		}
		if (arg[0] == 'class')
		{
			div_class = arg[1];
			has_div_class = 1;
		}
		if (arg[0] == 'local')
		{
			is_local = arg[1];
		}
	}

	if (has_width == 0)
	{
		request += '&width=' + default_width;
		width = default_width;
	}


	if (has_height == 0)
	{
		request += '&height=' + default_height;
		height = default_height;
	}
	
	if (has_div_class == 0)
		div_class = default_div_class;
	

	if (is_local == false)
	{
		var d = new Date();
		var url = http_root2 + 'include/ajax/ajax_dialog.php?' + encodeURI(request) + '&ts='+d.getTime();
		//alert(url);

		new Ajax.Request(url, 
		  {
			method: 'get',		
			onSuccess: function(transport) 
			{ 
				var html_ajax_ret = transport.responseText;
				if (html_ajax_ret)
				{
					create_div_dialog(div_id, html_ajax_ret, width, height, div_class)
				}
				
			} 
		  }
		);
	}
	else
	{
		create_div_dialog(div_id, null, width, height, div_class);
	}
}




function create_div_dialog(id, html, width, height, div_class)
{
	var border_width = width + (2 * border_size);
	var border_height = height + (2 * border_size);

	if (id == 'alert' && $('dialog_border_alert'))
	{
		document.body.removeChild($('dialog_border_alert'));
		document.body.removeChild($('dialog_box_alert'));
	}
	
	if (id == 'confirm' && $('dialog_border_confirm'))
	{	
		document.body.removeChild($('dialog_border_confirm'));
		document.body.removeChild($('dialog_box_confirm'));
	}

	if ($('dialog_border_' + id) == null)
	{
		var div_border = document.createElement('div');
		var div_box = document.createElement('div');
		
		div_border.id = 'dialog_border_' + id;
		div_box.id = 'dialog_box_' + id;
		
		div_border.style.position = "absolute";
		div_box.style.position = "absolute";
		div_border.style.display = 'none';
		div_box.style.display = 'none';
		
		div_border.style.width = border_width + 'px';
		div_border.style.height = border_height + 'px';
		
		div_box.style.width = width + 'px';
		div_box.style.height = height + 'px';
		
		div_border.className = 'dialog_border';
		div_box.className = 'dialog_box ' + div_class;
		
		if (html)
		{
			div_box.innerHTML = html;
			document.body.appendChild(div_border);
			document.body.appendChild(div_box);
		}
		else
		{
			document.body.appendChild(div_border);
			document.body.appendChild(div_box);
			div_box.appendChild($(id));
			$(id).style.display = 'block';
			if (is_local == true)
				is_local = false;
		}
	}
	else
	{
		var div_border = $('dialog_border_' + id);
		var div_box = $('dialog_box_' + id);
	}
	
	
	dialog_position.dialog_width = border_width;
	dialog_position.dialog_height = border_height;
	dialog_position.calc_dialog_position();
	div_border.style.left = dialog_position.left + 'px';
	div_border.style.top = dialog_position.top + 'px';
	div_box.style.left = dialog_position.left + border_size + 'px';
	div_box.style.top = dialog_position.top + border_size  + 'px';


//$('dialog_border_' + id).style.display = 'block';
//$('dialog_box_' + id).style.display = 'block';
	new Effect.Grow('dialog_border_' + id, { duration: 0.4});
	new Effect.Appear('dialog_box_' + id, { duration: 0.3, delay: 0.3});
	opened_dialog = id;

}



function dialog_close(id)
{
	if (loading_displayed == 1)
	{
		$('dialog_box_' + id).style.display = 'none';
		loading_displayed = 0;
		
		opened_dialog = '';
		new Effect.Parallel([
		  new Effect.Fade('div_loading', { duration: 0.5}), 
		  new Effect.Fade('dialog_border_' + id, { duration: 0.5})
		], {
		  duration: 0.5,
		  delay: 0
		});
	}
	else
	{
		opened_dialog = '';
		new Effect.Parallel([
		  new Effect.Fade('dialog_border_' + id, { duration: 0.5}), 
		  new Effect.Fade('dialog_box_' + id, { duration: 0.5}) 
		], {
		  duration: 0.5,
		  delay: 0
		});
	}
	

}



var dialog_position = {

	dialog_width: 0,
	dialog_height: 0,

	left : 0,
	top : 0,
	
	win_width: 0,
	win_height: 0,
	scroll_x: 0,
	scroll_y: 0,
	
	calc_dialog_position : function()
	{
		this.get_win_size();
		this.get_win_scroll();
		this.get_dialog_position();

	},
	

	get_dialog_position : function()
	{
		this.left = (this.win_width / 2) - (this.dialog_width / 2) + this.scroll_x;
		this.top = (this.win_height / 2) - (this.dialog_height / 2) + this.scroll_y;
/*
		alert(
			'win_width : ' + this.win_width 
			+ '\n win_height : ' +  this.win_height
			+ '\n scroll_x : ' + this.scroll_x 
			+ '\n scroll_y : ' +  this.scroll_y
			+ '\n dialog_width : ' +  this.dialog_width
			+ '\n dialog_height : ' +  this.dialog_height
			+ '\n left : ' +  this.left
			+ '\n top : ' +  this.top
			);
	*/
	},
	
	
	get_win_size : function()
	{
		if (isNaN(window.innerWidth))
		{
			if (isNaN(document.documentElement.clientWidth))
			{//IE 6
				this.win_width = document.body.clientWidth;
				this.win_height = document.body.clientHeight;
			}
			else
			{//IE > 6
				this.win_width = document.documentElement.clientWidth;
				this.win_height = document.documentElement.clientHeight;
			}
		}
		else
		{		
			this.win_width = window.innerWidth;
			this.win_height = window.innerHeight;
		}
	},


	get_win_scroll : function()
	{
		if (isNaN(window.pageXOffset))
		{
			if (isNaN(document.documentElement.scrollLeft))
			{	//IE 6
				this.scroll_x = document.body.scrollLeft;
				this.scroll_y = document.body.scrollTop;			
			}
			else
			{	//IE > 6
				this.scroll_x = document.documentElement.scrollLeft;
				this.scroll_y = document.documentElement.scrollTop;
			}
		}
		else
		{
			this.scroll_x = window.pageXOffset;
			this.scroll_y = window.pageYOffset;
		}
	},
	
	
	get_loading_left : function(loading_width)
	{
		//this.left = (this.win_width / 2) - (this.dialog_width / 2) + this.scroll_x;
		//this.top = (this.win_height / 2) - (this.dialog_height / 2) + this.scroll_y;
		var left_pos = (this.dialog_width / 2) - (loading_width / 2);
		return left_pos;
	},

	get_loading_top : function(loading_height)
	{
		var top_pos = (this.dialog_height / 2) - loading_height;
		return top_pos;
	}


};




function dialog_submit(dialog, form_name)
{
	show_loading(dialog);

	var form_item = $(form_name);
	var form_data = Array();
	
	if(form_item)
	{
		var form_data_count = 0;
		var input_fields = form_item.getElementsByTagName("input");
		for(var i=0; i < input_fields.length; i++)
		{
			if (input_fields[i].name)
			{
				if (input_fields[i].type == 'checkbox')
				{
					if (input_fields[i].checked == true)
					{
						form_data[form_data_count] = Array();
						form_data[form_data_count]['name'] = input_fields[i].name;
						form_data[form_data_count]['value'] = input_fields[i].value;
						form_data_count++;
					}
				}
				else
				{
					form_data[form_data_count] = Array();
					form_data[form_data_count]['name'] = input_fields[i].name;
					form_data[form_data_count]['value'] = input_fields[i].value;
					form_data_count++;
				}
			}
		}
		
		var select_fields = form_item.getElementsByTagName("select");
		for(var i=0; i < select_fields.length; i++)
		{
			if (select_fields[i].name)
			{
				form_data[form_data_count] = Array();
				form_data[form_data_count]['name'] = select_fields[i].name;
				form_data[form_data_count]['value'] = select_fields[i].value;
				form_data_count++;
			}
		}
		
		var textarea_fields = form_item.getElementsByTagName("textarea");
		for(var i=0; i < textarea_fields.length; i++)
		{
			if (textarea_fields[i].name)
			{
				form_data[form_data_count] = Array();
				form_data[form_data_count]['name'] = textarea_fields[i].name;
				form_data[form_data_count]['value'] = textarea_fields[i].value;
				form_data_count++;
			}
		}
		
		var post_vars = '';
		for(var i=0; i < form_data.length; i++)
		{
			post_vars += form_data[i]['name'] + '*:*' + encodeURI(form_data[i]['value']) + '*;*';
		}

		var d = new Date();
		var url = http_root2 + 'include/ajax/ajax_dialog.php?ts=' + d.getTime();
		new Ajax.Request(url, 
			{
				method: 'post',
				parameters: {dialog:dialog, submit:1, form_post:post_vars},
				onSuccess: function(transport) 
				{ 
					var ajax_ret = transport.responseText;
					dialog_close(dialog);
					if (ajax_ret == 'refresh_page')
						window.location.reload();
						
					if (ajax_ret != '')
						dialog_alert(ajax_ret);
				} 
			}
		);

	}

}

/*

function dialog_action(dialog)
{
	var div_id = 'dialog_' + dialog; 
	var request = 'dialog=' + dialog;

}
*/

function dialog_alert(msg)
{
	var width = 400;
	var height = 120;
	var dialog_title = '';

	
	for( var i = 1; i < arguments.length; i++ )
	{
		var arg = arguments[i].split('=');
		if (arg[0] == 'width')
			width = arg[1];
		if (arg[0] == 'height')
			height = arg[1];
		if (arg[0] == 'dialog_title')
			dialog_title = arg[1];
	}
	
	dialog_open('alert', 'message=' + msg, 'dialog_title=' + dialog_title, 'width=' + width , 'height=' + height);
	
}


function dialog_confirm(message)
{
	var width = 400;
	var height = 120;
	var ret_function = '';
	var ret_function_arg = '';
	var dialog_title = '';
	
	for( var i = 1; i < arguments.length; i++ )
	{
		var arg = arguments[i].split('=');
		if (arg[0] == 'width')
			width = arg[1];
		if (arg[0] == 'height')
			height = arg[1];
		if (arg[0] == 'ret_function')
			ret_function = arg[1];
		if (arg[0] == 'ret_function_arg')
			ret_function_arg = arg[1];
		if (arg[0] == 'dialog_title')
			dialog_title = arg[1];
	}
	if (ret_function != '')
		dialog_open('confirm', 'message=' + message, 'ret_function=' + ret_function, 'ret_function_arg=' + ret_function_arg, 'dialog_title=' + dialog_title, 'width=' + width , 'height=' + height);
	else
		alert('You must create a parameter ret_function');
}



function show_loading(dialog)
{
	if ($('div_loading'))
		document.body.removeChild($('div_loading'));

	var div_box = $('dialog_box_' + dialog);
	
	var img_width = 220;
	var img_height = 19;
	loading_left = dialog_position.get_loading_left(img_width);
	loading_top = dialog_position.get_loading_top(img_height);
	
	var div_loading = document.createElement('div');
	div_loading.id = "div_loading";
	div_loading.style.position = "absolute";
	div_loading.style.width = div_box.style.width;
	div_loading.style.height = div_box.style.height;
	div_loading.style.left = div_box.style.left;
	div_loading.style.top = div_box.style.top;
	div_loading.style.background = 'white';
	
	var div_loading_content = document.createElement('div');
	div_loading_content.className = 'loading';
	div_loading_content.style.left = loading_left + 'px';
	div_loading_content.style.top = loading_top + 'px';

	document.body.appendChild(div_loading);
	div_loading.appendChild(div_loading_content);
	loading_displayed = 1;
}


function confirm_response(response, input_hidden_link)
{
	if (response == 1)
	{
		var link = $(input_hidden_link).value;
		document.location.href = link;
	}
}



