(function($)
{
	$.fn.NXCselect = function( options )
	{
		var defaults = {
			verticalScroll: false,
			height: "90px",
			arrowSensitivity: -4
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(
			function()
			{
				var obj = $( this );
				if( obj.length > 0 )
				{
					//create new select
					var newSelectBlock = $( '<div class="nxc_select"></div>' );
					var newSelect = $( '<div class="nxc_select_content"></div>' );
					var newSelectList = $( '<ul class="nxc_select_list"></ul>' );
					var currentIndex = 0;
					var opened = false;
					var scrollBlockHeight = 0;
					var scrollSpace = 0;
					var curretStatus = { 'mouseclicked': false, 'firstY': 0, 'lastY': 0, 'currentY': 0 };
					
					$.each( $( obj ).find( 'option' ),
						function( index, item )
						{
							if( $( item ).attr( 'selected' ) )
							{
								var listItem = $( '<li class="current" rel="' + $( item ).val() + '">' + $( item ).text() + '</li>' );
								var currentValue = $( '<div class="nxc_select_list_default"><span class="nxc_select_list_default_content" rel="' + $( item ).val() + '">' + $( item ).text() + '</span><span class="nxc_select_list_default_button">&nbsp;</span></div>' );
								currentIndex = index;
								$( newSelectBlock ).append( currentValue );
							}
							else
							{
								var listItem = $( '<li rel="' + $( item ).val() + '">' + $( item ).text() + '</li>' );
							}
							$( newSelectList ).append( listItem );
						}
					);
					$( newSelect ).append( newSelectList );
					$( newSelectBlock ).append( newSelect );
					var newScroll = $( '<div class="nxc_select_scroll"><div class="nxc_scroll_up_arrow"></div><div class="nxc_scroll_control"><div class="nxc_scroll_curret"></div></div><div class="nxc_scroll_down_arrow"></div></div>' );
					$( newSelect ).append( newScroll );
					$( obj ).parent().append( newSelectBlock );
					
					//calculate default value
					var contentHeight = $( newSelect ).height();
					
					$( newSelectBlock ).width( $( obj ).outerWidth() );
					if( options.height == "auto" )
					{
						$( newScroll ).height( parseInt( $( newSelect ).height() ) - 2 );
						scrollBlockHeight = parseInt( $( newSelect ).height() ) - 2;
					}
					else if( options.height.indexOf( "pt" ) != -1 )
					{
						
					}
					else if( options.height.indexOf( "px" ) != -1 )
					{
						$( newSelect ).height( parseInt( options.height ) );
						$( newScroll ).height( parseInt( $( newSelect ).height() ) - 2 );
						scrollBlockHeight = parseInt( $( newSelect ).height() ) - 2;
					}
					
					scrollSpace = scrollBlockHeight - parseInt( $( newSelectBlock ).find( 'div.nxc_scroll_up_arrow' ).height() ) - parseInt( $( newSelectBlock ).find( 'div.nxc_scroll_down_arrow' ).height() );
					if( scrollSpace > 0 )
					{
						$( newSelectBlock ).find( 'div.nxc_scroll_control' ).height( scrollSpace );
						$( newSelectBlock ).find( 'div.nxc_scroll_curret' ).height( scrollSpace * ( scrollBlockHeight / contentHeight ) );
						//console.log( scrollSpace * ( scrollBlockHeight / contentHeight ) );
					}
					var maxScrollPosition = scrollSpace - ( scrollSpace * ( scrollBlockHeight / contentHeight ) );
					
					//events
					$( newSelectBlock ).find( 'div.nxc_select_list_default' ).click(
						function( event )
						{
							if( opened )
							{
								$( newSelect ).fadeOut( 200 );
								opened = false;
							}
							else
							{
								$( newSelect ).fadeIn( 200 );
								opened = true;
							}
						}
					);
					
					$( newSelectBlock ).mouseleave(
						function( event )
						{
							if( opened )
							{
								$( newSelect ).fadeOut( 200 );
								curretStatus.mouseclicked = false;
								opened = false;
							}
						}
					);
					
					$.each( $( newSelectList ).find( 'li' ),
						function( index, item )
						{
							$( item ).click(
								function( event )
								{
									if( currentIndex != index )
									{
										$( newSelectBlock ).find( 'span.nxc_select_list_default_content' ).text( $( item ).text() );
										$( newSelectBlock ).find( 'span.nxc_select_list_default_content' ).attr( 'rel', $( item ).attr( 'rel' ) );
										$( obj ).val( $( item ).attr( 'rel' ) );
									}
									if( opened )
									{
										$( newSelect ).fadeOut( 200 );
										opened = false;
									}
									currentIndex = index;
								}
							);
						}
					);
					
					//scroll events
					$( newSelectBlock ).find( 'div.nxc_scroll_up_arrow' ).click(
						function( event )
						{
							var nextTop = parseInt( $( newSelectBlock ).find( 'div.nxc_scroll_curret' ).css( 'top' ) ) + options.arrowSensitivity;
							if( nextTop < 0 )
							{
								nextTop = 0;
							}
							if( nextTop > maxScrollPosition )
							{
								nextTop = maxScrollPosition;
							}
							$( newSelectBlock ).find( 'div.nxc_scroll_curret' ).css( 'top', nextTop );
							$( newSelectList ).css( 'top', - ( contentHeight - scrollBlockHeight ) * ( nextTop / maxScrollPosition ) );
							return false;
						}
					);
					$( newSelectBlock ).find( 'div.nxc_scroll_down_arrow' ).click(
						function( event )
						{
							var nextTop = parseInt( $( newSelectBlock ).find( 'div.nxc_scroll_curret' ).css( 'top' ) ) - options.arrowSensitivity;
							if( nextTop < 0 )
							{
								nextTop = 0;
							}
							if( nextTop > maxScrollPosition )
							{
								nextTop = maxScrollPosition;
							}
							$( newSelectBlock ).find( 'div.nxc_scroll_curret' ).css( 'top', nextTop );
							$( newSelectList ).css( 'top', - ( contentHeight - scrollBlockHeight ) * ( nextTop / maxScrollPosition ) );
							return false;
						}
					);
					
					$( newSelectBlock ).find( 'div.nxc_scroll_curret' ).mousedown(
						function( event )
						{
							curretStatus.mouseclicked = true;
							curretStatus.firstY = event.clientY;
							curretStatus.lastY = event.clientY;
							return false;
						}
					);
					$( newSelectBlock ).find( 'div.nxc_scroll_curret' ).mouseup(
						function( event )
						{
							curretStatus.mouseclicked = false;
							return false;
						}
					);
					/*$( newSelectBlock ).find( 'div.nxc_scroll_curret' ).mouseleave(
						function( event )
						{
							curretStatus.mouseclicked = false;
							return false;
						}
					);*/
					$( newSelectBlock ).mouseleave(
						function( event )
						{
							curretStatus.mouseclicked = false;
						}
					);
					$( newSelectBlock ).mouseup(
						function( event )
						{
							curretStatus.mouseclicked = false;
						}
					);
					$( newSelectBlock ).find( 'div.nxc_scroll_curret' ).mousemove(
						function( event )
						{
							if( curretStatus.mouseclicked )
							{
								curretStatus.currentY = event.clientY;
								var nextTop = parseInt( $( this ).css( 'top' ) ) + ( curretStatus.currentY - curretStatus.lastY );
								if( nextTop < 0 )
								{
									nextTop = 0;
								}
								if( nextTop > maxScrollPosition )
								{
									nextTop = maxScrollPosition;
								}
								$( this ).css( 'top', nextTop );
								$( newSelectList ).css( 'top', - ( contentHeight -scrollBlockHeight ) * ( nextTop / maxScrollPosition ) );
								curretStatus.lastY = curretStatus.currentY;
							}
						}
					);
				}
			}
		);
	};
})(jQuery);
/*
$( document ).ready(
	function()
	{
		$( 'select' ).NXCselect();
	}
);*/

