function add_product( product_id, count, auto_increment )
{
	if ( count == null ) count = 1;
	if ( auto_increment == null ) auto_increment = true;
	
	var products = [];
	var result = [];

	var cookie = $.cookie( 'products' );
	
	if ( cookie != null ) 
		products = cookie.split(',');

	var found = false;
	
	for ( var i = 0; i < products.length; i++ )
	{
		product = products[i].split('x');

		var current_product_id = product[0];
		var product_count = 1;
		
		if ( product[1] )
			product_count = Number( product[1] );
			
		if ( product[0] && product[0] == product_id )
		{
			if ( auto_increment )
				product_count = product_count + count;
			else
				product_count = count;
			
			found = true;
		}

		if ( product_count > 1 )
			result.push( current_product_id + 'x' + product_count );
		else if ( product_count == 1 )
			result.push( current_product_id );
	}
	
	if ( ! found )
	{
		if ( count > 1 )
			result.push( product_id + 'x' + count );
		else if ( count == 1 )
			result.push( product_id );
	}
	
	$.cookie( 'products', result.join( ',' ), { expires: 14, path: '/' } ); 

	$.post( '/basket/ajax_get_basket', {}, 
		function ( data )
		{
			set_basket_info( data );
		},
		'json'
	);
	
	return false;
}

function change_product_count( product_id, el )
{
	var count = parseInt( $( el ).val() );
	add_product( product_id, count, false );
}

function remove_product_from_basket( product_id, el )
{
	if ( ! confirm( 'Вы действительно хотите убрать товар из корзины?' ) ) return;
	
	add_product( product_id, 0, false ); 
	var row = $( el ).parent().parent();

	row.slideUp( 300, 
		function() 
		{
			row.remove();
		}
	);
}

function set_basket_info( info )
{
	$( 'span.basket_products_total_count' ).html( info.products_total_count.toString() );
	$( 'span.basket_total_price' ).html( info.total_price.toString() );
}

function search_by_price()
{
	var from = $( 'input#search_price_from' ).val();
	var to = $( 'input#search_price_to' ).val();
	
	window.location = '/search/product/pricefrom/' + from + '/priceto/' + to + '/';
}

function search_by_text()
{
	var text = $( 'input#search_text' ).val();
	
	window.location = '/search/product/name/' + text + '/';
}

function category_products_by_price()
{
	var to = $( 'input#category_price_to' ).val();
	
	window.location = $category_url + 'priceto/' + to + '/';
}

function check_order_form()
{
	if ( $( '#order_name' ).val() == '' )
	{
		alert( 'Необходимо указать имя' );
		$( '#order_name' ).focus();

		return false;
	}

	if ( $( '#order_phone' ).val() == '' )
	{
		alert( 'Необходимо указать контактный телефон' );
		$( '#order_phone' ).focus();

		return false;
	}

	return true;
}

$(
	function()
	{
		$( 'img.additional-image' ).click( 
			function()
			{
				$( 'img#main' ).attr( 'src', $( this ).attr( 'altsrc' ) );
			}
		);
	}
);


// JavaScript Document

function shw_hd_sbmn(obj) 
{
	if (document.getElementById(obj).style.display == 'none') 
	{
		document.getElementById(obj).style.display = 'block';
	} 
	else 
	{
		document.getElementById(obj).style.display = 'none';
	}
}
		
function change_brand(cat_id) 
{
	for (i=1; i<5; i++) 
	{
		document.getElementById('brnd0'+i).className = '';
		document.getElementById('cat0'+i).style.display = 'none';
	}
	
	document.getElementById('brnd0'+cat_id).className = 'selected';
	document.getElementById('cat0'+cat_id).style.display = 'block';
}


function show_desc(obj) 
{
	if ( document.getElementById(obj).style.display == 'none' ) 
	{
		document.getElementById(obj).style.display = 'block';
	} 
	else 
	{
		document.getElementById(obj).style.display = 'none';
	}
}
