// Edit by Maikel 25-01-2012 -> added is:visable statement
// Edit by Maikel 25-01-2012 -> formatted JS code

if(jQuery)
(
	function($)
	{
		$.extend($.fn,
		{
			magicSelect: function(o, callback)
			{
				// Default options
				if( !o ) var o = {};
				var specialobj;
				
				// Initialize each magicSelect
				$(this).each
				(
					function(i)
					{
						var select = $(this);
						
						if($(select).css('display') != "none")
						{
							if($(this).attr('multiple'))
							{
								var html = '<input type="text" readonly="readonly" class="magicSelect" id="'+$(this).attr('id')+'" value="" style="cursor: default;">';
								html += '<div class="magicSelectOptions" style="position: absolute; z-index: 666; display: none;">';
								
								var count = 0;
								$(select).find('OPTGROUP').each
								(
									function()
									{
										count ++;
										html += '<div><b>'+$(this).attr('label')+'</b></div>';
										$(this).find('OPTION').each
										(
											function ()
											{
												if( $(this).val() != '' )
												{
													html += '<div><input type="checkbox" name="' + $(select).attr('name') + '"value="' + $(this).val() + '"';
													if( $(this).attr('selected') ) html += ' checked="checked"';
													html += ' ><label>' + $(this).html() + '</label></div>';
												}
											}
										);
									}
								);
								if(count == 0)
								{
									$(select).find('OPTION').each
									(
										function()
										{
											if( $(this).val() != '' )
											{
												html += '<div><input type="checkbox" name="' + $(select).attr('name') + '" value="' + $(this).val() + '"';
												if( $(this).attr('selected') ) html += ' checked="checked"';
												html += ' ><label>' + $(this).html() + '</label></div>';
											}
										}
									);
								}
							}
							else
							{
								///// single select
								var html = '<input type="text" readonly="readonly" class="magicSelect" id="'+$(this).attr('id')+'" value="" style="cursor: default;">';
								html += '<div class="magicSelectOptions" style="position: absolute; z-index: 666; display: none;">';
								
								var count = 0;
								$(select).find('OPTGROUP').each
								(
									function()
									{
										count ++;
										html += '<div><b>'+$(this).attr('label')+'</b></div>';
										$(this).find('OPTION').each
										(
											function ()
											{
												if( $(this).val() != '' )
												{
													html += '<div><input type="radio" name="' + $(select).attr('name') + '" value="' + $(this).val() + '"';
													if( $(this).attr('selected') ) html += ' checked="checked"';
													html += '><label>' + $(this).html() + '</label></div>';
												}
											}
										);
									}
								);
								
								if(count == 0)
								{
									$(select).find('OPTION').each
									(
										function()
										{
											if( $(this).val() != '' )
											{
												html += '<div><input type="radio" name="' + $(select).attr('name') + '" value="' + $(this).val() + '"';
												if( $(this).attr('selected') ) html += ' checked="checked"';
												html += '><label>' + $(this).html() + '</label></div>';
											}
										}
									);
								}
							}
							
							html += '</div>';
							$(select).wrap('<span class="magicSelectParent">'+'</span>');
							$(select).after(html);

							// margin counting for easier styling
							//$('.magicSelect:eq('+i+'), select:eq('+i+'), .magicSelectOptions:eq('+i+')').wrapAll('<span class="magicSelectParent">'+'</span>');
							$(select).parent('.magicSelectParent').css('marginTop', $(select).next('.magicSelect').css('marginTop')); 
							$(select).parent('.magicSelectParent').css('marginRight', $(select).next('.magicSelect').css('marginRight'));
							$(select).parent('.magicSelectParent').css('marginBottom', $(select).next('.magicSelect').css('marginBottom'));
							$(select).parent('.magicSelectParent').css('marginLeft', $(select).next('.magicSelect').css('marginLeft'));
							$(select).next('.magicSelect').css('marginTop', '0px');
							$(select).next('.magicSelect').css('marginRight', '-2px');
							$(select).next('.magicSelect').css('marginBottom', '0px');
							$(select).next('.magicSelect').css('marginLeft', '0px');
							
							// Events
							$(select).next('.magicSelect').mouseover( function() {
								$(this).addClass('hover');
							}).mouseout( function() {
								$(this).removeClass('hover');
							}).click( function() {
								//Show/hide on click
								if( $(this).hasClass('active') ) {
									$(this).magicSelectOptionsHide(); 
								} else {
									//specialobj = $(this); //hidden for more multiselect bahavior
									$(this).magicSelectOptionsShow();
								}
								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');
							});
							
							// Handle checkboxes	
							$(select).next('.magicSelect').next('.magicSelectOptions').find('div').find('input').click
							(
								function()
								{
									if($(this).is(':not(:checked)')){
										$(this).attr('checked',true);
									} else {
										$(this).attr('checked',false);
									}
								}
							);
							
							$(select).next('.magicSelect').next('.magicSelectOptions').find('div').click(
								function()
								{
									if($(this).find('input').is(':not(:checked)')){
										$(this).find('input').attr('checked',true).trigger('change');
										$(this).parent('.magicSelectOptions').prev('.magicSelect').prev(select).find('option[value="'+ $(this).find('input:checked').val() +'"]').attr('selected',true);
										$(this).parent('.magicSelectOptions').prev('.magicSelect').prev(select).trigger('change');   
									} else {
										$(this).find('input').attr('checked',false);
										$(this).parent('.magicSelectOptions').prev('.magicSelect').prev(select).find('option[value="'+ $(this).find('input:not(:checked)').val() +'"]').attr('selected',false);  
									}
									
									$(this).parent().magicSelectUpdateSelected(o);
									$(this).parent().find('div').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
									$(this).parent().prev('.magicSelect').focus();
									
									if( callback ) callback($(this));
								}
							);

							// Initial display
							if($(select).find('option:first').val() != "NULL" )
							{
								$(select).next('.magicSelect').next('.magicSelectOptions').each
								(
									function()
									{
										$(this).magicSelectUpdateSelected(o);
										$(this).find('INPUT:checked').parent().addClass('checked');
									}
								);
							}

							// Handle hovers
							$(select).next('.magicSelect').next('.magicSelectOptions').find('div').mouseover
							(
								function()
								{
									$(this).parent().find('div').removeClass('hover');
									$(this).addClass('hover');
								}
							).mouseout(
								function()
								{
									$(this).parent().find('div').removeClass('hover');
								}
							);
							
							//.click( function() {
							//specialobj.magicSelectOptionsHide();
							//}); //hidden for more multiselect bahavior

							// Keyboard
							$(select).next('.magicSelect').keydown
							(
								function(e)
								{
									// Is dropdown visible?
									if( $(this).next('.magicSelectOptions').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('.magicSelectOptions').find('div').hasClass('hover') )
											{
												// Default to first item
												$(this).next('.magicSelectOptions').find('div:first').addClass('hover');
											} else {
												// Move down, cycle to top if on bottom
												$(this).next('.magicSelectOptions').find('div.hover').removeClass('hover').next('div').addClass('hover');
												if( !$(this).next('.magicSelectOptions').find('div').hasClass('hover') ) {
													$(this).next('.magicSelectOptions').find('div:first').addClass('hover');
												}
											}

											// Adjust the viewport if necessary
											$(this).magicSelectAdjustViewport($(this) );	
											return false;
										}
										
										// Up
										if( e.keyCode == 38 )
										{
											if( !$(this).next('.magicSelectOptions').find('div').hasClass('hover') )
											{
												// Default to first item
												$(this).next('.magicSelectOptions').find('div:first').addClass('hover');
											} else {
												// Move up, cycle to bottom if on top
												$(this).next('.magicSelectOptions').find('div.hover').removeClass('hover').prev('div').addClass('hover');
												if( !$(this).next('.magicSelectOptions').find('div').hasClass('hover') ) {
													$(this).next('.magicSelectOptions').find('div:last').addClass('hover');
												}
											}
											
											// Adjust the viewport if necessary
											$(this).magicSelectAdjustViewport($(this) );
											return false;
										}	
										
										// Enter, Space
										if( e.keyCode == 13 || e.keyCode == 32 )
										{
											// Other checkboxes
											if( $(this).next('.magicSelectOptions').find('div.hover INPUT').attr('checked') )
											{
												// Uncheck
												$(this).next('.magicSelectOptions').find('div.hover INPUT').attr('checked', false);
												$(this).next('.magicSelectOptions').magicSelectUpdateSelected(o);
												$(this).next('.magicSelectOptions').find('div').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
												if( callback ) callback($(this));
											}
											else
											{
												// Check
												$(this).next('.magicSelectOptions').find('div.hover INPUT').attr('checked', true);
												$(this).next('.magicSelectOptions').magicSelectUpdateSelected(o);
												$(this).next('.magicSelectOptions').find('div').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('.magicSelectOptions').find('div: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;
								}
							);
							
							// BKo: the selected value was not shown in my profile on page load (#164828)
							$(select).next('.magicSelect').next('.magicSelectOptions').magicSelectUpdateSelected(o);
							
							// Eliminate the original form element
							$(select).css('display','none');
						
							// STOP IF
						}
					}
				);	
				// END HERE
			},

			// Hide the dropdown
			magicSelectOptionsHide: function()
			{
				$(this).removeClass('active').next('.magicSelectOptions').hide();
				$(this).parent().removeClass('magicSelectRised');
				$(this).parent().parent().removeClass('magicSelectRised');
			},

			// Show the dropdown
			magicSelectOptionsShow: function()
			{
				// Hide any open option boxes
				$('.magicSelect').magicSelectOptionsHide();

				$(this).next('.magicSelectOptions').find('div').removeClass('hover');
				$(this).addClass('active').next('.magicSelectOptions').show();
				$(this).parent().addClass('magicSelectRised');
				$(this).parent().parent().addClass('magicSelectRised');
				
				// Position it
				var offset = $(this).position();
				$(this).next('.magicSelectOptions').css({ top:  offset.top + $(this).outerHeight() + 'px' });
				$(this).next('.magicSelectOptions').css({ left: offset.left + 'px' });
	
				// Disappear on hover out
				magicSelectCurrent = $(this);
				var timer = '';
				$(this).next('.magicSelectOptions').hover
				(
					function()
					{
						clearTimeout(timer);
					},
					function()
					{
						timer = setTimeout('jQuery(magicSelectCurrent).magicSelectOptionsHide(); $(magicSelectCurrent).unbind("hover");', 250);
					}
				);
			},

			// Update the textbox with the selected values
			magicSelectUpdateSelected: function(o)
			{
				s = '';
				$(this).find('INPUT:checked').each
				(
					function(j ,che)
					{
						s = s + $(che).next().text() + '   ';
					}
				);
				$(this).prev('INPUT.magicSelect').val(s);
			},

			// Ensures that the selected item is always in the visible portion of the dropdown (for keyboard controls)
			magicSelectAdjustViewport: function(el)
			{
				// Calculate positions of elements
				var i = 0;
				var selectionTop = 0, selectionHeight = 0;
				$(el).next('.magicSelectOptions').find('div').each
				(
					function()
					{
						if( $(this).hasClass('hover') ) { selectionTop = i; selectionHeight = $(this).outerHeight(); return; }
						i += $(this).outerHeight();
					}
				);
				
				var divScroll = $(el).next('.magicSelectOptions').scrollTop();
				var divHeight = $(el).next('.magicSelectOptions').height();
				
				// Adjust the dropdown scroll position
				$(el).next('.magicSelectOptions').scrollTop(selectionTop - ((divHeight / 2) - (selectionHeight / 2)));
			}
		});
	}
)(jQuery);

/* changes transport*/
$(document).ready(function()
{
	var optText = new Array();
	$('select').change(function()
	{
		$('select').each
		(
			function(i,selec)
			{
			optText[i] = $(selec).text();
				setTimeout
				(
					function()
					{
						if ( optText[i] != $(selec).text() )
						{
							$(selec).insertBefore( $(selec).parent('.magicSelectParent') );
							$(selec).next('.magicSelectParent').remove();
							$(selec).magicSelect();
							if( $(selec).parent().parent('span').hasClass('roundInInner') )
							{
								$(selec).parent().insertBefore( $(selec).parents('.roundInOuter') );
								$(selec).parent().next('.roundInOuter').remove(); 
								$(selec).parent().roundedInputs();
							}
						}
					}
					,710
				);
			}
		);
	});
});	
