/*
popin - The pop-in window, based on prototype and script.aculo.us.

Par David Spaeth pour brocelia, 2009

VERSION 1.0.0

 */

if (!window.PopIn){
	var PopIn = new Object();
	window.PopIn = PopIn;

PopIn.Methods = {

		_baseOptions : {

	width :800, // Default width in px
	height :600, // Default height in px
	overlayOpacity :.65, // Default overlay opacity
	overlayDuration :.25, // Default overlay fade in/out duration in
	// seconds
	slideDownDuration :.5, // Default Modalbox appear slide down effect in
	// seconds
	slideUpDuration :.5, // Default Modalbox hiding slide up effect in
	// seconds
	resizeDuration :.25, // Default resize duration seconds
	transitions :true, // Toggles transition effects. Transitions are
	// enabled by default
	closeString :"Fermer", // Default title attribute for close window link
	closeValue :"&times;",
	maskColor :"black",
	hideCallBack:Prototype.emptyFunction,
	contentColor :'#F9F9F9',
	headColor :'#DDD',
	title :'Popup',
	loadContent:'chargement en cours...',
	contentLoaded :Prototype.emptyFunction,
	autoFocusing :true
	// Toggles auto-focusing for form elements. Disable for long text pages.
},// options
_options :new Object,
_initialized :false,

_setOptions : function(options) {
	Object.extend(this._options, options || {});
},// _setOptions

_init : function(options) {

	if (this._initialized)
		return;
	Object.extend(this._options, this._baseOptions);
	this._setOptions(options);

	var body = $(document.body);

	this.mask = new Element("div", {
		id :"MP_MASK",
		opacity :"0"
	});
	
	var maskStyle = {
			height :'100%',
			width :Prototype.Browser.IE ? '101.6%' : '100%',
					position :Prototype.Browser.IE ? 'absolute' : 'fixed',
							top :'0',
							left :'0',
							zIndex :'9999',
							margin :'auto'

		};
	this.mask.setStyle( maskStyle);

	this.MPParent = new Element("div", {
		id :"MP_PARENT",
		style :"display: none"
	})

			
	this.MPParent.setStyle( {
		zIndex :'10000',
		backgroundColor :this._options.contentColor
		|| body.getStyle("backgroundColor"),
		border :'1px solid black'
	});

	

	this.MPHead = new Element("div", {
		id :"MP_HEAD"
	}).update(this.MPTitle = new Element("span", {
		id :'MP_TITLE'
	}).update(this._options.title))

	this.MPHead.setStyle( {
		backgroundColor :this._options.headColor,
		borderBottom :'2px solid #CCC',
		margin :'0',
		padding :'0',
		height :'20px'

	});

	this.MPTitle.setStyle( {
		fontFamily :'"Lucida Grande",Arial,sans-serif',
		fontSize :"100%",
		fontSizeAdjust :'none',
		fontStretch :'normal',
		fontStyle :'normal',
		fontVariant :'normal',
		fontWeight :'bold',
		lineHeight :'normal',
		margin :'0',
		marginLeft :'12px',
		textAlign :'left',
		textShadow :'0 1px 0 #FFFFFF',
		marginBottom :'2px'

	});

	this.MPLoader = new Element('div',{id:'MPLOADER'}).setStyle({
		position :'absolute',
		top:"24px"
	});


	if ( parent.frames[0] != undefined)
	{
		var d =parent.frames[0].document;
		this.maskFrame = $(d.createElement("div"));
		this.maskFrame.setStyle(maskStyle);
		this.maskFrame.id="maskFrame";
		
		//??? strange
		this.maskFrame.getInlineOpacity = function(){  
			 return $(this).style.opacity || '';
		}  
		

		d.body.insertBefore(this.maskFrame,	d.body.firstChild);
	
	}

	this.MPParent.insert( {
		'top' :this.MPHead
	});
	this.MPParent.insert( {
		'top' :this.MPLoader
	});
	this.MPClose = new Element("a", {
		id :"MP_CLOSE",
		title :this._options.closeString,
		href :"#"
	}).update(this._options.closeValue);

	this.MPClose.setStyle( {

		fontWeight :'bold',
		position :'absolute',
		right :'5px',
		textDecoration :'none',
		top :'0'
	});

	this.MPHead.insert( {
		'bottom' :this.MPClose
	});

	body.insert( {
		'top' :this.MPParent
	});
	body.insert( {
		'top' :this.mask
	});

	/*Event.observe(this.MPIframe, 'load', function(e) {
		if (this.src.length == 0)
			return;
		PopIn.MPLoader.hide();
		
		 PopIn._options.contentLoaded.call(PopIn.MPIframe,e);
		
		
	});*/
	this._closePopupEvent = this._closePopup.bindAsEventListener(this);
	this.MPClose.observe('click', this._closePopupEvent);
	this.initScrollX = window.pageXOffset || document.body.scrollLeft
	|| document.documentElement.scrollLeft;
	this.initScrollY = window.pageYOffset || document.body.scrollTop
	|| document.documentElement.scrollTop;
	this._initialized = true;

},

show : function(content, options) {

	this._init(options);

	this.content = content;
	this._appear();

},

_closePopup : function(e) {
	e.stop();
	if (this._options.transitions)
		Effect.SlideUp(this.MPParent, {
			duration :this._options.slideUpDuration,
			transition :Effect.Transitions.sinoidal,
			afterFinish :this._deinit.bind(this)
		});
	else {
		$(this.MBParent).hide();
		this._deinit();
	}

},

hide: function(callback){
	callback = callback || {};
	
	callback.contentHided = callback.contentHided || Prototype.emptyFunction;
	callback.beforeHide = callback.beforeHide || Prototype.emptyFunction;
	callback.beforeHide();
	this._deinit(callback.contentHided);
	
},
_deinit : function(callbackFunction) {
	
	callbackFunction = callbackFunction || this._options.hideCallBack;
	
if(!Object.isFunction(callbackFunction))
	callbackFunction =  this._options.hideCallBack;

	if (this._options.transitions) {
		Effect.toggle(this.mask, 'appear', {
			duration :this._options.overlayDuration,
			afterFinish :function(){
		
			this._removeElements.call(this);
			
			if(!this.maskFrame  )
				callbackFunction();
		}.bind(this)
		});
		if(this.maskFrame)
			Effect.toggle(this.maskFrame, 'appear', {
				duration :this._options.overlayDuration,
				afterFinish: callbackFunction
			});
	} else {
		if(this.maskFrame)
			this.maskFrame.hide();
		this.mask.hide();
		this._removeElements();
		callbackFunction();
	}

},

_removeElements : function() {

	this.mask.remove();
	
	if(this.maskFrame)
		this.maskFrame.remove();

	this.MPParent.remove();

	if (Prototype.Browser.IE && !navigator.appVersion.match(/\b7.0\b/)) {
		this._prepareIE("", "");
		window.scrollTo(this.initScrollX, this.initScrollY);
	}

	this._setOptions(this._baseOptions);
	this._initialized = false;
},

_appear : function() {
	if (Prototype.Browser.IE && !navigator.appVersion.match(/\b7.0\b/)) { // Preparing
		// IE 6
		window.scrollTo(0, 0);
		this._prepareIE("100%", "hidden");
	}

	this._setWidth();
	this._setPosition();
	if (this._options.transitions) {
		this.mask.setStyle( {
			opacity :0,
			backgroundColor :this._options.maskColor
		});

		if(this.maskFrame)
			this.maskFrame.setStyle( {
				opacity :0,
				backgroundColor :this._options.maskColor
			});

		new Effect.Fade(this.mask, {
			from :0,
			to :this._options.overlayOpacity,
			duration :this._options.overlayDuration,
			afterFinish : function() {
			new Effect.SlideDown(this.MPParent, {
				duration :this._options.slideDownDuration,
				transition :Effect.Transitions.sinoidal,
				afterFinish : function() {
				this._setPosition();

				this.loadContent();
			}.bind(this)
			});
		}.bind(this)


		});
		if(this.maskFrame)
			new Effect.Fade(this.maskFrame, {
				from :0,
				to :this._options.overlayOpacity,
				duration :this._options.overlayDuration});


	} else {
		this.mask.setStyle( {
			opacity :this._options.overlayOpacity
		});
		if(this.maskFrame)
			this.maskFrame.setStyle( {
				opacity :this._options.overlayOpacity
			});

		$(this.MPParent).show();
		this._setPosition();
		this.loadContent();
	}

},

_prepareIE : function(height, overflow) {
	// TODO: Passer avec une iframe
	$$('html, body').invoke('setStyle', {
		width :height,
		height :height,
		overflow :overflow
	}); // IE requires width and height set to 100% and overflow hidden
	$$("select").invoke('setStyle', {
		'visibility' :overflow
	}); // Toggle visibility for all selects in the common document
},

_setPosition : function() {
	var left = Math.round((Element.getWidth(document.body) - Element
			.getWidth(this.MPParent)) / 2);


	this.MPParent.setStyle(
			{
				left :left+ "px",
				position :"absolute",
				top:2
			});
	//this.MPLoader.setStyle({left:((this._options.width-left)/2)+ "px"})
},

loadContent : function() {
	this.MPLoader.update(this.content);
	
	//this.MPIframe.setAttribute('src', this.url);

},

_setWidth : function() { //Set size
	$(this.MPParent).setStyle( {
		width :this._options.width + "px",
		height :this._options.height + "px"
	});

}

}
//PopIn
Object.extend(PopIn, PopIn.Methods);
}