var fast_basket_selected_color = '0';

// #################################################

function FastBasket_UpdateStatus()
{
	new Ajax.Request( '/basket/act/ajax_get_status/',
	{
		method:'get',
		onSuccess: function(transport)
		{
			$j('#basket').html(transport.responseText);
		}
	});
}

// #################################################

function FastBasket_SetSuccessMessage( text )
{
	var html = '<div class="auth" style="padding:30px 0;"><span id="fast_basket_success" class="message-success">' + text + '</span></div>';
	$j('#fast_basket_container').html(html);
}
function FastBasket_SetErrorMessage( text )
{
	var html = '<div class="auth" style="padding:30px 0;"><span id="fast_basket_error" class="message-error">' + text + '</span></div>';
	$j('#fast_basket_container').html(html);
}

// #################################################

function FastBasket_WindowOpen( id_goods )
{
	//-------
	id_goods = id_goods || 0;
	if( id_goods == 0 ) return;
	//-------
	window_manager.Close();
	FastBasket_SetSuccessMessage('Чтение информации, секундочку...');
	window_manager.Open('window_source_basket_add');
	$j('#fast_basket_container').html('');
	
	temp_timer = setInterval( function(){
		clearInterval(temp_timer);
		new Ajax.Request( '/goods/act/ajax_get_fast_basket/?id_goods=' + id_goods,
		{
			method:'get',
			onSuccess: function(transport)
			{
				window_manager.Close();
				$j('#fast_basket_container').html(transport.responseText);
				window_manager.Open('window_source_basket_add');
				$j('#fast_basket_container').html('');
			}
		});
	} , 1000 );	
	//-------
}

function FastBasket_WindowClose( buy )
{
	if( buy || 0 )
	{
		window_manager.Close();
		FastBasket_SetSuccessMessage('Товар добавлен в корзину.');
		window_manager.Open('window_source_basket_add');	
		$j('#fast_basket_container').html('');
		
		temp_timer = setInterval( function(){
			clearInterval(temp_timer);
			window_manager.Close();	
		} , 2000 );	
	}
	else
		window_manager.Close();	
}

// #################################################

function FastBasket_ColorSelect( path , color )
{
	$j('#fast_basket_color_image').attr( 'src' , path );
	fast_basket_selected_color = color;
}

// #################################################

function FastBasket_LeftRightSelect( type )
{	
	if( type == 'equal' )
	{
		if( !$j('#fast_basket_eye_equal').hasClass('selected') )
		$j('#fast_basket_eye_equal').addClass('selected');
			
		if( $j('#fast_basket_eye_different').hasClass('selected') )
		$j('#fast_basket_eye_different').removeClass('selected');
		
		$j('#fast_basket_char_left_title').hide();
		$j('#fast_basket_char_right_title').hide();
		
		for (var i=1; i < 100; i++) 
		{
			if( $j('#fast_basket_char_left_container_' + i).attr('id') == undefined ) break;
			$j('#fast_basket_char_left_container_' + i).show();
		}
		for (var i=1; i < 100; i++) 
		{
			if( $j('#fast_basket_char_right_container_' + i).attr('id') == undefined ) break;
			$j('#fast_basket_char_right_container_' + i).hide();
		}
			
		$j('#fast_basket_amount_left_title').hide();
		$j('#fast_basket_amount_right_title').hide();
		
		$j('#fast_basket_amount_left_container').show();
		$j('#fast_basket_amount_right_container').hide();
	}
	if( type == 'different' )
	{
		if( !$j('#fast_basket_eye_different').hasClass('selected') )
		$j('#fast_basket_eye_different').addClass('selected');
			
		if( $j('#fast_basket_eye_equal').hasClass('selected') )
		$j('#fast_basket_eye_equal').removeClass('selected');
		
		$j('#fast_basket_char_left_title').show();
		$j('#fast_basket_char_right_title').show();
		
		for (var i=1; i < 100; i++) 
		{
			if( $j('#fast_basket_char_left_container_' + i).attr('id') == undefined ) break;
			$j('#fast_basket_char_left_container_' + i).show();
		}
		for (var i=1; i < 100; i++) 
		{
			if( $j('#fast_basket_char_right_container_' + i).attr('id') == undefined ) break;
			$j('#fast_basket_char_right_container_' + i).show();
		}
				
		$j('#fast_basket_amount_left_title').show();
		$j('#fast_basket_amount_right_title').show();
		
		$j('#fast_basket_amount_left_container').show();
		$j('#fast_basket_amount_right_container').show();
	}
}

