var upgLib = {
	emptyFunction: function() { return false; },

	setOpacity: function(obj, o) {
		obj.style.opacity=(o/100);
		obj.style.filter='alpha(opacity='+o+');';
	},
	getTopPos: function(obj) {
		var r = obj.offsetTop;
		while((obj = obj.offsetParent) != null) r += obj.offsetTop;
		return r;
	},
	getLeftPos: function(obj) {
		var r = obj.offsetLeft;
		while((obj = obj.offsetParent) != null) r += obj.offsetLeft;
		return r; 
	},
	addCSS: function (css) {
		var style = document.createElement('style');
		style.type = 'text/css';
		if (style.styleSheet) {
			style.styleSheet.cssText = css;
		} else {
			style.appendChild(document.createTextNode(css));
		}
	   this.body.appendChild(style);
	},
	body: document.getElementsByTagName("body").item(0),
	isOpera: navigator.userAgent.toLowerCase().indexOf('opera')>=0,
	disallowPNG: (navigator.appVersion.indexOf('MSIE') != -1) && !this.isOpera
};

var screenMask = {
	scrmask: false,
	init: function() {
		this.scrmask = document.createElement('div');
		this.scrmask.id = 'scrmask';
		upgLib.body.appendChild(this.scrmask);
		if(upgLib.disallowPNG) upgLib.addCSS('* html #scrmask, * html #popup { background-image: none; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/images/transparent.png", sizingMethod="scale"); }');
	},
	show: function() { this.scrmask.style.display = 'block'; },
	hide: function() { this.scrmask.style.display = 'none'; },
	height: function(x) { this.scrmask.style.height = x; }
}; screenMask.init();

var mouseX, mouseY;
function mouseMove(e) {
	// On mémorise les pisitions 
	mouseX = (!document.all) ? e.pageX : event.x+document.body.scrollLeft;
	mouseY = (!document.all) ? e.pageY : event.y+document.body.scrollTop;
}
document.body.onmousemove = mouseMove;

var smailPopup = {
	div: false,
	divC: false,
	close: false,
	title: false,
	content: false,
	move: false,
	moveX: 0,
	moveY: 0,

	init: function() {
		var _this = this;
		this.div = document.createElement('div');
		this.div.id = 'popup';

		var divC = document.createElement('div');
		divC.id = 'popupC';
		this.div.appendChild(divC);
		this.divC = divC;

		this.close = document.createElement('a');
		this.close.href = '#';
		this.close.innerHTML = '&nbsp;';
		this.close.id='closePopup';
		this.close.title = 'Fermer';
		this.close.onclick = function() { _this.hide(); return false; };
		divC.appendChild(this.close);
		
		this.title = document.createElement('div');
		this.title.id = 'popupTitle';
		divC.appendChild(this.title);

		this.content = document.createElement('div');
		this.content.id = 'popupContent';
		divC.appendChild(this.content);
		
		upgLib.body.appendChild(this.div);

		// Move
		this.title.onmousedown = function() { 
			smailPopup.move = true; 
			smailPopup.moveX = mouseX - upgLib.getLeftPos(smailPopup.div);
			smailPopup.moveY = mouseY - upgLib.getTopPos(smailPopup.div);
			upgLib.setOpacity(smailPopup.divC, 70);
		};
		this.title.onmouseup = function() { 
			smailPopup.move = false; 
			upgLib.setOpacity(smailPopup.divC, 100);
		};
		var mm = document.body.onmousemove;

		document.body.onmousemove = function(e) {
			mm(e); 
			try	{
				if(smailPopup && smailPopup.move) {
					smailPopup.div.style.top = (mouseY-smailPopup.moveY) + 'px';
					smailPopup.div.style.left = (mouseX-smailPopup.moveX) + 'px';
				}
			} catch (e) {}
		};
	},

	setTitle: function(t, c) {
		this.title.innerHTML = t ? t : 'Smail.fr';
		this.div.className = c ? c : 'smail';
	},
	show: function(nc) {
		this.hideSelect(true);
		this.div.style.visibility = 'hidden';
		this.div.style.display = 'block';
		this.close.style.display = (nc==true)?'none':'block';
		this.resize();
		this.div.style.visibility = 'visible';
		screenMask.show();
	},
	hide: function() {
		this.clear();
		this.hideSelect(false);
		this.div.style.display = 'none';
		screenMask.hide();
	},
	resize: function() {
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		this.div.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 15 - this.div.offsetHeight) / 2) - 10 + 'px');
		this.div.style.left = (((arrayPageSize[0] - this.div.offsetWidth) / 2) + 'px');
		screenMask.height(arrayPageSize[1] + 'px');
	},
	clear: function() {
		var f = null;
		while(f = this.content.firstChild) this.content.removeChild(f);
	},
	hideSelect: function(v) {
		var selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = v?'hidden':'visible';
		}
	}
}; smailPopup.init();

