
//
// Copyright (c) Art. Voronov Studio | http://www.sobcom.ru/
// Author - sobcom@rambler.ru
//


var start_current_product = 20;
var start_left = -150;
var start_right = 300;
var animate_duration = 0.8;
var animate_easingFunc = Tween.strongEaseInOut;



DoubleSelectChooser = function(sProduct_name, aProduct, oProduct1, oProduct2, subtypeCode, productCode, SelectedSubtypeOption){
	this.init(sProduct_name, aProduct, oProduct1, oProduct2, subtypeCode, productCode, SelectedSubtypeOption)
}

var d = DoubleSelectChooser.prototype;

d.init = function(sProduct_name, aProduct, oProduct1, oProduct2, subtypeCode, productCode, SelectedSubtypeOption){
	if (!arguments.length) return;
	this.sProduct_name = sProduct_name;
	this.aProduct = aProduct;
	this.oProduct1 = oProduct1;
	this.oProduct2 = oProduct2;
	this.subtypeCode = subtypeCode;
	this.productCode = productCode;
	
	this.selectedType = 0;
	this.selectedProduct = 0;
	this.current_product = this.oProduct1;
	this.not_current_product = this.oProduct2;
	this.exchange_product;
	this.not_current_product_move;
	this.current_product_move;
	this.is_moving = false;
	
	for ( var i = 0; i < this.aProduct.length; i++){
		this.subtypeCode.options[i]= new Option(this.aProduct[i][0]);
	}
	if (SelectedSubtypeOption != 0) {
		this.subtypeCode.selectedIndex = (SelectedSubtypeOption - 1);
	}
	if(this.subtypeCode.length == 1){
		this.subtypeCode.style.display = 'none';
	}
	
	if (typeof this.sProduct_name != 'undefined' && this.sProduct_name !=''){

		for ( var i in this.aProduct){
			for ( var j in this.aProduct[i][1]){
				if(this.aProduct[i][1][j].name == this.sProduct_name){
					this.subtypeCode.selectedIndex = this.selectedType = i;
					this.selectedProduct = j;
				}
			}
		}


	}
	
	this.change_type();
	
}

d.callback = function(){
	
}








d.change_type = function () {
	/* выбор из селекта типа продуктов */
	/* удаляем опшины продуктов */
//	for ( var i =  productCode.options.length - 1; 0; i--){
//		productCode.options[i]= null;
//	}
	this.productCode.options.length = 0;
	var selectedType_old = this.selectedType;
	/*назначаем текущий тип продуктов*/
	var iType = this.selectedType = (this.subtypeCode.selectedIndex == -1) ? 0 : this.subtypeCode.selectedIndex;
	
	/* добавляем новые опшины продуктов */
	for (var i = 0; i < this.aProduct[iType][1].length; i++) {
		if (typeof this.aProduct[iType][1][i] != 'undefined') {
			this.productCode.options[i] = new Option(this.aProduct[iType][1][i].name_selector ? this.aProduct[iType][1][i].name_selector : this.aProduct[iType][1][i].name, this.aProduct[iType][1][i].url ? this.aProduct[iType][1][i].url : '');
		}
	}

	
	/* обновляем выбранный продукт */
	if(this.selectedProduct > 0 && this.selectedProduct < this.aProduct[iType][1].length ){
		this.productCode.selectedIndex = this.selectedProduct;
	} else {
		this.selectedProduct = this.productCode.selectedIndex ;
	}
	
	/* переходим сразу к анимации */
	this.show_product((selectedType_old > this.subtypeCode.selectedIndex) ? 'up' : 'down');
	
	return true;
}

d.select_product = function() {
	/* выбор из селекта продукта */
	var selectedProduct_old = this.selectedProduct;
	this.selectedProduct = this.productCode.selectedIndex;
	this.show_product(((selectedProduct_old > this.selectedProduct) ? '+' : '-'),true);
	return true;
}

d.show_next_product_back = function () {
	this.show_next_product('-');
}

d.show_next_product_forward = function () {
	this.show_next_product('+');
}

d.show_next_product = function (direction) {
	/* клик по стрелке */
	if( typeof this.aProduct[this.selectedType][1][this.selectedProduct + (direction == '+' ? 1 : -1)] != 'undefined'){
		this.selectedProduct = this.selectedProduct + (direction == '+' ? 1 : -1);
	} else {
		if( this.selectedProduct == 0){
			this.selectedProduct = this.aProduct[this.selectedType][1].length - 1;
		} else {
			this.selectedProduct = 0
		}
	}
	this.show_product(direction);
}

