﻿var disappear_duration = 300 / 1000;
var appear_duration    = 1800 / 1000;
function photo_show (img_src, dest_id, origin){
	dest_id = $('photo0');
	if (img_src != dest_id.src) {
		if (origin) {
			Effect.Pulsate($(origin), { pulses: 2, duration: 0.3 });
		}
		Effect.Fade(dest_id, {duration:(disappear_duration)});
		setTimeout('$("' + dest_id.id + '").src=\'' + img_src + '\';Effect.Appear("' + dest_id.id + '", {duration:' + (appear_duration) + '});', disappear_duration * 1000);
	}
}


var previous_time = 0;
var swap_duration = 0.5;
var disappear_duration = 500 / 1000;
var appear_duration    = 800 / 1000;
var tparent_action = new Array();


function display_childs (id) {
	Effect.BlindDown('child_' + id, {duration: appear_duration});
	Effect.Appear('child_' + id, {duration: appear_duration});
}

function load_form_part (url, destination, on_complete) {
	new Ajax.Updater (destination, url,
	                  {method:'get',
					   evalScripts : false,
	                   onCreate:function () {
	                            	//alert('url created : ' + "\n" + url)
	                            },
	                   onFailled:function () {
	                              	alert('url failled : ' + "\n" + url)
	                              }, 
	                   onComplete:function (transport) {
	                              	if (on_complete) {
	                              		eval(on_complete);
	                              	}
	                              }
	                 });
}
function show_child (id_container, content) {
	
	$(id_container).innerHTML = content;
	new Effect.BlindDown(id_container, {duration: appear_duration});
}

function load_child (url, id_container, param, option_error, posted, nofx) {
	i = 0;
	new Ajax.Request(url, {
	  parameters: { 
		id_parent    : param,
		option_error : option_error,
		posted       : posted,
		evalScripts  : true
	  },
	  onSuccess: function (response) {
		endDate = new Date;
		endDate = parseInt(endDate.getTime());
		if ('' != response.responseText) {
			
			if ('none' == $(id_container).parentNode.style.display) {
				$(id_container).parentNode.style.display = "block";

			}
			//Effect.Queues.get('global').interval = 15000;
			if (nofx) {
				$(id_container).innerHTML = response.responseText;
			}
			else {
				var  reg = new  RegExp("'", "g");
				var html = response.responseText;
				    html = html.replace(reg, "\\'");
				setTimeout("$('" + id_container + "').innerHTML = ''", disappear_duration * 1000);
				new Effect.BlindUp(id_container, {duration: disappear_duration});
				setTimeout("show_child('"+id_container+"', '"+html+"')", appear_duration * 1000);
				/*
				//new Effect.Appear(id_container, {duration: appear_duration, queue: { position: 'end', scope: 'menuxscope' }});
				//new Effect.BlindDown(id_container, {duration: appear_duration});
				//new Effect.Appear(id_container, {duration: appear_duration+5, queue: { position: 'end', scope: 'menuxscope' } });
				*/
				
			}
			var evaled = response.responseText.evalScripts();
			//alert(evaled);
		}
	}
	});
	
}
function hide_child (id_container) {
	new Effect.BlindUp(id_container, {duration: disappear_duration});
}
function change_product_image (uri_image) {
	if (uri_image != $('photo0').src) {
		$('photo0').src= uri_image ;
	}
}
var toption_price = new Array();
var toption_name  = new Array();
var nbr_option = 0;
var amount_0 = 0;
function get_num_option (v, t) {
	var key = 0;
	while (t[key]) {
		if (v == t[key]) {
			return key;
		}
		else {
			//alert(v + ' != ' + t[key]);
		}
		key++;
	}
	return false;
}
function get_selected_update_price (id_option, url) {
	idSelected = $(id_option).value;
	new Ajax.Request(url, {
	  parameters: { id_option: idSelected },
	  requestHeaders: {Accept: 'application/json'},
	
	  onSuccess: function (response) {
		jSonArray = response.responseText.evalJSON(true);
		update_price(id_option, jSonArray["prix_ve"], jSonArray["price_rule"]);
	}
	});
}

