

var navigation = {
							
	inicialize: function(options){
		
		this.options = Object.extend({
			appearDuration: 500,
			repositionDuration: 800,
			repositionTransition: Fx.Transitions.Cubic.easeInOut,
			resizeDuration: 400,
			resizeTransition: Fx.Transitions.sineInOut,
			container: document.body,
			url: ''
		}, options || {});
		
		if( $('osmoaqua') ) {			
			$('osmoaqua').onclick = this.click.pass($('osmoaqua'), this);
		}
		
		//alert('ei');
		
		if( $('back_flash') ) {			
			$('back_flash').onclick = this.showFlash.pass($('back_flash'), this);
		}
				

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^section/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		
		
		this.fx = {
			appear: 	$('back_flash_cont').effect('opacity', { duration: this.options.appearDuration }),
			reposition: $('bottom_cont').effects({duration: this.options.repositionDuration, wait: false, transition: this.options.repositionTransition })
		};
		
		
		this.ajaxRequest = Class.empty;
		this.content_cont = $('content_cont');
		this.content = $('content');

	},
	
	
	showFlash: function () {
		
		// Hide the hide-flash option
		this.fx.appear.start(1,0);
		
		// Modify the bottom_cont top position
		var reposition = new Fx.Styles('bottom_cont', {
			duration:800,
			wait:false,
			transition: Fx.Transitions.Cubic.easeInOut
		});
		
		reposition.start({
			'top': 525
		});
		
		this.content_cont.setStyles({
			display: 'none' 
		});	
		
		// Erase the class of the button (now the section is not selected)
		this.anchors.each( function(item,index) {
			if ( item.hasClass('current') ) { item.removeClass('current'); }							
		}.bind(this));
		
		this.resizeMenu('less');
		
		return false;
		
	},
	
	
	click: function (element) {
		
		this.url = element.href;		
		
		if ( $('bottom_cont').getStyle('top').toInt() > 150  ) {
			
			this.fx.reposition.start({'top': 150}).addEvent('onComplete',function() {
				
				//$('flash_cont').effect('opacity', {duration: 1}).hide();
				this.fx.appear.start(0,1);
			
			}.bind(this));
		}
		
		//alert(this.content.getStyle('height').toInt());
		
		if ( isNaN(this.content_cont.getStyle('height').toInt()) ) {
			this.loadContents();
		}
		else {
		
			// Modify the bottom_cont top position
			var reposition = new Fx.Styles('content_cont', {
				duration:400,
				wait:false,
				transition: Fx.Transitions.Cubic.easeInOut
			});
			reposition.start({
				'height': 0
			}).addEvent('onComplete',function() {
				this.loadContents();				
			}.bind(this));
			
		}
		
		this.resizeMenu('more');
		
		this.changeActSectButton(element);

		return false;
		
	},
	
	
	/**
	 * It is necessary to hide the bottom of the flash
	 */
	resizeMenu: function(way) {
		
		// Modify the bottom_cont top position
		var reposition = new Fx.Styles('bottom_cont', {
			duration:800,
			wait:false,
			transition: Fx.Transitions.Cubic.easeInOut
		});
		
		if (way == 'less' )
			reposition.start({'height': 139});
		else 
			reposition.start({'height': 675});
		
	},
	
	
	/**
	 * change active section button
	 *
	 */	
	changeActSectButton: function(link) {
		
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^section/i) ) {
				if ( el.hasClass('current') ) { el.removeClass('current'); }
				if ( el.href == link ) { el.addClass('current'); }
			}
		}, this);
		
		return false;
	
	},
	
	
	loadContents: function () {
		
		// Insert the ajax folder to call an ajax controller
		url = this.url.replace(".net/", ".net/ajax/");
		url = url.replace(".eu/", ".eu/ajax/");
		
		this.url = url;		

		//*
		//* The simple way for an Ajax request, use onRequest/onComplete/onFailure
		//* to do add your own Ajax depended code.
		var showContents = this.showContents.bind(this);
		 
		var ajaxOptions = {
			method: 'get',
			update: $('content'),
			onComplete: showContents
			};
			
		this.ajaxRequest = new Ajax(this.url, ajaxOptions).request();
			
		return false;
	},
	
	
	showContents: function () {
		
		//alert(this.content.getStyle('height').toInt());
		
		this.content_cont.setStyle('height',0);
		this.content_cont.style.display = '';
		
		// Modify the bottom_cont top position
		var reposition = new Fx.Styles('content_cont', {
			duration:1100,
			wait:false,
			transition: Fx.Transitions.Cubic.easeInOut
		});
		
		var new_height = this.content.getStyle('height').toInt() + 52 + 40;
		
		reposition.start({
			'height': new_height
		}).addEvent('onComplete',function() {
			
		}.bind(this));
		
		this.scrollNav();
		
		// Inicialize forms to send it by AJAX
		forms.inicialize();
		
		return false;
		
	},
	
	scrollNav: function() {
		
		if ($('scrollToTop')) {
			$('scrollToTop').addEvent('click', function(e) {				
				new Event(e).stop();
		
				var scroller = new Fx.Scroll(window,{
					duration: 800, 
					transition: Fx.Transitions.Cubic.easeInOut
				});								
				scroller.toElement('top_cont');
			});
		}
	}
		
};
