 ( function( $ ) {
 
	var int_uid = ( new Date() ).getTime();
	
	$.fn.respul = function ( json_options )
	{
	
		var json_defaults = {
			'idPrefix' : 'respul_',
			'idSuffix' : '',
			'classNameRespul' : 'respul',
			'classNameSource' : 'respsrc',
			'classNameJs' : 'js',
			'classNameCss' : 'css',
			'responsiveCss' : false,
			'boundaryWidth' : 600,
			'optionText' : 'Go to...'
		};
		
		if ( ! json_options ) { json_options = {}; }
		json_options = $.extend( json_defaults, json_options );
		
		var _hasRespul = function ( that )
		{
			var str_id = that.attr( 'id' );
			return ( 0 < $( '#' + json_options[ 'idPrefix' ] + str_id + json_options[ 'idSuffix' ] ).length );
		}
		
		var _getId = function ( that )
		{
			var str_id = that.attr( 'id' );
			if ( ! str_id )
			{
				str_id = 'uid' + int_uid++;
				that.attr( 'id', str_id );
			}
			return str_id;
		}
		
		var _createRespul = function ( that )
		{
			// Get id
			var str_id = _getId( that );
			
			// Create respul
			if ( ! _hasRespul( that ) )
			{
				var str_class_extend = '';
                
				if ( undefined !== that.attr( 'class' ) )
				{
					str_class_extend += ' ' + that.attr( 'class' );
				}
				
				if ( false === json_options[ 'responsiveCss' ] )
				{
					str_class_extend += ' ' + json_options[ 'classNameJs' ];
				}
				else
				{
					str_class_extend += ' ' + json_options[ 'classNameCss' ];
				}
				
				that.attr( 'class', json_options[ 'classNameSource' ] + str_class_extend );
				
				var jobj_respul = $( '<div>' )
					.attr( 'id', json_options[ 'idPrefix' ] + str_id + json_options[ 'idSuffix' ] )
					.attr( 'class', json_options[ 'classNameRespul' ] + str_class_extend );
					
				var jobj_select = $( '<select>' )
					.attr( 'id', json_options[ 'idPrefix' ] + str_id + json_options[ 'idSuffix' ] + '_select' )
					.bind( 'change',
						function( event )
						{
							location.href = $( this ).find( 'option:selected' ).val();
						}
					);
				
				var jobj_option_default = $( '<option>', {
					'text' : json_options[ 'optionText' ],
					'value' : '#'
				});
                
				jobj_option_default.appendTo( jobj_select );
				
				that.find( 'li' ).each(
					function ()
					{
						var jobj_li = $( this );
						var jobj_a = $( this ).find( 'a' );
						var jobj_option = $( '<option>', {
							'text' : jobj_a.text(),
							'value' : jobj_a.attr( 'href' )
						});
						jobj_option.appendTo( jobj_select );
						if ( jobj_li.hasClass( 'active' ) || jobj_li.hasClass( 'parent-active' ) )
						{
							jobj_option.attr( 'selected', 'selected' );
						}
					}
				);
				
				if ( undefined !== that.attr('title') )
				{		
					$('<label>')
						.attr( 'for', json_options[ 'idPrefix' ] + str_id + json_options[ 'idSuffix' ] + '_select' )
						.text( that.attr('title') )
						.appendTo( jobj_respul );
				}
				
				jobj_select.appendTo( jobj_respul );
				
				jobj_respul.insertBefore( that );
			}
		};
    
		var _showRespul = function ( that )
		{
			var str_id = that.attr( 'id' );
			$( '#' + str_id ).hide();
			$( '#' + 'respul_' + str_id ).show();
		};
		
		var _hideRespul = function ( that )
		{
			var str_id = that.attr( 'id' );
			$( '#' + str_id ).show();
			$( '#' + 'respul_' + str_id ).hide();
		};
		
		var _isUnderBoundary = function ()
		{
			return ( json_defaults.boundaryWidth >= $( window ).width() );
		}
		
		var _checkBoundaries = function ( that )
		{
			if ( ! _hasRespul( that ) )
			{
				_createRespul( that );
			}
			if ( false === json_options[ 'responsiveCss' ] )
			{
				if ( _isUnderBoundary() )
				{
					_showRespul( that );
				}
				else
				{
					_hideRespul( that );
				}
			}
		}
		
		return this.each(
			function ()
			{
				$( window ).bind( 'resize',
					( function ( that )
						{
							return function ()
							{
								_checkBoundaries( that );
							};
					}( $( this ) ) )
				);
				_checkBoundaries( $( this ) );
			}
		);
		
	}
	
} )( jQuery );
