
/**************************************************************

	Script		: MultiBox
	Version		: 1.3.1
	Authors		: Samuel Birch
	Desc		: Supports jpg, gif, png, flash, flv, mov, wmv, mp3, html, iframe
	Licence		: Open Source MIT Licence

**************************************************************/

var MultiBox = new Class({
	
	getOptions: function(){
		return {
			initialWidth: 250,
			initialHeight: 250,
			container: document.body,
			useOverlay: true,
			contentColor: '#FFF',
			showNumbers: false,
			waitDuration: 2000,
			offset: {x:0, y:25},
			fixedTop: false,
			onOpen: Class.empty,
			onClose: Class.empty,
			openFromLink: true
		};
	},

	initialize: function(className, options){
		this.setOptions(this.getOptions(), options);
		this.openClosePos = {};
		this.timer = 0;
		this.contentToLoad = {};
		this.index = 0;
		this.opened = false;
		this.contentObj = {};
		this.containerDefaults = {};

		this.content = $$('.'+className);

    this.overlay = new Element('div', {'id': 'lbOverlay'}).setStyles({'height': window.getScrollHeight(),'background-color': '#ffffff','opacity':0}).addEvent('click',this.close.bind(this));
		this.container = new Element('div').setProperties({id: 'MultiBoxContainer'}).addClass('MultiBoxContainer');
		this.iframe = new Element('iframe').setProperties({
			'id': 'multiBoxIframe',
			'name': 'multiBoxIframe',
			'frameborder': 0,
			'scrolling': 'no'
		}).setStyles({
			'position': 'absolute',
			'top': 0,
			'left': 0,
			'width': '100%',
			'height': '100%',
      'display': 'none'
		});
		this.box = new Element('div').setProperties({id: 'MultiBoxContent'}).addClass('MultiBoxContent');
    this.contentContainer = new Element('div').setProperties({id: 'MultiBoxContentContainer'}).setStyle('opacity',0);
		this.closeButton = new Element('div').addClass('MultiBoxClose').setStyle('opacity',0);

		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));
			}
		}, this);
		
		this.containerEffects = new Fx.Styles(this.container, {duration: 400, transition: Fx.Transitions.sineInOut});
    this.overlayEffect = new Fx.Style(this.overlay, 'opacity', {duration: 400, transition: Fx.Transitions.sineInOut});
    this.closeButtonEffect = new Fx.Style(this.closeButton, 'opacity', {duration: 400, transition: Fx.Transitions.sineInOut});
		this.reset();
	},
	
	setContentType: function(link){
		var str = link.href.substr(link.href.lastIndexOf('.')+1).toLowerCase();
		var contentOptions = {};
		if($chk(link.rel)){
			var optArr = link.rel.split(',');
			optArr.each(function(el){
				var ta = el.split(':');
				contentOptions[ta[0]] = ta[1];
			});
		}

		this.contentObj = {};
		this.contentObj.url = link.href;
		this.contentObj.xH = 0;

    this.type = 'ajax';
	},
	
	reset: function(){
		this.container.setStyles({
			'opacity': 0
		});
		this.opened = false;
	},
	
	getOpenClosePos: function(el){
		if (this.options.openFromLink) {
      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
      };
		}else{
			if(this.options.fixedTop){
				var top = this.options.fixedTop;
			}else{
				var top = ((window.getHeight()/2)-(this.options.initialHeight/2)-this.container.getStyle('border').toInt())+this.options.offset.y;
			}
			this.openClosePos = {
				width: this.options.initialWidth,
				height: this.options.initialHeight,
				top: top,
				left: ((window.getWidth()/2)-(this.options.initialWidth/2)-this.container.getStyle('border').toInt())+this.options.offset.x
			};
		}
		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;

      this.container.injectInside(this.options.container);
      this.iframe.injectInside(this.container).setStyle('display','block');
      this.box.injectInside(this.container);
      this.contentContainer.injectInside(this.box);
      this.closeButton.injectInside(this.container).addEvent('click', this.close.bind(this));

      if(this.options.useOverlay){
        this.overlay.injectInside(document.body);
        this.overlayEffect.start(0.5);
      }
			
			this.container.setStyles(this.getOpenClosePos(el));
			this.container.setStyles({
				opacity: 0
			});
			
			if(this.options.fixedTop){
				var top = this.options.fixedTop;
			}else{
				var top = ((window.getHeight()/2)-(this.options.initialHeight/2)-this.container.getStyle('border').toInt())+this.options.offset.y;
			}
			
			this.containerEffects.start({
				width: this.options.initialWidth,
				height: this.options.initialHeight,
				top: top,
				left: ((window.getWidth()/2)-(this.options.initialWidth/2)-this.container.getStyle('border').toInt())+this.options.offset.x,
				opacity: 1
			});
			
			this.load(this.index);
		
		}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);
		}
	},

  getContentObjSize: function() {
    this.contentObj.width = this.contentContainer.getFirst().getSize().size.x;
    this.contentObj.height = this.contentContainer.getFirst().getSize().size.y;
    this.resize();
  },

	getContent: function(index){
		this.setContentType(this.content[index]);
    var AjaxContent = new Ajax(this.contentObj.url, {
      method: 'get',
      update: 'MultiBoxContentContainer',
      evalScripts: true,
      autoCancel: true,
      onComplete: this.getContentObjSize.bind(this)
    }).request();
	},
	
	close: function(){
		this.hideContent();
    this.overlayEffect.stop();
		this.containerEffects.stop();
		this.zoomOut.bind(this).delay(500);
		this.options.onClose();
	},
	
	zoomOut: function(){
		this.containerEffects.start({
			width: this.openClosePos.width,
			height: this.openClosePos.height,
			top: this.openClosePos.top,
			left: this.openClosePos.left,
			opacity: 0
		});
    if(this.options.useOverlay){
      this.overlayEffect.start(0);
    }
		this.reset.bind(this).delay(500);
	},
	
	load: function(index){
		this.box.addClass('MultiBoxLoading');
		this.getContent(index);
	},

	resize: function(){
		if (this.options.fixedTop) {
			var top = this.options.fixedTop;
		} else {
			var top = ((window.getHeight() / 2) - ((Number(this.contentObj.height) + this.contentObj.xH) / 2) - this.container.getStyle('border').toInt() + window.getScrollTop()) + this.options.offset.y;
		}
		var left = ((window.getWidth() / 2) - (this.contentObj.width / 2) - this.container.getStyle('border').toInt()) + this.options.offset.x;
		if (top < 0) {
			top = 0
		}
		if (left < 0) {
			left = 0
		}

		this.containerEffects.stop();
		this.containerEffects.start({
			width: this.contentObj.width,
			height: Number(this.contentObj.height) + this.contentObj.xH,
			top: top,
			left: left,
			opacity: 1
		});
		this.timer = this.showContent.bind(this).delay(500);
	},
	
	showContent: function(){
		this.box.removeClass('MultiBoxLoading');
		this.contentEffects = new Fx.Styles(this.contentContainer, {duration: 500, transition: Fx.Transitions.linear});
		this.contentEffects.start({
			opacity: 1
		});
    this.closeButtonEffect.start(1);
	},
	
	hideContent: function(){
		this.box.addClass('MultiBoxLoading');
		this.contentEffects.start({
			opacity: 0
		});
    this.closeButtonEffect.start(0);
		this.removeContent.bind(this).delay(1000);
	},
	
	removeContent: function(){
		if(this.container){
			this.container.remove();	
		}
    if(this.options.useOverlay){
      this.overlay.remove();
    }
	}
});
MultiBox.implement(new Options);
MultiBox.implement(new Events);

window.addEvent('domready', function() {
  mb_tools = new MultiBox('mb_tool');
});

openContact = function() {
  mb_tools.open($E('.resultat_fiche_contact a'));
};

/*************************************************************/

