$(document).ready(function()
{
	webutils_handleIncludes();
	webutils_handleRepeaters();
	webutils_handlePopup();
});

/**
 * AJAX Templating - Include function
 * Loads the content of the "rel" attribute into the div using ajax/jquery 
 * ex: 
 * <div class="include" rel="header.html"></div>
 */
function webutils_handleIncludes()
{
	$(".include").each(function(){
		$(this).load($(this).attr("title"));
	});
}

/**
 * Repeaters
 * Loads the content of an xml and repeats the template and parses the values 
 * ex: 
 * <div class="repeater" title="info.xml">
 * 	<h3>@name@</h3>
 * 	<p>@description@</p>
 * </div>
 */
function webutils_handleRepeaters()
{
	$(".repeater").hide();
	
	$(".repeater").each(function(){

		$.ajax({
			url: $(this).attr("title"),
			success: webutils_repeaterParseXml,
			repeater: $(this),
			filter:$(this).attr("rel")
		});

	});
}

function webutils_handlePopup()
{
	var popupHTML = '<div id="popupOverlay"></div><div id="popup"><div id="popupContent"></div><div id="popupHeader"><div id="popupTitle"></div><button id="popupClose" onclick="hidePopup()">X</button></div></div>';
	$(popupHTML).appendTo("body")
}

function webutils_repeaterParseXml(xml)
{
	var content = "";
	var item = "";
	var template = this.repeater.html();
	var reg;
	
	$(xml).find(this.filter).each(function(){
		
		item = template;
		
		$(this).children().each(function(){
			
			reg = new RegExp("@" + this.tagName.toLowerCase() + "@", "g");
			item = item.replace(reg, $(this).text());
			
		});
		
		item = item.replace(/@[^@]*@/g, "");
		
		content += item;
	})
	
	this.repeater.html(content);
	this.repeater.show();
}

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

//show popup with an iframe
function showPopupFrame(url, title, width, height)
{
	showPopup("<iframe src='" + url + "' frameborder='0' style='width:" + (width-20) + "px;height:" + (height-20) + "px'></iframe>", title, width, height, false);
}

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

// show popup with html content
function showPopup(html, title, width, height, isScrollbars)
{
	$("#popupOverlay").css("z-index", "998");
	$("#popup").css("z-index", "999");
	
	$("#popupOverlay").fadeTo(0, 0);
	$("#popupOverlay").show();
	
	$("#popupOverlay").fadeTo(500, 0.5, function()
	{
		$("#popup").animate({width:100, height:100, marginLeft:-50, marginTop:-50}, 0);
		//$("#popup").fadeTo(300, 1);
		
		$('#popupContent').html("");
		$("#popup").animate({width:width, height:height, marginLeft:-width/2, marginTop:-height/2}, 500, 'swing', function()
		{
			$('#popupTitle').html(title);
			$("#popupContent").html("<div style='padding:10px;overflow:" + (isScrollbars ? "auto" : "hidden") + "'>" + html + "</div>");
			$("#popupOverlay").bind('click', hidePopup);
		});
	});
}

// Hides the popup
function hidePopup()
{
	$("#popup").animate({width:16, height:16, marginLeft:-8, marginTop:-8}, 200, function()
	{
		$("#popup").hide();
		$("#popupOverlay").fadeTo(100, 0, function()
		{
			$("#popupOverlay").hide();
		});
	});
}

//Function qui remplace les espaces vide par un +
function ReplaceText(text)
{
	return text.replace(/\ /g, "+");
}
		
function AfficheText(text)
{
	return ReplaceText(text);
}

//Function qui remplace les + par un espace vide	
function ReplaceTextPlus(text)
{
	return text.replace(/\+/g, " ");
}
		
function AfficheTextPlus(text)
{
	return ReplaceTextPlus(text);
}
