﻿var multi = {
	id: 0,
	getID: function() {
		this.id++;
		return this.id;
	}
}


/*
// jQuery multiSelect
//
// Version 1.0 beta
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 06 April 2008
//
// Visit http://abeautifulsite.net/notebook.php?article=62 for more information
//
// Usage: $('#control_id').multiSelect( options, callback )
//
// Options:  selectAll          - whether or not to display the Select All option; true/false, default = true
//           selectAllText      - text to display for selecting/unselecting all options simultaneously
//           noneSelected       - text to display when there are no selected items in the list
//           oneOrMoreSelected  - text to display when there are one or more selected items in the list
//                                (note: you can use % as a placeholder for the number of items selected).
//                                Use * to show a comma separated list of all selected; default = '% selected'
//
// Dependencies:  jQuery 1.2 or higher (http://jquery.com/)
//                the jQuery Dimensions plugin (http://plugins.jquery.com/project/dimensions)
//
// Licensing & Terms of Use
// 
// jQuery File Tree is licensed under a Creative Commons License and is copyrighted (C)2008 by Cory S.N. LaViska.
// For details, visit http://creativecommons.org/licenses/by/3.0/us/
//	
*/
if(jQuery) (function($){
	
	$.extend($.fn, {
		multiSelect: function(o, callback) {
//			alert($(this).html());
			// Default options
			if( !o ) var o = {};
			if( o.selectAll == undefined ) o.selectAll = true;
			if( o.selectAllText == undefined ) o.selectAllText = "Выбрать все";
			if( o.noneSelected == undefined ) o.noneSelected = 'Выберите элемент';
			if( o.oneOrMoreSelected == undefined ) o.oneOrMoreSelected = '% выбрано';
			if ( o.workMode == undefined ) o.workMode = false;
			
			// Initialize each multiSelect
			$(this).each( function() {
				var select = $(this);
				select.wrap('<div class="multiSelectContainer"></div>');
				//select.after('<input type="text" readonly="readonly" class="multiSelect" value="" style="cursor: default;" />');
				var html = (jQuery)(document.createElement('div'))
						.addClass('multiSelectOptions')
						.attr('id', $(select).attr('id'))
						.css('cursor', 'default')
						.css('display', 'none')				
				//var html =
				//	'<input type="text" readonly="readonly" class="multiSelect" value="" style="cursor: default;" />\
				//	<div class="multiSelectOptions" id="'+$(select).attr('id')+'" style="display: none;">';
				//if( o.selectAll ) html += '<span class="label selectAll"><input type="checkbox" class="selectAll" />' + o.selectAllText + '</span>';
				$(select).find('OPTION').each( function() {
					if( $(this).val() != '' ) {
						multiID = multi.getID();
						line = (jQuery)(document.createElement('span'))
							.addClass('label');
						var checkbox = (jQuery)(document.createElement('input'))
								.attr('type', 'checkbox')
								.attr('name', $(this).attr('name'))
								.attr('value', $(this).val())
								.attr('id', 'multi_'+multiID);
						if ($(this).attr('selected')) {
							checkbox.attr('checked', 'checked');
							line.addClass('active');
						}
						var text = (jQuery)(document.createElement('label'))
								.html($(this).html())
								.attr('for', 'multi_'+multiID);
						line.append(checkbox);
//						line.append('<div class="full_size"><img src="'+$(this).html()+'" />'+$(this).attr('title')+'</div><a href="#"><img src="'+$(this).html()+'" width="15" /></a>');
						line.append('<a href="#" title="'+$(this).attr('title')+'"><img src="'+$(this).html()+'" width="15" /><span>'+$(this).attr('title')+'</span></a>');
						html.append(line);
					}
				});
				//html += '</div>';

				$(select).after(html);
				
				if ($.browser.opera) {
					$('.multiSelectOptions').css('position', 'static');
				}
//				$(select).after('<input type="text" readonly="readonly" class="multiSelect" value="" style="cursor: default;" />');
				$(select).after('<div class="multiSelect">Выберите</div>');				
//				$(select).next('.multiSelect').next('.multiSelectOptions').
//				$(select).next().find('.multiSelectOptions').css('width', $(select).next().html().find('input:text').width());
				
				// Events
				$(select).next('.multiSelect').mouseover( function() {
					$(this).addClass('hover');
				}).mouseout( function() {
					$(this).removeClass('hover');
				}).click( function() {
					// Show/hide on click
					if( $(this).hasClass('active') ) {
						$(this).multiSelectOptionsHide();
					} else {
						$(this).multiSelectOptionsShow();
					}
					return false;
				}).focus( function() {
					// So it can be styled with CSS
					$(this).addClass('focus');
				}).blur( function() {
					// So it can be styled with CSS
					$(this).removeClass('focus');
				});
				
				// Determine if Select All should be checked initially
				/*if( o.selectAll ) {
					var sa = true;
					$(select).next('.multiSelect').next('.multiSelectOptions').find('INPUT:checkbox').not('.selectAll').each( function() {
						if( !$(this).attr('checked') ) sa = false;
					});
					if( sa ) $(select).next('.multiSelect').next('.multiSelectOptions').find('INPUT.selectAll').attr('checked', true).parent().addClass('checked');
				}*/
				
				// Handle Select All
				$(select).next('.multiSelect').next('.multiSelectOptions').find('INPUT.selectAll').click( function() {
					/*if ($(this).attr('checked')) {
						$(this).parent().parent().find('INPUT:checkbox').not('.selectAll').each(function(){
							if (!$(this).attr('checked')) {
//								$(this).attr('checked', 'checked');
								$(this).trigger('click');
							}
							//$(this).trigger('click');
							$(this).parent().addClass('checked');
						});
					} else {
						$(this).parent().parent().find('INPUT:checkbox').not('.selectAll').each(function(){
							if ($(this).attr('checked')) {
//								$(this).removeAttr('checked');
								$(this).trigger('click');
							}
							$(this).parent().removeClass('checked');
						});
					}*/
				});
				
				// Handle checkboxes
				$(select).next('.multiSelect').next('.multiSelectOptions').find('INPUT:checkbox').click( function() {
					$(this).parent().parent().multiSelectUpdateSelected(o);
					$(this).parent().parent().find('span.label').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
					$(this).parent().parent().prev('.multiSelect').focus();
					//if( !$(this).attr('checked') ) $(this).parent().parent().find('INPUT:checkbox.selectAll').attr('checked', false).parent().removeClass('checked');
					if( callback ) callback($(this));
				});
				
				// Initial display
				$(select).next('.multiSelect').next('.multiSelectOptions').each( function() {
					$(this).multiSelectUpdateSelected(o);
					$(this).find('INPUT:checked').parent().addClass('checked');
				});
				
				// Handle hovers
				$(select).next('.multiSelect').next('.multiSelectOptions').find('span.label').mouseover( function() {
					$(this).parent().find('span.label').removeClass('hover');
					$(this).addClass('hover');
				}).mouseout( function() {
					$(this).parent().find('span.label').removeClass('hover');
				});
				
				// Keyboard
				$(select).next('.multiSelect').keydown( function(e) {
					// Is dropdown visible?
					if( $(this).next('.multiSelectOptions').is(':visible') ) {
						// Dropdown is visible
						// Tab
						if( e.keyCode == 9 ) {
							$(this).addClass('focus').trigger('click'); // esc, left, right - hide
							$(this).focus().next(':input').focus();
							return true;
						}
						
						// ESC, Left, Right
						if( e.keyCode == 27 || e.keyCode == 37 || e.keyCode == 39 ) {
							// Hide dropdown
							$(this).addClass('focus').trigger('click');
						}
						// Down
						if( e.keyCode == 40 ) {
							if( !$(this).next('.multiSelectOptions').find('span.label').hasClass('hover') ) {
								// Default to first item
								$(this).next('.multiSelectOptions').find('span.label:first').addClass('hover');
							} else {
								// Move down, cycle to top if on bottom
								$(this).next('.multiSelectOptions').find('span.label.hover').removeClass('hover').next('LABEL').addClass('hover');
								if( !$(this).next('.multiSelectOptions').find('span.label').hasClass('hover') ) {
									$(this).next('.multiSelectOptions').find('span.label:first').addClass('hover');
								}
							}
							return false;
						}
						// Up
						if( e.keyCode == 38 ) {
							if( !$(this).next('.multiSelectOptions').find('span.label').hasClass('hover') ) {
								// Default to first item
								$(this).next('.multiSelectOptions').find('span.label:first').addClass('hover');
							} else {
								// Move up, cycle to bottom if on top
								$(this).next('.multiSelectOptions').find('span.label.hover').removeClass('hover').prev('span.label').addClass('hover');
								if( !$(this).next('.multiSelectOptions').find('span.label').hasClass('hover') ) {
									$(this).next('.multiSelectOptions').find('span.label:last').addClass('hover');
								}
							}
							return false;
						}
						// Enter, Space
						if( e.keyCode == 13 || e.keyCode == 32 ) {
							// Select All
							if( $(this).next('.multiSelectOptions').find('span.label.hover INPUT:checkbox').hasClass('selectAll') ) {
								if( $(this).next('.multiSelectOptions').find('span.label.hover INPUT:checkbox').attr('checked') ) {
									// Uncheck all
									$(this).next('.multiSelectOptions').find('INPUT:checkbox').attr('checked', false).parent().removeClass('checked');
								} else {
									// Check all
									$(this).next('.multiSelectOptions').find('INPUT:checkbox').attr('checked', true).parent().addClass('checked');
								}
								$(this).next('.multiSelectOptions').multiSelectUpdateSelected(o);
								if( callback ) callback($(this));
								return false;
							}
							// Other checkboxes
							if( $(this).next('.multiSelectOptions').find('span.label.hover INPUT:checkbox').attr('checked') ) {
								// Uncheck
								$(this).next('.multiSelectOptions').find('span.label.hover INPUT:checkbox').attr('checked', false);
								$(this).next('.multiSelectOptions').multiSelectUpdateSelected(o);
								$(this).next('.multiSelectOptions').find('span.label').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
								// Select all status can't be checked at this point
								$(this).next('.multiSelectOptions').find('INPUT:checkbox.selectAll').attr('checked', false).parent().removeClass('checked');
								if( callback ) callback($(this));
							} else {
								// Check
								$(this).next('.multiSelectOptions').find('span.label.hover INPUT:checkbox').attr('checked', true);
								$(this).next('.multiSelectOptions').multiSelectUpdateSelected(o);
								$(this).next('.multiSelectOptions').find('span.label').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
								if( callback ) callback($(this));
							}
						}
						return false;
					} else {
						// Dropdown is not visible
						if( e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 13 || e.keyCode == 32 ) { // down, enter, space - show
							// Show dropdown
							$(this).removeClass('focus').trigger('click');
							$(this).next('.multiSelectOptions').find('span.label:first').addClass('hover');
							return false;
						}
						//  Tab key
						if( e.keyCode == 9 ) {
							// Shift focus to next INPUT element on page
							$(this).focus().next(':input').focus();
							return true;
						}
					}
					// Prevent enter key from submitting form
					if( e.keyCode == 13 ) return false;
				});
				
				// Eliminate the original form element
				$(select).remove();
			});
			
		},
		
		// Hide the dropdown
		multiSelectOptionsHide: function() {
			$(this).removeClass('active').next('.multiSelectOptions').hide();
		},
		
		// Show the dropdown
		multiSelectOptionsShow: function() {
			// Hide any open option boxes
			$('.multiSelect').multiSelectOptionsHide();
			$(this).next('.multiSelectOptions').find('span.label').removeClass('hover');
			$(this).addClass('active').next('.multiSelectOptions').show();
			
			// Position it
			var offset = $(this).offset();
			if ($.browser.mozilla) {
				$(this).next('.multiSelectOptions').css({ top:  offset.top + $(this).outerHeight() + 'px' });
			}
			//$(this).next('.multiSelectOptions').css({ left: offset.left + 'px' });
			
			// Disappear on hover out
			multiSelectCurrent = $(this);
			var timer = '';
			$(this).next('.multiSelectOptions').hover( function() {
				clearTimeout(timer);
			}, function() {
				timer = setTimeout('$(multiSelectCurrent).multiSelectOptionsHide(); $(multiSelectCurrent).unbind("hover");', 250);
			});
			
		},
		
		// Update the textbox with the total number of selected items
		multiSelectUpdateSelected: function(o) {
			var i = 0, s = '';
			var that = $(this);
			if (that.find('span.label').length==0) {
				that = this.parents('span.label').parent();
			}
			that.find('INPUT:checkbox').not('.selectAll').each( function() {
				if ($(this).attr('checked')) {
					i++;
				}
			});
			if( i == 0 ) {
				that.prev('.multiSelect').html( 'Выберите' );
			} else {
				if( o.oneOrMoreSelected == '*' ) {
					var title = that.prev('div.multiSelect');
					that.find('input[type=checkbox]').each(function(){
						span = title.find('span[umi-id='+$(this).attr('value')+']');
						if ($(this).attr('checked')) {
							if (span.length==0) {
								if (title.text().length>0 && title.find('.color').length==0) {
									title.html('');
								}
								title.append('<span umi-id="'+$(this).attr('value')+'" class="color">'+$(this).parent().find('a').html()+'</span>');
							}
						} else {
							if (span.length>0) {
								span.remove();
							}
						}
					});
					/*var display = '';
					that.find('INPUT:checkbox:checked').each( function() {
						var text = '<span>'+$(this).parents('span.label').find('a').html()+'</span>';
						if ( text != o.selectAllText ) display = display + text;
					});
					that.prev('div.multiSelect').html( display );*/
				} else {
					that.prev('div.multiSelect').html( o.oneOrMoreSelected.replace('%', i) );
				}
			}
		}
		
	});
	
})(jQuery);