/* Tools
---------------------- */
function getById(el) {
	return document.getElementById(el);
}

function getByTag(par,el) {
	var par = (par == '') ? document.body : par;
	if (!par) return new Array();
	return par.getElementsByTagName(el);
}

function addClass(obj,newClass) {
	if(!obj.className.match(new RegExp(newClass)))
		obj.className+=(obj.className.length>0? " ": "") + newClass;
}
	
function removeClass(obj,oldClass) {
	obj.className=obj.className.replace(new RegExp("( ?|^)"+oldClass+"\\b"), "");
}

function getByClass(cla,par,el) {
	var par = (par == '') ? document.body : par;
	var el = (el == '') ? '*' : el;
	if(/.*native code.*/.test(document.getElementsByClassName)) {
		return par.getElementsByClassName(cla);
	}
	else {
		var tagColl = par.getElementsByTagName(el);
		trimedColl = new Array;
		for (var i = 0; tagColl[i]; i++) {
			if(tagColl[i].className.match(new RegExp(cla))) {
				trimedColl[trimedColl.length]=tagColl[i];
			}
		}
		return trimedColl;
	}
}

function getStyle(obj,cssRule) {
	var cssVal = "";
	if (document.defaultView && document.defaultView.getComputedStyle) {
		cssVal = document.defaultView.getComputedStyle(obj, "").getPropertyValue(cssRule);
	}
	else if (obj.currentStyle) {
		cssVal = cssRule.replace(/\-(\w)/g, function (match, p1) {
			return p1.toUpperCase();
		});
		cssVal = obj.currentStyle[cssVal];
	}
	return cssVal;
}

function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function getPageY() {
	var docHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	// Order maters here as for IE 6 documentElement.clientHeight != body.clientHeight
	var docScrollY = document.documentElement.scrollTop || document.body.scrollTop;
	return [docHeight,docScrollY];
}

var addEvent = function() {
  if (window.addEventListener) {
    return function(el, type, fn) {
      el.addEventListener(type, fn, false);
    };
  } else if (window.attachEvent) {
    return function(el, type, fn) {
      var f = function() {
        fn.call(el, window.event);
      };
      el.attachEvent('on' + type, f);
    };
  }
}();

function GET_XMLHTTPRequest() {
	var request;
	try{
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(ex1){
		try{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex2){
			try{
				request = new ActiveXObject("Msxml3.XMLHTTP");
			}
			catch(ex3){
				request = null;
			}
		}
	}
	if(!request && typeof XMLHttpRequest != "undefined"){
		request = new XMLHttpRequest();
	}
	return request;
}

function setLinks(linkTags){
	var linkCollection = [];
	for (var j = 0; linkTags[j]; j++) {
		linkCollection[j] = getByTag('',linkTags[j]);
		for (var i = 0; linkCollection[j][i]; i++) {
			if(/bookmark|external|corporate|sponsor|download/.test(linkCollection[j][i].getAttribute('rel'))) {
				linkCollection[j][i].onclick = function(e) {
					if (!e) var e = window.event;
					if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey)
						return true;
					window.open(this.href);
					return false;
				}
			}
			else if(/zoom/.test(linkCollection[j][i].getAttribute('rel'))) {
				new Zoom(linkCollection[j][i]);
			}
			else if(/print/.test(linkCollection[j][i].getAttribute('rel'))) {
				new PrintOW(linkCollection[j][i]);
			}
		}
	}
}

/* Print
---------------------- */
function PrintOW(a) {
	this.a = a;
	this.a.onclick = function (that) {
        return function () {
            that.click(this.href);
			return false;
        }
    }(this);
}

PrintOW.prototype = {
	click: function (file) {
		window.open(file);
	}
}

/* Overlay
---------------------- */
function Zoom(a) {
	initOverlay();
	this.a = a;
	this.overlay = getById('overlay');
	this.container = getById('overlayContainer');
	this.container.w = 400;
	this.container.h = 250;
	this.a.onclick = function (that) {
        return function () {
            that.click(this.href);
			return false;
        }
    }(this);
}

