var packSelector_activeTab;
var packSelector_selectedBoardBrand = new Array;
var packSelector_selectedBoardCollection = new Array;
var packSelector_selectedBoardGender = new Array;
var packSelector_selectedBoardLevel = new Array;
var packSelector_selectedBoardProgram = new Array;
var packSelector_selectedBindingBrand = new Array;
var packSelector_selectedBindingCollection = new Array;
var packSelector_selectedBindingGender = new Array;
var packSelector_selectedBindingBurton = new Array;
var packSelector_searchTimer;
var packSelector_margin = packSelector_newMargin = 0;
var packSelector_moving = null;
var packSelector_productInformation;
var packSelector_selectedId, packSelector_selectedBoard, packSelector_selectedBinding;

Event.observe(window, 'load', packSelector_init);

function packSelector_init(event){
		//Stop listen load Event
	Event.stop(event);
	Event.stopObserving(window, 'load', packSelector_init);

	if($('packselector') != null){
		packSelector_activeTab = 'board';

		$('brand_search_binding').setStyle({'display':'none'});
		$('other_search_binding').setStyle({'display':'none'});

		$('tab_board').observe('click', packSelector_tabHandler);
		$('tab_binding').observe('click', packSelector_tabHandler);

		$('bt_more_board').observe('click', packSelector_moreHandler);
		$('bt_more_binding').observe('click', packSelector_moreHandler);

		$$('#brand_search_board li').each(function(li){
			if(li.innerHTML != '&nbsp;'){
				li.observe('click', packSelector_toggleSelectedBrand);
				li.observe('dblclick', packSelector_preventSelect);
			}
		});
		
		$$('#brand_search_binding li').each(function(li){
			if(li.innerHTML != '&nbsp;'){
				li.observe('click', packSelector_toggleSelectedBrand);
				li.observe('dblclick', packSelector_preventSelect);
			}
		});

		$$('#other_search_board .ps_collection p').each(function(p){
			p.observe('click', packSelector_toggleSelectedCollection);
			p.observe('dblclick', packSelector_preventSelect);
		});

		$$('#other_search_binding .ps_collection p').each(function(p){
			p.observe('click', packSelector_toggleSelectedCollection);
			p.observe('dblclick', packSelector_preventSelect);
		});

		$$('#other_search_board .ps_gender p').each(function(p){
			p.observe('click', packSelector_toggleSelectedGender);
			p.observe('dblclick', packSelector_preventSelect);
		});

		$$('#other_search_binding .ps_gender p').each(function(p){
			p.observe('click', packSelector_toggleSelectedGender);
			p.observe('dblclick', packSelector_preventSelect);
		});

		$$('#other_search_board .ps_Niveau p').each(function(p){
			p.observe('click', packSelector_toggleSelectedLevel);
			p.observe('dblclick', packSelector_preventSelect);
		});

		$$('#other_search_board .ps_Programme_snowboard p').each(function(p){
			p.observe('click', packSelector_toggleSelectedProgram);
			p.observe('dblclick', packSelector_preventSelect);
		});

		$$('#other_search_binding .ps_Compatibilite_Burton p').each(function(p){
			p.observe('click', packSelector_toggleSelectedBurton);
			p.observe('dblclick', packSelector_preventSelect);
		});

		$$('#packselector .direct_search input')[1].observe('click', packSelector_searchRequest);
		$$('#packselector .direct_search form')[0].observe('submit', packSelector_searchRequest);
	}
}

function packSelector_preventSelect(event){
		//Stop listen load Event
	Event.stop(event);

	if(document.selection && document.selection.empty){
		document.selection.empty();
	}else if(window.getSelection){
		window.getSelection().removeAllRanges();
	}
}

function packSelector_tabHandler(event){
	if(typeof(event) == 'string'){
		var dispatcher = new Element('span', {'id':event});
	}else{
		//Stop event propagation
		Event.stop(event);
	
			//Local var init
		var dispatcher = event.element();
	}

	if(dispatcher.getAttribute('id') == 'tab_board' || ($$('#packselector .your .board')[0] != null && dispatcher.descendantOf($$('#packselector .your .board')[0]))){
		if($('tab_board').getAttribute('src').match(/disable/g) != null){
			packSelector_activeTab = 'board';
			$('tab_board').setAttribute('src', $('tab_board').getAttribute('src').replace(/disable/g, 'enable'));
			$('tab_binding').setAttribute('src', $('tab_binding').getAttribute('src').replace(/enable/g, 'disable'));
			$('brand_search_binding').setStyle({'display':'none'});
			$('brand_search_board').setStyle({'display':'block'});
			$('other_search_binding').setStyle({'display':'none'});
			$('other_search_board').setStyle({'display':'block'});
		}
	}else if(dispatcher.getAttribute('id') == 'tab_binding' || ($$('#packselector .your .binding')[0] != null && dispatcher.descendantOf($$('#packselector .your .binding')[0]))){
		if($('tab_binding').getAttribute('src').match(/disable/g) != null){
			packSelector_activeTab = 'binding';
			$('tab_binding').setAttribute('src', $('tab_binding').getAttribute('src').replace(/disable/g, 'enable'));
			$('tab_board').setAttribute('src', $('tab_board').getAttribute('src').replace(/enable/g, 'disable'));
			$('brand_search_board').setStyle({'display':'none'});
			$('brand_search_binding').setStyle({'display':'block'});
			$('other_search_board').setStyle({'display':'none'});
			$('other_search_binding').setStyle({'display':'block'});
		}
	}

	$$('#packselector .direct_search input')[0].value = '';
	packSelector_searchRequest(null);
}