function update_price (option, amount, price_rule) {
	//alert(option+','+ amount+','+ price_rule)

	if (! $('prix_nombre')) {
		return false;
	}
	current_price = $('prix_nombre').innerHTML;
	if (0 == price_rule) {
		amount_0 = amount;
	}
	else if (1 == Math.abs(price_rule)) {
		amount = price_rule * amount;
		num_option = get_num_option(option, toption_name);
		if (false === num_option) {
			nbr_option++;
			num_option = nbr_option - 1;
			toption_name[num_option] = option;
		}
		else {
			//alert('option '+ option + ' exists : ' + num_option);
		}
		toption_price[num_option] = amount;
	}
	final_price = amount_0;
	if (toption_price.length > 0) {
		if (isNaN(final_price)) {
			final_price = 0;
		}
		for (key = 0 ; key <= (nbr_option - 1) ; key++) {
			price = toption_price[key];
			final_price += price;
			//alert(final_price+ ' + '+price);
		}
	}
	
	if (0 == final_price) {
		final_price = $('prix_nombre').innerHTML;
	}
	final_price = final_price.toString();
	if (-1 != final_price.lastIndexOf('.')) {
		final_price = final_price.replace('.', ',');
		if (final_price.lastIndexOf(',') != (final_price.length - 1) && final_price.lastIndexOf(',') != (final_price.length - 3)) {
			final_price += '0';
		}
	}
	$('prix_nombre').innerHTML = final_price;
	$('prix').style.display = 'block';
	if (current_price != final_price) {
		Effect.Pulsate($('prix_nombre'), { pulses: 2, duration: 0.3 });
		//Effect.Appear($('prix_nombre'), { duration: 4.0 });
	}
}
function ie_textarea_convert (temp_textarea_value) {
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var reg = new RegExp("(^|\r\n)(\r\n|$)", "g");
		while (temp_textarea_value.match(reg)) {
			temp_textarea_value = temp_textarea_value.replace(reg, "$1 $2");
		}
	}
	return temp_textarea_value;
}
function check_textarea (p, max_nbr_char, max_nbr_char_per_line, max_nbr_line) {
	
	temp_textarea_value = p.value;
	
	var textarea_cut_alert = false;
	if (0 != max_nbr_char) {
		temp_textarea_value_2 = ie_textarea_convert(temp_textarea_value);
		if (navigator.appName == 'Microsoft Internet Explorer') {
			var reg = new RegExp("\r\n", "g");
			var tline = temp_textarea_value_2.split(reg);
			limit = max_nbr_char + tline.length - 1;
		}
		else {
			limit = max_nbr_char;
		}
		
		if (temp_textarea_value.length > limit) {
			var reg = new RegExp("\r\n$", "g");
			temp_textarea_value = temp_textarea_value.replace(reg, '');
			final_length = limit;
			p.value = temp_textarea_value.substr(0, limit);
			alert('Vous avez dépassé le nombre maximum de caractères (' + max_nbr_char + ')');// + '\nlimit=' + limit + 
			textarea_cut_alert = true;
		}
	}
	
	if (max_nbr_char_per_line && max_nbr_line) {
		temp_textarea_value = ie_textarea_convert(temp_textarea_value);
		
		var reg = new RegExp("\r?\n", "g");
		var tline = temp_textarea_value.split(reg);
		var textarea_value = '';
		var textarea_line_cut     = false;
		var textarea_line_deleted = false;
		var textarea_line_cut_alert = false;
		
		for (var i = 0 ; i <= (tline.length - 1) ; i++) {
			if (i <= (max_nbr_line - 1)) {
				line_value = tline[i].substr(0, max_nbr_char_per_line);
				if (' ' != line_value) {
					if (tline[i].length > max_nbr_char_per_line && i == (tline.length - 1) && i != (max_nbr_line - 1)) {
						if (' ' == tline[i].substr(max_nbr_char_per_line, 1)) {
							textarea_value += line_value;
							textarea_value += "\n" + tline[i].substr((max_nbr_char_per_line + 1), max_nbr_char_per_line);
						}
						else if (-1 != line_value.lastIndexOf(' ')) {
							textarea_value += tline[i].substr(0, line_value.lastIndexOf(' '));
							textarea_value += "\n" + tline[i].substr((line_value.lastIndexOf(' ') + 1), max_nbr_char_per_line);
						}
						else {
							textarea_value += line_value;
							textarea_value += "\n" + tline[i].substr(max_nbr_char_per_line, max_nbr_char_per_line);
						}
					}
					else {
						textarea_value += line_value;
					}
				}
				if (i != (max_nbr_line - 1) && i != (tline.length - 1)) {
					textarea_value += "\n";
				}
				if (tline[i].length > max_nbr_char_per_line) {
					textarea_line_cut = true;
					if (i != (tline.length - 1) || i == (max_nbr_line - 1)) {
						textarea_line_cut_alert = true;
					}
				}
			}
			else {
				textarea_line_deleted = true;
				alert('Vous avez dépassé le nombre maximum de lignes autorisé (' + max_nbr_line + ')');
				break;
			}
		}
		if (textarea_line_cut || textarea_line_deleted) {
			p.value = textarea_value;
			if (false !== textarea_line_cut && textarea_line_cut_alert && false === textarea_cut_alert) {
				alert('Vous avez dépassé le nombre maximum de caractères par ligne autorisé (' + max_nbr_char_per_line + ')');
			}
		}
	}
}