/**
 * @author plema
 */
var MapLei = new Class({

	getOptions: function(){
		return {
			initialWidth: 800,
			initialHeight: 600,
			container: document.body,
			useOverlay: true,
			linkXHR: false,
			classType: false,
			contentDetails: false,
			onOpen: Class.empty,
			onClose: Class.empty
		};
	},

	initialize: function(className,options){

		this.setOptions(this.getOptions(), options);
		this.opened=false;
		this.openClosePos = {};

		if(this.options.useOverlay){
			this.overlay = new Overlay({container: this.options.container, onClick:this.close.bind(this)});
		}

		this.content = $$('.'+className);

		this.container = new Element('div').addClass('MapLeiContainer').injectInside(this.options.container);
		this.box = new Element('div').addClass(this.options.classType).injectInside(this.container);

		this.closeButton = new Element('div').addClass('MapLeiClose').injectInside(this.container).addEvent('click', this.close.bind(this));

		this.content.each(function(el,i){
			el.index = i;
			el.addEvent('click', function(e){
				new Event(e).stop();
				this.open(el);
			}.bind(this));
			if(el.href.indexOf('#') > -1){
				el.content = $(el.href.substr(el.href.indexOf('#')+1));
				if(el.content){el.content.setStyle('display','none');}
			}
		}, this);

		this.containerEffects = new Fx.Styles(this.container, {duration: 400, transition: Fx.Transitions.sineInOut});
		this.reset();
	},

	reset: function(){
		this.container.setStyles({
			'opacity': 0,
			'display': 'none'
		});
		this.opened=false;
	},

	zoomOut: function(){
		this.containerEffects.start({
			width: this.openClosePos.width,
			height: this.openClosePos.height,
			top: this.openClosePos.top,
			left: this.openClosePos.left,
			opacity: 0
		});
		this.reset.bind(this).delay(500);
	},

	getOpenClosePos: function(el){
		if(el.getFirst()){
			var w = el.getFirst().getCoordinates().width-(this.container.getStyle('border').toInt()*2);
			if(w < 0){w = 0}
			var h = el.getFirst().getCoordinates().height-(this.container.getStyle('border').toInt()*2);
			if(h < 0){h = 0}
			this.openClosePos = {
				width: w,
				height: h,
				top: el.getFirst().getCoordinates().top,
				left: el.getFirst().getCoordinates().left
			};
		}else{
			var w = el.getCoordinates().width-(this.container.getStyle('border').toInt()*2);
			if(w < 0){w = 0}
			var h = el.getCoordinates().height-(this.container.getStyle('border').toInt()*2);
			if(h < 0){h = 0}
			this.openClosePos = {
				width: w,
				height: h,
				top: el.getCoordinates().top,
				left: el.getCoordinates().left
			};
		}
		return this.openClosePos;
	},

	open: function(el){

		this.options.onOpen();

		this.index = this.content.indexOf(el);

		this.openId = el.getProperty('id');

		if(!this.opened){
			this.opened = true;

			if(this.options.useOverlay){
				this.overlay.show();
			}

			this.container.setStyles(this.getOpenClosePos(el));
			this.container.setStyles({
				opacity: 0,
				display: 'block'
			});

			this.containerEffects.start({
				width: this.options.initialWidth,
				height: this.options.initialHeight,
				top: (window.getHeight()/2)-(this.options.initialHeight/2)-this.container.getStyle('border').toInt()+window.getScrollTop(),
				left: (window.getWidth()/2)-(this.options.initialWidth/2)-this.container.getStyle('border').toInt(),
				opacity: [0, 1]
			});

			this.load.pass(this.index,this).delay(1000);

		}else{

			this.getOpenClosePos(this.content[this.index]);
			this.timer = this.hideContent.bind(this).delay(500);
			this.timer = this.load.pass(this.index, this).delay(1100);

		}

	},

	close: function(){
		if(this.options.useOverlay){
			this.overlay.hide();
		}
		this.hideContent();
		this.containerEffects.stop();
		this.zoomOut.bind(this).delay(500);
		this.options.onClose();
	},

	load: function(index){
		this.box.addClass('MapLeiLoading');
		var reg=new RegExp("&", "g");
		var link_lei=this.content[index].href.replace(reg,"#81#lei;");
		this.contentContainer = new Element('div').setProperties({id: 'MapLeiContentContainer'}).injectInside(this.box);
		new Ajax(this.options.linkXHR+'/file_contents.php', {
			method: 'post',
			postBody:'link='+link_lei,
			update: 'MapLeiContentContainer',
			evalScripts: true,
			autoCancel: false
		}).request();
	},

	hideContent: function(){
		this.box.addClass('MapLeiLoading');
		this.removeContent.bind(this).delay(500);
	},

	removeContent: function(){
		if($('MapLeiContentContainer')){
			$('MapLeiContentContainer').remove();
		}
	}
});
MapLei.implement(new Options);
MapLei.implement(new Events);