function packSelector_moreHandler(event){
		//Stop event propagation
	Event.stop(event);

		//Local var init
	var dispatcher, dropdown;

	if(packSelector_activeTab == 'board'){
		dispatcher = $('bt_more_board');
		dropdown = $$('#other_search_board .collapse')[0];
	}else{
		dispatcher = $('bt_more_binding');
		dropdown = $$('#other_search_binding .collapse')[0];
	}

	if(dispatcher.getAttribute('src').match(/more/g) != null){
		dispatcher.setAttribute('src', dispatcher.getAttribute('src').replace(/more/g, 'less'));
		dropdown.setStyle({'height':'auto'});
	}else if(dispatcher.getAttribute('src').match(/less/g) != null){
		dispatcher.setAttribute('src', dispatcher.getAttribute('src').replace(/less/g, 'more'));
		dropdown.setStyle({'height':'0px'});
	}

	$$('#packselector .direct_search input')[0].value = '';
	packSelector_searchRequest(null);
}

function packSelector_toggleSelectedBrand(event){
		//Stop event propagation
	Event.stop(event);

	clearTimeout(packSelector_searchTimer);
	$$('#packselector .direct_search input')[0].value = '';

		//Local vars init
	var dispatcher = event.element();
	var add = true;
	var localArray, i;

	if(packSelector_activeTab == 'board'){
		localArray = packSelector_selectedBoardBrand;
	}else{
		localArray = packSelector_selectedBindingBrand;
	}

	for(i = 0; i < localArray.length; i++){
		if(localArray[i] == dispatcher.innerHTML){
			add = false;
			dispatcher.setStyle({'backgroundColor':'transparent', 'color':'#666666', 'backgroundImage':'none'});
			if(i == 0){
				localArray = localArray.slice(1, localArray.length);
			}else if(i == localArray.length - 1){
				localArray = localArray.slice(0, localArray.length - 1);
			}else{
				localArray = localArray.slice(0, i).concat(localArray.slice(i + 1, localArray.length));
			}
		}
	}
	if(add){
		localArray.push(dispatcher.innerHTML);
		dispatcher.setStyle({'color':'white', 'background':'#fa9233 url(' + GFX + 'graphic/snowshop/pack_selector/cross.png) no-repeat top right'});
	}

	if(packSelector_activeTab == 'board'){
		packSelector_selectedBoardBrand = localArray;
	}else{
		packSelector_selectedBindingBrand = localArray;
	}

	packSelector_searchTimer = setTimeout(function(searchEvent){packSelector_searchRequest(null);}, 500);
}

function packSelector_toggleSelectedCollection(event){
		//Stop event propagation
	Event.stop(event);

	clearTimeout(packSelector_searchTimer);
	$$('#packselector .direct_search input')[0].value = '';

		//Local vars init
	var dispatcher = event.element();
	var add = true;
	var localArray, i;

	if(packSelector_activeTab == 'board'){
		localArray = packSelector_selectedBoardCollection;
	}else{
		localArray = packSelector_selectedBindingCollection;
	}

	for(i = 0; i < localArray.length; i++){
		if(localArray[i] == dispatcher.innerHTML){
			add = false;
			dispatcher.setStyle({'backgroundColor':'transparent', 'color':'#666666', 'backgroundImage':'none'});
			if(i == 0){
				localArray = localArray.slice(1, localArray.length);
			}else if(i == localArray.length - 1){
				localArray = localArray.slice(0, localArray.length - 1);
			}else{
				localArray = localArray.slice(0, i).concat(localArray.slice(i + 1, localArray.length));
			}
		}
	}
	if(add){
		localArray.push(dispatcher.innerHTML);
		dispatcher.setStyle({'color':'white', 'background':'#fa9233 url(' + GFX + 'graphic/snowshop/pack_selector/cross.png) no-repeat top right'});
	}

	if(packSelector_activeTab == 'board'){
		packSelector_selectedBoardCollection = localArray;
	}else{
		packSelector_selectedBindingCollection = localArray;
	}

	packSelector_searchTimer = setTimeout(function(searchEvent){packSelector_searchRequest(null);}, 500);
}