/* A SUPPR */

/*
* Alertes
*/
var smailAlert = {
	buttondiv: null,
	okButton: null,
	cancelButton: null,
	onOK: upgLib.emptyFunction,

	resetFunction: function() { this.onOK = upgLib.emptyFunction; },

	alert: function(msg, title, type) {
		this.resetFunction(); 
		this.confirm(msg, title, type || 'alert', true);
	},

	confirm: function(msg, title, type, justOK) {
		smailPopup.setTitle(title, type);
		smailPopup.content.innerHTML = msg;
		smailPopup.content.appendChild(this.buttondiv);
		this.cancelButton.style.display = (justOK)?'none':'inline';
		smailPopup.show(true);
	},

	init: function() {
		var _this = this;
		this.buttondiv = document.createElement('div');
		this.buttondiv.className = 'input';
		var ok = this.okButton = document.createElement('input');
		ok.type='button';
		ok.value = 'Ok';
		ok.onclick = function() { smailPopup.hide(); _this.onOK(); };
		this.buttondiv.appendChild(ok);
		var cancel = this.cancelButton = document.createElement('input');
		cancel.type='button';
		cancel.value = 'Annuler';
		cancel.onclick = function() { smailPopup.hide(); };
		this.buttondiv.appendChild(cancel);
	}

}; smailAlert.init();

/* 
* AutoScrolling
*/
var Scroll = {
	timer: false,
	dest: false,
	inc: false,
	current: false,
	goToId: function(n) {
		this.goTo(document.getElementById(n));
	},
	goTo: function(e, prec) {
		this.current = getPageScroll()[1];
		this.dest = upgLib.getTopPos(e);
		this.start(prec);
	},
	top: function() {
		this.dest = 0;
		this.current = getPageScroll()[1];
		this.start();
		return false;
	},
	start: function(prec) {
		var p = getPageSize();
		if(this.dest < 0) this.dest = 0;
		if(this.dest > (p[1]-p[3])) this.dest = (p[1]-p[3]);
		this.dest += prec || 0;
		// Calcul de la vitesse
		this.inc = Math.round((this.dest - this.current)/20); 
		// Pas trop lentement
		if(this.inc > -10 && this.inc < 10) this.inc = (this.inc>0)?10:-10;
		var _this = this;
		this.timer = setInterval(function() { _this.step(); }, 10);
	},
	step: function() {
		if((this.inc > 0 && this.current >= this.dest-9) || (this.inc < 0 && this.current <= this.dest+9)) clearInterval(this.timer); 
		else { self.scrollBy(0,this.inc); this.current += this.inc; }
	}
};

/* 
* Fonctions de Fading
*
* Pas encore utilisé le onsuccess donc à supprimer si personne ne l'utilise
*/
function Fade(object, options) {
	this.obj = object;
	this.remove = false;
	this.hide = false;
	this.inc = -0;
	this.limit = 0;
	this.start = 100; 
	this.height = 0;
	this.nofx = false;
	this.onsuccess = upgLib.emptyFunction;

	if(options) {
		this.start = options.start || this.start;
		this.limit = options.limit || this.limit;
		this.remove = options.remove || this.remove;
		this.hide = options.hide || this.hide;
		this.nofx = options.nofx || this.nofx;
		this.onsuccess = options.onsuccess || this.onsuccess;
	} 

	this.inc = (this.start>this.limit)?-4:4;
	this.opacity = upgLib.isOpera?this.limit:this.start;

	var _this = this;    
	this.timer = setInterval(function() { _this.setOpacity(); }, 10);

}

