// ========================================================================================================
// "ExtraSize" - Module de changement de taille d'une balise
//   by Extrafine (contact@extrafine.fr)
// ========================================================================================================
/*
 * Copyright (c) 2011 Extrafine
 * Version: 1.3.1 (06/03/2011)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: Extra v1.0 or later
 */
// ========================================================================================================
/*
 * ver 1.3.1 :
 * Add -> Attr : SizeChild (Number) // Redimmensionner les images du noeud
 * Add -> Attr : SizeNot (Array) // Tableau des styles fils non redimensionnés
 */
// ========================================================================================================
//

function ExtraSize()
{
	// Attribut ###############################################
	this.InSize = false;		// Mouvement en cours
	this.WidthGo = 0;			// Largeur de destination
	this.HeightGo = 0;			// Hauteur de destination
	this.SizeInterval = null;	// Identifiant de l'Interval
	this.SizeFondu = true;		// Fondu du Mouvement
	this.Ratio = true;			// Conserve les proportions
	this.SizeCoef = 10;			// Coefficient du Fondu
	this.SizeChild = 0;			// Redimensionne les balises (IMG et DIV) du noeud (0= Non | 1= Width & Height | 2= Width | 3= Height | 4= Contenu dans Parent)
	this.SizeNot = new Array();	// Tableau exclusion

	// Methode ################################################
	//	this.DoSize 		// Modification de la Taille (x,y)
	//	this.SizeInit 		// Initialisation Taille
	//	this.SizeToW 		// Changement vers la largeur (x,[v => Vitesse])
	//	this.SizeToH		// Changement vers la hauteur (y,[v => Vitesse])
	//	this.SizeTo 		// Changement vers la hauteur et la largeur (x,y,[v => Vitesse])
	//	this.SizeBy 		// Changement de la hauteur et de la largeur (x,y,[v => Vitesse])
	//	this.SizeSet 		// Calcul de la Taille

	// Modification de la Taille (x,y)
	this.DoSize = function(x,y)
	{
		this.Width = x;
		this.Height = y;
		var Element = document.getElementById(this.Id);
		Element.style.width = x +"px";
		Element.style.height = y +"px";
		if(this.SizeChild != 0) {
			var IsNot = false;
			var Childs = Element.getElementsByTagName('img');
			for(var i = 0; i < Childs.length; i++) {
				IsNot = false;
				for(Index in this.SizeNot){
					var cReg = new RegExp(this.SizeNot[Index]);
					if(Childs[i].className.search(cReg) != -1){
						IsNot = true;
						break;
					}
				}
				if(!IsNot){
					switch(this.SizeChild){
						case 1 :
							Childs[i].style.width = x +'px';
							Childs[i].style.height = y +'px';
							break;
						case 2 :
							Childs[i].style.width = x +'px';
							break;
						case 3 :
							Childs[i].style.height = y +'px';
							break;
						case 4 :
							Childs[i].style.maxWidth = x +'px';
							Childs[i].style.maxHeight = y +'px';
							break;
					}
				}
			}
			var Childs = Element.getElementsByTagName('div');
			for(var i = 0; i < Childs.length; i++) {
				IsNot = false;
				for(Index in this.SizeNot){
					var cReg = new RegExp(this.SizeNot[Index]);
					if(Childs[i].className.search(cReg) != -1){
						IsNot = true;
						break;
					}
				}
				if(!IsNot){
					switch(this.SizeChild){
						case 1 :
							//Childs[i].width = x +'px';
							Childs[i].style.width = x +'px';
							//Childs[i].height = y +'px';
							Childs[i].style.height = y +'px';
							break;
						case 2 :
							//Childs[i].height = Math.round(x * Childs[i].scrollHeight / Childs[i].scrollWidth) +'px';
							Childs[i].style.height = Math.round(x * Childs[i].style.height / Childs[i].style.width) +'px';
							//Childs[i].width = x +'px';
							Childs[i].style.width = x +'px';
							break;
						case 3 :
							//Childs[i].width = Math.round(y * Childs[i].scrollWidth / Childs[i].scrollHeight) +'px';
							Childs[i].style.width = Math.round(y * Childs[i].style.width / Childs[i].style.height) +'px';
							//Childs[i].height = y +'px';
							Childs[i].style.height = y +'px';
							break;
						case 4 :
							Childs[i].style.maxWidth = x +'px';
							Childs[i].style.maxHeight = y +'px';
							break;
					}
				}
			}
		}
	}

	// Initialisation Taille
	this.SizeInit = function()
	{
		if(this.Height == 0 ^ this.Width == 0) {
			if(this.Height == 0) {
				document.getElementById(this.Id).style.width = this.Width +"px";
				this.Height = parseInt(document.getElementById(this.Id).offsetHeight)-2;
			} else {
				document.getElementById(this.Id).style.height = this.Height +"px";
				this.Width = parseInt(document.getElementById(this.Id).offsetWidth)-2;
			}
		} else {
			if(this.Width == 0) {
				if(typeof(document.getElementById(this.Id).width) != 'undefined')
					this.Width = document.getElementById(this.Id).width;
				else
					this.Width = document.getElementById(this.Id).style.width.replace(/px/,'');
			}
			if(this.Height == 0) {
				if(typeof(document.getElementById(this.Id).height) != 'undefined')
					this.Height = document.getElementById(this.Id).height;
				else
					this.Height = document.getElementById(this.Id).style.height.replace(/px/,'');
			}
			this.DoSize(this.Width,this.Height);
		}
	}

	// Changement vers la largeur (x,[v => Vitesse])
	this.SizeToW = function(x,v)
	{
		if(v)
			this.SizeCoef = v;
		if(this.Ratio) {
			var y = Math.round(x * this.Height / this.Width);
			this.SizeTo(x,y);
		} else
			this.SizeTo(x,this.Height);
	}

	// Changement vers la hauteur (y,[v => Vitesse])
	this.SizeToH = function(y,v)
	{
		if(v)
			this.SizeCoef = v;
		if(this.Ratio) {
			var x = Math.round(y * this.Width / this.Height);
			this.SizeTo(x,y);
		} else
			this.SizeTo(this.Width,y);
	}

	// Changement vers la hauteur et la largeur (x,y,[v => Vitesse])
	this.SizeTo = function(x,y,v)
	{
		this.WidthGo = x;
		this.HeightGo = y;
		if(v)
			this.SizeCoef = v;
		if(this.SizeFondu) {
			if(!this.InSize){
				this.InExtra = true;
				this.InSize = true;
				clearInterval(this.SizeInterval);
				var myThis = this;
				this.SizeInterval = setInterval(function(){myThis.SizeSet()},this.Frequence);
			}
		} else
			this.DoSize(x,y);
	}

	// Changement de la hauteur et de la largeur (x,y,[v => Vitesse])
	this.SizeBy = function(x,y,v)
	{
		this.WidthGo = this.Width + parseInt(x);
		this.HeightGo = this.Height + parseInt(y);
		if(v)
			this.SizeCoef = v;
		if(this.SizeFondu) {
			if(!this.InSize){
				this.InExtra = true;
				this.InSize = true;
				clearInterval(this.SizeInterval);
				var myThis = this;
				this.SizeInterval = setInterval(function(){myThis.SizeSet()},this.Frequence);
			}
		} else
			this.DoSize(x,y);
	}

	// Calcul de la Taille
	this.SizeSet = function()
	{
		var diffValX = Math.round(((this.Width - this.WidthGo) / this.SizeCoef));
		var diffValY = Math.round(((this.Height - this.HeightGo) / this.SizeCoef));

		var valX = (this.Width - diffValX);
		var valY = (this.Height - diffValY);

		if(diffValX == 0 && diffValY == 0) {
			if(valX == this.WidthGo && valY == this.HeightGo) {
				clearInterval(this.SizeInterval);
				this.InExtra = false;
				this.InSize = false;
//				if(typeof(ExtraScroll) == 'function' && this.Scroll) {
//					this.ScrollMaj();
//				}
				return true;
			} else {
				if((this.WidthGo - this.Width) > 0)
					valX = (this.Width + 1);
				else if((this.WidthGo - this.Width) < 0)
					valX = (this.Width - 1);
				if((this.HeightGo - this.Height) > 0)
					valY = (this.Height + 1);
				else if((this.HeightGo - this.Height) < 0)
					valY = (this.Height - 1);
			}
		}

		this.DoSize(valX,valY);
	}
}

function array_search(what, where){
	var index_du_tableau = -1;
	for(elt in where){
		index_du_tableau++;
		if(where[elt] == what)
			return index_du_tableau;
	}
	index_du_tableau = -1;
	return index_du_tableau;
}