function packSelector_toggleSelectedGender(event){
		//Stop event propagation
	Event.stop(event);

	clearTimeout(packSelector_searchTimer);
	$$('#packselector .direct_search input')[0].value = '';

		//Local vars init
	var dispatcher = event.element();
	var add = true;
	var localArray, i;

	if(packSelector_activeTab == 'board'){
		localArray = packSelector_selectedBoardGender;
	}else{
		localArray = packSelector_selectedBindingGender;
	}

	for(i = 0; i < localArray.length; i++){
		if(localArray[i] == dispatcher.innerHTML){
			add = false;
			dispatcher.setStyle({'backgroundColor':'transparent', 'color':'#666666', 'backgroundImage':'none'});
			if(i == 0){
				localArray = localArray.slice(1, localArray.length);
			}else if(i == localArray.length - 1){
				localArray = localArray.slice(0, localArray.length - 1);
			}else{
				localArray = localArray.slice(0, i).concat(localArray.slice(i + 1, localArray.length));
			}
		}
	}
	if(add){
		localArray.push(dispatcher.innerHTML);
		dispatcher.setStyle({'color':'white', 'background':'#fa9233 url(' + GFX + 'graphic/snowshop/pack_selector/cross.png) no-repeat top right'});
	}

	if(packSelector_activeTab == 'board'){
		packSelector_selectedBoardGender = localArray;
	}else{
		packSelector_selectedBindingGender = localArray;
	}

	packSelector_searchTimer = setTimeout(function(searchEvent){packSelector_searchRequest(null);}, 500);
}

function packSelector_toggleSelectedLevel(event){
		//Stop event propagation
	Event.stop(event);

	clearTimeout(packSelector_searchTimer);
	$$('#packselector .direct_search input')[0].value = '';

		//Local vars init
	var dispatcher = event.element();
	var add = true;
	var i;

	for(i = 0; i < packSelector_selectedBoardLevel.length; i++){
		if(packSelector_selectedBoardLevel[i] == '["' + dispatcher.innerHTML + '"]'){
			add = false;
			dispatcher.setStyle({'backgroundColor':'transparent', 'color':'#666666', 'backgroundImage':'none'});
			if(i == 0){
				packSelector_selectedBoardLevel = packSelector_selectedBoardLevel.slice(1, packSelector_selectedBoardLevel.length);
			}else if(i == packSelector_selectedBoardLevel.length - 1){
				packSelector_selectedBoardLevel = packSelector_selectedBoardLevel.slice(0, packSelector_selectedBoardLevel.length - 1);
			}else{
				packSelector_selectedBoardLevel = packSelector_selectedBoardLevel.slice(0, i).concat(packSelector_selectedBoardLevel.slice(i + 1, packSelector_selectedBoardLevel.length));
			}
		}
	}
	if(add){
		packSelector_selectedBoardLevel.push('["' + dispatcher.innerHTML + '"]');
		dispatcher.setStyle({'color':'white', 'background':'#fa9233 url(' + GFX + 'graphic/snowshop/pack_selector/cross.png) no-repeat top right'});
	}

	packSelector_searchTimer = setTimeout(function(searchEvent){packSelector_searchRequest(null);}, 500);
}

function packSelector_toggleSelectedProgram(event){
		//Stop event propagation
	Event.stop(event);

	clearTimeout(packSelector_searchTimer);
	$$('#packselector .direct_search input')[0].value = '';

		//Local vars init
	var dispatcher = event.element();
	var add = true;
	var localArray, i;

	for(i = 0; i < packSelector_selectedBoardProgram.length; i++){
		if(packSelector_selectedBoardProgram[i] == '["' + dispatcher.innerHTML + '"]'){
			add = false;
			dispatcher.setStyle({'backgroundColor':'transparent', 'color':'#666666', 'backgroundImage':'none'});
			if(i == 0){
				packSelector_selectedBoardProgram = packSelector_selectedBoardProgram.slice(1, packSelector_selectedBoardProgram.length);
			}else if(i == packSelector_selectedBoardProgram.length - 1){
				packSelector_selectedBoardProgram = packSelector_selectedBoardProgram.slice(0, packSelector_selectedBoardProgram.length - 1);
			}else{
				packSelector_selectedBoardProgram = packSelector_selectedBoardProgram.slice(0, i).concat(packSelector_selectedBoardProgram.slice(i + 1, packSelector_selectedBoardProgram.length));
			}
		}
	}
	if(add){
		packSelector_selectedBoardProgram.push('["' + dispatcher.innerHTML + '"]');
		dispatcher.setStyle({'color':'white', 'background':'#fa9233 url(' + GFX + 'graphic/snowshop/pack_selector/cross.png) no-repeat top right'});
	}

	packSelector_searchTimer = setTimeout(function(searchEvent){packSelector_searchRequest(null);}, 500);
}

