
/**
 * SimpleTabs - Unobtrusive Tabs with Ajax
 *
 * @example
 *
 *	var tabs = new SimpleTabs($('tab-element'), {
 * 		selector: 'h2.tab-tab'
 *	});
 *
 * @version		1.0
 *
 * @license		MIT License
 * @author		Harald Kirschner <mail [at] digitarald.de>
 * @copyright	2007 Author
 */
var SimpleTabs = new Class({

	Implements: [Events, Options],

	/**
	 * Options
	 */
	options: {
		show: 0,
		selector: '.tab-tab',
		classWrapper: 'tab-wrapper',
		classMenu: 'tab-menu',
		classContainer: 'tab-container',
		onSelect: function(toggle, container, index) {
			toggle.addClass('tab-selected');
			container.setStyle('display', '');
			launchGmap();
		},
		onDeselect: function(toggle, container, index) {
			toggle.removeClass('tab-selected');
			container.setStyle('display', 'none');
		},
		onRequest: function(toggle, container, index) {
			container.addClass('tab-ajax-loading');
		},
		onComplete: function(toggle, container, index) {
			container.removeClass('tab-ajax-loading');
		},
		onFailure: function(toggle, container, index) {
			container.removeClass('tab-ajax-loading');
		},
		onAdded: Class.empty,
		getContent: null,
		ajaxOptions: {},
		cache: true
	},

	/**
	 * Constructor
	 *
	 * @param {Element} The parent Element that holds the tab elements
	 * @param {Object} Options
	 */
	initialize: function(element, options) {
		this.element = $(element);
		this.setOptions(options);
		this.selected = null;
		this.build();
	},

	build: function() {
		this.tabs = [];
		this.menu = new Element('ul', {'class': this.options.classMenu});
		this.wrapper = new Element('div', {'class': this.options.classWrapper});

		this.element.getElements(this.options.selector).each(function(el) {
			var content = el.get('href') || (this.options.getContent ? this.options.getContent.call(this, el) : el.getNext());
			this.addTab(el.innerHTML, el.title || el.innerHTML, content);
		}, this);
		this.element.empty().adopt(this.menu, this.wrapper);

		if (this.tabs.length) this.select(this.options.show);
	},

	/**
	 * Add a new tab at the end of the tab menu
	 *
	 * @param {String} inner Text
	 * @param {String} Title
	 * @param {Element|String} Content Element or URL for Ajax
	 */
	addTab: function(text, title, content) {
		var grab = $(content);
		var container = (grab || new Element('div'))
			.setStyle('display', 'none')
			.addClass(this.options.classContainer)
			.inject(this.wrapper);
		var pos = this.tabs.length;
		var evt = (this.options.hover) ? 'mouseenter' : 'click';
		var tab = {
			container: container,
			toggle: new Element('li').grab(new Element('a', {
				href: '#',
				title: title
			}).grab(
				new Element('span', {html: text})
			)).addEvent(evt, this.onClick.bindWithEvent(this, [pos])).inject(this.menu)
		};
		if (!grab && $type(content) == 'string') tab.url = content;
		this.tabs.push(tab);
		return this.fireEvent('onAdded', [tab.toggle, tab.container, pos]);
	},

	onClick: function(evt, index) {
		this.select(index);
		return false;
	},

	/**
	 * Select the tab via tab-index
	 *
	 * @param {Number} Tab-index
	 */
	select: function(index) {
		if (this.selected === index || !this.tabs[index]) return this;
		if (this.ajax) this.ajax.cancel().removeEvents();
		var tab = this.tabs[index];
		var params = [tab.toggle, tab.container, index];
		if (this.selected !== null) {
			var current = this.tabs[this.selected];
			if (this.ajax && this.ajax.running) this.ajax.cancel();
			params.extend([current.toggle, current.container, this.selected]);
			this.fireEvent('onDeselect', [current.toggle, current.container, this.selected]);
		}
		this.fireEvent('onSelect', params);
		if (tab.url && (!tab.loaded || !this.options.cache)) {
			this.ajax = this.ajax || new Request.HTML();
			this.ajax.setOptions({
				url: tab.url,
				method: 'get',
				update: tab.container,
				onFailure: this.fireEvent.pass(['onFailure', params], this),
				onComplete: function(resp) {
					tab.loaded = true;
					this.fireEvent('onComplete', params);
				}.bind(this)
			}).setOptions(this.options.ajaxOptions);
			this.ajax.send();
			this.fireEvent('onRequest', params);
		}
		this.selected = index;
		return this;
	}

});

/* Mootools Tabs */
window.addEvent('domready', function() {
 
	/**
	 * Element with id 'demo-block' is the container and all h4-elements
	 * inside are fetched as tab headers. The following elements are their
	 * content.
	 */
	var tabs = new SimpleTabs('sitraFicheContent', {
		selector: 'h4'
	});	

});


/**********************************************************************************************************
* @function : GoogleMapLoad
* @aim      	 : Fonction principale � appeller au chargement de la page
* @param     : void
* @return     : void
***********************************************************************************************************/
function GoogleMapLoad(x,y) {
	 if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('mapGeolocalisation'));
		map.setMapType(G_HYBRID_MAP);
		map.setCenter(new GLatLng(x, y), 10);
		map.addControl(new GSmallMapControl());
		var marker = new GMarker(new GLatLng(x, y));
		map.addOverlay(marker);
		cacher();
	} 
	else 
	{
		cacher();
		alert("Localisation impossible");
	}
}

function cacher(){

	$$('div.hidden div.popupGrise').setStyle('display','none') ;
	$$('div.hidden div.popupGrise').setStyle('visibility','visible') ;
	
	height_container_default = $('conteneurPopup').getCoordinates().height ;
	$$('ul.menuPopup li').each(function(el) {
		
		el.setStyle('cursor', 'pointer');
		el.addEvent('click', function() {
			
			// On cache toutes les divs "popup"...
			$$('div.hidden div.popupGrise').setStyle('display','none') ;

			if ( $$('div.'+this.className) != '' ) {
				// ... et on affiche la bonne
				$$('div.'+this.className).setStyle('display','block') ;

				// Variables n�cessaires au calculs
				var top_li = this.getCoordinates().top ;
				var height_div = $$('div.'+this.className)[0].getCoordinates().height ;
				var height_container = $('conteneurPopup').getCoordinates().height ;
				
				
				// On met � jour la taille de la div #conteneurPopup
				
				if ( height_container_default < ( height_div + top_li ) )
					$('conteneurPopup').setStyle('height', height_div + top_li) ;
				else
					$('conteneurPopup').setStyle('height', height_container_default) ;
	
				// On met la bonne position de la div
				$$('div.'+this.className).setStyle('top', (top_li - $('conteneurPopup').getCoordinates().top)  ) ;
			}
		}) ;
	});
	
}