d.show_product = function (direction,not_change_select) {
	if( typeof this.aProduct[this.selectedType][1][this.selectedProduct] != 'undefined'){
		
		if( this.is_moving ){
			//this.not_current_product_move.fforward();
			//this.current_product_move.fforward();
		}
		
		if(typeof this.aProduct[this.selectedType][1][this.selectedProduct].url != 'undefined')this.not_current_product.url.href = this.aProduct[this.selectedType][1][this.selectedProduct].url;
		this.not_current_product.name.innerHTML = this.aProduct[this.selectedType][1][this.selectedProduct].name;
		this.not_current_product.image.src = this.aProduct[this.selectedType][1][this.selectedProduct].img;
		
		this.is_moving = true;
		
		if( direction == '+' || direction == '-'){
			/* клик по стрелкам или селект продукта */
			
			this.not_current_product.block.style.top = '0px'; /* выставляем то что не будет изменяться */
			this.current_product.block.style.top = '0px';
			
			this.current_product_move = new Tween(this.current_product.block.style,'left',animate_easingFunc, start_current_product , (direction == '+') ? start_right : start_left ,animate_duration,'px');
			this.current_product_move.start();
		
			this.not_current_product_move = new Tween(this.not_current_product.block.style,'left',animate_easingFunc, (direction == '+') ? start_left : start_right , start_current_product ,animate_duration,'px');
			this.not_current_product_move.start();
			
			
			if(!not_change_select){
				this.productCode.selectedIndex = this.selectedProduct;
			}
		} else {
			/* первая загрузка или изменение типа */
			this.not_current_product.block.style.left = start_current_product; /* выставляем то что не будет изменяться */
			
			this.not_current_product_move = new Tween(this.not_current_product.block.style,'top',animate_easingFunc, (direction == 'up') ? 150 : -150 , 0 ,animate_duration,'px');
			this.not_current_product_move.start();
			
			this.current_product_move = new Tween(this.current_product.block.style,'top',animate_easingFunc, 0 , (direction == 'up') ? -150 : 150 ,animate_duration,'px');
			this.current_product_move.start();
			
			
		}
		
		this.current_product_move.onMotionFinished = function () {
			/*
				current_product.url.href = '';
				current_product.name.innerHTML = 'aaa';
				current_product.image.src = '/f/1/global/d-t.gif';
			*/
			
			
			

		};
		this.update();
		this.callback();
		
	}
}

update = function(){
	d.update();
}

d.update = function(){
	exchange_product = this.current_product;
	this.current_product = this.not_current_product;
	this.not_current_product = exchange_product;
	exchange_product = '';
	
	this.is_moving = false;
}








var start_current_produc = 18;
var start_left = -150;
var start_right = 300;
var animate_duration = 0.8;
var animate_easingFunc = Tween.strongEaseInOut;



DoubleSelectChoose = function(sProduc_name, aProduc, oProduc1, oProduc2, subtypCode, producCode, SelectedSubtypeOptio){
	this.init(sProduc_name, aProduc, oProduc1, oProduc2, subtypCode, producCode, SelectedSubtypeOptio)
}

var d = DoubleSelectChoose.prototype;

d.init = function(sProduc_name, aProduc, oProduc1, oProduc2, subtypCode, producCode, SelectedSubtypeOptio){
	if (!arguments.length) return;
	this.sProduc_name = sProduc_name;
	this.aProduc = aProduc;
	this.oProduc1 = oProduc1;
	this.oProduc2 = oProduc2;
	this.subtypCode = subtypCode;
	this.producCode = producCode;
	
	this.selectedType = 0;
	this.selectedProduc = 0;
	this.current_produc = this.oProduc1;
	this.not_current_produc = this.oProduc2;
	this.exchange_produc;
	this.not_current_produc_move;
	this.current_produc_move;
	this.is_moving = false;
	
	for ( var i = 0; i < this.aProduc.length; i++){
		this.subtypCode.options[i]= new Option(this.aProduc[i][0]);
	}
	if (SelectedSubtypeOptio != 0) {
		this.subtypCode.selectedIndex = (SelectedSubtypeOptio - 1);
	}
	if(this.subtypCode.length == 1){
		this.subtypCode.style.display = 'none';
	}
	
	if (typeof this.sProduc_name != 'undefined' && this.sProduc_name !=''){

		for ( var i in this.aProduc){
			for ( var j in this.aProduc[i][1]){
				if(this.aProduc[i][1][j].name == this.sProduc_name){
					this.subtypCode.selectedIndex = this.selectedType = i;
					this.selectedProduc = j;
				}
			}
		}


	}
	
	this.change_type();
	
}

d.callback = function(){
	
}