function packSelector_toggleSelectedBurton(event){
		//Stop event propagation
	Event.stop(event);

	clearTimeout(packSelector_searchTimer);
	$$('#packselector .direct_search input')[0].value = '';

		//Local vars init
	var dispatcher = event.element();
	var add = true;
	var localArray, i;

	for(i = 0; i < packSelector_selectedBindingBurton.length; i++){
		if(packSelector_selectedBindingBurton[i] == '["' + dispatcher.innerHTML + '"]'){
			add = false;
			dispatcher.setStyle({'backgroundColor':'transparent', 'color':'#666666', 'backgroundImage':'none'});
			if(i == 0){
				packSelector_selectedBindingBurton = packSelector_selectedBindingBurton.slice(1, packSelector_selectedBindingBurton.length);
			}else if(i == packSelector_selectedBindingBurton.length - 1){
				packSelector_selectedBindingBurton = packSelector_selectedBindingBurton.slice(0, packSelector_selectedBindingBurton.length - 1);
			}else{
				packSelector_selectedBindingBurton = packSelector_selectedBindingBurton.slice(0, i).concat(packSelector_selectedBindingBurton.slice(i + 1, packSelector_selectedBindingBurton.length));
			}
		}
	}
	if(add){
		packSelector_selectedBindingBurton.push('["' + dispatcher.innerHTML + '"]');
		dispatcher.setStyle({'color':'white', 'background':'#fa9233 url(' + GFX + 'graphic/snowshop/pack_selector/cross.png) no-repeat top right'});
	}

	packSelector_searchTimer = setTimeout(function(searchEvent){packSelector_searchRequest(null);}, 500);
}

function packSelector_searchRequest(event){
		//Stop event propagation
	if(event != null){
		Event.stop(event);
	}

	$$('#packselector .search')[0].insert({'bottom':new Element('div', {'class':'loader'})});
	$$('#packselector .search .loader')[0].setStyle({'width':$$('#packselector .search')[0].getWidth() + 'px', 'height':$$('#packselector .search')[0].getHeight() - 50 + 'px'});
	$$('#packselector .search .loader')[0].update('Chargement en cours…<br />');
	$$('#packselector .search .loader')[0].insert({'bottom':new Element('img', {'src':GFX + 'graphic/shared/loading.gif'})});

	var brand, collection, gender, more;

	if(packSelector_activeTab == 'board'){
		brand = packSelector_selectedBoardBrand.join('|-|');
	}else{
		brand = packSelector_selectedBindingBrand.join('|-|');
	}

	if(packSelector_activeTab == 'board'){
		collection = packSelector_selectedBoardCollection.join('|-|');
	}else{
		collection = packSelector_selectedBindingCollection.join('|-|');
	}

	if(packSelector_activeTab == 'board'){
		gender = packSelector_selectedBoardGender.join('|-|');
	}else{
		gender = packSelector_selectedBindingGender.join('|-|');
	}

	if((packSelector_activeTab == 'board' && $$('#other_search_board .collapse')[0].getStyle('height') != '0px') || (packSelector_activeTab == 'binding' && $$('#other_search_binding .collapse')[0].getStyle('height') != '0px')){
		more = 1;
	}else{
		more = 0;
	}

	if($$('#packselector .img_slider')[0] != null){
		$$('#packselector .img_slider')[0].remove();
	}

	if($$('#packselector .selection')[0] != null){
		$$('#packselector .selection')[0].remove();
	}

	packSelector_margin = packSelector_newMargin = 0;

	if($$('#packselector .direct_search input')[0].value != ''){
		$$('#brand_search_' + packSelector_activeTab + ' li').each(function(li){
			li.setStyle({'backgroundColor':'transparent', 'color':'#666666'});
		});
		$$('#other_search_' + packSelector_activeTab + ' listElement p').each(function(p){
			p.setStyle({'backgroundColor':'transparent', 'color':'#666666'});
		});
		if(packSelector_activeTab == 'board'){
			packSelector_selectedBoardBrand = new Array;
			packSelector_selectedBoardCollection = new Array;
			packSelector_selectedBoardGender = new Array;
			packSelector_selectedBoardLevel = new Array;
			packSelector_selectedBoardProgram = new Array;
		}else{
			packSelector_selectedBindingBrand = new Array;
			packSelector_selectedBindingCollection = new Array;
			packSelector_selectedBindingGender = new Array;
			packSelector_selectedBindingBurton = new Array;
		}
		new Ajax.Request('/product_models/ajax_packSelectorSearch/', {
			'parameters':{
				'tab':packSelector_activeTab,
				'string':$$('#packselector .direct_search input')[0].value,
				'more':0
			},
			'onSuccess':packSelector_searchResponse
		});
	}else{
		new Ajax.Request('/product_models/ajax_packSelectorSearch/', {
			'parameters':{
				'tab':packSelector_activeTab,
				'brand':brand,
				'collection':collection,
				'gender':gender,
				'level':packSelector_selectedBoardLevel.join('|-|'),
				'program':packSelector_selectedBoardProgram.join('|-|'),
				'burton':packSelector_selectedBindingBurton.join('|-|'),
				'more':more
			},
			'onSuccess':packSelector_searchResponse
		});
	}
}

