/* Ajax pour les formats */
var productAjax = function(){
	var fxChangeInfo = new Fx.Style('actualProductInfo','background-color');
	var fxChangeImage = new Fx.Style('productImage','opacity');
	
	var loading = function() {
		$('actualProductInfo').setStyle('visibility','hidden');
				if ($('loadingGif')){
					$('loadingGif').setStyle('visibility','visible');
				} else {
					var imgLoading = new Element('img', {
						'styles': {'position':'absolute', 'visibility':'visible'},
						'src': '../images/loading.gif',
						'id': 'loadingGif'
					});
					imgLoading.injectTop('actualProductInfo');
				}
	}; 
	
	function getOptionsAttrValue(){
		var attrValue = '';
		$$('#attributes .options').each(function(elm) { 
				attrValue += '&'+elm.name+'='+elm.value;
		});
		return attrValue;
	}
	
	var attributesFunc = function(){
		if ($('sizeSelected')) {
		   var sizeId = $('sizeSelected').getPrevious().value;
		}
		new Ajax('product_info_ajax.php', {
			method: 'post',
			data:   'ajax=options&product_id=' + $('ajaxProductId').getText() + '&size_id=' + sizeId + getOptionsAttrValue() ,
			onRequest: function(){
				loading();
			},
			onSuccess: function() {
				var r = Json.evaluate(this.response.text);
				if (r.error) {
				   eval(r.error);
				} else {
					$('loadingGif').setStyle('visibility','hidden');
					$('productPrice').setHTML(r.price);
					$('currentOptions').setHTML(r.currentOptions);
					$('actualProductInfo').setStyle('visibility','visible');
					$('buyProductSize').setProperty('value',r.size_id);
					fxChangeInfo.start('DACAB8','FDFAF4');
				}
			},
			onFailure: function() {
				$('actualProductInfo').setText('Une erreur c\'est produite veuillez réessayer.');
			}
		}).request();
		
	};

	var sizeFunc = function(e){
		var hiddenInputValue = this.getChildren()[0].value;
		var input = this.getChildren()[1];
		new Event(e).stop();
		this.setAttribute('action','product_info_ajax.php');
		new Ajax('product_info_ajax.php', {
			method: 'post',
			data:   'ajax=sizes&product_id=' + $('ajaxProductId').getText() + '&size_id=' + hiddenInputValue + getOptionsAttrValue(),
			onRequest: function(){
				loading();
			},
			onSuccess: function() {
				var r = Json.evaluate(this.response.text);
				if (r.error) {
				  eval(r.error);
				} else {
					$$('#sizes  .formInput').each(function(e){e.removeProperty('id')});
					input.setProperty('id','sizeSelected');
					
					$('loadingGif').setStyle('visibility','hidden');
					
					if ($('attributes')){
						$('attributes').setHTML(r.attributes);
						attributesFunc();
					} else { 
						$('productPrice').setHTML(r.price);
					}
					$('currentOptions').setHTML(r.currentOptions);
					$('actualSize').setHTML(r.size);
					$('actualProductInfo').setStyle('visibility','visible');
					$('buyProductSize').setProperty('value',r.size_id);
					fxChangeInfo.start('DACAB8','FDFAF4');
					if ($$('#attributes .options')) {
						$$('#attributes .options').each(function(elm){
								elm.addEvent('change', attributesFunc);
						});
					}
				}
			},
			onFailure: function() {
				$('actualProductInfo').setText('Une erreur c\'est produite veuillez réessayer.');
			}
		}).request();
	};
	
	$$('#sizes .formatForm').addEvent('submit', sizeFunc);
	if ($$('#attributes .options')) {
		$$('#attributes .options').each(function(elm){
			elm.addEvent('change', attributesFunc);
		});
	}
};

window.addEvent('domready', productAjax);

/* tabs */
var mootabs = new Class({
	initialize: function(element, options) {
		this.options = Object.extend({
			activateOnLoad:		'first'
		}, options || {});

		this.el = $(element);
		this.elid = element;
		this.titles = $$('#' + this.elid + ' ul li');
		this.panels = $$('#' + this.elid + ' .tabcontent');
		this.titles.each(function(item) {
			item.addEvent('click', function(){
					item.removeClass(this.options.mouseOverClass);
					this.activate(item);
				}.bind(this)
			);
		}.bind(this));
		this.activate(this.titles[0]);
	},
	
	activate: function(tab){
		if($type(tab) == 'string'){
			myTab = $$('#' + this.elid + ' ul li').filterByAttribute('title', '=', tab)[0];
			tab = myTab;
		}
		
		if($type(tab) == 'element'){
			var newTab = tab.getProperty('title');
			this.panels.removeClass('active');
			this.panels.addClass('nonActive');
			this.activePanel = this.panels.filterById(newTab)[0];
			this.activePanel.removeClass('nonActive');
			this.activePanel.addClass('active');
			this.titles.removeClass('activeTab');
			tab.addClass('activeTab');
			this.activeTitle = tab;
		}
	}
});
if (!window.ie6) document.write('<style type="text/css">.tabcontent{display:none;}</style>'); 
window.addEvent('domready', function() {
	new mootabs('tabs');
});