// #################################################

function FastBasket_PriceUpdate( id_goods )
{	
	//-------------
	id_goods = id_goods || 0;
	if( id_goods == 0 ) return;
	//-------------

	//-------------
	var url = '?id_goods=' + id_goods;
	//-------------
	
	//-------------
	if( $j('#fast_basket_amount_left_input').val() == '' )
	url += '&amount_left=1';
	else
	url += '&amount_left=' + $j('#fast_basket_amount_left_input').val();
	
	if( $j('#fast_basket_eye_different').hasClass('selected') )
	{
		if( $j('#fast_basket_amount_right_input').val() == '' )
		url += '&amount_right=1';
		else
		url += '&amount_right=' + $j('#fast_basket_amount_right_input').val();
	}
	//-------------
	
	//-------------
	for (var i=1; i < 100; i++) 
	{
		if( $j('#fast_basket_char_left_select_' + i).attr('id') == undefined ) break;
		url += '&char_left_' + i + '=' + $j('#fast_basket_char_left_select_' + i).val();
	}
	
	if( $j('#fast_basket_eye_different').hasClass('selected') )
	{
		for (var i=1; i < 100; i++) 
		{
			if( $j('#fast_basket_char_right_select_' + i).attr('id') == undefined ) break;
			url += '&char_right_' + i + '=' + $j('#fast_basket_char_right_select_' + i).val();
		}
	}
	//-------------

	//-------------
	new Ajax.Request( '/goods/act/ajax_get_fast_basket_price/' + url,
	{
		method:'get',
		onSuccess: function(transport)
		{
			$j('#fast_basket_price_container').html(transport.responseText);
		}
	}
	);
	//-------------
}

// #################################################

function FastBasket_ToBasket( id_goods )
{
	//-------------
	id_goods = id_goods || 0;
	if( id_goods == 0 ) return;
	//-------------
		
	//-------------
	var url_left = '?id_goods=' + id_goods;
	//-------------
	
	//-------------
	if( $j('#fast_basket_amount_left_input').val() == '' )
	url_left += '&amount=1';
	else
	url_left += '&amount=' + $j('#fast_basket_amount_left_input').val();
	
	url_left += '&type_basket=1';
	
	for (var i=1; i < 100; i++) 
	{
		if( $j('#fast_basket_char_left_select_' + i).attr('id') == undefined ) break;
		url_left += '&char_' + i + '=' + $j('#fast_basket_char_left_select_' + i).val();
	}
	
	url_left += '&color=' + fast_basket_selected_color;
	//-------------
	
	//-------------	
	var url_right = '';
	//-------------
	
	//-------------
	if( $j('#fast_basket_eye_different').hasClass('selected') )
	{
		url_right = '?id_goods=' + id_goods;

		if( $j('#fast_basket_amount_right_input').val() == '' )
		url_right += '&amount=1';
		else
		url_right += '&amount=' + $j('#fast_basket_amount_right_input').val();
		
		url_right += '&type_basket=1';
		
		for (var i=1; i < 100; i++) 
		{
			if( $j('#fast_basket_char_right_select_' + i).attr('id') == undefined ) break;
			url_right += '&char_' + i + '=' + $j('#fast_basket_char_right_select_' + i).val();
		}
		
		url_right += '&color=' + fast_basket_selected_color;
	}
	//-------------

	//-------------
	FastBasket_SetSuccessMessage('Добавляется, секундочку...');
	//-------------
	
	//-------------
	temp_timer = setInterval( function(){
		clearInterval(temp_timer);
		
		new Ajax.Request( '/basket/act/add_goods/' + url_left,
		{
			method:'get',
			onSuccess: function(transport)
			{
				//-----------------			
				_gaq.push(['_trackPageview', '/addToCart']);
				
				if( url_right != '' )
				{					
					new Ajax.Request( '/basket/act/add_goods/' + url_right,
					{
						method:'get',
						onSuccess: function(transport)
						{
							_gaq.push(['_trackPageview', '/addToCart']);
							
							FastBasket_UpdateStatus();
							FastBasket_WindowClose(1);	
						}
					}
					);
				}
				else
				{
					FastBasket_UpdateStatus();
					FastBasket_WindowClose(1);
				}
				//-----------------			
			}
		});
	} , 1000 );	
	//-------------
}

// #################################################

