var MooEngagement = new Class({
	initialize: function(config, options) {
		this.config=config;
	},
	parsingRingSize : function(ring_size_info){
		ring_size_array = ring_size_info.split(';');
		return(ring_size_array[0]);
	},
	buy : function(){
		showLoading(this.config.priceBlock);
		$(this.config.ringPriceFormName).send({onStateChange:this.config.onStateBuy, onComplete:this.config.onCompleteBuy});
	},
	getProductPrice : function (mode){
		showLoading(this.config.priceBlock);
		new Ajax(this.config.priceURL, {
			method:'get',
			data:'products_id='+on_products_id+'&get='+mode,
			update: this.config.priceBlock,
			evalScripts: true,
			onComplete: function(){
				;
			}.pass('', this)
		}).request();
	},
	updatePrice : function (mode){
		switch(mode){
		case 'size'	: populate_attributes(this.config.ringSizeFormName);break;
		default://size
			populate_attributes(this.config.ringSizeFormName);
			break;
		}
		this.getProductPrice(mode);
	},
	modifySize :function(selection_name, action){
		this.modify(this.config.ringSizeFormName, selection_name, action, 'ring');
		this.updatePrice('size');
	},
	modify : function(form_name, selection_name, action, attribute){
		inner_text = '';
		if(typeof(document.forms[form_name].elements[selection_name])!='undefined'){
			if(action==''){
				selection = document.forms[form_name].elements[selection_name];
				options_length = selection.length;
				for(i=0;i<options_length;i++){
					option_value=selection.options[i].text.replace(';def=true', '').replace(';def=false', '').replace(/ /g, '');
					attribute = attribute.replace(/ /g, '');
					if(option_value==attribute){
						selection.selectedIndex = i;
						break;
					}
				}
			}else{
				selection = document.forms[form_name].elements[selection_name];
				options_length = selection.length;
				selected_index = selection.selectedIndex;
				switch(action){
					case '+':
						selected_index++;				
						break;
					case '-':
						selected_index--;
						break;
				}
				if(selected_index>=options_length){
					if(attribute.indexOf('metal')!=-1){
						selected_index = 0;
					}else{
						selected_index = options_length -1;
					}
				}
				if(selected_index<0) selected_index = 0;
				selection.selectedIndex=selected_index;
				//diamond and ring size change use arrow button 
				if(selected_index==options_length-1){
					if ((x=$('plus_'+selection_name))!=null){x.src=this.config.ringSizePlusOff}
				}else{
					if ((x=$('plus_'+selection_name))!=null){x.src=this.config.ringSizePlusOn}
				}
				if(selected_index==0){
					if ((x=$('min_'+selection_name))!=null){x.src=this.config.ringSizeMinOff}
				}else{
					if ((x=$('min_'+selection_name))!=null){x.src=this.config.ringSizeMinOn}
				}

				switch(attribute){
					case 'ring':
						inner_text = this.parsingRingSize(selection.options[selected_index].text);
						if ((x=$('text_'+selection_name))!=null){x.innerHTML='<b>'+inner_text+'</b>';}
						break;
				}
			}
		}
	}
});
