/**
 * Make tabs on the movie sheet
 * 
 * @license MIT License
 * @copyright e-TF1 Web Agency
 * @author Yannick Croissant
 */

$(document.getElementsByTagName('html')[0]).addClass('hasjs');

DOMAssistant.DOMReady(function(){
	var h3 = $('details').getElementsByTagName("h3");
	var div = $('details').getElementsByTagName("div");
	// Add events
	var l = h3.length;
	for(i=0;i<l;i++) {
		h3[i].num = i;
		if(h3[i].innerText) var txt = h3[i].innerText;
		else var txt = h3[i].textContent;
		
		h3[i].hash = movie.clearText(txt);
		$(h3[i]).addEvent('click', function (){
			movie.activate(this.num,this.hash);
		});
	}
	
	// Set the active tab
	movie.activate();
});

movie = new Object();

/**
 * Set the active tab
 * @method
 * @param {string} hash
 */
movie.activate = function(num,hash) {
	if (!hash) {
		var hash = document.location.hash.replace(/[^a-z]/g, '');
		if(hash=='') var hash = 'photos';
		var num = movie.findByHash(hash).num;
	}
	
	movie.hideAllExcept(num);
	movie.desactivateAllExcept(num);
	movie.setHash(hash);
};

/**
 * Hide all content zones, except the given in parameter
 * @method
 * @param {integer} except
 */
movie.hideAllExcept = function(except) {
	var div = $('details').getElementsByTagName("div");
	var l = div.length;
	
	for(var i=0;i<l;i++) {
		div[i].style.display = 'none';
	}
			
	if(except != null) {
		div[except].style.display = 'block';
		
		var patch = 0;
		if (window.ActiveXObject && !window.XMLHttpRequest) patch = 34;
		
		$('details').style.paddingBottom = div[except].offsetHeight+patch+'px';
	}
};

/**
 * Remove active class on all tabs, except the given in parameter
 * @method
 * @param {integer} except
 */
movie.desactivateAllExcept = function(except) {
	var h3 = $('details').getElementsByTagName("h3");
	var l = h3.length;
	
	for(var i=0;i<l;i++) {
		$(h3[i]).removeClass('active');
	}
	
	if(except != null) {
		h3[except].addClass('active');
	}
};

/**
 * Set the current hash
 * @method
 * @param {string} hash
 */
movie.setHash = function(hash) {
	document.location.hash = hash;
};

/**
 * LowerCase a string and only keep alpha characters
 * @method
 * @param {string} txt
 * @return {string}
 */
movie.clearText = function(txt) {
	return txt.toLowerCase().replace(/[^a-z]/g,'');
};

/**
 * Find a title by his hash
 * @method
 * @param {string} txt
 * @return {Object}
 */
movie.findByHash = function(hash) {
	var h3 = $('details').getElementsByTagName("h3");
	var l = h3.length;
	
	for(var i=0;i<l;i++) {
		if(h3[i].hash == hash) return h3[i];
	}
};