$(document).ready(function ()
{
	$('input[type=text], textarea, select').eq(0).focus();
});

function checkAllBoxes(id)
{
	$('div#' + id + ' input').attr('checked', true);
}

function uncheckAllBoxes(id)
{
	$('div#' + id + ' input').removeAttr('checked');
}

$(document).ready(function()
{
	if (typeof LightBox !== 'undefined') 
		LightBox.createMain();
});

function LiveAdd($f, options)
{
	this.$field = $f;
	this.settings = $.extend({
		_save_button: 'input[type=submit], button[type=submit]'
	}, options);
	this.init();
	this.load();
}

LiveAdd.prototype = {
	init: function()
	{
		var me = this;
		this.lightbox = new LightBox($('#lightbox_main'), $.extend(true, {
			load: {
				url: this.settings.url
			},
			
			onSaveSuccess: function(resp)
			{
				me.onSave(resp);
			},
			
			onSuccess: function ()
			{
				this.$box.find('input[type=text], input[type=password]').each(function ()
				{
					wrapField.call(this);
				});
				this.$box.find('input[type=text]').eq(0).focus();
			}
		}, this.settings));
	},
	
	load: function()
	{
		this.lightbox.load();
	},
	
	onSave: function(resp)
	{
		var parts = resp.resp.split('|');
		if (parseInt(parts[0]) > 0) 
		{
			this.addField(parts[0], parts[1]);
		}
	},
	
	addField: function(id, value)
	{
		switch (this.settings.type)
		{
			case 'option':
				this.addOption(id, value);
				break;
			case 'checkbox':
			case 'radio':
				this.addInput(id, value, this.settings.type);
				break;
			default:
				alert('Undefined type "' + this.settings.type + '"');
				break;
		}
	},
	
	addOption: function(id, value)
	{
		var new_html = '<option value="' + id + '">' + value + '</option>';
		this.$field.append(new_html);
		this.$field.find('option:last').attr('selected', true);
	},
	
	addInput: function(id, value, type)
	{
		var nb = this.$field.children('input').size();
		if (nb > 5) 
			this.$field.addClass('multiple_checkbox');
		var new_html = '';
		if (nb == 3) 
			this.$field.children('label').after('<br />');
		else if (nb > 3) 
			new_html += '<br />';
		else 
			new_html += ' ';
		new_html += '<input type="' + type + '" \
		id="' +
		this.$field.attr('id') +
		'_' +
		value +
		'" \
		name="' +
		this.$field.attr('id') +
		'[]" \
		value="' +
		id +
		'" /> \
		<label for="' +
		this.$field.attr('id') +
		'_' +
		id +
		'">' +
		value +
		'</label>';
		this.$field.append(new_html);
		this.$field.scrollTo('input:last', 500);
		this.$field.find('input:last').attr('checked', true);
	}
};

(function($)
{
	var o = $.scrollTo = function(a, b, c)
	{
		o.window().scrollTo(a, b, c)
	};
	o.defaults = {
		axis: 'y',
		duration: 1
	};
	o.window = function()
	{
		return $($.browser.safari ? 'body' : 'html')
	};
	$.fn.scrollTo = function(l, m, n)
	{
		if (typeof m == 'object') 
		{
			n = m;
			m = 0
		}
		n = $.extend({}, o.defaults, n);
		m = m || n.speed || n.duration;
		n.queue = n.queue && n.axis.length > 1;
		if (n.queue) 
			m /= 2;
		n.offset = j(n.offset);
		n.over = j(n.over);
		return this.each(function()
		{
			var a = this, b = $(a), t = l, c, d = {}, w = b.is('html,body');
			switch (typeof t)
			{
				case 'number':
				case 'string':
					if (/^([+-]=)?\d+(px)?$/.test(t)) {
						t = j(t);
						break
					}
					t = $(t, this);
				case 'object':
					if (t.is || t.style) 
						c = (t = $(t)).offset()
			}
			$.each(n.axis.split(''), function(i, f)
			{
				var P = f == 'x' ? 'Left' : 'Top', p = P.toLowerCase(), k = 'scroll' + P, e = a[k], D = f == 'x' ? 'Width' : 'Height';
				if (c) 
				{
					d[k] = c[p] + (w ? 0 : e - b.offset()[p]);
					if (n.margin) 
					{
						d[k] -= parseInt(t.css('margin' + P)) || 0;
						d[k] -= parseInt(t.css('border' + P + 'Width')) || 0
					}
					d[k] += n.offset[p] || 0;
					if (n.over[p]) 
						d[k] += t[D.toLowerCase()]() * n.over[p]
				}
				else 
					d[k] = t[p];
				if (/^\d+$/.test(d[k])) 
					d[k] = d[k] <= 0 ? 0 : Math.min(d[k], h(D));
				if (!i && n.queue) 
				{
					if (e != d[k]) 
						g(n.onAfterFirst);
					delete d[k]
				}
			});
			g(n.onAfter);
			function g(a)
			{
				b.animate(d, m, n.easing, a &&
				function()
				{
					a.call(this, l)
				})
			};
			function h(D)
			{
				var b = w ? $.browser.opera ? document.body : document.documentElement : a;
				return b['scroll' + D] - b['client' + D]
			}
		})
	};
	function j(a)
	{
		return typeof a == 'object' ? a : {
			top: a,
			left: a
		}
	}
})(jQuery);

function deleteFile(obj, url, name, id)
{
	$(obj).unbind('click');
	var old_html = $(obj).html();
	$(obj).html('Suppression en cours ...');
	$.ajax({
		url: url,
		data: 'id=' + id,
		success: function (resp)
		{
			if (resp == '1')
			{
				$(obj).parent().parent().find('input[name=' + name + '_src]').val('');
				$(obj).parent().remove();
			}
			else
			{
				alert(resp);
				$(obj).html(old_html).click(function ()
				{
					deleteFile(obj, url, name, id);
				});
			}
		}
	});
}

$(document).ready(function ()
{
	if ($.datepicker) 
	{
		$.datepicker.setDefaults({
			showOn: 'both',
			altFormat: 'dd/mm/yy',
			dateFormat: 'dd/mm/yy',
			buttonImageOnly: true,
			buttonImage: R + 'lib/img/icons/calendar.png',
			buttonText: 'Calendrier',
			showOtherMonths: true,
			prevText: 'Mois précédent',
			nextText: 'Mois suivant',
			firstDay: 1
		});
		$('input.calendar').each(function()
		{
			var me = this;
			$(this).datepicker({
				monthNames: $(this).attr('month_names').split('|'),
				monthNamesShort: $(this).attr('month_names_short').split('|'),
				dayNamesMin: $(this).attr('day_names_min').split('|'),
				dayNamesShort: $(this).attr('day_names_short').split('|'),
				dayNames: $(this).attr('day_names').split('|'),
				beforeShow: function(input)
				{
					var inst = this;
					this.hours = $(input).val().substring(10);
					$(input).bind('keyup', function()
					{
						inst.hours = $(this).val().substring(10);
					});
				},
				onSelect: function(dateText, inst)
				{
					$(me).val(dateText.substring(0, 10) + this.hours);
					$(me).unbind('keypress');
				}
			});
		});
	}
});

