<!--

var _clayer_obj = null;
var _clayer_id = 1;

_clayer.instances = new Array();

_clayer_events = {
     "addEventListener" : function(element, eventName, eventHandler, scope)
    {
        var scopedEventHandler = scope ? function(e) { eventHandler.apply(scope, [e]); } : eventHandler;
        
        if(document.addEventListener)
        {
            element.addEventListener(eventName, scopedEventHandler, false);
        }
        else if(document.attachEvent)
        {
            element.attachEvent("on"+eventName, scopedEventHandler);
    	}
    }
}


function _clayer(classname,closeButtonAttachment)
{
	this.id = _clayer.instances.length;
	_clayer.instances[this.id] = this;
	
	this.imagePath = '';
	
	this.debug_id = 0;
	
	this.div_inside = null;				/* identifiant du bloc image */
	this.div_container = null;			/* identifiant du bloc conteneur */
	this.div_close = null;
	
	if(typeof(classname)!=='undefined')
	{
		this.classname = classname;
	}
	else
	{
		this.classname = '';
	}
		
	this.insideDivWidth = 0;
	this.insideDivHeight = 0;
	
	this.desactiveTimer = null;
	
	this.onclickeventhandler = null;
	
	this.animationDuration = 0.5;
	
	this.setup();
	
	if(typeof(closeButtonAttachment)!=='undefined')
	{
		this.attachCloseTo = closeButtonAttachment;
	}
	else
	{
		this.attachCloseTo = 'container';
	}
	
	this.setCloseButton();
	
	this.intelligentCloseButton = false;
}

_clayer.prototype.setup = function()
{
	//var posx = parseInt(document.body.clientWidth/2) - parseInt(_div_getWidth(div)/2);
	
	// création du bloc conteneur
	
	var width = parseInt(document.body.clientWidth);
	var height = parseInt(document.body.clientHeight);
	
	this.div_container = _div_create('','','body',width,height,0,0,'');
	_div_setStyleProperty(this.div_container,'position','fixed');
	_div_setStyleProperty(this.div_container,'left','0');
	_div_setStyleProperty(this.div_container,'top','0');
	_div_setStyleProperty(this.div_container,'display','none');
	_div_setStyleProperty(this.div_container,'visibility','hidden');
	_div_setStyleProperty(this.div_container,'opacity','0.0');
	_div_setStyleProperty(this.div_container,'z-index','10');
		
	if(this.classname!='')
	{
		this.div_container.className = this.classname;
	}
	else
	{	
		_div_setStyleProperty(this.div_container,'background-color','rgba(0,0,0,0.8)');
		//_div_setStyleProperty(this.div_container,'cursor','pointer');
	}
	
	var browserPreffix = _getBrowserStylesPreffix();

	this.setAnimate(true);
	
	_clayer_obj = this;

	
	this.div_inside = _div_create('','',this.div_container,0,0,0,0,'');
	_div_setStyleProperty(this.div_inside,'position','absolute');

	if(this.classname!='')
	{
		this.div_inside.className = this.classname+'-in';
	}
	else
	{
		_div_setStyleProperty(this.div_inside,'background-repeat','no-repeat');
		_div_setStyleProperty(this.div_inside,'background-position','center');
		_div_setStyleProperty(this.div_inside,'background-size','auto 100%');	
	}	
}

_clayer.prototype.refreshCloseButton = function()
{
	
}

_clayer.prototype.setCloseButton = function()
{
	if(this.div_close===null)
	{
		if(this.classname=='')
		{
			this.div_close = _div_create2(this.div_container,'');
			_div_setStyleProperty(this.div_close,'cursor','pointer');
			_div_setStyleProperty(this.div_close,'width','100%');
			_div_setStyleProperty(this.div_close,'height','100%');
		}
		else
		{
			this.div_close = _div_create2(this.div_container,this.classname+'-close');
		}
	}
	else
	{
		_div_setParent(this.div_close,this.div_container);
	}
	
	this.addOnclickEventHandler()
}

_clayer.prototype.setAnimate = function(animate)
{
	var browserPreffix = _getBrowserStylesPreffix();
	
	if(animate)
	{
		//alert('animate ON');
		_div_setStyleProperty(this.div_container,browserPreffix+'transition-property','opacity');
		_div_setStyleProperty(this.div_container,browserPreffix+'transition-duration',this.animationDuration+'s');
		
		//alert('animation on');
	}
	else
	{
		//alert('animate OFF');
		_div_setStyleProperty(this.div_container,browserPreffix+'transition-property','');
		//_div_setStyleProperty(this.div_container,browserPreffix+'transition-duration','0.0s');
	}
}

_clayer.prototype.immediateAppear = function(color)
{
	this.setAnimate(false);
	this.appear();
}

_clayer.prototype.setBackgroundColor = function(color)
{
	_div_setStyleProperty(this.div_container,'background-color',color);
}

_clayer.prototype.addOnclickEventHandler = function()
{
	//alert('addOnclickEventHandler()');
	var that = this;
	
	this.onclickeventhandler = function(e){ that.disappear() };
	
	_event_listen(this.div_close,'click',this.onclickeventhandler);
}

_clayer.prototype.removeOnclickEventHandler = function()
{
	var that = this;
	
	if(this.onclickeventhandler !== null)
	{
		_event_stopListening(this.div_close,'click',this.onclickeventhandler);
		this.onclickeventhandler = null;
	}
}