function packSelector_searchResponse(response){
	$$('#packselector .search .loader')[0].remove();

	switch(response.responseJSON.returnCode){
		case 0:
				//Return OK => make page
			if(response.responseJSON.productCount > 1){
				$$('#packselector .search_result_str h4')[0].update(response.responseJSON.productCount + ' produits correspondent a votre sélection.');
			}else{
				$$('#packselector .search_result_str h4')[0].update(response.responseJSON.productCount + ' produit correspond a votre sélection.');
			}

			if(!MSIE6 && !MSIE7){
				Cufon.replace('#packselector .search_result_str h4');
			}
			$$('#packselector .search_result_str')[0].setStyle({'borderBottom':'1px solid #d9d9d9'});

			$$('#packselector .search_result_str')[0].insert({after:response.responseJSON.imgSlider});
			var width;
			if(MSIE6){
				width = 108;
			}else{
				width = 107;
			}
			$$('#packselector .img_slider .elements')[0].setStyle({'width':($$('#packselector .img_slider .elements .element').length * width) + 'px'});
			if($$('#packselector .img_slider .slider_arrow')[1].childElements()[0].getAttribute('src').match(/enable/g) != null){
				$$('#packselector .img_slider .slider_arrow')[1].childElements()[0].observe('click', packSelector_slidLeft);
			}
			$$('#packselector .img_slider .slider_arrow').each(function(arrow){
				if(packSelector_activeTab == 'board'){
					arrow.setStyle({'paddingTop':'51px'});
					arrow.setStyle({'paddingBottom':'115px'});
				}else{
					arrow.setStyle({'paddingTop':'0px'});
					arrow.setStyle({'paddingBottom':'64px'});
				}
			});

			$$('#packselector .elements .element').each(function(element){
				element.childElements().each(function(subElement){
					subElement.observe('click', packSelector_selectProduct);
				});
			});
			packSelector_productInformation = response.responseJSON.info;
			break;



		case 1:
		default:
			alert('Erreur de requête AJAX');
			break;



		case 2:
			$$('#packselector .search_result_str h4')[0].update('Merci de sélectionner au moins un critère.');
			if(!MSIE6 && !MSIE7){
				Cufon.replace('#packselector .search_result_str h4');
			}
			$$('#packselector .search_result_str')[0].setStyle({'borderBottom':'none'});
			if($$('#packselector .img_slider')[0] != null){
				$$('#packselector .img_slider')[0].remove();
			}
			break;



		case 3:
			$$('#packselector .search_result_str h4')[0].update('Aucun produit ne correspond à votre sélection.');
			if(!MSIE6 && !MSIE7){
				Cufon.replace('#packselector .search_result_str h4');
			}
			$$('#packselector .search_result_str')[0].setStyle({'borderBottom':'none'});
			if($$('#packselector .img_slider')[0] != null){
				$$('#packselector .img_slider')[0].remove();
			}
			break;
	}
}

function packSelector_slidLeft(event){
		//Stop event propagation
	if(event != null){
		Event.stop(event);
	}

	$$('#packselector .img_slider .slider_arrow')[0].childElements()[0].stopObserving('click', packSelector_slidRight);
	$$('#packselector .img_slider .slider_arrow')[1].childElements()[0].stopObserving('click', packSelector_slidLeft);

	if(packSelector_margin == packSelector_newMargin && packSelector_moving == null){
		packSelector_newMargin = packSelector_newMargin - $$('#packselector .img_slider .element')[0].getWidth();
	}

	if(packSelector_margin > packSelector_newMargin){
		packSelector_margin = packSelector_margin - 10;
		$$('#packselector .img_slider .elements')[0].setStyle({'marginLeft':packSelector_margin + 'px'});
		packSelector_moving = setTimeout(function(){packSelector_slidLeft(null);}, 1);
	}else{
		packSelector_margin = packSelector_newMargin;
		$$('#packselector .img_slider .elements')[0].setStyle({'marginLeft':packSelector_margin + 'px'});
		clearTimeout(packSelector_moving);
		packSelector_moving = null;

		if(packSelector_margin < 0){
			$$('#packselector .img_slider .slider_arrow')[0].childElements()[0].setAttribute('src', $$('#packselector .img_slider .slider_arrow')[0].childElements()[0].getAttribute('src').replace(/disable/g, 'enable'));
			$$('#packselector .img_slider .slider_arrow')[0].childElements()[0].observe('click', packSelector_slidRight);
		}

		var width;
		if(MSIE6){
			width = 540;
		}else{
			width = 535;
		}

		if(Math.abs(packSelector_margin) + width < $$('#packselector .img_slider .elements')[0].getWidth()){
			$$('#packselector .img_slider .slider_arrow')[1].childElements()[0].observe('click', packSelector_slidLeft);
		}else{
			$$('#packselector .img_slider .slider_arrow')[1].childElements()[0].setAttribute('src', $$('#packselector .img_slider .slider_arrow')[1].childElements()[0].getAttribute('src').replace(/enable/g, 'disable'));
		}
	}
}

