/* -----------------------------------------------------------------
Version	1.0
Date de création modification 	13 Mars 2008
Auteur	Thierry
Améliorations possibles :
-------------------------------------------------------------------- */

	function getItemStyle (klass, type) { 	
		var res="";
		$each(document.styleSheets, function(style){
			var rules = style.rules || style.cssRules;
			$each(rules, function(rule){
				var klass0 = rule.selectorText;
				if (klass==klass0) {
					var sd = "rule.style." + type ;
					res = eval(sd) ;
				}
			});
		});
		return res;
	}
	
	var eqxImgView = new Class({
		
		closeString: "Fermer la fenêtre", 
		closeValue: "&times;", 

 		initialize: function(node){
				
			this.nodeSrc = node.getProperty("src");
			if (this.nodeSrc=="" || !$defined(this.nodeSrc)) {
				alert ("L'attribut src doit être renseigné pour les widgets eqxImgView!");
				return ;
			}

			this.title = node.getProperty("title");
			if (this.title=="" || !$defined(this.title)) {
				alert ("L'attribut title doit être renseigné pour les widgets eqxImgView!");
				return ;
			}

			var lKlassImg = node.getProperty("classImg") ;
			if (lKlassImg=="" || !$defined(lKlassImg)) {
				alert ("L'attribut classImg doit être renseigné pour les widgets eqxImgView!");
				return ;
			}		 
			this.classImg = "." + lKlassImg ;

			this.eqxWidth = getItemStyle(this.classImg,"width") ;
			if (this.eqxWidth=="" || !$defined(this.eqxWidth)) {
				alert ("Le style width doit être renseigné pour la class classImg des widgets eqxImgView!");
				return ;
			}
			
			this.eqxHeight = getItemStyle(this.classImg,"height") ;
			if (this.eqxHeight=="" || !$defined(this.eqxHeight)) {
				alert ("Le style height doit être renseigné pour la class classImg des widgets eqxImgView!");
				return ;
			}
			
			this.srcImg = node.getProperty("srcImg") ;
			if (this.srcImg=="" || !$defined(this.srcImg)) {
				this.srcImg = this.nodeSrc ;
			}

			var w = parseInt(this.eqxWidth) ;
			var h = parseInt(this.eqxHeight);

			this.node = document.createElement("div"); 		
			this.node.widget=this;
			this.node.style.margin="0px";	
			this.node.style.padding="0px";	
			document.body.appendChild(this.node);

			
			var me=this;			
			this.bandeau = document.createElement("div"); 		
			this.node.appendChild(this.bandeau);
			this.bandeau.style.width	=(w)+"px";
			this.bandeau.className=lKlassImg ;
			
			this.classPar = node.getProperty("classPar") ;
			if (this.classPar=="" || !$defined(this.classPar)) {
				this.bandeau.style.margin="0px";	
				this.bandeau.style.paddingTop="5px";	
				this.bandeau.style.paddingLeft="5px";	
				this.bandeau.style.border="0px none";	
				this.bandeau.style.backgroundColor="white";	
				this.bandeau.style.color="black";	
			}
			else {
				this.bandeau.className=this.classPar;
			}


			this.closePar = document.createElement("a"); 		
			this.bandeau.appendChild(this.closePar);
			this.closePar.innerHTML=this.closeValue;
			this.closePar.title=this.closeString;
			this.closePar.href="#";	
			this.closePar.style.textDecoration="none";	
			this.closePar.style.margin="0px";	
			this.closePar.style.paddingTop="0px";	
			this.closePar.style.paddingRight="5px";	
			this.closePar.style.fontSize="1.2em";	
			$(this.closePar).setStyle('float','right');

			this.textPar = document.createElement("p"); 		
			this.bandeau.appendChild(this.textPar);
			this.textPar.innerHTML=this.title;
			this.textPar.style.textAlign="left";	

			this.node.style.visibility='visible';
			this.node.style.position='absolute';
			this.node.style.left	=(getWidth()/2)+"px";
			this.node.style.top		=(getHeight()/2)+"px";
			this.node.style.width	="10px";
			this.node.style.height	="10px";
		
			this.node.style.border="0px none white";	
			this.node.style.backgroundColor="white";	
			
			this.node.style.zIndex  = 101;
	
			var left = (getWidth()-w)/2 ;
			var top = (getHeight()-h)/2 ;

				
	
			// Ouverture et positionnement de la fenêtre
			var bandeau=this.bandeau;
			bandeau.style.visibility="hidden";		
			
			var effet2 = new Fx.Styles(
				this.node, 
				{	duration: 500, 
					transition: Fx.Transitions.Sine,
					onComplete: function () {
						var img0 = new Asset.image("http://www.e-quinox.fr/al0.gif", {
						});
						img0.inject(me.node,'top');
						img0.style.position="absolute";
						img0.style.left = (w/2 - 28)+"px" ;
						img0.style.top  = (h/2 - 28)+"px";
						var img = new Asset.image(me.srcImg, {
							onload: function () {
								img.inject(me.node,'top');
								img.style.width	=w+"px";
								img.style.height	=h+"px";
								img.className=lKlassImg ;
								img0.style.display="none";
								
								// Pour déplacer la fenêtre
								img.style.cursor="move";
								new Drag.Move (me.node, {handle:$(img)});
								
								var effet3 = new Fx.Style(img, 
									'opacity', 
									{
										duration:1500,
										onComplete: function () {
											bandeau.style.visibility="visible";
										}
									}
								);
								$(img).setOpacity(0);
								effet3.start(0,1);
							}
						});
					}
				}
			);

			var effet1 = new Fx.Styles(
				this.node, 
				{	duration: 500, 
					transition: Fx.Transitions.Sine,
					onComplete: function () {
						effet2.start({
							'top': [getHeight()/2, top],
							'height': [1,h]
						});
					}
				}
			);
				
			// new Fx.Style(this.node, 'opacity', {duration: 0}).set(0);
			effet1.start({
				'left': [getWidth()/2, left],
				'width': [1,w]
			});
			
			// Les evt
			this.closePar.widget=this ; // pour le click
			$(this.closePar).addEvent('click', 
				function (evt) {
					var evt0 = new Event (evt) ;					
					var me = evt0.target ;
					if (me.getTag()=="a") { // robustesse
						if (me.widget) me.widget.terminate ();
					}
				}
			);
			
		},

		close: function () {
			this.terminate();
		},
		terminate: function () {
			this.node.style.display='none';
			document.body.removeChild (this.node);
		}
		
	});

		
	/*----------------------------------------------------------*/
	// Initialisation après fabrication du DOM
	window.addEvent('domready', function(){
		$ES('img').each ( function (h) {
			if (h.hasClass('eqxImgView')) {
				$(h).addEvent('click', 
					function (evt) {
						var evt0 = new Event (evt) ;					
						var me = evt0.target ;
						new eqxImgView (me);
					}
				);
			}
		});
	});
		