var po_s = {}
function fn_on_change_selectbox_option(product_id, option_id)
{
	value = $('#option_' + product_id + '_' + option_id).val();

	var i = 0;
	if (value == '') {
		for (po_id in pr_o[product_id]) {
			if (pr_o[product_id][po_id].type == 'S' && pr_o[product_id][po_id].inventory == 'Y') {
				i++;
				$('#option_' + product_id + '_' + po_id).empty();
				$('#option_' + product_id + '_' + po_id).append( $('<option value=' + '' + '>' + options_lang.select + '</option>'));
				$('#option_' + product_id + '_' + po_id).removeClass('main_selectbox');
				for (v_id in pr_o[product_id][po_id]['v']) {
					$('#option_' + product_id + '_' + po_id).append($('<option value=' + v_id + '>' + pr_o[product_id][po_id]['v'][v_id] + '</option>'));
				}
				$('#option_' + product_id + '_' + po_id + ' :first').attr("selected", "selected");

				if (i > 1) {
					break;
				}
			}
		}

		fn_check_exceptions(product_id);
		delete po_s[product_id];
		return;
	}
	
	if (po_s[product_id] == undefined) {
		po_s[product_id] = {}
	}

	change_option_value(product_id, option_id);
}

function check_variant(id, variants, combination_key)
{
	for (v_id in variants) {
		if (combination_key.indexOf(id + '_' + v_id + '_') >= 0) {
			return v_id;
		}
	}

	return false;
}
function lenght_of_arrray(arr)
{
var nelem = 0;
for(var i in arr) { nelem++ }
return nelem;
}
function sortBubble(keys, data) {
    var tmp;

	for (var i = lenght_of_arrray(data) - 1; i > 0; i--) {
		for (var j = 0; j < i; j++) {
			if (data[j] > data[j+1]) {
				tmp = data[j];
				tmpkey = keys[j];
				data[j] = data[j+1];
				data[j+1] = tmp;
				keys[j] = keys[j+1];
				keys[j+1] = tmpkey;
			}
		}
	}
}

function change_option_value(product_id, option_id)
{
	if (po_s[product_id]['main_option_value'] == undefined) {
		po_s[product_id]['main_option_value'] = pr_o[product_id][option_id]['selected_value'];
		po_s[product_id]['main_option_id'] = option_id;
		var i = 0;

		for (po_id in pr_o[product_id]) {
			if (pr_o[product_id][po_id].type == 'S'  && pr_o[product_id][po_id].inventory == 'Y') {
				i++;
				if (po_id != option_id) {
					po_s[product_id]['slave_option_value'] = pr_o[product_id][po_id]['selected_value'];
					po_s[product_id]['slave_option_id'] = po_id;
					break;
				}
			}
		}

		$('#option_' + product_id + '_' + option_id).addClass('main_selectbox');
	}

	if (option_id == po_s[product_id]['main_option_id'] && typeof(po_s[product_id]['slave_option_id']) != 'undefined') {
		var slave_id = po_s[product_id]['slave_option_id'];
		var starting = true;
		var slave_selected_value = pr_o[product_id][slave_id]['selected_value'];
		var option_value_not_changed = false;

		$('#option_' + product_id + '_' + slave_id).empty();
		$('#option_' + product_id + '_' + slave_id).append( $('<option value=' + '' + '>' + options_lang.none + '</option>'));

		var slave_values = {
			'v_id': {},
			'vp': {}
		};

		var i = 0;

		for (combination_key in pr_a[product_id]) {
			  if (combination_key.indexOf(option_id + '_' + pr_o[product_id][option_id]['selected_value'] + '_') >= 0 && pr_a[product_id][combination_key]['amount'] != '0') {
					 var v_id = check_variant(slave_id, pr_o[product_id][slave_id]['v'], combination_key);
					if (v_id != false) {
						option_value_not_changed = (v_id == slave_selected_value) ? true : option_value_not_changed;
						if (starting) {
							$('#option_' + product_id + '_' + slave_id).empty();
							$('#option_' + product_id + '_' + slave_id).append( $('<option value=' + '' + '>' + options_lang.select + '</option>'));
							starting = false;
						}
						for (v_id_ in slave_values) {
							if (v_id_ == v_id) {
								continue;
							}
						}
						slave_values['v_id'][i] = v_id;
						slave_values['vp'][i] = pr_o[product_id][slave_id]['vp'][v_id];
						i++;
					}
			  }
		}
	if (starting == false) {
	sortBubble(slave_values['v_id'], slave_values['vp']);

	for (key_v_id in slave_values['v_id']) {
			v_id = slave_values['v_id'][key_v_id];
			$('#option_' + product_id + '_' + slave_id).append( $('<option value=' + v_id + '>' + pr_o[product_id][slave_id]['v'][v_id] + '</option>'));
		}
	}

	if (option_value_not_changed) {
		$('#option_' + product_id + '_' + slave_id + ' [value=' + slave_selected_value + ']').attr("selected", "selected");

	} else {
		$('#option_' + product_id + '_' + slave_id + ' :nth-child(2)').attr("selected", "selected");
	}

	fn_check_exceptions(product_id);
	fn_change_variant_image(product_id, option_id, pr_o[product_id][slave_id]['selected_value']);

	}
}

function fn_out_of_stock_options_replace_stock_label(data)
{
	if ($('#selectboxes_flag_' + data['id']).val() == 'Y') {
	   $('#in_stock_info_' + data['id']).hide();
	}
}
