


/**
 *------------------------------------------------------------------------------
 *							Loading Class Constructor
 *------------------------------------------------------------------------------
 */

var LOADING_CLASS_NAME = 'loading';
var LOADING_TXT_LOADING = 'Chargement en cours...';

var Loading = Class.create();

Loading.prototype = {	
	
	/**
	 * Constructeur de la classe Loading
	 *
	 * Il faut savoir que l'élément loading sera attaché au BODY. Il peut y avoir une
	 * image dedans. Dans ce cas, il faut renseigner pathImg.
	 *
	 * @access	public
	 * @param	string		pathImg		Le chemin vers l'image
	 * @param	string		type		Le type d'affichage (center, fix)
	 * @param	HTMLObject	parentObj	Le parent du div qui contient le load (body par défaut)		
	 */
	initialize : function (imgsPath, type, parentObj) {
		try {
			this.imgsPath = imgsPath;
			this.visible = false;
			this.multipleOpening = true;
			this.openedTimes = 0;
			this.y = 0;
			this.x = 0;
			
			this.setType(type);
			
			var parent = document.getElementsByTagName('body')[0];
			if (parentObj){
				parent = $(parentObj);
			}
			this.parent = parent;
			this.obj = this._create();
			
			this.parent.appendChild(this.obj);
			
			this.hide();
			LoadingManager.add(this);
		} catch (err) {
			alert(err.message);
		}
	},
	
	
	/**
	 *------------------------------------------------------------------------------
	 *							Loading getters and setters
	 *------------------------------------------------------------------------------
	 */
	
	/**
	 * Set les coordonnées auxquelles le loading s'affichera en mode 'fix'
	 *
	 * @access public
	 * @param integer x La position en x
	 * @param integer y La position en y
	 * @return void
	 */
	setCoordinates : function (x, y) {
		this.y = y;
		this.x = x;
	},
	
	getCoordinates : function () {
		return {
			'y' : this.y,
			'x' : this.x
		}
	},
	
	setType : function (type) {
		this.type = (typeof(type) == 'undefined') ? 'center' : type;
	},
	
	
	/**
	 *------------------------------------------------------------------------------
	 *							Loading public functions
	 *------------------------------------------------------------------------------
	 */
	
	hide : function () {
		if (this.multipleOpening) {
			this.openedTimes -= 1;
			if (this.openedTimes < 0) this.openedTimes = 0;
			if (this.openedTimes <= 0) {
				this.obj.style.display = 'none';
			}
		} else {
			this.obj.style.display = 'none';
			this.visible = false;
		}
	},
	
	/**
	 * La fonction montre() est dépréciée. Utiliser show() à la place
	 */
	montre : function (type) {this.show(type);},
	show : function (type) {
		var choix = (typeof(type) == 'undefined') ? this.type : type;
		var x = 0;
		var y = 0;
		switch (choix) {
			case 'fix' :
				var pos = this.getCoordinates();
				x = pos.x;
				y = pos.y;
				break;
			case 'center' :
			default :
				var posScroll = LoadingManager.getScrollPosition();
				var dimScreen = LoadingManager.getWindowDimensions();
				var dimObj = this.getDimensions();
				x = posScroll.x + (dimScreen.width / 2) - (dimObj.width / 2);
				y = posScroll.y + (dimScreen.height / 2) - (dimObj.height / 2);
		}
		this.openedTimes++;
		this.setPosition(x, y);
		this.obj.style.display = '';
		this.visible = true;
	},
	
	isVisible : function () {
		return this.visible;
	},
	
	/**
	 * Set la position où s'affichera le loading, quel que soit le mode
	 *
	 * @access public
	 * @param integer x La position en x
	 * @param integer y La position en y
	 * @return void
	 */
	setPosition : function (x, y) {
		this.obj.style.top = y;
		this.obj.style.left = x;
	},
	
	getDimensions : function () {
		this.obj.style.display = '';
		var width = this.obj.offsetWidth;
		var height = this.obj.offsetHeight;;
		this.obj.style.display = (this.isVisible()) ? '' : 'none';
		return {
			'width' : width,
			'height' : height
		}
	},

	
	
	/**
	 *------------------------------------------------------------------------------
	 *							Loading private functions
	 *------------------------------------------------------------------------------
	 */
	
	_create : function () {
		var container = document.createElement('div');
		var borderWhite = document.createElement('div');
		
		// Création
		if (this.imgsPath){
			var img = document.createElement('img');
			img.setAttribute('src', this.imgsPath);
			borderWhite.appendChild(img);
		} else {
			var txt = document.createTextNode(LOADING_TXT_LOADING);
			borderWhite.appendChild(txt);
		}
		
		// Styles
		Element.addClassName(container, LOADING_CLASS_NAME);
		
		// Append
		container.appendChild(borderWhite);
		return container;
	}
};















/**
 *------------------------------------------------------------------------------
 *							TafelTree Class Constructor
 *------------------------------------------------------------------------------
 */

var LoadingManager = {
	
	/**
	 *------------------------------------------------------------------------------
	 *							LoadingManager properties
	 *------------------------------------------------------------------------------
	 */
	
	loadingObjects : [],
	
	
	
	/**
	 *------------------------------------------------------------------------------
	 *							LoadingManager public function
	 *------------------------------------------------------------------------------
	 */
	
	add : function (load) {
		this.loadingObjects.push(load);
	},

	getScrollPosition : function () {
		var scrollT = 0;
		if(document.documentElement.scrollTop!=0) scrollT=document.documentElement.scrollTop;
		else if(document.body.scrollTop!=0) scrollT=document.body.scrollTop;
		// Gestion X
		var scrollL = 0;
		if(document.documentElement.scrollLeft!=0) scrollL=document.documentElement.scrollLeft;
		else if(document.body.scrollLeft!=0) scrollL=document.body.scrollLeft;
		return  {
			'y' : scrollT,
			'x' : scrollL
		};
	},
	
	getWindowDimensions : function () {
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
				winW = window.innerWidth;
				winH = window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				winW = document.body.offsetWidth;
				winH = document.body.offsetHeight;
			}
		}		
		return {
			'width' : winW,
			'height' : winH
		}
	}
	
};