var timeouts = [];
function popup(url, name, height, width)
{
	if (!height)
		height = 635;
	if (!width)
		width = 635;
	if (!name)
		name = '_blank';
	var __popup = window.open(url, name, 'width=' + width + ',height=' + height + ',scrollbars=yes', true);
	__popup.focus();
}
function display_trace()
{
	var trace = document.getElementById('_trace');
	if (trace.style.display == 'none')
		trace.style.display = '';
	else
		trace.style.display = 'none';
}
function print_r(obj) 
{
	win_print_r = window.open('about:blank', 'win_print_r');
	win_print_r.document.write('<html><body>');
	r_print_r(obj, win_print_r);
	win_print_r.document.write('</body></html>');
}

function r_print_r(theObj, win_print_r) 
{
	if(theObj.constructor == Array || theObj.constructor == Object)
	{
		if (win_print_r == null)
			win_print_r = window.open('about:blank', 'win_print_r');
	}
	for(var p in theObj)
	{
		if(theObj[p].constructor == Array || theObj[p].constructor == Object)
		{
			win_print_r.document.write("<li>["+p+"] =>"+typeof(theObj)+"</li>");
			win_print_r.document.write("<ul>")
			r_print_r(theObj[p], win_print_r);
			win_print_r.document.write("</ul>")
		} 
		else 
		{
			win_print_r.document.write("<li>["+p+"] =>"+theObj[p]+"</li>");
		}
	}
	win_print_r.document.write("</ul>");
}

function urlencode(str)
{
	while (str.indexOf(' ') > -1)
		str = str.replace(' ', '+');
	str = escape(str);
	return str;
}

function onEnter(evt, id) 
{
	var keyCode = null;	
	if(evt.which) 
	{
		keyCode = evt.which;
	} 
	else if(evt.keyCode) 
	{
		keyCode = evt.keyCode;
	}
	if(13 == keyCode) 
	{
		document.getElementById(id).click();
		return false;
	}
	return true;
}

// jQuery
function scrollLeft(id)
{
	var scroller = document.getElementById(id);
	if (!scroller)
		return;
	var toScroll = scroller.scrollWidth - 280;
	var time = Math.round(toScroll/40) + 3;
	if (toScroll <= 0)
		return;
	$('#' + id).scrollTo(toScroll, time*1000, {axis:'x', onAfter:function(){setTimeout("scrollRight('" + id + "');", 500);}});
}

function scrollRight(id)
{
	var scroller = document.getElementById(id);
	if (!scroller)
		return;
	var toScroll = scroller.scrollWidth - 280;
	var time = Math.round(toScroll/40) + 3;
	if (toScroll <= 0)
		return;
	$('#' + id).scrollTo(0, time*1000, {axis:'x', onAfter:function(){setTimeout("scrollLeft('" + id + "');", 500);}});
}

function refreshContent(id, url, time)
{
	$('#' + id).load(url);
	timeouts[timeouts.length] = setTimeout("refreshContent('" + id + "', '" + url + "', " + time + ");", time);
}
function clearTimeouts()
{
	var len = timeouts.length;
	for (var i = 0; i < len; i++)
		clearTimeout(timeouts[i]);
	timeouts = [];
}
function multiCheckboxIds(obj)
{
	var ids = [];
	obj.each(
		function () 
		{
			if (this.checked)
				ids.push(this.value);
		}
	);
	return ids.join(',');
}
function setUpImage()
{
	if (content_image && !image_enabled.in_array(content_image))
	{
		image_enabled.unshift(content_image);
	}
	if (image_enabled.length == 0)
	{
		$("#image_display").html("");
		$("#image_display").hide();
		$("#image_hidden").val("");
		return;
	}
	$("#image_display").show();
	if (image_enabled.in_array($("#image_hidden").val()))
	{
		for (var i = 0; i < image_enabled.length; i++)
		{
			if (image_enabled[i] == $("#image_hidden").val())
			{
				break;
			}
		}
	}
	switchImage(i);
	if (image_enabled.length > 1)
	{
		$("#image_selector").html('<a href="#" onclick="switchImage(\'-\'); return false;">Prec.</a> <a href="#" onclick="switchImage(\'+\'); return false;">Suiv.</a>');
	}
	else
	{
		$("#image_selector").html("");
	}
	$("#image_selector").append(' <a href="#" onclick="$(\'#image_input\').show(); return false;">[ Charger photo ]</a>');
}

