//gestion liens sur homepage
$.fn.wait = function(time, type) {
	time = time || 500;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};

var Page = {
	_sectionHeight: 0, // Height of the entire UL in #marques
	_speed: 2500, // Default value of animation speed
	_topSectionHeight: 283, // Assume that the header section is 283px height
	_bodyHeight: -1, // Height of the available space in the BODY section (according to the screen height)
	
	_sectionWidth: 0, // Width of the entire UL in #marques
	_rightSectionWidth: 880, // Assume that the right of the section is 880px width
	
	/* Start animation : scrolls brands on the homepage */
	runIt: function(_moveTo, _maxScroll) {
		if (_moveTo == 'bottom')
			$("#marques > .list > ul").animate({top:'-='+_maxScroll},( _maxScroll * this._speed / 500 ));
		else if (_moveTo == 'top')
			$("#marques > .list > ul").animate({top:'+='+_maxScroll},( _maxScroll * this._speed / 500 ));
	},
	
	/* Init function to start and stop animation */
	slideBrands: function() {
		if ($('#marques > div.bottom') && $("#marques > .list > ul")[0]) {
			this._sectionHeight = $("#marques > .list > ul")[0].getElementsByTagName('li').length * 16;
			this._speed = 5000;
			
			if (!parseInt($("#marques > .list > ul")[0].style.top))
				$("#marques > .list > ul")[0].style.top = 0+'px';
			
			// Scroll to top
			$('#marques > div.top').mouseover(function() {
				var _top = $("#marques > .list > ul")[0].style.top.replace(new RegExp("px", "g"), "");
				var _maxScroll = (0 - _top);
				//alert('toTop: top='+_top+',maxScroll='+_maxScroll);
				if (_top < 0 && _top < Page._sectionHeight)
					Page.runIt('top', _maxScroll);
			});
			$('#marques > div.top').mouseout(function() {
				$("#marques > .list > ul").stop();
			});
			
			// Scroll to bottom
			$('#marques > div.bottom').mouseover(function() {
				var _top = $("#marques > .list > ul")[0].style.top.replace(new RegExp("px", "g"), "");
				var _maxScroll = (Page._sectionHeight + _top * 1);
				//alert('toBottom: top='+_top+',maxScroll='+_maxScroll);
				if (_top <= 0 && _top > (0-Page._sectionHeight))
					Page.runIt('bottom', _maxScroll);
			});
			$('#marques > div.bottom').mouseout(function() {
				$("#marques > .list > ul").stop();
			});
		}
	},
	
	/* Adjust the size of the 'wrapper' on the products page */
	resizeProductsArea: function() {
		if (document.getElementById('wrapper')) {
			if (typeof(window.innerHeight) == 'number')
				this._bodyHeight = window.innerHeight;
			else if (document.documentElement && document.documentElement.clientHeight)
				this._bodyHeight = document.documentElement.clientHeight;
			
			document.getElementById('wrapper').style.height = (this._bodyHeight - this._topSectionHeight + 11)+'px';
		}
	},
	
	/* Toggle submenus on products page */
	initAnimateMenu: function() {
		if ($('products')) {
			$('#products > p').each(
				function() {
					$(this).bind("click", function() {
						try {
							if ($(this).next('ul')[0].tagName.toLowerCase() == 'ul')
								$(this).next().toggle("slow");
						}
						catch (e) { }
					});
				}
			);
		}
	},
	
	/* Toggle sur collections/index */
	initAnimateCollection: function(){
		$( "li.collection" ).hover( 
			function(){ $( this ).find( "div.text" ).slideDown("slow"); }, 
			function(){ $( this ).find( "div.text" ).slideUp("slow"); }
		);
	},
	
	/* D?finlement horizontale des marques */
	slideInlineBrands: function(){
		// largeur de tous les li
		$("#marques > .list > ul > li").each(
			function(){ 
				if( $.browser.mozilla )
					Page._sectionWidth += ( $( this ).find( 'a' ).width() + 32);
				else if( $.browser.safari )
					Page._sectionWidth += ( $( this ).find( 'a' ).width() + 29);
				else
					Page._sectionWidth += ( $( this ).find( 'a' ).width() + 26);
			}
		);
		//alert( Page._sectionWidth );
		//alert( $.browser.mozilla );
		
		// D?placement ? gauche
		$( "div.moveLeft" ).hover( 
			function(){ 
				var _moveToLeft = -( $( '#list-marques' ).position().left ) - ( Page._sectionWidth - Page._rightSectionWidth + $( '#list-marques' ).position().left ) ;
				if( _moveToLeft >= 0 ){
					_moveToLeft = -( $( '#list-marques' ).position().left ) - ( Page._sectionWidth - Page._rightSectionWidth );
				}
				if( -( $( '#list-marques' ).position().left ) >= ( Page._sectionWidth - Page._rightSectionWidth ) ){
					_moveToLeft = 0;
				}
					//alert( _moveToLeft );
				$( '#list-marques' ).animate( { left: "+="+ _moveToLeft +"px" }, -( _moveToLeft * 8000 / 1850 ) ) 
			}, 
			function(){ $( '#list-marques' ).stop(); }
		);
		// D?placement ? droite
		$( "div.moveRight" ).hover( 
			function(){ 
				var _moveToRight = ( 0 - $( '#list-marques' ).position().left );
					//alert( _moveToRight );
				$( '#list-marques' ).animate( { left: "+="+ _moveToRight +"px" }, ( _moveToRight * 8000 / 1850 ) ) 
			}, 
			function(){ $( '#list-marques' ).stop(); }
		);
	},
	
	/* Changement de l'image principale d'un produit quand on clique sur une miniature */
	changeImageByMini: function(){
		$( '#detail1' ).click(
			function(){
				var srcImageBig = $( '#imageBig' ).attr('src');
				$( '#imageBig' ).attr( 'src', $( '#detail1 > img' ).attr('src').replace( 'th_', 'bg_' ) );
				$( '#imageBig' ).attr( 'alt', $( '#detail1 > img' ).attr('alt').replace( 'th_', 'Bg_' ) );
				$( '#detail1 > img' ).attr( 'src', srcImageBig.replace( 'bg_', 'th_' ) );
				return false;
			}
		);
		$( '#detail2' ).click(
			function(){
				var srcImageBig = $( '#imageBig' ).attr('src');
				$( '#imageBig' ).attr( 'src', $( '#detail2 > img' ).attr('src').replace( 'th_', 'bg_' ) );
				$( '#imageBig' ).attr( 'alt', $( '#detail2 > img' ).attr('alt').replace( 'th_', 'Bg_' ) );
				$( '#detail2 > img' ).attr( 'src', srcImageBig.replace( 'bg_', 'th_' ) );
				return false;
			}
		);
	}  
};

$(document).ready(function() {
	Page.slideBrands();
	//Page.resizeProductsArea();
	//Page.initAnimateMenu();
	Page.initAnimateCollection();
	Page.slideInlineBrands();
	Page.changeImageByMini();
});

$(window).load(function() {
      var text = $('#body.produit #main.page #detail #detail_content').html();
      var new_text =  text.replace(/<br>/gi,'</p><p>');
      $('#body.produit #main.page #detail #detail_content').html(new_text);
});