function packSelector_slidRight(event){
		//Stop event propagation
	if(event != null){
		Event.stop(event);
	}

	$$('#packselector .img_slider .slider_arrow')[0].childElements()[0].stopObserving('click', packSelector_slidRight);
	$$('#packselector .img_slider .slider_arrow')[1].childElements()[0].stopObserving('click', packSelector_slidLeft);

	if(packSelector_margin == packSelector_newMargin && packSelector_moving == null){
		packSelector_newMargin = packSelector_newMargin + $$('#packselector .img_slider .element')[0].getWidth();
	}

	if(packSelector_margin < packSelector_newMargin){
		packSelector_margin = packSelector_margin + 10;
		$$('#packselector .img_slider .elements')[0].setStyle({'marginLeft':packSelector_margin + 'px'});
		packSelector_moving = setTimeout(function(){packSelector_slidRight(null);}, 1);
	}else{
		packSelector_margin = packSelector_newMargin;
		$$('#packselector .img_slider .elements')[0].setStyle({'marginLeft':packSelector_margin + 'px'});
		clearTimeout(packSelector_moving);
		packSelector_moving = null;

		if(packSelector_margin >= 0){
			$$('#packselector .img_slider .slider_arrow')[0].childElements()[0].setAttribute('src', $$('#packselector .img_slider .slider_arrow')[0].childElements()[0].getAttribute('src').replace(/enable/g, 'disable'));
		}else{
			$$('#packselector .img_slider .slider_arrow')[0].childElements()[0].observe('click', packSelector_slidRight);
		}

		var width;
		if(MSIE6){
			width = 540;
		}else{
			width = 535;
		}

		if(Math.abs(packSelector_margin) + width < $$('#packselector .img_slider .elements')[0].getWidth()){
			$$('#packselector .img_slider .slider_arrow')[1].childElements()[0].observe('click', packSelector_slidLeft);
			$$('#packselector .img_slider .slider_arrow')[1].childElements()[0].setAttribute('src', $$('#packselector .img_slider .slider_arrow')[1].childElements()[0].getAttribute('src').replace(/disable/g, 'enable'));
		}
	}
}