var imageVersion = new Date().getTime();
function switchImage(sDir)
{
	numImage = image_enabled.length;
	if (sDir == "+")
		newImage = currentImage + 1;
	else if (sDir == "-")
		newImage = currentImage - 1;
	else
		newImage = sDir;
	if (!newImage) newImage = 0;
	if (newImage < 0) newImage = numImage - 1;
	if (newImage > numImage - 1) newImage = 0;
	
	$("#image_display").css("background", "#000 url(" + image_enabled[newImage] + "?" + imageVersion + ") no-repeat center center");
	$("#image_hidden").val(image_enabled[newImage]);
	currentImage = newImage;
}

var mediaHtml = {};

function addMedia(id, html, url)
{
	for (var idx in mediaHtml)
	{
		if (idx == id)
			return;
	}
	mediaHtml[id] = html;
	initMedia();
	$('#text_display').append('<div style="padding:10px; background:#eee; border:1px solid #ccc; margin-bottom:2px;" id="text_display_' + id + '"><a style="float:right;" href="#" onclick="deleteMedia(' + id + '); return false;">Delete</a><div class="clear"></div></div>');
	$.post(url, {}, function (data) {$('#text_display_' + id).prepend(data);} );
}

function deleteMedia(id)
{
	delete(mediaHtml[id]);
	initMedia();
	$('#text_display_' + id).remove();
}

function initMedia()
{
	var html = '';
	for (var id in mediaHtml)
	{
		html += mediaHtml[id];
	}
	$('#content_text').val(html);
}

function initTag(element)
{
	var tags = [];
	tags = $("#" + element).val().toString().split(',');
	for (var i = 0; i < tags.length; i++)
	{
		addTag(tags[i], element);
	}
}

function addTag(tag, element)
{
	var t = tag;
	t = t.toId();
	if (t == "")
		return;
	if (eval(element + ".in_array(t)"))
	{
		$("#" + element + "_selector").val("");
		return;
	}
	$("#" + element + "_display").append('<div class="tag" style="display:none;" id="' + t + '">' + t + '</div>');
	$("#" + t).fadeIn("slow");	
	$("#" + t).click(function(){
		if (confirm("Supprimer le tag '" + t + "'?"))
		{
			eval(element + ".remove(t)");
			$("#" + t).remove();
			$("#" + element).val(eval(element + ".join(',')"));
		}
	});
	if ($("#" + element + "_selector").val() != $("#" + element + "_selector").attr('title'))
		$("#" + element + "_selector").val("");
	eval(element + ".push(t)");
	$("#" + element + "").val(eval(element + ".join(',')"));
}

// Prototype
String.prototype.trim = function(c)
{
	var s = this.toString();
	if (!c)
		c = "\s";
	return eval("s.replace(/^" + c + "+/g, '').replace(/" + c + "+$/g, '')");
}
String.prototype.toId = function()
{
	var s = this.toString();
	s = s.replace(/[àäâ]/gi, 'a');
	s = s.replace(/[éèëê]/gi, 'e');
	s = s.replace(/[ìïî]/gi, 'i');
	s = s.replace(/[òöô]/gi, 'o');
	s = s.replace(/[ùüû]/gi, 'u');
	s = s.replace(/[ÿ]/gi, 'y');
	s = s.replace(/[ç]/gi, 'c');
	s = s.replace(/[&]/gi, 'n');
	s = s.replace(/[^a-z0-9]/gi, '-');
	while (s.indexOf('--') > -1)
	{
		s = s.replace(/--/gi, '-');
	}
	s = s.trim('-');
	return s.toLowerCase();
}
Array.prototype.in_array = function(p_val)
{
	for(var i = 0, l = this.length; i < l; i++)
	{
		if(this[i] == p_val)
			return true;
	}
	return false;
}
Array.prototype.remove = function(s)
{
	var index = -1;
	for (var i = 0, l = this.length; i < l; i++)
	{
		if (this[i] == s)
			index = i;
	}
	if (index != -1)
		this.splice(index, 1);
}