function userfontsize(){
	if (cookie.read("fontsize")){
		changeGrosseurText(
			document.getElementById(
				cookie.read("fontsize").substr( 
					cookie.read("fontsize").lastIndexOf("[")+1,
					cookie.read("fontsize").lastIndexOf("]")-1
				)
			), cookie.read("fontsize").substr(
					cookie.read("fontsize").lastIndexOf("]")+1,
					cookie.read("fontsize").length
				)
		)
	}
}

function clone(node){
	//console.dir(node);
	var clone = document.createElement(node.tagName.toLowerCase());
	for(var i = 0, l = node.attributes.length; i < l; i++){
		clone.setAttribute(node.attributes[i].nodeName, node.attributes[i].value);
	}
	return clone;
}

function cloneAttributes(to, from){
	for(var i = 0, l = from.attributes.length; i < l; i++){
		to.setAttribute(from.attributes[i].nodeName, from.attributes[i].value);
	}
	return to;
}

Array.prototype.indexOf = function(obj){
	for(var i = 0, l = this.length; i < l; i++){
		if(this[i] == obj) return i;
	}
	return -1;
}
String.prototype.clean = function(){
	return this.replace(/\s+/i, "");
}
String.prototype.contains = function(str){
	return (this.indexOf(str) != -1);
}

function addImageToLink(anchor, image){
	var img = document.createElement("img");
	img = cloneAttributes(img, image);
	anchor.setAttribute("target", "_blank");
	var doc = document;
	if(document.createDocumentFragment) doc = document.createDocumentFragment();
	var space = document.createTextNode(" ");
	if(doc != document){
		doc.appendChild(space);
		doc.appendChild(img);
		anchor.appendChild(doc);
	} else {
		anchor.appendChild(img);
	}
	//anchor.title =  text + ". En cliquant sur le lien, une nouvelle fen" + "\u00ea" + "tre sera ouverte.";
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	var image = new Image();
	image.setAttribute("src", "/data/luciebruneau/files/image/external.png");
	image.setAttribute("alt", "Ce lien s'ouvrira dans une nouvelle fen" + "\u00ea" + "tre.");
	image.onload = function(){http://luciebruneau.genclik.ca/fr/main_nav/enseignement/
		image.setAttribute("width", image.width);
		image.setAttribute("height", image.height);
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			var host = anchor.host+'';
			if( anchor.getAttribute("href")
				&& (anchor.protocol+'').contains("http")
				&& !host.contains("luciebruneau")
				&& !(host.contains("generationclik") || host.contains("generationmultimedia"))
				){
				addImageToLink(anchor, image);
			}
			if (anchor.getAttribute("href") && anchor.getAttribute("id") == "download"){
				if (!anchor.className)
					anchor.className = "download";
			}		
		}
	}
}
	
function addHandlers()
{
	userfontsize()
	modifyLink();
	var x=document.getElementsByTagName("a");
	var theclass="vide";

	for (var i=0; i<x.length; i++) {
		//external links --------------------------------------------------------------------------->	
		theclass=x[i].className;
		  if(theclass=="externe"){
			//if(x[i].lang=="en"){
				//x[i].innerHTML=x[i].innerHTML+"<img src='/data/luciebruneau/files/image/external.png' alt='The link will open a new window.' />"
			//}else{
				x[i].innerHTML=x[i].innerHTML+"<span lang='fr'><img src='/data/luciebruneau/files/image/external.png' alt='Ce lien s" + "\&#39;" + "ouvrira dans une nouvelle fen" + "\u00ea" + "tre.' /></span>";
			//}
			
			x[i].onclick = function(event){return launchWindow(this, event);}
    		x[i].onkeypress = function(event){return launchWindow(this, event);}
		}
		//telecharger links --------------------------------------------------------------------------->
		if(theclass=="telecharger"){
			/*if(x[i].lang=="en"){
				x[i].innerHTML=x[i].innerHTML+"<img src='/data/luciebruneau/files/image/download.png' alt='' />"
			}else{*/
				x[i].innerHTML=x[i].innerHTML+"<span lang='fr'><img src='/data/luciebruneau/files/image/download.png' alt='' /></span>";
			//}
		}
	}

}