_clayer.prototype.setPicture = function(path)
{
	myImage = new Image;

	if(this.onclickeventhandler==null)
	{
		this.addOnclickEventHandler();
	}

	if(typeof(path)!=='undefined')
	{
		imagePath = path;
	}
	else
	{
		imagePath = this.imagePath;
	}

	
	_clayer_obj = this;
	
	myImage.onload = function() 
	{
		var imageWidth = this.width;
		var imageHeight = this.height;
		
		if(imageHeight>parseInt(window.innerHeight))
		{
			imageHeight = parseInt(window.innerHeight*0.95);
		}
		
		_clayer_obj.insideDivWidth = imageWidth;
		_clayer_obj.insideDivHeight = imageHeight;
		
		//_div_setStyleProperty(_clayer_obj.div_inside,'width',imageWidth+'px');
		_div_setStyleProperty(_clayer_obj.div_inside,'width',imageWidth+'px');
		_div_setStyleProperty(_clayer_obj.div_inside,'height',imageHeight+'px');
		_div_setStyleProperty(_clayer_obj.div_inside,'background-image','url('+imagePath+')');



		_clayer_obj.appear();
		
		_clayer_obj = null;
		
		this.onload = null;
	}
	
	//alert('chargement de '+imagePath);
	myImage.src = imagePath;
}



_clayer.prototype.debug = function()
{

}

_clayer.prototype.draw = function()
{
	//_div_setStyleProperty(this.div_id,'left',parseInt(this.div_posx)+"px");
   	//_div_setStyleProperty(this.div_id,'top',parseInt(this.div_posy)+"px");
}

_clayer.prototype.attachDiv = function(div)
{
	var cdiv = _div_object(div);
	
	if(this.div_inside!=cdiv)
	{	
		//alert('div différent ..');

		this.insideDivWidth = _div_getWidth(cdiv);
		this.insideDivHeight = _div_getHeight(cdiv);
		
		if(this.div_inside)
		{
			//alert('div défini : on le rattache au body');
			_div_remove(this.div_inside);
			this.div_inside = null;
		}
	
		this.div_inside = cdiv;

		//alert(this.insideDivWidth+'/'+this.insideDivHeight);
		
		if(_div_getParent(cdiv)!=this.div_container)
		{
			_div_setParent(cdiv,this.div_container);
		}
		
		_div_setStyleProperty(this.div_inside,'display','block');
		_div_setStyleProperty(this.div_inside,'visibility','visible');
	}
	else
	{
		//alert('div identique');
	}
}

_clayer.prototype.setDiv = function(div)
{
	this.attachDiv(div);
	this.appear();
}


_clayer.prototype.appear = function()
{
	_div_setStyleProperty(this.div_container,'display','block');
	_div_setStyleProperty(this.div_container,'visibility','visible');
	
	// recalculs
	
	var bodySize = _bodySize();
	var viewportSize = _viewportSize();
	
	if(_isIPad())
	{
		//alert('ipad');
		var visibleWidth = viewportSize.w;
		var visibleHeight = viewportSize.h;
		_div_setStyleProperty(this.div_container,'width',visibleWidth+'px');
		_div_setStyleProperty(this.div_container,'height',visibleHeight+'px');		
	}
	else
	{
		var visibleWidth = bodySize.w;
		var visibleHeight = viewportSize.h;	
		_div_setStyleProperty(this.div_container,'width',visibleWidth+'px');
		_div_setStyleProperty(this.div_container,'height','100%');
	}
	
	var posx = parseInt(visibleWidth / 2.0 - this.insideDivWidth / 2.0);
	var posy = parseInt(visibleHeight / 2.0 - this.insideDivHeight / 2.0);
	
	//alert(this.insideDivWidth+'/'+this.insideDivHeight);
	//alert(posx+';'+posy);
	
	if(this.intelligentCloseButton)
	{
		//alert('intelligentCloseButton');
		
		if(this.attachCloseTo=='container')
		{
			var x = posx+this.insideDivWidth;
			var y = posy-_div_getHeight(this.div_close);
			
			_div_setStyleProperty(this.div_close,'top',y+'px');
			_div_setStyleProperty(this.div_close,'left',x+'px');
		}
		else
		{
			var x = posx+this.insideDivWidth-_div_getWidth(this.div_close);
			var y = posy;
			
			_div_setStyleProperty(this.div_close,'top',y+'px');
			_div_setStyleProperty(this.div_close,'left',x+'px');		
		}
	
	}
	
	_div_setStyleProperty(this.div_inside,'display','block');
	_div_setStyleProperty(this.div_inside,'visibility','visible');
	_div_setStyleProperty(this.div_inside,'left',posx+'px');
	_div_setStyleProperty(this.div_inside,'top',posy+'px');
	_div_setStyleProperty(this.div_inside,'width',this.insideDivWidth+'px');
	_div_setStyleProperty(this.div_inside,'height',this.insideDivHeight+'px');
	
	_div_setStyleProperty(this.div_container,'opacity','1.0');

}

_clayer.prototype.desactive = function()
{
	//alert('desactive');

	clearInterval(this.desactiveTimer);
	this.desactiveTimer = null;

	_div_setStyleProperty(this.div_container,'display','none');
	_div_setStyleProperty(this.div_container,'visibility','hidden');
	
}

_clayer.prototype.disappear = function()
{
	this.setAnimate(true);
	
	if(this.desactiveTimer)
	{
		clearTimeout(this.desactiveTimer);
		this.desactiveTimer = null;
	}
	
	//alert('disappear');
	

	_div_setStyleProperty(this.div_container,'opacity','0.0');
	
	var that = this;
	
	this.desactiveTimer = setTimeout( "_clayer.instances["+this.id+"].desactive()",parseInt(this.animationDuration*1000)+200);
}


//-->
