$( function(){
	
	params = { 	items: '#so_items', 
				prev: '.arrow_up', 
				next: '.arrow_down',
				speed: 600, 
				mousewheel: true, 
				vertical: true
			}
	$( '#special_offer' ).scrollable( params )
	
	params = { 	items: '#action_items', 
				prev: '.arrow_up', 
				next: '.arrow_down',
				speed: 600, 
				mousewheel: true, 
				vertical: true
			}
	$( '#actions' ).scrollable( params )
	
	
	$( '.offers_wrapper, .actions_wrapper' ).mouseover( function(){
		$(this).find( '.arrows' ).show()
	}).mouseout(function(){
		$(this).find( '.arrows' ).hide()
	})
	
	
	$( '.quick_search' ).live( "submit", function(){
		if ( $( '#popup_category_select .select_button' ).length > 0 ){
			$( '#popup_category_select .select_button' ).click()
		}
		if ( $( '#popup_company_select .select_button' ).length > 0 ){
			$( '#popup_company_select .select_button' ).click()
		}
		$( '.search_form' ).submit()
		return false;
	})
	$( '#category_select, #category_search_select' ).colorbox()
		$( '#company_select, #company_search_select' ).colorbox()
	
	$( "#popup_category_select input[name='category[]']" ).live( "click", function(){
		set_selected_count( '#popup_category_select', 'category[]' );
	})
	$( "#popup_category_select input[name='parent_category[]']" ).live( "click", function(){
		$( '.child_' + $(this).val() ).attr( 'checked', $(this).attr( 'checked' ) )
		set_selected_count( '#popup_category_select', 'category[]' );
	})
	
	$( '#popup_category_select .close_button' ).live( 'click', function(){
		$.colorbox.close()
	})
	$( '#popup_category_select .select_button' ).live( 'click', function(){
		set_info_2_form( 'search_form', '#popup_category_select', 'category[]' )
		set_selected_count_2_form( 'furniture_search_form', 'category' )
		$.colorbox.close()
	})
	
	
	
	$( "#popup_company_select input[name='company[]']" ).live( "click", function(){
		set_selected_count( '#popup_company_select', 'company[]' );
	})
	
	$( '#popup_company_select .close_button' ).live( 'click', function(){
		$.colorbox.close()
	})
	$( '#popup_company_select .select_button' ).live( 'click', function(){
		set_info_2_form( 'search_form', '#popup_company_select', 'company[]' )
		set_selected_count_2_form( 'furniture_search_form', 'company' )
		$.colorbox.close()
	})
	
	
	price_from = $( ".search_form input[name='price_from']" ).eq(0).val() ? $( ".search_form input[name='price_from']" ).eq(0).val() : 0
	price_to = $( ".search_form input[name='price_to']" ).eq(0).val() ? $( ".search_form input[name='price_to']" ).eq(0).val() : 50000
	
	var x = $( "#slider-range" ).slider({
			range: true,
			min: 0,
			max: 50000,
			step: 500,
			values: [ price_from, price_to ],
			slide: set_input_value,
			change: check_slider_value
		});
	$( ".search_form input[name='price_from']" ).val( price_from )
	if ( price_to < 50000 ){
		$( ".search_form input[name='price_to']" ).val( price_to );
	}
})

function set_selected_count_2_form( form_id, input_name ){
	
	var cnt = $( "#" + form_id + " input[name='" + input_name + "[]']" ).length
	if ( cnt > 0 ){
		$( '#' + form_id + ' .' + input_name + '_select_text' ).text( '(' + cnt + ')' )
	} else {
		$( '#' + form_id + ' .' + input_name + '_select_text' ).text( '(все)' )
	}
	
}

function set_info_from_form( form_class, container, input_name ){
	$( '.' + form_class + " :hidden[name='" + input_name + "']" ).each( function(){ $( container + " input[name='" + input_name + "'][value='" + $(this).val() + "']" ).attr( 'checked', true ) } )
}

function set_info_2_form( form_class, container, input_name ){
	$( '.' + form_class + " :hidden[name='" + input_name + "']" ).remove()
	$( container + " input[name='" + input_name + "']:checked" ).each(function(){
		$( '.' + form_class ).append( '<input type="hidden" name="' + input_name + '" value="' + $(this).val() + '" />')
	})
}

function set_selected_count( container, input_name ){
	var cnt = $( container + " input[name='" + input_name + "']:checked" ).length
	$( container + ' .cnt' ).text( cnt )
}

function check_slider_value( event, ui ){
	if ( ui.values[ 1 ] == 50000 ){
		$( ".search_form input[name='price_to']" ).val( '' );
	}
}

function set_input_value( event, ui ){
	$( ".search_form input[name='price_from']" ).val( ui.values[ 0 ] )
	$( ".search_form input[name='price_to']" ).val( ui.values[ 1 ] );
	check_slider_value( event, ui )
}
