if ((typeof Tiendy) == 'undefined') {
  var Tiendy = {};
}



function uniqueArray( myArr ) {
  var r = [];
  o : for( var i = 0, n = myArr.length; i < n; i++ ){
    for( var x = 0, y = r.length; x < y; x++ ){
      if( r[ x ] == myArr[ i ] ){
        continue o;
      }
    }
    if( !!myArr[ i ] ) r[ r.length ] = myArr[ i ];
  }
  return r;
}


Tiendy.attributeSelects = function(currentSelect, options) {

	
	// Oculto el select actual
	// jQuery('#' + currentSelect).css('display','none');
	current = document.getElementById(currentSelect);
	if (current) {
		current.style["display"] = "none";
	}
	
	this.product = options.product;
	this.currentSelect = currentSelect;
	
	// Sacar los valores de cada atributo
	
	this.values = {};
	for (var i in this.product.options) {
		this.values['option' + i] = new Array();
		for (k = 0; k < this.product.variants.length; k++) {
			
				this.values['option' + i][k] = this.product.variants[k]['option' + i];
			
		}	
		this.values['option' + i] = uniqueArray(this.values['option' + i]);
	}
	
	//1) Crear los selects
	var html = '';
	for (var i in this.product.options) {
		html += '<div class="attribute-selector-group"><label>' + this.product.options[i] + '</label><select id="variant-attribute-' + i + '" class="attribute-selector">';

		for (k = 0; k < this.values['option' + i].length; k++) {
			html += '<option';
			if (k == 0)
				html += ' selected="selected"';
			html += '>' + this.values['option' + i][k] + '</option>';
		}
		html += '</select></div>'; 
		
	}
	jQuery('#' + currentSelect).after(html);
	
	jQuery('.attribute-selector').each (function (i) {
		jQuery(this).change(function (e) {
			var id = jQuery(this).attr('id');
			
			var sels = jQuery('.attribute-selector');
			var valores = new Array();
			for (i = 0; i < sels.length; i++) {
				vid = jQuery(sels[i]).attr('id');
				tmp = vid.split('-'); // variant-attribute-X
				pos = parseInt(tmp[2]);
				// valores['option'+(i+1)] = jQuery(sels[i]).val();
				valores['option'+(pos)] = jQuery(sels[i]).val();
			}
			
			var variant = null;
			for (k = 0; k < options.product.variants.length && !variant; k++) {
				if ( 
				(typeof(valores['option1']) == 'undefined' || options.product.variants[k]['option1'] == valores['option1']) &&
				(typeof(valores['option2']) == 'undefined' || options.product.variants[k]['option2'] == valores['option2']) &&	
				(typeof(valores['option3']) == 'undefined' || options.product.variants[k]['option3'] == valores['option3']) &&	
				(typeof(valores['option4']) == 'undefined' || options.product.variants[k]['option4'] == valores['option4']) &&	
				(typeof(valores['option5']) == 'undefined' || options.product.variants[k]['option5'] == valores['option5']) &&	
				(typeof(valores['option6']) == 'undefined' || options.product.variants[k]['option6'] == valores['option6'])) {
					variant = options.product.variants[k]; 
				}
			}
			if (variant){
				jQuery('#' + currentSelect).val(variant.id);
			}
			options.onAttributeSelected (variant, jQuery('#' + id));
			
		
		});
	});
	
	// Lanzar evento inicial
	var variant = this.product.variants[0];
	if (variant) {
		for (i = 1; i <= 6; i++) {
			if (variant['option'+i])
				jQuery('#variant-attribute-' + i).val(variant['option'+i]);
		}
		options.onAttributeSelected (variant, jQuery('.attribute-selector:first'));
	}

}

