 function replaceAll(src, strTarget, strSubString)
{
    var strText = src;
    var intIndexOfMatch = strText.indexOf( strTarget );

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1)
    {
        // Relace out the current instance.
        strText = strText.replace( strTarget, strSubString )

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf( strTarget );
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return( strText );
}
/** init tab menu **/
var OTabs = Class.create();			
OTabs.prototype = {
	initialize : function(element) {
		if($$(element) && $$(element).first()){
			this.element = $$(element);
			var options = Object.extend({}, arguments[1] || {});
			this.menu = $A(this.element.first().getElementsByTagName('a'));
			this.show(this.getInitialTab());
			this.menu.each(this.setupTab.bind(this));
		}
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
	},
	hide : function(elm) {
		$(elm).up('li').removeClassName('active');
		$(this.tabID(elm)).removeClassName('active-tab-body');
		$(this.tabID(elm)).hide();
	},
	show : function(elm) {
		$(elm).up('li').addClassName('active');
		$(this.tabID(elm)).addClassName('active-tab-body');
		$(this.tabID(elm)).show();
		this.menu.without(elm).each(this.hide.bind(this));
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	}
}

/** init  fold/unfold menus **/
var OArboMenu = Class.create();			
OArboMenu.prototype = {
	initialize : function(element,num) {
		if($$(element)){
			this.element = $$(element);
			var options = Object.extend({}, arguments[1] || {});				
				if(!num)
				{					
					for(i=0;i<this.element.length;i++)
					{
						this.ArboMenu = $A(this.element[i].getElementsByTagName('a'));						
						this.ArboMenu.each(this.setupArboMenu.bind(this));
					}
				}else
				{
					this.initializeTH(this.element);
					var sel = this.element.first();
					this.showTH(sel);
				}			
		}
	},
	initializeTH : function(element) {	
		this.element = element;
		var options = Object.extend({}, arguments[1] || {});		
		this.element.each(this.setupArboMenuTH.bind(this));		
	},	
	setupArboMenu: function(elm){
		this.ArboMenu.legalstatus = '0';
		this.hide(this.ArboMenu.first());
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},	
	setupArboMenuTH: function(elm){
		Event.observe(elm,'click',this.activateTH.bindAsEventListener(elm),false);
		this.arboMenuTH = $(elm).nextSiblings();
		($(elm).down('th').hasClassName('active'))?$(elm).down('th').removeClassName('active'):$(elm).down('th').addClassName('active');
		
		for(i=0;i<this.arboMenuTH.length;i++){
			if($(this.arboMenuTH[i]).hasClassName('contentTR'))
			{
				$(this.arboMenuTH[i]).hide();
			}
			else{
				return;
			}
		}		
	},	
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		switch(this.ArboMenu.legalstatus){
			case '0':
				this.hide(elm)
			break;
			case 1:
				this.show(elm)
			break;
			default:								
				this.show(elm)
			break;
		}					
	},
	activateTH :  function(ev) {
		var elm = Event.element(ev).up(); 
		Event.stop(ev);
		this.arboMenuTH = $(elm).nextSiblings();

		($(elm).down('th').hasClassName('active'))?$(elm).down('th').removeClassName('active'):$(elm).down('th').addClassName('active');
		
		for(i=0;i<this.arboMenuTH.length;i++){
			if($(this.arboMenuTH[i]).hasClassName('contentTR'))
			{
				$(this.arboMenuTH[i]).toggle();
					}
			else{
				return;
			}
		}
	},
	showTH : function(elm) {
		if($(elm))
		{
			this.arboMenuTH = $(elm).nextSiblings();
			$(elm).down('th').removeClassName('active');
			
			for(i=0;i<this.arboMenuTH.length;i++){
				if($(this.arboMenuTH[i]).hasClassName('contentTR'))
				{
					$(this.arboMenuTH[i]).toggle();
				}
				else{
					return;
				}
			}
		}
	},
	hide : function(elm) {
		$(elm).addClassName('activate');
		$(this.contentID(elm)).hide();
		this.ArboMenu.legalstatus = '1';
	},
	show : function(elm) {
		$(elm).removeClassName('activate');
		$(this.contentID(elm)).show();
		this.ArboMenu.legalstatus = '0';
	},
	contentID : function(elm) {
		return (elm.href.match(/#(\w.+)/)[1]);
	}
}


/*************************************/

/*** gestion de la barre de menu principale **/
document.observe("dom:loaded", function() {
	if (Prototype.Browser.IE) {
		Prototype.Browser.IEVersion = parseFloat(navigator.appVersion.split(';')[1].strip().split(' ')[1]);
		Prototype.Browser.IE6 =  Prototype.Browser.IEVersion == 6;
	}
	/** pour IE6 qui ne gère pas le hover sur li **/
	if(Prototype.Browser.IE6){
	 $$('#headerNav li').each(function(elm){
				Event.observe($(elm),'mouseover',function(ev){
					var el = Event.findElement(ev,'li');
					$(el).addClassName('sfhover');
					Event.stop(ev);							
				});
				
				Event.observe($(elm),'mouseleave',function(ev){
					var el = Event.findElement(ev,'li');
					$(el).removeClassName('sfhover');
					Event.stop(ev);
				});
				
		});
	}	
	/** hide accordion elements via js **/
	$$('.accordionBlock div').each(function(elm){
		$(elm).addClassName('off');
	},false);
});
/***************************************/



/* global onload for class behaviors */
Event.observe(window,'load',function(){ 
	/*** product images behaviors **/
	var pathImgProd = 'img/dynamic/';

	$$('#fpDisplayAllPictures a').each(function(elm){
		var imgUri = replaceAll($(elm).readAttribute('rel'),"'",'"').evalJSON();
		
		Event.observe(elm,'mouseover',function(ev){
			$$('#fpDisplayAllPictures li').each(function(elm){
				$(elm).removeClassName('active');
			});
			$(elm).up('li').addClassName('active');
			Event.stop(ev);					
			$$('#fpDisplayPicture img').first().src=pathImgProd+imgUri.imgM;
			$$('#zoom a').first().href=pathImgProd+imgUri.imgB;
			Shadowbox.setup();
		});	
		Event.observe(elm,'click',function(ev){			
			Event.stop(ev);				
		});	
	});
	
	
	/** init popins **/
	var opt = {loadingImage: GLOBAL_SERVEUR_HTTP+'img/common/loading.gif',overlayColor:'#000',overlayOpacity:'0.6',keysClose:['c', 27]};//,overlayBgImage:"img/common/overlay-85.png"
		if (typeof Shadowbox != 'undefined') Shadowbox.init(opt);	


	// init the product page tabs
	new OTabs('.navTabs');
	new OTabs('.blockTabNav');
	
	/** adding behavior for legal link on/off **/
	new OArboMenu('.legalAccess',false);	

	/** adding behavior for product page fold/unfold menus **/
	var header = new OArboMenu('tr.heading',1);	

	/** increase/decrease quantity **/
	$$('.incLink').each(function(elm){
			Event.observe(elm,'click',function(elm){
				Event.stop(elm);
				var inputId = this.href.match(/#(\w.+)/)[1];
				var inputValP = $F(inputId);
				inputValP++;
				Form.Element.setValue(inputId,inputValP);
			});
	},false);
	$$('.decLink').each(function(elm){
			Event.observe(elm,'click',function(elm){
				Event.stop(elm);
				var inputId = this.href.match(/#(\w.+)/)[1];
				var inputValM = $F(inputId);
				inputValM--;
				if(inputValM<1)inputValM=1;
				Form.Element.setValue(inputId,inputValM);
			});
	},false);

	if (typeof accordion != 'undefined') 
	{
		// instanciate the right accordions
		if($('nosEngagements'))
		{	
			var nosEngagements = new accordion('nosEngagements',{onEvent : 'mouseover'});
			
			// activate first module of accordions
			nosEngagements.activate($$('#nosEngagements .accordion_toggle')[0]);
		}
		if($('nosEngagementsSmall'))
		{
			var nosEngagementsSmall = new accordion('nosEngagementsSmall',{onEvent : 'mouseover'});	
			// activate first module of accordions	
			nosEngagementsSmall.activate($$('#nosEngagementsSmall .accordion_toggle')[0]);
		}		

	}

	/** Meilleures ventes & sélections **/
	$$('.contentListPdt li a.actionBt').each(function(elm){
		Event.observe(elm,'click',function(ev){
			var elm = Event.findElement(ev, "a");	
			Event.stop(ev);
			$(elm).up('li').siblings().each(function(el){
				$(el).removeClassName('active');
			});			
			$(elm).up('li').addClassName('active');
		});
	})
	
	/** recharges **/
	$$(".stdDeco input[type='radio'], .controlDeco input[type='radio'], .enhancedDeco input[type='radio']").each(function(elm){
			Event.observe(elm,'click',function(event){
				$$(".stdDeco input[type='radio'], .controlDeco input[type='radio'], .enhancedDeco input[type='radio']").each(function(elm){
					elm.up('p').removeClassName('active');
				});
				var element = event.element(); 
				$(element).up('p').addClassName('active');
			});
	});

	/** recharge rebond **/
	$$(".numCheck input").each(function(elm){
		new Form.Element.Observer(
		  elm,
		  0.02,  // 2 milliseconds
		  function(el, value){
			if(value.length == 2 && $(el).next('input'))$(el).next('input').select();
		  }
		)
	});
	$$(".cardNum input").each(function(elm){
		new Form.Element.Observer(
		  elm,
		  0.02,  // 2 milliseconds
		  function(el, value){
			if(value.length == 4 && $(el).next('input'))$(el).next('input').select();
		  }
		)
	});
	
		
 },false);
