// JavaScript Document

var JFD09 = new Class({
	bindedFunctions:{},
	elements:{},
	effects:{},
	panels:{},
	config:{},
	plugins:{},
	initialPage:"",
	
	initialize: function(config) {
		this.config = config;
		//MooTools.lang.setLanguage("en-US");
		this.changeLinksTargets();
		this.bindedFunctions();
		this.initElements();
		this.initEffects();
		this.initPlugins();
		this.verifyAnchor();
	},
	
	verifyAnchor: function() {
		var address = window.location.href;
		var toFind = this.config.base_url+'#/';
		var adresse_start = address.indexOf(toFind);
		if(adresse_start>-1) {
			this.initialPage = address.substring(toFind.length+adresse_start);
			var plugin = this.plugins[this.initialPage];
			if(plugin) {
				plugin.open();
			} else {
				var projectPath = 'projects/';
				var projectIndex = this.initialPage.indexOf(projectPath);
				if(projectIndex>-1) {
					var project = this.initialPage.substring(projectIndex+projectPath.length);
					this.plugins.projects.open($(project));
				}
			}
		}
	},
	
	initElements: function() {
		this.elements.overlay = new Element('div', {id:'overlay'}).inject($(document.body), 'inside');
	},
	
	initEffects: function() {
		this.effects.overlay = new Fx.Tween(this.elements.overlay, {duration:500});
		this.effects.overlay.set('opacity', 0);
	},
	
	initContactModule: function() {
		$('contactForm').action = "javascript://";
		this.plugins.formValidator = new FormValidator.Tips('contactForm', {
			errorPrefix: '<strong>Error : </strong>',
			onFormValidate : this.validateContactForm.bind(this),
			pointyTipOptions:{
				pointyOptions: {
					theme:'light', 
					themes:{
						light:{
							imgset:'light',
							bgColor:'#e56a1d',
							fgColor:'#fff',
							baseHref:this.config.base_url+'assets/medias/jfdPointyTheme/'
						}
					}
				}
			}
		});
	},
	
	validateContactForm: function(valid) {
		if(valid) {
			$('contactForm').set('send', {url: this.config.base_url+'sendContact/', method: 'post', onComplete:this.contactFormSended.bind(this)});
			$('contactForm').send(); //Sends the form.
			$('contactForm').fade('out');
		}
	},
	
	contactFormSended:function () {
		var contactFormTween =  new Fx.Tween($('contactForm'), {duration:400, onComplete:function() {
			var p = new Element('p', {text:'Your message had been sent. Thanks for your comment. I\'ll anwser you as soon as possible.'}).setStyle('opacity', 0).inject($('contactForm'), 'after');
				p.fade('in');
			$('contactForm').destroy();																			  
		}});
	},
		
	bindedFunctions: function() {
		this.bindedFunctions = {
			showOverlay		: this.showOverlay.bind(this),
			hideOverlay		: this.hideOverlay.bind(this),
			openLightBox	: this.lightboxOpened.bind(this),
			closeLightBox	: this.lightBoxClosed.bind(this)
		};
	},
	
	initPlugins: function() {
		if(config.activatedModules.contains('twitter')) {
			this.plugins.twitter = new JFDPanel({
				id				: 'twitter',
				buttonPosition	: 80,
				onShowOverlay 	: this.bindedFunctions.showOverlay,
				onHideOverlay	: this.bindedFunctions.hideOverlay
			});
		} if(config.activatedModules.contains('contact')) {
			this.plugins.contact = new JFDPanel({
				id				: 'contact',
				buttonPosition	: 10,
				onShowOverlay 	: this.bindedFunctions.showOverlay,
				onHideOverlay	: this.bindedFunctions.hideOverlay
			});
			this.initContactModule();				
		} if(config.activatedModules.contains('about')) {
			this.plugins.about = new JFDPanel({
				id				: 'about',
				buttonPosition	: 50,
				onShowOverlay 	: this.bindedFunctions.showOverlay,
				onHideOverlay	: this.bindedFunctions.hideOverlay
			});
		}
			
		this.plugins.projects = new JFDLightBox({
			className		: '.jfdLB',
			container		: $(document.body),
			startObject		: $('mask'),
			onOpen			: this.bindedFunctions.openLightBox,
			onClose			: this.bindedFunctions.closeLightBox
		});
	
		this.plugins.slide = new JFDSlide({
			next			: 'next',
			prev			: 'prev',
			info			: 'screenshotInfo',
			items			: $$('#handler h3'),
			container		: 'handler',
			size			: 373
		});
	},
	
	lightboxOpened: function(closeFunction) {
		this.showOverlay(102);
		this.elements.overlay.addEvent('click', closeFunction);
		this.elements.overlay.addEvent('click', this.removeOverlayClick.bind(this));
	},
	
	removeOverlayClick: function() {
		this.elements.overlay.removeEvents('click');
	},
	
	lightBoxClosed: function() {
		this.hideOverlay();
	},
	
	hideOverlay: function() {
		this.effects.overlay.removeEvents('click');
		this.effects.overlay.cancel().start('opacity', 0);
	},
	
	showOverlay: function(zindex, callOnClick) {
		if(callOnClick) this.elements.overlay.addEvent('click', callOnClick);
		if(zindex) this.effects.overlay.set('z-index', zindex);
		this.effects.overlay.cancel().start('opacity', 0.85);
	},
	
	changeLinksTargets: function() {
		$$('.externalLinks').set('target', '_blank');	
	}
});

window.addEvent('domready', function() { var jfd09 = new JFD09(config); });
