/****************************************/
/*										*/
/*		(c) 2011 DAVID KMENTA			*/
/*		david.kmenta@web-media.cz		*/
/*		VERSION: 0.5b					*/
/*										*/
/****************************************/

// KNOWN ISSUES:
// - Multiple doplňuje vždy na konec čárku
// - Nefunguje kliknutí přímo na CHECKBOX, pokud je zobrazen
// - OnChange zatím nic nevrací :-)
// - Neřeší se velikost okna
// - Hledání není plovoucí

zIndexRoll = 10000;

jQuery.fn.wmRolls = function(o) {
	var params = {
		roll		: ((o.roll == undefined) ? this : o.roll),
		multiple	: ((o.multiple == undefined) ? false : o.multiple),
		text		: ((o.text == undefined) ? '--- Vyberte ---' : o.text),
		hidden		: ((o.hidden == undefined) ? true : o.hidden),
		className	: ((o.className == undefined) ? 'wmRoll' : o.className),
		width		: ((o.width == undefined) ? false : o.width),
		height		: ((o.height == undefined) ? false : o.height),
		delay		: ((o.delay == undefined) ? 'fast' : o.delay),
		disabled	: ((o.disabled == undefined) ? false : o.disabled),
		search		: true,
		onchange	: false
	}
	
	var rollObj = $(params.roll); 
	
	// Zabrání redeklaraci
	var isRoll	= rollObj.hasClass(params.className);
	if(isRoll) return false;
	
	rollObj.addClass(params.className);
	if(params.width) rollObj.css('width', params.width);
	
	if($(rollObj).hasClass('disabled') || params.disabled) {
		rollObj.addClass('wmRoll-disabled');
		params.disabled = true;
	}
		
	if($('.title', rollObj).text()) params.text = $('.title', rollObj).text();
	
	// default init
	if(!params.disabled) {
		rollObj.click(function() {
			var contentData = $('.data', this);
			var dataCheckboxes = $('ul li :checkbox', contentData);
			
			if(params.height) contentData.css('height', params.height);
			
			if(contentData.css('display') == 'none') {
				rollObj.css('zIndex', zIndexRoll++);
				contentData.slideDown(params.delay, function() {
					if(params.multiple) {
						contentData.click(function() {
							return false;
						});
					}
					
					$('body').one('click', function(e) {
						contentData.slideUp(params.delay);
					});
				});
			} else {
				if(!params.multiple) {
					contentData.slideUp(params.delay);
				}
			}
			
			initDefaultText();
		});
		
		if(!params.multiple) {
			$('.data ul', rollObj).prepend('<li' + ((!initChecked()) ? ' class="active"' : '') + '><p><input type="checkbox"' + ((!initChecked()) ? ' checked="checked"' : '') + '> ' + params.text + '<\/p><\/li>');
		}
		
		$('.data ul li', rollObj).each(function() {
			$(this).click(function() {
				if(!params.multiple) {
					$('.data ul li :checkbox', rollObj).each(function() {
						$(this).attr('checked', false);
					});
				}
				
				$(':checkbox', this).attr('checked', !$(':checkbox', this).attr('checked'));
				
				initDefaultText();
			});
		});
		
		if(params.search) {
			// Sanity check 
			var inp, rgx = new RegExp(), titles = $('.data ul li p', rollObj), keys;
			if (titles.length === 0) {
				return false;
			}
			
			// The list with keys to skip (esc, arrows, return, etc)
			// 8 is backspace, you might want to remove that for better usability
			keys = [ 13, 27, 32, 37, 38, 39, 40 ];
			
			if($('.data .search', rollObj).length < 1) $('.data', rollObj).prepend('<div class="search"><input type="text" style="width: 100%"><\/div>');
			$('.data .search :input', rollObj).bind('keyup', function (e) {
				if (jQuery.inArray(e.keyCode, keys) >= 0) {
					return false;
				}
			
				// Building the regex from our user input, 'inp' should be escaped
				inp = $(this).attr('value');
				rgx.compile(inp, 'im');
				titles.each(function () {
					if (rgx.source !== '' && !rgx.test($(this).html())) {
						$(this).parent('li').hide();
					} else {
						$(this).parent('li').show();   
					}
				});
			});
		}
	}
	
	function disableRoll() {
		$(rollObj).addClass('wmRoll-disabled');
		params.disabled = true;
	}
	
	function initDefaultText() {
		
		var newText = "";
		$('.data ul li :checkbox', rollObj).each(function() {
			if(params.hidden) $(this).css('display', 'none')
			newText += ($(this).is(':checked')) ? $(this).parent('p').text() + ((params.multiple) ? ',&nbsp;' : '') : '';
			
			$(':checkbox', $(this).parent()).each(function() {
				if($(this).is(':checked')) $(this).closest('li').addClass('active');
				else $(this).closest('li').removeClass('active');
			});
		});
		
		$('p:first', rollObj).html('<strong>' + ((newText == '') ? params.text : newText) + '<\/strong>');
	}
	
	function initChecked() {
		var anyChecked = false;
		
		$('.data ul li :checkbox', rollObj).each(function() {
			if($(this).is(':checked')) anyChecked = true;
		});
		
		return anyChecked;
	}
	
	initDefaultText();
	
	return {
		disableRoll: function() { disableRoll() }
	}
}