Fade.prototype = {
	setOpacity: function() {
		upgLib.setOpacity(this.obj, this.opacity);
		if((this.start > this.limit && this.opacity > this.limit) || (this.start < this.limit && this.opacity < this.limit) ) {
			this.opacity += this.inc;
		} else {
			clearInterval(this.timer);
			if(this.remove) {
				if(this.nofx) return this.obj.parentNode.removeChild(this.obj);
				this.height = this.obj.offsetHeight - 1;
				this.obj.style.visibility = 'hidden';
				this.obj.style.overflow = 'hidden';
				var _this = this;
				this.timer = setInterval(function() { _this.setHeight(); }, 7);
				return true;
			} else if(this.hide) {
				this.obj.style.display = 'none';
			}
			this.finish();
		}
	},

	setHeight: function() {
		this.obj.style.height = this.height + 'px';
		if(this.height > 1) {
			this.height = this.height-1;
		} else {
			clearInterval(this.timer);
			this.obj.parentNode.removeChild(this.obj);
			this.finish();
		}
	},

	finish: function() {
		this.onsuccess.apply(this.obj);
	}
};

function getPageScroll(){
	var yScroll;
	var xScroll;
	if (self.pageYOffset) {
		xScroll = self.pageXOffset;
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
		if(window.scrollMaxY > 0 ) { windowWidth -= 17; }
		if(window.scrollMaxX > 0 ) { windowHeight -= 17; }
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}


function $(d) { return document.getElementById(d); }

function Ajax() {
	this.failed = false;
	this.xmlhttp = null;
	this.method = 'GET';
	this.URLString = null;
	this.timer = null;
	this.nocache = 0;

	this.resetFunction();
}

Ajax.prototype = {
	resetFunction: function() {
		this.onloading = upgLib.emptyFunction;
		this.onsuccess = upgLib.emptyFunction;
		this.createAJAX();
	},

	createAJAX: function() {
		this.failed = false;
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}
		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	},

	timerRequest: function(file, time, options) {
		var _this = this;
		this.timer = setInterval(function() { _this.request(file, options); }, time);
	},

	stopTimer: function() {
		clearInterval(this.timer);
	},

	request: function(file, options) {
		if(options) {
			this.onsuccess = options.onsuccess || this.onsuccess;
			this.method = options.method || this.method;
			this.nocache = options.nocache || this.nocache;
		}
		if(!this.failed) {
			if (this.xmlhttp) {
				var self = this;
				if (this.nocache) file += ((file.indexOf('?')==-1)?'?':'&')+"rnd=" + Math.random();
				if (this.method == "GET") {
					this.xmlhttp.open(this.method, file, true);
				} else {
					this.xmlhttp.open(this.method, file, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}
				
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
						case 2:
						case 3:
							self.onloading();
							break;
						case 4:
							self.failed = false;
							self.responseText = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.onsuccess(self.xmlhttp);
					}
				};
				this.failed = true;
				this.xmlhttp.send(this.URLString);
			}
		}

	}
};

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|s)" + className + "(s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++) {
		current = elements[i ];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function getDataFromForm(Form) {
    var data="";
    var key=0;
    for (key=0;key<Form.elements.length;key++) {      
         var currentValue=(Form.elements[key].tagName.toLowerCase()=="select")?getSelectValue(Form.elements[key]):Form.elements[key].value;
         data+=escape(Form.elements[key].name)+"="+escape(currentValue)+"&";
    }
    return data.substr(0, data.length-1);
}
 
function getSelectValue(select) {
 //  if (select.value!="") return select.value;

   var value="";
   for (var i=0; true; i++) {
      if ((select.option) && (select.option[i])) {
         if (select.options[i].selected) {
            value += select.options[i].value + ",";
         }
      } else { 
		return (value=="")?select.value:value.substr(0, value.length-1); 
	  }
   }
}