Zoom.prototype = {
	click: function (file) {
		this.file = file + '&js=true';
		this.overlay.innerHTML = '<span id="loading"></span>';
		getById('loading').style.top = ((getPageY()[0] - 36) / 2) + getPageY()[1] + 'px';
	//	this.changePosition();
		removeClass(this.overlay,'hidden');
		removeClass(this.container,'hidden');
		// this.changePosition();
		var req = GET_XMLHTTPRequest();
		if (req) {
			req.open("GET", this.file, true);
			req.setRequestHeader('User-Agent','XMLHTTP/1.0');
			req.onreadystatechange = function (that) {
		        return function (aEvt) {
					if (req.readyState != 4) return;
					if (req.status != 200 && req.status != 304) {
						that.handleError(req);
						return;
					}
		            if(req.readyState == 4){
						that.handleRequest(req);
					}
		        }
		    }(this);
			req.send(null);
		}
		else {
			getById('overlayContainer').innerHTML = 'Unable to load content';
			this.createNav();
			this.changePosition();
		}
	},
	changePosition: function () {
		this.container.style.width = this.container.w + 'px';
		this.container.style.height = this.container.h + 'px';
		this.container.style.marginLeft = -(this.container.w)/2 + 'px';
		var t = ((getPageY()[0] - this.container.h) / 2) + getPageY()[1];
		if(t < 0 ) {t = 0;}
		this.container.style.top = t + 'px';
		this.overlay.innerHTML = '';
	},
	handleRequest: function(req) {
		this.container.innerHTML = req.responseText;
		var innerContent = getById('overlayInnerContent');
		if(innerContent) {
			this.createNav();
			this.container.w = innerContent.offsetWidth;
			this.container.h = innerContent.offsetHeight;
			this.changePosition();
		}
	},
	handleError: function(req) {
		this.container.innerHTML = '<strong>Data error :</strong> HTTP error' + req.status + '';
		this.container.w = 400;
		this.container.h = 250;
		this.createNav();
		this.changePosition();
	},
	createNav: function () {
		if(!getById('overlayNav')){
			var div = document.createElement('div');
			div.id = 'overlayNav';
			this.container.appendChild(div);
		}
		this.nav = getById('overlayNav');
		this.nav.items = getByTag(this.nav,'a');
		for(var i = 0; this.nav.items[i]; i++) {
			new Zoom(this.nav.items[i]);
		}
		var c = document.createElement('a');
		c.href= "#";
		c.className = "closer";
		c.onclick = function (that) {
	        return function () {
				that.close();
				return false;
			}
		}(this);
		this.closer = this.nav.appendChild(c);
	},
	close: function () {
		addClass(this.container,'hidden');
		addClass(this.overlay,'hidden');
		this.container.innerHTML = '';
		return false;
	}
}

function initOverlay () {
	if(!getById('overlay')) {
		var ov = document.createElement('div');
		ov.id = "overlay";
		ov.className = 'hidden';
		ov.style.height = getById('page').offsetHeight + 'px';
		document.getElementsByTagName('body')[0].appendChild(ov); 
	}
	if(!getById('overlayContainer')) {
		var ovCt = document.createElement('div');
		ovCt.id = "overlayContainer";
		ovCt.className = 'hidden';
		document.getElementsByTagName('body')[0].appendChild(ovCt); 
	}
}

/* Prints
---------------------- */
function setPrints() {
	var ps = getByClass('printable',getById("page"),'div');
	if(!ps || ps.length == 0) return;
	var a = document.createElement('a');
	a.href='#';
	a.className= 'printer';
	a.innerHTML = 'Imprimer cette page';
	a.onclick = function() {
		if(window.print) window.print();
		return false;
	}
	ps[0].appendChild(a);
}

/* Init
-------------------- */
var init = function() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if(!document.getElementsByTagName)
		return;
	addClass(getById('page'),'scripted');
	setLinks(['a','area']);
	setPrints();
	/*@cc_on @*/
	/*@if (@_win32)
	var ua = navigator.userAgent;
	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null) {
		rv = parseFloat( RegExp.$1 );
		// if(rv < 7) fixLowIE();
		// if(rv < 8) fixIE();
	}
	/*@end @*/
}

if (/WebKit/i.test(navigator.userAgent)) {
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			clearInterval(_timer);
			init();
		}
	}, 10);
}

else if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, null);
}

else {
	/*@cc_on @*/
	/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init();
		}
	};
	/*@end @*/
	window.onload = init;
}