/**
 * FIX IE6 IMAGE FLICKER PROBLEM
 *
 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


var _xml;
var _currLanguage = 'english';

/**
 * DOM READY
 *
 */
$(document).ready(Init);

function Init() {
	// Chart / Raw Data
	ChartRawDataInit();

	// Set Language
	_currLanguage = ($("#wrapper").attr("class") == "en") ? "english" : "french";

	// Equal Height Boxes
	EqualHeightBoxes("toolbox", "didyouknow");

	// Zebra tables
	Zebra();

	$.ajax({
		dataType: ($.browser.msie) ? "text" : "xml",
		//error: function(XMLHttpRequest, textStatus, errorThrown){
		//	alert(textStatus);
		//},
		success: ToolTips,
		type: "GET",
		url: "data.xml"
	});
}


// Switch view mode of the chart/raw data.
function ChartRawDataInit()
{
	if($("#charts-and-raw ul.toc").length < 1)
	{
		return false;
	}

	$("#charts-and-raw ul.toc li a").click(function(e){
		// Update navigation
		$("#charts-and-raw ul.toc li a").each(function(i){
			$(this).removeClass("lit");
		});

		$(this).addClass("lit");

		// Update content
		$("#charts-and-raw div.box").hide();
		$("#" + $(this).attr("rel")).show();

		if($(this).attr("rel") == "raw")
		{
			$("#charts-and-raw p.center").hide();
		}
		else
		{
			$("#charts-and-raw p.center").show();
		}

		return false;
	});
}


function HelpBoxInit()
{
	if($("span.togglehelp").length < 1)
	{
		return false;
	}

	// Ignore this event if user has clicked on a drop-down menu.
	$("span.togglehelp select").click(function(e){
		return false;
	});

	// Toggle visibilty of help box.
	if(_xml)
	{
		$(".togglehelp").click(function(e){

			if($(_xml).find("tooltip[@id='" + $(this).attr("id") + "']").length > 0)
			{
				// Build help box on first click
				if($(this).next("div.help").length == 0)
				{
					var html = '';

					html += '<div class="help help-' + $(this).attr("id") + '">';
					html += $(_xml).find("tooltip[@id='" + $(this).attr("id") + "'] " + _currLanguage).text();
					html += '<a class="hdr-graphic" href="#" title="Close/Fermer">Close/Fermer</a>';
					html += '</div>';

					$(html).insertAfter($(this)).show("fast");

					// Setup "close" button click event.
					$("a.hdr-graphic", $(this).next("div.help")).click(function(e){
						$(this).parent("div.help").hide("fast");
						return false;
					});
				}
				else if($(this).next("div.help:visible").length > 0)
				{
					// Remove "close" button event.
					$(this).next("div.help").hide("fast", function() {
						$(this).unbind("click", "a.hdr-graphic");
					});
				}
				else
				{
					$(this).next("div.help").show("fast");

					// Setup "close" button click event.
					$("a.hdr-graphic", $(this).next("div.help")).click(function(e){
						$(this).parent("div.help").hide("fast");
						return false;
					});
				}
			}

			return false;
		});
	}
}


function EqualHeightBoxes()
{
	var maxHeight = 0;

	for(var x = 0; x < arguments.length; x++)
	{
		if($("#" + arguments[x]).height() > maxHeight)
		{
			maxHeight = $("#" + arguments[x]).height();
		}
	}

	for(var x = 0; x < arguments.length; x++)
	{
		$("#" + arguments[x]).height(maxHeight);
	}
}


function Zebra()
{
	$("table.data").each(function(i){
		$("tr", $(this)).each(function(j){
			if(j % 2 == 0)
			{
				$(this).addClass("shade");
			}
		});
	});
}


function ToolTips(data, textStatus)
{
	if(typeof data == "string")
	{
       	_xml = new ActiveXObject("Microsoft.XMLDOM");
       	_xml.async = false;
		_xml.loadXML(data);
    }
	else
	{
		_xml = data;
	}

	HelpBoxInit();
}

function STF(loc,lang){
var dest;
if (lang=='fr'){
    dest = 'email_fr.aspx?' + loc;
}else{
    dest = 'email.aspx?' + loc;
}
var x = window.open(dest, 'email', 'height=700,width=740,resizable,scrollbars=yes', false);
if (window.focus) {x.focus()}

}
