/**
 * Fonction d'initialisation du système (attache les évènements).
 */
function initWebsite() {
	
	// Listing des produits, on ajoute une class css sur les produits qui apparaissent dans la deuxieme colonne (pair)
	$$('#list-product .product-wrap:nth-child(even)').setStyle('float','right');
	$$('#list-product .product-wrap:nth-child(2n+1)').setStyle('clear','both');

	$$('#list-news li:nth-child(even)').setStyle('float','right');

	// Listing des catégories, on ajoute une class css tous les 3 li de la liste (pour gérer la marge du li le plus à droite)
	$$('#list-categories li:nth-child(3n+3)').addClass('last');

	// Breadcrumb, changer le style du dernier lien
	$$('#breadcrumb a:last-child').addClass('last');

	nav();
	equalize();
	griding();	
	navAccount();
}

/**
 * Menu en accordeon
 * On désactive l'action du lien déclenchant les sous menu,
 * puis on déclare l'accordéon
 */
function nav() {
	// Ajout de span autour des liens (temporaire)
	$$('.navigation ul li:first-child').addClass('first');
	$$('.navigation ul li:last-child').addClass('last');
	
	$$('.sub-navigation').addEvent('click', function() { return false; });
	var sublistnav = $$('.sub-navigation').getNext('ul');
	
	var nav = new Accordion('.sub-navigation', sublistnav, {
		opacity: true,
		show: -1,
		alwaysHide: true,
		onActive: function(toggler, content) {
			toggler.addClass('toggled');
		},
		onBackground: function(toggler, content) {
			toggler.removeClass('toggled');
		}
	});
	
	
	// Ouverture du menu de gauche lorsqu'on est dans une rubirque ou produit
	var idCategoryToLoad = null;
	if (( $('catalog-product-view') || $('catalog-category-view')) && $$('.catalog-category-display')) {
		idCategoryToLoad = $$('.catalog-category-display').shift().getProperty('id').match(/^catalog-category-active-([0-9]+)/)[1];
		
		if( idCategoryToLoad != null ) {
			var numMenuCategoryElement = 0;
			$$('.navigation li.menu-category-level-1').each(function(item) {
				if(item.getProperty('id').match(/^menu-category-level-1-([0-9]+)/)[1] == idCategoryToLoad ) {
					nav.display(numMenuCategoryElement);
				}
				numMenuCategoryElement++;				
			});
		}
	}	

}

function griding() {
	$$('#document-document-list .datagrid col').each(function(item, i) {
		item.addClass('col-' + i);
	});
	$$('#document-document-list .datagrid thead th:first-child span').hide();
	$$('#document-document-list .datagrid thead th:last-child span').setStyle('float','none');
	$$('#document-document-list .datagrid td:nth-child(3)').addClass('document-name');

	if($('list-doc')) {
		$$('#list-doc .datagrid-toolbar:first-child').set('id','top-toolbar');
	}
	if($('tab-annuaire')) {
		$$('#tab-annuaire .datagrid-toolbar:first-child').set('id','top-toolbar');
	}

	$$('#datagrid-account .datagrid col').each(function(item, i) {
		item.addClass('col-annuaire-' + i);
	});
	$$('#datagrid-account .datagrid tr td:first-child').addClass('first');

}

function navAccount() {
	if($('profile-nav')) {
		var listel = $$('#profile-nav li');

		listel.each(function(fire) {
			var blocks = fire.getElement('dl');
			blocks.getElement('dd').setStyle('opacity','0');
			fire.addEvents({
					'mouseenter': function() {
						fire.addClass('hover');
						blocks.getElement('dd').fade('in');
					},
					'mouseleave': function() {
						fire.removeClass('hover');
						blocks.getElement('dd').fade('out');
					}
			});
		});
	}
	if($('inner-nav')) {
		var navel = $$('#inner-nav li');
		navel.each(function(fire) {
			fire.addEvents({
					'mouseenter': function() {
						fire.addClass('hover');
					},
					'mouseleave': function() {
						fire.removeClass('hover');
					}
			});
		});

	}
}


/**
 * Class Equalizer
 * Pour des éléments de même hauteur
 */
var Equalizer = new Class({
	initialize: function(elements) {
		this.elements = $$(elements);
	},
	equalize: function(hw) {
		if(!hw) { hw = 'height'; }
		var max = 0,
			prop = (typeof document.body.style.maxHeight != 'undefined' ? 'min-' : '') + hw; //ie6 ftl
			offset = 'offset' + hw.capitalize();
		this.elements.each(function(element,i) {
			var calc = element[offset];
			if(calc > max) { max = calc; }
		},this);
		this.elements.each(function(element,i) {
			element.setStyle(prop,max - (element[offset] - element.getStyle(hw).toInt()));
		});
		return max;
	}
});

function equalize() {
	var highlightsHome = new Equalizer('.highlights .content').equalize('height'); 
}

/**
 * Fonction qui permet de changer l'image principale en cliquant sur les miniatures (utilisée sur la fiche produit notamment)
 * #JSproductImages : div principal contenant les images
 * JSimage : classe sur les liens des miniatures (a.JSimage)
 * JSdisplayImage : id de l'image principale
 */
function fChangeImage() {
	var productImages = $$('#JSproductImages a.JSimage');
	if( productImages.length > 0 ) {
		productImages.each( function(el, index) {
			el.addEvent('click', function(e) {
				e.stop(); 
				var currentImageHref = el.getProperty('href');
				var currentImageSrc = el.getElement('img').getProperty('src').replace(/(thumb)/,"small");
				$('JSdisplayImage').getParent().setProperty('href',currentImageHref);
				$('JSdisplayImage').setProperty('src',currentImageSrc).setStyle('opacity','0');
				$('JSdisplayImage').set('tween', {duration:1000});
				$('JSdisplayImage').tween('opacity', [0, 1]);
			});
		});
	}
}

window.addEvent('domready', initWebsite);