function packSelector_selectProduct(event){
		//Stop event propagation
	Event.stop(event);

	packSelector_selectedId = event.findElement('div').getAttribute('id');

	$$('#packselector .elements .element').each(function(element){
		element.childElements()[0].setStyle({'borderColor':'#e6e6e6'});
	});
	event.findElement('div').childElements()[0].setStyle({'borderColor':'#1d99f3'});

	if($$('#packselector .selection')[0] != null){
		$$('#packselector .selection')[0].remove();
	}
	$$('#packselector .img_slider')[0].insert({'after':new Element('div', {'class':'selection'})});
	$$('#packselector .selection')[0].insert({'bottom':new Element('h4')});
	$$('#packselector .selection h4')[0].update('Votre sélection');
	if(!MSIE6 && !MSIE7){
		Cufon.replace('#packselector .selection h4');
	}

	if(packSelector_productInformation[packSelector_selectedId].img != null){
		$$('#packselector .selection')[0].insert({'bottom':new Element('div', {'class':'img'})});
		$$('#packselector .selection .img')[0].insert({'bottom':new Element('img', {'src':packSelector_productInformation[packSelector_selectedId].img})});
	}

	$$('#packselector .selection')[0].insert({'bottom':new Element('div', {'class':'product_info'})});
	if(packSelector_productInformation[packSelector_selectedId].img != null && packSelector_activeTab == 'board'){
		$$('#packselector .selection .product_info')[0].setStyle({'width':'428px'});
	}else if(packSelector_productInformation[packSelector_selectedId].img != null && packSelector_activeTab == 'binding'){
		$$('#packselector .selection .product_info')[0].setStyle({'width':'343px'});
	}
	$$('#packselector .selection .product_info')[0].insert({'bottom':new Element('div', {'class':'title'})});
	$$('#packselector .selection .product_info .title')[0].update(packSelector_productInformation[packSelector_selectedId].brand + ' ' + packSelector_productInformation[packSelector_selectedId].title);
	if(!MSIE6 && !MSIE7){
		Cufon.replace('#packselector .selection .product_info .title');
	}

	$$('#packselector .selection .product_info')[0].insert({'bottom':new Element('div', {'class':'price'})});
	$$('#packselector .selection .product_info .price')[0].update(packSelector_productInformation[packSelector_selectedId].fullPrice.toString().replace(/\./, ',') + '&nbsp;€');

	if(Object.isArray(packSelector_productInformation[packSelector_selectedId].size) == false){
		packSelector_productInformation[packSelector_selectedId].size = Object.values(packSelector_productInformation[packSelector_selectedId].size);
	}
	$$('#packselector .selection .product_info')[0].insert({'bottom':new Element('div', {'class':'size'})});
	if(packSelector_activeTab == 'board'){
		$$('#packselector .selection .product_info .size')[0].update('<div class="s">Sélectionnez la taille de votre planche&nbsp;:</div><div>');
	}else{
		$$('#packselector .selection .product_info .size')[0].update('<div class="s">Sélectionnez la taille de vos fixations&nbsp;:</div><div>');
	}
	packSelector_productInformation[packSelector_selectedId].size.each(function(size){
		$$('#packselector .selection .product_info .size')[0].update($$('#packselector .selection .product_info .size')[0].innerHTML + '<input type="radio" id="size_' + size.sizeIdx + '_' + size.size + '" name="size" /> <label for="size_' + size.sizeIdx + '_' + size.size + '">' + size.sizeC + '</label>');
	});
	$$('#packselector .selection .product_info .size')[0].update($$('#packselector .selection .product_info .size')[0].innerHTML + '</div><div class="bt"><img id="" src="' + GFX + 'graphic/snowshop/pack_selector/bt_add_pack.png" alt="Ajouter au pack" /></div>');
	if(!MSIE6 && !MSIE7){
		Cufon.replace('#packselector .selection .product_info .size div.s');
	}
	$$('#packselector .selection .product_info .size .bt img')[0].observe('click', packSelector_checkSize);

	$$('#packselector .selection .product_info')[0].insert({'bottom':new Element('div', {'class':'desc'})});
	$$('#packselector .selection .product_info .desc')[0].update(packSelector_productInformation[packSelector_selectedId].desc);

	$$('#packselector .selection .product_info')[0].insert({'bottom':new Element('div', {'class':'link'})});
	$$('#packselector .selection .product_info .link')[0].update('<a href="' + packSelector_productInformation[packSelector_selectedId].url + '">Voir la fiche technique complète.</a>');
}

function packSelector_checkSize(event){
		//Stop event propagation
	Event.stop(event);

		//Local var
	var add = false;

	$$('#packselector .selection .product_info .size input').each(function(input){
		if(input.checked){
			add = true;
		}
	});

	if(add){
		packSelector_Add();
	}else{
		alert('Merci de sélectionner une taille.');
	}
}