function launchWindow(objAnchor, objEvent)
{
  var iKeyCode, bSuccess=false;

  // If the event is from a keyboard, we only want to open the
  // new window if the user requested the link (return or space)
  if (objEvent && objEvent.type == 'keypress')
  {
    if (objEvent.keyCode)
      iKeyCode = objEvent.keyCode;
    else if (objEvent.which)
      iKeyCode = objEvent.which;

    // If not carriage return or space, return true so that the user agent
    // continues to process the action
    if (iKeyCode != 13 && iKeyCode != 32)
      return true;
  }
   
  bSuccess = window.open(objAnchor.href);

  // If the window did not open, allow the browser to continue the default
  // action of opening in the same window
  if (!bSuccess)
    return true;

  // The window was opened, so stop the browser processing further
  return false;
}


function aOver(a){
	a.getElementsByTagName("img")[0].src = a.getElementsByTagName("img")[0].src.replace(/-a.gif/, "-a-on.gif");
}
function aOff(a){
	if (a.className != "selected")
		a.getElementsByTagName("img")[0].src = a.getElementsByTagName("img")[0].src.replace(/-a-on.gif/, "-a.gif");
}

function loadCSS(css_path){
	if(document.createStyleSheet) {
		document.createStyleSheet(css_path);
	} else {
		var newCSS=document.createElement('link');
		newCSS.rel ='stylesheet';
		newCSS.type ='text/css';
		newCSS.href = css_path;
		document.getElementsByTagName("head")[0].appendChild(newCSS);
	}
}
function initTailleCourante(){
	var tailleCourante = document.getElementById('small-a');
	userfontsize()
}
function changeGrosseurText(caller, combien){
	var tailleCourante = document.getElementById('small-a');
	if (tailleCourante){
		document.body.style.fontSize = (combien/16) + "em";
		tailleCourante.getElementsByTagName("img")[0].src = tailleCourante.getElementsByTagName("img")[0].src.replace(/-a-on.gif/, "-a.gif");
		tailleCourante.className = "";
		tailleCourante = caller;
		tailleCourante.className = "selected";
		tailleCourante.getElementsByTagName("img")[0].src = tailleCourante.getElementsByTagName("img")[0].src.replace(/-a.gif/, "-a-on.gif");
		cookie.erase("fontsize");
		cookie.create("fontsize", "["+caller.id+"]"+combien, 7);
	}
}
function cookie(){ }
cookie.prototype = {
	init: function(){
		return this;
	},
	create: function(name, value, days){
		var expires = "";
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		document.cookie = name+"="+value+expires+"; path=/";
	},
	read: function(name){
		var nameEQ = name + "=";
		var arrCookie = document.cookie.split(';');
		for(var i=0;i < arrCookie.length;i++) {
			var cookie = arrCookie[i];
			while (cookie.charAt(0)==' ')
				cookie = cookie.substring(1, cookie.length);
			if (cookie.indexOf(nameEQ) == 0)
				return cookie.substring(nameEQ.length, cookie.length);
		}
		return null;
	},
	erase: function(name){
		this.create(name, "", -1);
	}
}
	cookie = new cookie();

function modifyLink()
{
	var z=document.getElementById("int_menu");
	if(z!=null){
	
	z=z.getElementsByTagName("a");
	var theclass="vide";
	var total=0;
	var totalclass=0;
	
	for (var i=0; i<z.length; i++) {
		theclass=z[i].className;
		if(theclass=="selected"){
			total=total+1;
		}
	}

	if(total>=2){
		for (var k=0; k<z.length; k++) {
			theclass=z[k].className;
			if(theclass=="selected"){
				totalclass=totalclass+1;
				if(totalclass<total){
					z[k].className="empty";
				}
			}
		}
	}
}
}

window.onload = addHandlers;