d.change_type = function () {
	/* выбор из селекта типа продуктов */
	/* удаляем опшины продуктов */
//	for ( var i =  producCode.options.length - 1; 0; i--){
//		producCode.options[i]= null;
//	}
	this.producCode.options.length = 0;
	var selectedType_old = this.selectedType;
	/*назначаем текущий тип продуктов*/
	var iType = this.selectedType = (this.subtypCode.selectedIndex == -1) ? 0 : this.subtypCode.selectedIndex;
	
	/* добавляем новые опшины продуктов */
	for (var i = 0; i < this.aProduc[iType][1].length; i++) {
		if (typeof this.aProduc[iType][1][i] != 'undefined') {
			this.producCode.options[i] = new Option(this.aProduc[iType][1][i].name_selector ? this.aProduc[iType][1][i].name_selector : this.aProduc[iType][1][i].name, this.aProduc[iType][1][i].url ? this.aProduc[iType][1][i].url : '');
		}
	}

	
	/* обновляем выбранный продукт */
	if(this.selectedProduc > 0 && this.selectedProduc < this.aProduc[iType][1].length ){
		this.producCode.selectedIndex = this.selectedProduc;
	} else {
		this.selectedProduc = this.producCode.selectedIndex ;
	}
	
	/* переходим сразу к анимации */
	this.show_produc((selectedType_old > this.subtypCode.selectedIndex) ? 'up' : 'down');
	
	return true;
}

d.select_produc = function() {
	/* выбор из селекта продукта */
	var selectedProduc_old = this.selectedProduc;
	this.selectedProduc = this.producCode.selectedIndex;
	this.show_produc(((selectedProduc_old > this.selectedProduc) ? '+' : '-'),true);
	return true;
}

d.show_next_produc_back = function () {
	this.show_next_produc('-');
}

d.show_next_produc_forward = function () {
	this.show_next_produc('+');
}

d.show_next_produc = function (direction) {
	/* клик по стрелке */
	if( typeof this.aProduc[this.selectedType][1][this.selectedProduc + (direction == '+' ? 1 : -1)] != 'undefined'){
		this.selectedProduc = this.selectedProduc + (direction == '+' ? 1 : -1);
	} else {
		if( this.selectedProduc == 0){
			this.selectedProduc = this.aProduc[this.selectedType][1].length - 1;
		} else {
			this.selectedProduc = 0
		}
	}
	this.show_produc(direction);
}

d.show_produc = function (direction,not_change_select) {
	if( typeof this.aProduc[this.selectedType][1][this.selectedProduc] != 'undefined'){
		
		if( this.is_moving ){
			//this.not_current_produc_move.fforward();
			//this.current_produc_move.fforward();
		}
		
		if(typeof this.aProduc[this.selectedType][1][this.selectedProduc].url != 'undefined')this.not_current_produc.url.href = this.aProduc[this.selectedType][1][this.selectedProduc].url;
		this.not_current_produc.name.innerHTML = this.aProduc[this.selectedType][1][this.selectedProduc].name;
		this.not_current_produc.image.src = this.aProduc[this.selectedType][1][this.selectedProduc].img;
		
		this.is_moving = true;
		
		if( direction == '+' || direction == '-'){
			/* клик по стрелкам или селект продукта */
			
			this.not_current_produc.block.style.top = '0px'; /* выставляем то что не будет изменяться */
			this.current_produc.block.style.top = '0px';
			
			this.current_produc_move = new Tween(this.current_produc.block.style,'left',animate_easingFunc, start_current_produc , (direction == '+') ? start_right : start_left ,animate_duration,'px');
			this.current_produc_move.start();
		
			this.not_current_produc_move = new Tween(this.not_current_produc.block.style,'left',animate_easingFunc, (direction == '+') ? start_left : start_right , start_current_produc ,animate_duration,'px');
			this.not_current_produc_move.start();
			
			
			if(!not_change_select){
				this.producCode.selectedIndex = this.selectedProduc;
			}
		} else {
			/* первая загрузка или изменение типа */
			this.not_current_produc.block.style.left = start_current_produc; /* выставляем то что не будет изменяться */
			
			this.not_current_produc_move = new Tween(this.not_current_produc.block.style,'top',animate_easingFunc, (direction == 'up') ? 150 : -150 , 0 ,animate_duration,'px');
			this.not_current_produc_move.start();
			
			this.current_produc_move = new Tween(this.current_produc.block.style,'top',animate_easingFunc, 0 , (direction == 'up') ? -150 : 150 ,animate_duration,'px');
			this.current_produc_move.start();
			
			
		}
		
		this.current_produc_move.onMotionFinished = function () {
			/*
				current_produc.url.href = '';
				current_produc.name.innerHTML = 'aaa';
				current_produc.image.src = '/f/1/global/d-t.gif';
			*/
			
			
			

		};
		this.update();
		this.callback();
		
	}
}

update = function(){
	d.update();
}

d.update = function(){
	exchange_produc = this.current_produc;
	this.current_produc = this.not_current_produc;
	this.not_current_produc = exchange_produc;
	exchange_produc = '';
	
	this.is_moving = false;
}



