var timeouts = [];
function popup(url, name, height)
{
	if (!height)
		height = 635;
	if (!name)
		name = '_blank';
	var __popup = window.open(url, name, 'width=635,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);
}

// Prototype
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 = this.indexOf(s);
	if (index != -1)
		this.splice(index, 1);
}