﻿$(document).ready(function(){

	$.extend($.fn, {

		makeHover: function(){
			$( this ).hover(function(){
				$( this ).addClass( 'Hover' );
			}, function(){
				$( this ).removeClass( 'Hover' );
			});
		},

		makeFocus: function(){
			$( this ).focus(function(){
				$( this ).addClass( 'Focus' );
			});
			$( this ).blur(function(){
				$( this ).removeClass( 'Focus' );
			});
		},

		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 ){
			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;
			}
		}

	});



	jQuery.numDecline = function( iNumber, sNominative, sGenetivSingular, sGenetivPlural, sNull ){
		if( !sNull ) sNull = '';
		iNumber = Number( iNumber.toString().replace(/,/, '.').replace(/ /, '') );
		var sNumber = Math.floor(iNumber).toString();
		if( sNumber == 0 ) return sNull;
		if( iNumber > 10 && Math.floor((iNumber % 100) / 10) == 1 ){
			return sGenetivPlural;
		} else {
			switch( Number(sNumber.substr(sNumber.length - 1)) ){
				case 1: return sNominative;
				case 2: return sGenetivSingular;
				case 3: return sGenetivSingular;
				case 4: return sGenetivSingular;
				default: return sGenetivPlural;
			}
		}
	}

	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,
			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;
		}

	}

	jQuery.alertObject = 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
	}

});