// The constructor should be called with
// the parent object (optional, defaults to window).

function Timer(){
    this.obj = (arguments.length)?arguments[0]:window;
    return this;
}

// The set functions should be called with:
// - The name of the object method (as a string) (required)
// - The millisecond delay (required)
// - Any number of extra arguments, which will all be
// passed to the method when it is evaluated.

Timer.prototype.setInterval = function(func, msec){
    var i = Timer.getNew();
    var t = Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setInterval(t,msec);
    return i;
}
Timer.prototype.setTimeout = function(func, msec){
    var i = Timer.getNew();
    Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
    return i;
}

// The clear functions should be called with
// the return value from the equivalent set function.

Timer.prototype.clearInterval = function(i){
    if(!Timer.set[i]) return;
    window.clearInterval(Timer.set[i].timer);
    Timer.set[i] = null;
}
Timer.prototype.clearTimeout = function(i){
    if(!Timer.set[i]) return;
    window.clearTimeout(Timer.set[i].timer);
    Timer.set[i] = null;
}

// Private data

Timer.set = new Array();
Timer.buildCall = function(obj, i, args){
    var t = "";
    Timer.set[i] = new Array();
    if(obj != window) {
        Timer.set[i].obj = obj;
        t = "Timer.set["+i+"].obj.";
    }
    t += args[0]+"(";
    if(args.length > 2){
        Timer.set[i][0] = args[2];
        t += "Timer.set["+i+"][0]";
        for(var j=1; (j+2)<args.length; j++){
            Timer.set[i][j] = args[j+2];
            t += ", Timer.set["+i+"]["+j+"]";
        }
    }
    t += ");";
    Timer.set[i].call = t;
    return t;
}

Timer.callOnce = function(i){
    if(!Timer.set[i]) return;
    eval(Timer.set[i].call);
    Timer.set[i] = null;
}

Timer.getNew = function(){
    var i = 0;
    while(Timer.set[i]) i++;
    return i;
}


function fadeIn(obj, to) {
    this.obj = obj;
    this.to = to;
    this.actual = 0;
    this.grow = function() {
        this.actual += 7;
        if (this.actual <= this.to) {
            setOpacity(this.obj, this.actual);
            setTimeout(this.grow, 1);
        }
    }
    this.obj.style.display = 'block';
    this.grow();
}

function fadeOff(obj, from) {
    this.obj = obj;
    this.to = 0;
    this.actual = from;
    this.grow = function() {
        this.actual -= 7;
        if (this.actual >= this.to) {
            setOpacity(this.obj, this.actual);
            setTimeout(this.grow, 1);
        } else {
            this.obj.style.display = 'none';
        }
    }
    this.grow();
}

function showAjaxMask() {
    var obj = document.getElementById('ajaxMask');
    if (obj.style.display != 'block') {
        fadeIn(obj, 60);
    }
}
function hideAjaxMask() {
    var obj = document.getElementById('ajaxMask');
    if (obj.style.display != 'none') {
        fadeOff(obj, 60);
    }
}

function ajaxWindow() {
    this.obj = document.getElementById('ajaxContent');
    this.w = 0;
    this.timer = new Timer(this);
    this.timeout = null;
}
ajaxWindow.prototype.setWidth = function(w) {
    this.obj.style.width = w+'px';
    this.w = w;
}
ajaxWindow.prototype.show = function() {
    this.obj.style.display = 'none';
    this.center();
    showAjaxMask()
    this.obj.style.display = 'block';
}
ajaxWindow.prototype.hide = function() {
    this.obj.style.display = 'none';
    hideAjaxMask();
}
ajaxWindow.prototype.close = function() {
    this.hide();
    this.obj.innerHTML = '';
    this.setWidth(0);
    this.timer.clearTimeout(this.timeout);
}
ajaxWindow.prototype.center = function() {
    this.obj.style.position = 'absolute';
    var size = window.size();
    var left = Math.max(0, ((size.width/2)-(this.w/2)));
    var top = 25;
    this.obj.style.left = left+'px';
    this.obj.left = left;
    this.obj.style.top = top+'px';
    this.obj.top = top;
}
ajaxWindow.prototype.setTimeout = function(time) {
    this.autoCloseControl = false;
    if (time > 0) {
        this.timeout = this.timer.setTimeout('close', time);
    } else {
        this.close();
    }
}

function setOpacity(obj, xOpacity) {
    obj.style.opacity = xOpacity/100;
    obj.style.MozOpacity = xOpacity/100;
    obj.style.KHTMLOpacity = xOpacity/100;
    obj.style.filter = 'alpha(opacity=' + (xOpacity) + ')';
}

function getOpacity(obj) {
    if (obj.style.opacity) {
        return obj.style.opacity*100;
    } else if(obj.style.MozOpacity) {
        return obj.style.MozOpacity*100;
    } else if(obj.style.KHTMLOpacity) {
        return obj.style.KHTMLOpacity*100;
    } else if(obj.style.filter) {
        var s = obj.style.filter.indexOf('opacity=')+8;
        var e = obj.style.filter.indexOf(')', s);
        return obj.style.filter.substr(s, e);
    }
}

window.size = function() {
	var w = 0;
	var h = 0;

	if(!window.innerWidth) {
		if(!(document.documentElement.clientWidth == 0)) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	} else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

function addClass(cName, obj) {
    if (obj.className.indexOf(cName) == -1) {
        if (obj.className == '') {
            obj.className = cName;
        } else {
            obj.className += ' '+cName;
        }
    }
}

function remClass(cName, obj) {
    if (obj.className.indexOf(cName) > -1) {
        if (obj.className == cName) {
            obj.className = '';
        } else {
            obj.className = obj.className.replace(new RegExp(' '+cName), '');
        }
    }
}


function initDhtml() {
    var x,y;
    if (document.body.scrollHeight > document.body.offsetHeight) {
    	x = document.body.scrollWidth;
    	y = document.body.scrollHeight;
    } else {
    	x = document.body.offsetWidth;
    	y = document.body.offsetHeight;
    }
    var obj = document.getElementById('ajaxMask');
    obj.style.width = x+'px';
    obj.style.height = y+'px';

    var obj = document.getElementById('ajaxContent');
    obj.innerHTML = obj.innerHTML.replace(/^\s+/, "");
    if (obj.innerHTML != '') {
        var toRead = obj.getElementsByTagName('div').length;
        obj.innerHTML = '<div class="closeAjaxPopup png"><a href="#" onclick="var w = new ajaxWindow(); w.close();">Fermer</a></div>'+obj.innerHTML;
        var w = new ajaxWindow();
        w.setWidth(600);
        w.show();
        w.setTimeout(1500+(toRead*500));
    }
}

dhtml_onload = window.onload;
window.onload = function() {
    if (dhtml_onload) {
        dhtml_onload();
    }
    initDhtml();
}

