
function check_box_click( img_id, name ){
	
	if( $('#' + img_id).hasClass('checked') == true ){
		$('#' + img_id).removeClass('checked');
		$('#' + name).removeAttr('checked');
	}else{
		$('#' + img_id).addClass('checked');
		$('#' + name).attr('checked', 'checked');
	}

}

function combo_box_click_handler(e){
	
	var el = null;
	
	if( e.target == undefined ){
		class_name = e.srcElement.className;
		el = e.srcElement;
	}else{
		class_name = e.target.className;
		el = e.target;
	}
	
	
	if( el != null ){
	
		if( 
			$(el).hasClass('combo_box_items') == false && 
			$(el).hasClass('combo_box_item') == false && 
			$(el).hasClass('combo_box_list') == false &&
			$(el).hasClass('combo_box_value') == false &&
			$(el).hasClass('combo_box_button') == false
			){
		
			// console.log( el );
			
			$('.combo_box_value').removeClass('opened');
			$('.combo_box_button').removeClass('opened');			
				
			$('.combo_box_list').hide();
				
		}
		
	}

}


function class_combo_box( id, params ){
	
	var o = {
		
		id: id,
		
		values: [],
		
		click: function(){
			
			var pos = $('#combobox_' + id).position();
			
			var width = $('#combobox_' + id).outerWidth();
			
			$('#combo_box_list_' + this.id).css('width', width);
			
			if( $('#combo_box_list_' + this.id).css('display') == 'block' ){
				
				$('#combo_box_list_' + this.id).hide();

				$('#combo_box_value_' + this.id).removeClass('opened');
				$('#combo_box_button_' + this.id).removeClass('opened');

				
			}else{

				$('#combo_box_list_' + this.id).show();

				$('#combo_box_value_' + this.id).addClass('opened');
				$('#combo_box_button_' + this.id).addClass('opened');
				
			}
	
		},
		
		item_mouseout: function(item){
			
			$( item ).removeClass('combo_box_active_item');
			
		},
		
		item_mouseover: function(item){
			
			$( item ).addClass('combo_box_active_item');
			
		},
		
		item_click: function(item, index){
			
			
			this.private_item_click(item, index);
			
			
		},
		
		private_item_click: function(item, index){
			
			var key = this.values[ index ][0];
			var value = this.values[ index ][1];
			$('#combo_box_value_input_' + this.id).val(key);
			$('#combo_box_value_' + this.id).html( value );
			
			$('#combo_box_list_' + this.id).hide();
			
			$('#combo_box_value_' + this.id).removeClass('opened');
			$('#combo_box_button_' + this.id).removeClass('opened');			
			
		}
		
	}
	
	
	o.values = params.values;


	// Перехват события click на всей странице.
	if(document.addEventListener){
		document.addEventListener('click', combo_box_click_handler, false);
	}else{
		document.attachEvent('onclick', function(e) {combo_box_click_handler(window.event);} );	
	}	

	
	return o;
	
}
