(function($) {
	var methods = {
		init : function( options ) {
			var listDiv=this;
			var selectedItem=listDiv.find('.custom-select-list-item-selected');
			var selectedItemId=selectedItem.children('.custom-select-list-item-id');
			var selectedItemLabel=selectedItem.children('.custom-select-list-item-label');

			var listInput=listDiv.children('.custom-select-list-input');

			var listLabel=listDiv.children('.custom-select-list-label');
			
			var listItems=listDiv.children('.custom-select-list-items');
			var listItemsUl=listItems.children('.custom-select-list-items-ul');
			var listItemsLi=listItemsUl.children('.custom-select-list-item');
			
			if (listItems.height() >  listItemsUl.height() ) listItems.css({height:'auto', overflow:'hidden'});
			
			listDiv[($.fn.hoverIntent) ? 'hoverIntent' : 'hover'](over,out);
			listInput.val(selectedItemId.html());
			if (selectedItemLabel.html() != null) {
				listLabel.html(selectedItem.html());
				listDiv.data('lastValue', listInput.val());
			}
			
			listItemsUl.css({height:listItemsUl.height()});
			listItemsUl.scrollTop(0);
			
			function over() {
				listDiv.click(function() {
					listItems.css({top: 'auto'});
				});
			}
			function out() {
				listDiv.unbind('click');
				listItems.css({top:-999});
			}
			function li_over() {
				$(this).addClass('custom-select-list-item-hover');
			}
			function li_out() {
				$(this).removeClass('custom-select-list-item-hover');
			}
			listItemsLi.each(function() {
				$(this)[($.fn.hoverIntent) ? 'hoverIntent' : 'hover'](li_over,li_out);
				$(this).click(function() {
				//warunek if last!=new
					listDiv.unbind('click');
					listItems.css({top:-999});
					listItem=$(this);
					listItemsLi.each(function(){
						$(this).removeClass('custom-select-list-item-selected');
					});
					$(this).addClass('custom-select-list-item-selected');
					label=$(this).html();			
					id=$(this).children('.custom-select-list-item-id').html();	
					listInput.val(id);
					listLabel.html(label);
					if (listDiv.data('lastValue') != listInput.val()) {
						listDiv.data('lastValue', listInput.val());
						listDiv.trigger('update');
					}
				});
			});
		}
	};

	$.fn.customSelectList = function( method ) {
		// Method calling logic
		if ( methods[method] ) {
		  return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
		  return methods.init.apply( this, arguments );
		} else {
		  $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
		}
	}
})(jQuery);
