
var IS_NETSCAPE = (window.navigator.userAgent.search(/netscape/i) > -1);
var IS_IE = (window.navigator.userAgent.search(/MSIE/i) > -1);

function get_flash_object(movie, id, w, h)
{
	var buf =
		'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + w + '" height="' + h + '" id="' + id + '">' +
		'<param name="allowScriptAccess" value="always" />' +
		'<param name="wmode" value="transparent">' +
		'<param name="salign" value="tl">' +
		'<param name="scale" value="noborder">' +
		'<param name="movie" value="' + movie + '" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="' + movie + '" quality="high" bgcolor="#ffffff" width="' + w + '" height="' + h + '" name="' + id + '" salign="tl" scale="noborder" allowScriptAccess="always" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
		'</object>';

	return buf;
}

function flash_home(version)
{
	var v = (version != null && version >= 8 ? '-f8' : '');
	var div = document.getElementById('body_subflash');
	div.innerHTML = get_flash_object("grafics-loader" + v + ".swf", "home-movie", 725, 275);
}

function flash_bookmarks()
{
	var div = document.getElementById('body_subflash');
	div.innerHTML = get_flash_object("bookmarks-loader.swf", "bookmarks", 725, 650);
}

function flash_publications()
{
	var div = document.getElementById('body_subflash');
	div.innerHTML = get_flash_object("publications-loader.swf", "publications", 725, 750);
}

function show_billboard(show)
{
	var home = (document.URL.search(/default\.asp$/) > -1) || (document.URL.search(/(fr|en)\/$/) > -1);

	var box = getObj('photo_box');
	if (home)
	{
		box.display = 'block';
	}
	else
	{
		var billboard_show = getObj('billboard_show');
		var billboard_hide = getObj('billboard_hide');
		if (show)
		{
			box.display = 'block';
			billboard_show.display = 'none';
			billboard_hide.display = 'block';
		}
		else
		{
			box.display = 'none';
			billboard_show.display = 'block';
			billboard_hide.display = 'none';
		}
	}
}

function getObj(name)
{
	if (document.getElementById)
	{
		return document.getElementById(name).style;
	}
	else if (document.all)
	{
		return document.all[name].style;
	}
	else if (document.layers)
	{
		return document.layers[name];
	}
	return null;
}



function ImageOverData(width, height, img_normal, img_over)
{
	this.img_normal = new Image(width, height);
	this.img_normal.src = img_normal;
	this.img_over = new Image(width, height);
	this.img_over.src = img_over;
}

function ImagesOver()
{
	this.imgs = {};
}

ImagesOver.prototype =
{

	add: function(id, width, height, img_normal, img_over)
	{
		var self = this;
		this.imgs[id] = new ImageOverData(width, height, img_normal, img_over);
		var href_el = $(id);
		appendChildNodes(href_el, IMG({src: img_normal, name: id, border: 0}));
		connect(href_el, 'onmouseover',
			function()
			{
				self.change_image(id, true);
			}
		);
		connect(href_el, 'onmouseout',
			function()
			{
				self.change_image(id, false);
			}
		);
	},

	change_image: function(id, over)
	{
		var img;
		if (over)
		{
			img = this.imgs[id].img_over.src;
		}
		else
		{
			img = this.imgs[id].img_normal.src;
		}
		document.images[id].src = img;
	}

};



function get_status(xml)
{
	return get_xml_node_value(xml, 'status');
}


function get_xml_node_value(xml, name, idx)
{
	if (idx == null) idx = 0;

	var buf = '';

	try
	{
		var nodes = xml.getElementsByTagName(name);
		if (nodes.length > 0 && idx < nodes.length && nodes[idx].childNodes.length > 0)
		{
			buf = nodes[idx].firstChild.nodeValue;
		}
	}
	catch (e) {}

	return buf;
}

