$(document).ready(function()
{
	webutils_handlePopup();
	webutils_handleExpandable();
	
	// Add an event listening for a click on the legal expand
	$("#expandLegal").click(function (){	
		webutils_handleLegals(this);
	});
});

// Handle legals
function webutils_handleLegals(clicked)
{
	var target = $(clicked).parent().next();
	
	if($(target).is(":hidden")){
		$(target).fadeIn(500);
		$("#expandLegal").html("( - )");
	}
	
	else{
		$(target).fadeOut(500);
		$("#expandLegal").html("( + )");
	}
	
}

function webutils_handlePopup()
{
	var popupHTML = '<div id="popupOverlay"></div><div id="popup"></div>';
	$(popupHTML).appendTo("body");
}

function webutils_handleExpandable()
{
	// links expands to <i> tags in <div> with expendable class
	$(".expandable a:first-child").each(function(){
		
		$(this).click(function(){
			
			if ($(this).parent().hasClass('expanded')) {
				$(this).parent().removeClass('expanded');
			} else {
				$(this).parent().addClass('expanded');
			}
		})
	});
	
	$(".expandAll").each(function(){
		$(this).click(function(){
			$(document).find(".expandable").addClass("expanded");
		});
	});
	
	$(".collapseAll").each(function(){
		$(this).click(function(){
			$(document).find(".expandable").removeClass("expanded");
		});
	});
}

/**
 * Expands all the child elements of the specified element with "expandable" class
 */
function webutils_expand(elementId)
{
	$("#"+elementId).find(".expandable").addClass("expanded");
}

/**
 * Collapses all the child elements of the specified element with "expandable" class
 */
function webutils_collapse(elementId)
{
	$("#"+elementId).find(".expandable").removeClass("expanded");
}

// show popup with content from external URL	
function showPopupAjax(url, title, width, height, cssClass)
{
	// load the html content in the popupContent DIV
	$.ajax({
		url: url,
		success: function(data) {
			showPopup(data, title, width, height, true, cssClass);
		}
	});
}

//show popup with an iframe
function showPopupFrame(url, title, width, height, cssClass)
{
	showPopup("<iframe src='" + url + "' frameborder='0' marginheight='0' marginwidth='0' style='margin-left:-28px;margin-top:-20px;width:" + (width-24) + "px;height:" + (height-70) + "px;background-color:#ffffff;'></iframe>", title, width, height, false, "");
}

// show popup with the content of a specified html element ID
function showPopupHtml(elementId, title, width, height, cssClass)
{
	showPopup($('#' + elementId).html(), title, width, height, true, cssClass);
}

function showPopupEmbed(elementId, title, width, height, cssClass)
{
	if ($("#" + elementId).parent().attr("id") != "popupContent") {
		$("#popupContent").html("");
		$("#popupContent").append($('#' + elementId));
	}
	
	showPopup("", title, width, height, true, cssClass);
}

// show popup with html content
function showPopup(html, title, width, height, isScrollbars, cssClass)
{
	$("#popupOverlay").css("z-index", "998");
	$("#popup").attr("class", "");
	
	if (cssClass != undefined) {
		$("#popup").attr("class", cssClass);
	}
	
	$("#popup").css("z-index", "999");
	$("#popup").css("width", width);
	$("#popup").css("height", height);
	$("#popup").css("marginLeft", -width/2);
	$("#popup").css("marginTop", -height/2);
	
	//$("#popup").animate({width:width, height:height, marginLeft:-width/2, marginTop:-height/2}, 0);
	
	$("#popupOverlay").fadeTo(0, 0);
	$("#popupOverlay").show();
	
	$("#popupOverlay").fadeTo(300, 0.5, function()
	{
		html = '<div class="popupClose"><a href="javascript:hidePopup()">&nbsp;</a></div>'
			+ '<div class="popupTitle" style="width:' + (width-70) + 'px">' + title + '</div>'
			+ '<div class="popupLeft" style="height:' + (height-50) + 'px">&nbsp;</div>'
			+ '<div id="popupContent" class="popupContent" style="width:' + (width-80) + 'px;height:' + (height-70) + 'px;">' + html + '</div>';
		$("#popup").html(html);
		
		if($.browser.msie)
		{
			$("#popup").show();
		}
		
		else{
			$("#popup").fadeIn(300);
		}
		
		$("#popupOverlay").bind('click', hidePopup);
	});
}

// Hides the popup
function hidePopup()
{
	$("#popupOverlay").unbind('click', hidePopup);
	
	$("#popup").hide();
	
	$("#popupOverlay").fadeTo(300, 0, function()
	{
		$("#popupOverlay").hide();
		// Vide le popup pout règler les problème de contenu flash qui continue à jouer en hidden
		$("#popup").html("");	
		
	});
}

function toXML(array, key)
{
	key = (key != undefined ? key : "xml" );
	
	var value = "<"+key+">";
	
	for (var index in array) {
		
		if (typeof(array[index]) == "string" || typeof(array[index]) == "number" || typeof(array[index]) == "boolean") {
			value += "<"+index+">" + array[index] + "</"+index+">";
		
		} else if (typeof(array[index]) == "array" || typeof(array[index]) == "object") {
			value += toXML(array[index], index);
		}
	}
	
	value += "</"+key+">";
	
	return value;
}