function packSelector_Add(){
	if($$('#pack_content .board')[0] == null && $$('#pack_content .binding')[0] == null){
		$('pack_content').update();
	}

		//Local var
	var size;

	$$('#packselector .selection .product_info .size input').each(function(input){
		if(input.checked){
			size = input.getAttribute('id');
		}
	});

	if(packSelector_activeTab == 'board'){
		if($$('#pack_content .board')[0] != null){
			$$('#pack_content .board')[0].remove();
		}

		packSelector_selectedBoard = packSelector_productInformation[packSelector_selectedId];

		packSelector_selectedBoard.selectedSizeId = size.replace(/size_([0-9]*)_.*/g, '$1');
		$('pack_content').insert({'top':new Element('div', {'class':'board'})});
		$$('#pack_content .board')[0].insert({'bottom':new Element('img', {'src':$$('#' + packSelector_selectedId + ' img')[0].getAttribute('src')})});
		$$('#pack_content .board')[0].insert({'bottom':new Element('p')});
		$$('#pack_content .board p')[0].update('<strong>' + packSelector_productInformation[packSelector_selectedId].brand + '</strong><br />' + packSelector_productInformation[packSelector_selectedId].title + '<br /><span class="right_price">' + packSelector_productInformation[packSelector_selectedId].fullPrice.toString().replace(/\./g, ',') + '&nbsp;€</span>Taille&nbsp;: ' + size.replace(/size_[0-9]*_/g, ''));
		$$('#pack_content .board p')[0].insert({'after':new Element('img', {'src':GFX + 'graphic/snowshop/pack_selector/update_choice.png'})});
		$$('#pack_content .board img')[1].setStyle({'border':'none', 'cursor':'pointer'});
		$$('#pack_content .board img')[1].observe('click', packSelector_tabHandler);
	}else{
		if($$('#pack_content .binding')[0] != null){
			$$('#pack_content .binding')[0].remove();
		}

		packSelector_selectedBinding = packSelector_productInformation[packSelector_selectedId];

		packSelector_selectedBinding.selectedSizeId = size.replace(/size_([0-9]*)_.*/g, '$1');
		if($$('#pack_content .board')[0] != null){
			$$('#pack_content .board')[0].insert({'after':new Element('div', {'class':'binding'})});
		}else{
			$('pack_content').insert({'top':new Element('div', {'class':'binding'})});
		}
		$$('#pack_content .binding')[0].insert({'bottom':new Element('img', {'src':$$('#' + packSelector_selectedId + ' img')[0].getAttribute('src')})});
		$$('#pack_content .binding')[0].insert({'bottom':new Element('p')});
		$$('#pack_content .binding p')[0].update('<strong>' + packSelector_productInformation[packSelector_selectedId].brand + '</strong><br />' + packSelector_productInformation[packSelector_selectedId].title + '<br /><span class="right_price">' + packSelector_productInformation[packSelector_selectedId].fullPrice.toString().replace(/\./g, ',') + '&nbsp;€</span>Taille&nbsp;: ' + size.replace(/size_[0-9]*_/g, ''));
		$$('#pack_content .binding p')[0].insert({'after':new Element('img', {'src':GFX + 'graphic/snowshop/pack_selector/update_choice.png'})});
		$$('#pack_content .binding img')[1].setStyle({'border':'none', 'cursor':'pointer'});
		$$('#pack_content .binding img')[1].observe('click', packSelector_tabHandler);
	}

	if($$('#pack_content .full_total')[0] != null){
		$$('#pack_content .full_total')[0].remove();
	}
	if($$('#pack_content .atc')[0] != null){
		$$('#pack_content .atc')[0].remove();
	}
	if($$('#pack_content .board')[0] != null && $$('#pack_content .binding')[0] != null){
		$('pack_content').insert({'bottom':new Element('div', {'class':'full_total'})});
		if(packSelector_selectedBoard.burton && packSelector_selectedBinding.burton){
			$$('#pack_content .full_total')[0].update('<strong>TOTAL</strong><span class="total_price">' + (parseFloat(packSelector_selectedBoard.fullPrice) + parseFloat(packSelector_selectedBinding.fullPrice)).toFixed(2).toString().replace(/\./g, ',') + '&nbsp;€</span>');
		}else{
			$$('#pack_content .full_total')[0].update('<strong>TOTAL</strong><span class="total_price">' + (parseFloat(packSelector_selectedBoard.packPrice) + parseFloat(packSelector_selectedBinding.packPrice)).toFixed(2).toString().replace(/\./g, ',') + '&nbsp;€</span><br /> au lieu de ' + (parseFloat(packSelector_selectedBoard.fullPrice) + parseFloat(packSelector_selectedBinding.fullPrice)).toFixed(2).toString().replace(/\./g, ',')  + '&nbsp;€');
		}

		$('pack_content').insert({'bottom':new Element('div', {'class':'atc'})});
		if(packSelector_selectedBoard.reserved || packSelector_selectedBinding.reserved){
			$$('#pack_content .atc')[0].insert({'bottom':new Element('img', {'src':GFX + 'graphic/snowshop/pack_selector/bt_pre_command.png'})});
		}else{
			$$('#pack_content .atc')[0].insert({'bottom':new Element('img', {'src':GFX + 'graphic/snowshop/pack_selector/order.png'})});
		}
		$$('#pack_content .atc img')[0].observe('click', packSelector_addRequest);
	}

	$('packselector').scrollTo();
	if(packSelector_activeTab == 'board'){
		packSelector_tabHandler('tab_binding');
	}
}

function packSelector_addRequest(event){
		//Stop event propagation
	Event.stop(event);

	$$('#pack_content .atc img')[0].stopObserving('click', packSelector_addRequest);
	new Ajax.Request('/carts/ajax_addPackSelector/', {
		'parameters':{
			'size':packSelector_selectedBoard.id + ' => ' + packSelector_selectedBoard.selectedSizeId + ', ' + packSelector_selectedBinding.id + ' => ' + packSelector_selectedBinding.selectedSizeId,
			'product':'5800-' + packSelector_selectedBoard.id + '-' + packSelector_selectedBinding.id
		},
		'onComplete':packSelector_addResponse
	});
}

function packSelector_addResponse(response){
	window.location= SITE + 'mon_panier/';
}