/* 	

	Options :	source	: 	'dom'		-> contenu d'un element dans le DOM (<a href="#domelementid">)	->	default : 'dom'
							'ajax'		-> contenu d'une page HTML (<a href="filename.html">)
							'img'		-> path d'une image
				target	: 	'domID'		-> id du DOM element ou se loadera le contenu					->	default : 'tabcontent'
				onLoad	: 	0			-> index du tab a loader au depart (0 pour le premier)			->	default : 0
				path	: 	false		-> dossier qui contient les fichiers (source ajax only)			->	default : false
				scroller:	false		-> scroll le content in-out (fade in-out si false)				->	default	: false	

	Usage :		window.addEvent('domready', function(){
					var tabber = new OM_MooTab({
						source: 'ajax',
						target: 'tabcontent',
						onLoad: 3,
						path: '/pages/'
					},'tabber');
				});
	
	Structure :	<div id="content1">				->	width/height : fixe
					<div id="content2">			->	overflow : hidden, width/height : pareil comme #content1
						<div id="scroller">		->	width : double de #content2 (si #content2 est 500px, #scroller doit etre 1000px)
						</div>
					</div>
				</div>

*/

var OM_MooTab = new Class({
	Implements: [Options, Events],
	options: {
		source: 'dom',
		target: 'scroller',
		onLoad: 0,
		path: false,
		scroller: false
	},
	initialize: function(options,ul){
		this.tabber = ul;
		this.setOptions(options);
		this.active = this.options.onLoad;
		window.addEvent('domready', this.domReady.bind(this));
	},
	domReady: function(){
		this.tabber = $(this.tabber);
		this.target = $(this.options.target);
		if($defined(this.tabber)){
			var tag = this.tabber.get('tag');
			if(tag!='ul'){
				this.tabber = this.tabber.getFirst('ul');
				if(!$defined(this.tabber)) return false;
			}
			if($defined(this.target)){
				this.loadDefault();
			}
			else
				return false;
		}
	},
	findTabs: function(ul){
		var tabs = ul.getChildren('li');
		tabs.each(function(item,index){
			var trigger = item.getFirst('a');
			if($defined(trigger)){
				trigger.addEvent('click', function(event){
					event.stop();
					this.active = index;
					var href = trigger.get('href');
					if($defined(href))
						this.loadContent(trigger.get('href'));
					else
						return false;
					this.setActiveTab(tabs);
				}.bind(this));
			}
			else return false;
		},this);
	},
	loadDefault: function(){
//		this.tabber.getChildren('li').each(function(item,index){
//			if(index==this.options.onLoad){
//				this.loadContent(item.getFirst('a').get('href'));
//				this.setActiveTab(this.tabber.getChildren('li'));
//			}
//		},this);
		this.findTabs(this.tabber);
	},
	loadContent: function(href){
		if($defined(href)){
			if(this.options.source=='dom')
				this.getContentDom(href);
			else if(this.options.source=='ajax')
				this.getContentAjax(href);
			else if(this.options.source=='img')
				this.getContentImg(href);
		}
		else return false;
	},
	getContentImg: function(source){
		var ext = ['jpg','jpeg','gif','png','bmp','tif','tiff'];
		var valid = false;
		ext.each(function(item){
			if(source.test(item)) valid = true;
		},this);
		if(valid){
			var content = '<img src="' + source + '" />';
			this.adoptContent(content);
		}
		else this.adoptContent('Extension invalide');
	},
	getContentDom: function(source){
		if(source.test('#')){
			var href = source.split('#');
			var id = href[1];
			var domobj = $(id);
			if($defined(domobj)){
				var content = domobj.get('html');
				if(content=='') content = 'Source trouvée mais ne contient aucun contenu.';
			}
			else var content = 'Source de contenu introuvable.';
		}
		else var content = 'Le lien n\'est pas un élément du DOM';
		this.adoptContent(content);
	},
	getContentAjax: function(source){
		var ext = ['asp','aspx','txt','html','htm'];
		var valid = false;
		ext.each(function(item){
			if(source.test(item)) valid = true;
		},this);
		if(valid){
			//if(this.options.path) source = this.options.path + source;
			if(this.options.path) source = this.options.path + source.replace('/expertise','');
			
			var data = new Request.HTML({url: source,
				onRequest: function(){
				}.bind(this),
				onSuccess: function(tree,elements,html,js){
					this.adoptContent(html);
					var tabobj = this;
					$$('.triggerTab').addEvent('click', function(event){
						event.stop();
						tabobj.active = this.get('rel');
						tabobj.loadContent(this.get('href'));
						tabobj.setActiveTab(tabobj.tabber.getChildren('li'));
					});
				}.bind(this),
				onFailure: function(){
					this.adoptContent('Une erreur est survenue');
				}.bind(this)
			}).get();
		}
		else this.adoptContent('Extension invalide');
	},
	adoptContent: function(html){
		if(this.options.scroller){
			// incomplet
			var scroll = new Fx.Scroll(this.options.target, {
				wait: false,
				duration: 1000,
				offset: {'x': 0, 'y': 0},
				transition: Fx.Transitions.Quad.easeIn
			});
		}
		else{
			this.target.fade('out').set('html','').setStyles({
				display: 'block',
				opacity: 0
			}).set('html', html).fade('in');
		}
	},
	setActiveTab: function(tabs){
		tabs.each(function(item,index){
			if(item.get('class').contains('active'))
				item.removeClass('active');
			if(index == this.active) item.addClass('active');
		},this);
	}
});