function get_inner_dim()
{
	if (window.innerHeight)
	{
		return {h: window.innerHeight, w: window.innerWidth};
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		return {h: document.documentElement.clientHeight, w: document.documentElement.clientWidth};
	}
	else if (document.body && document.body.clientHeight)
	{
		return {h: document.body.clientHeight, w: document.body.clientWidth};
	}
	else
	{
		return {h: 800, w: 600};	// guess someting conservative
	}
}

function get_scroll_pos()
{
	if (window.scrollY)
	{
		 return {x: window.scrollX, y: window.scrollY};
	}
	else
	{
		return {x: document.body.scrollLeft, y: document.body.scrollTop};
	}
}

function show_note(ref_id, note_id)
{
	var note_div = $(note_id);
	note_div.style.display = "";
	var note_div_dim = elementDimensions(note_div);
	var scroll_y = get_scroll_pos().y;
	var ref_pos = elementPosition(ref_id);
	var ref_dim = elementDimensions(ref_id);
	var win_dim = getViewportDimensions();

	var topleft = {x: 0, y: 0};

	topleft.y = ref_pos.y - note_div_dim.h - 5; // try first to position above
	if (topleft.y < scroll_y)
	{
		topleft.y = (ref_pos.y + ref_dim.h + 5);
	}
	topleft.x = ref_pos.x + ref_dim.w + 20;
	if ((topleft.x + note_div_dim.w) > win_dim.w)
	{
		topleft.x = ref_pos.x - note_div_dim.w - 20;
	}

	note_div.style.top = topleft.y + "px";
	note_div.style.left = topleft.x + "px";

}

function close_note(note_id)
{
	$(note_id).style.display = "none";
}

function numbers_notes()
{
	var refs = getElementsByTagAndClassName(null, "note_ref");
	for (var i = 0; i < refs.length; i++)
	{
		refs[i].innerHTML = '[' + (i + 1) + ']';
	}
}

function field_nodes_to_obj(data_obj, fields)
{
	for (var i = 0; i < fields.length; i++)
	{
		data_obj[fields[i].getAttribute('name')] = (fields[i].firstChild && fields[i].firstChild.nodeValue ? fields[i].firstChild.nodeValue : '');
	}
}

function open_win_center(h, w, url, win_name)
{
	if (win_name == null) win_name = "new";
	var y = (window.screen.availHeight - h) / 2;
	var x = (window.screen.availWidth - w) / 2;
	var params = "scrollbars=yes,resizable=no,menubar=yes,width=" + w + ",height=" + h + ",top=" + y + ",left=" + x;

	 var win = window.open(url, win_name, params);
	 win.focus();
}

function top_button(e)
{
	var button_left = $('page_top_button_left');
	var button_right = $('page_top_button_right');
	var scroll_y = get_scroll_pos().y;

	if (scroll_y > 0)
	{
		var ml_pos = elementPosition('main_layout');
		var ml_dim = elementDimensions('main_layout');
		var inner_dim = getViewportDimensions();

		var top = scroll_y + inner_dim.h - 40;
		button_left.style.left = (ml_pos.x - 10 - 25) + "px"; // 25 is the width of the arrow image
		button_left.style.top = top + "px";
		button_right.style.left = (ml_pos.x + ml_dim.w + 10) + "px";
		button_right.style.top = top + "px";

		button_left.style.display = "";
		button_right.style.display = "";
	}
	else
	{
		button_left.style.display = "none";
		button_right.style.display = "none";
	}
}

function goto_page_top()
{
	window.scrollTo(0, 0);
}

function LOG(msg)
{
	if (!IS_NETSCAPE) log(msg);
}

function Debug(active)
{
	if (active == null || active == undefined || active)
	{
		try { $('debug').style.display = ""; } catch (e) { LOG('Debug() error'); }
	}
}

Debug.prototype = {

	add: function(msg)
	{
		try
		{
			$('debug').innerHTML += msg + '<br>';
		}
		catch (e)
		{
			LOG('Debug() error');
		}
	}

};

