$(document).ready(function(){

	$.extend($.fn, {

		toggleExpand: function(){
			$( this )
				.toggleClass( 'Expand' )
				.toggleClass( 'Collapse' );
			return this;
		},

		makeHover: function( sClassName ){
			var sClassName = sClassName || 'Hover';
			$( this ).unbind( 'hover' ).hover(function(){
				$( this ).addClass( sClassName );
			}, function(){
				$( this ).removeClass( sClassName );
			});
			return this;
		},

		makeFocus: function( sClassName ){
			var sClassName = sClassName || 'Focus';
			$( this ).unbind( 'focus' ).focus(function(){
				$( this ).addClass( sClassName );
			});
			$( this ).unbind( 'blur' ).blur(function(){
				$( this ).removeClass( sClassName );
			});
			return this;
		},

		disable: function(){
			if( $( this ).is( 'form' ) ){
				$( 'input, textarea, select', this ).disable();
			} else {
				$( this ).attr( 'disabled', 'disabled' );
				$( this ).addClass( 'Disabled' );
			}
			return this;
		},

		enable: function(){
			if( $( this ).is( 'form' ) ){
				$( 'input, textarea, select', this ).enable();
			} else {
				$( this ).removeAttr( 'disabled' );
				$( this ).removeClass( 'Disabled' );
			}
			return this;
		},

		check: function( bState ){
			// TODO: if( typeof bState is 'number' ) $( this ).val( bState );
			if( bState == undefined ){
				return $( this ).attr( 'checked' ) ? 1 : 0;
			} else {
				if( bState > 1 || bState == true || bState == 'checked' ){
					$( this ).attr( 'checked', 'checked' );
				} else {
					$( this ).removeAttr( 'checked' );
				}
				return this;
			}
		}

	});
	// end $.extend()



	jQuery.json = function( data, callback, beforeSend, complete ){
		var beforeSend = ( beforeSend ) ? beforeSend : null;
		var complete = ( complete ) ? complete : null;
		var oAjax = jQuery.ajax({
			data: prepare( data ),
			dataType: 'json',
			beforeSend: beforeSend,
			complete: complete,
			error: function( s,q,a ){
				alert(q);
			},
			success: callback
		});

		function prepare( _Data, sPrefix, queryText ){
			var result;
			if( sPrefix == null ) sPrefix = "";
			if( (''+typeof( _Data )).toLowerCase() == 'object' ){
				result = {}
				for( var Key in _Data ){
					var Data = _Data[Key];
					Key = this.escape( Key );
					if( Data === null ) continue;
					if( Data instanceof Function ) continue;
					if( Data instanceof Object ){
						result[ Key ] = prepare( Data )
					}
					if( Data === true ) Data = 1;
					if( Data === false ) Data = '';
					result[ Key ] = Escape( '' + Data );
				}
			} else {
				result = Escape( _Data );
			}
			return result;

			// JS escape() does not quote '+'
			function Escape( sData ) {
				return escape( sData ).replace( new RegExp('\\+','g'), '%2B' );
			}
			return _Data;
		}
	}
	// end $.json()



	jQuery.alert = function( Data, sPrefix ){
		sPrefix = sPrefix ? sPrefix : '';
		var result = '';
		for( key in Data ){
//			if( typeof Data[key] == 'object' ){
//				result += jQuery.alertObject( Data[key], sPrefix + '    ' );
//			} else {
				result += sPrefix + key + ':	' + Data[key] + '\n';
//			}
		}
		alert( result );
//		return result
	}
	// end $.alert()



	/*
		Функция склоняет имя существительное в зависимости от числительного
		NumDecline(
			_Number				Исходное число или объект input
			sNominative			Именительный падеж
			sGenitiveSingular	Единственное число
			sGenitivePlural		Множественное число
			sNull				Значение для пустой строки/нуля
		)
	*/
	jQuery.NumDecline = function( _Number, sNominative, sGenitiveSingular, sGenitivePlural, sNull ){
		if( typeof( _Number ) == 'object' ) _Number = _Number.value;
		_Number = _Number.toString();
		if( !sNull ) sNull = '';
		_Number = Number( _Number.replace(/,/, '.').replace(/ /, '') );
		var sNumber = Math.floor( _Number ).toString();
		if( sNumber == 0 ) return sNull;
		if( _Number > 10 && Math.floor( (_Number % 100) / 10 ) == 1 ){
			return sGenitivePlural;
		} else {
			switch( Number( sNumber.substr( sNumber.length - 1 ) ) ){
				case 1: return sNominative
				case 2: return sGenitiveSingular;
				case 3: return sGenitiveSingular;
				case 4: return sGenitiveSingular;
				default: return sGenitivePlural;
			}
		}
	}
	// end NumDecline()


	/*
		Функция проверят полученный адрес эл.почты на соотутствие стандарту
	*/
	jQuery.IsEmail = function( sEmail ){
		return sEmail.lastIndexOf('@') && /^(?:[-a-z\d\+\*\/\?!{}`~_%&'=^$#]+(?:\.[-a-z\d\+\*\/\?!{}`~_%&'=^$#]+)*)@(?:[-a-z\d_]+\.){1,60}[a-z]{2,6}$/i.test( sEmail );
	}
	// end IsEmail()
});
