
var Popin = {

	init: function (options) {
		// init default options
		this.options = Object.extend({
			resizeDuration: 400,	// Duration of height and width resizing (ms)
			initialWidth: 200,		// Initial width of the box (px)
			initialHeight: 200,		// Initial height of the box (px)
			defaultWidth: 400,		// Default width of the box (px)
			defaultHeight: 400,	// Default height of the box (px)
			animateCaption: true	// Enable/Disable caption animation
		}, options || {});
		this.anchors = [];

		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			center: this.center.effects({duration: 500, transition: Fx.Transitions.sineInOut, onComplete: nextEffect})
		};
	},

	click: function(link) {
	
     		return this.open (link.href, link.title, link.rel);

	},
	open: function(sLinkHref, sLinkTitle, sLinkRel) {
		this.href = sLinkHref;
		this.title = sLinkTitle;
		this.rel = sLinkRel;
		this.position();
		
		this.setup();
		this.top = Window.getScrollTop() + (Window.getHeight() / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.start(0.8);
		this.step = 1;
		this.center.setStyle('background','#fff url(/images/loading.gif) no-repeat center');
		this.fx.center.start({'height': [this.options.contentsHeight]});
	},

	setup: function(){
		var aDim = this.rel.match(/[0-9]+/g);
		this.options.contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : this.options.defaultWidth;
		this.options.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : this.options.defaultHeight;

	},

	position: function(){
	height = window.getScrollHeight();
    this.overlay.setStyles({'top': 0+'px', 'height': height+'px'});
	},


	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.fx.center.start({'width': [this.options.contentsWidth], 'marginLeft': [this.options.contentsWidth/-2]});
			break;
			this.step++;
		case 2:
			this.center.setStyle('background','#fff');
			this.center.setStyle('height','auto');
			html = '<div align="right"><a href="#" onClick="Popin.close(); return false" >Inchide</a></div>';
			html += document.getElementById(this.href).innerHTML;
			this.abc = document.getElementById(this.href).innerHTML;
			document.getElementById(this.href).innerHTML = '';
			this.center.setHTML(html);
			this.step++;
		}
	},

	close: function(){
		this.fx.overlay.start(0);
		this.center.innerHTML = '';
		document.getElementById(this.href).innerHTML = this.abc;
		this.center.setStyle('display','none');
		this.center.display = 'none';
		if (document.getElementById('categorie') && document.getElementById('produse'))
		{
			document.getElementById('categorie').style.display = '';
			document.getElementById('produse').style.display = '';
		}
		return false;

	}

};

window.addEvent('domready', Popin.init.bind(Popin));