jQuery.fn.upload = function(table, field, options) {
	var settings = jQuery.extend( {
		multi : false,
		width : 800,
		height : 600,
		buttonText : 'Upload',
		buttonImg : '/img/upload.gif',
		widthBtn : 118,
		destination : '/uploads/' + table
	}, options);
	if (settings.multi) {
		$('#' + field + '_show').sortable( {
			cursor : 'move',
			opacity : 0.5,
			tolerance : "pointer",
			revert : false
		});
	}
	$('#' + field + '_show' + ' a.del_img').live(
			'click',
			function() {
				if (settings.multi) {
					id_parent = $('#' + field + '_show').attr('alt');
					deleteFile($(this).attr('href'), table, field, settings.multi,
							id_parent);
				} else {
					deleteFile($(this).attr('href'), table, field, settings.multi);
				}

				//$(this).parent().html(	'<input type="hidden" name="' + field + '" value=""/>');
				$(this).parent().remove();
				return false;
			});
	$(this)
			.uploadify(
					{
						'uploader' : '/xajax/uploadify/uploadify.swf',
						'cancelImg' : '  /xajax/uploadify/cancel.png',
						'script' : '/xajax/xajax.php',
						'auto' : true,
						'folder' : '/temp',
						'scriptData' : {
							'width' : settings.width,
							'height' : settings.height,
							'func' : 'uploadify',
							'prefix' : table,
							'destination' : settings.destination
						},
						'sizeLimit' : '4097152',
						'multi' : settings.multi,
						'buttonText' : settings.buttonText,
						'buttonImg' : settings.buttonImg,
						'width' : settings.widthBtn,
						'fileDesc' : '*.jpeg;*.gif;*.png;*.jpg',
						'fileExt' : '*.jpeg;*.gif;*.png;*.jpg',
						onComplete : function(evt, queueID, fileObj, fname, data) {
							var path = fileObj.filePath;
							path = path.replace(fileObj.name, fname);
							if (settings.multi) {
								$('#' + field + '_show')
										.append(
												'<li class="upload_li"><img src="/100x200'
														+ path
														+ '""/><a href="'
														+ path
														+ '" class="del_img"><img src="/img/del.png" /><input type="hidden" name="'
														+ field + '[]" value="' + path + '"/></a>'
														+ '</li>');
							} else {

								deleteFile(
										$('#' + field + '_show' + ' a.del_img').attr('href'),
										table, field, settings.multi);
								$('#' + field + '_show').html(
										'<span><img src="/100x200' + path + '"" /><a href="' + path
												+ '" class="del_img"><img src="/img/del.png" /></a>'
												+ '<input type="hidden" name="' + field + '" value="'
												+ path + '"/></span>');
							}
						}
					});
}

jQuery.fn.limit = function(options) {
	var settings = jQuery.extend( {
		maxChars : $(this).attr('limit'),
		leftChars : "осталось",
		chars : 'символов',
		show : true
	}, options);

	return this.each(function() {
		var me = $(this);
		var l = settings.maxChars;
		if (settings.show)
			me.after('<span>' + settings.leftChars + '  ' + settings.maxChars + '  '
					+ settings.chars + '</span>');
		if (me.val().length > settings.maxChars) {
			me.val(me.val().substr(0, settings.maxChars));
		}
		l = settings.maxChars - me.val().length;
		if (settings.show)
			me.next('span').html(
					settings.leftChars + '  ' + l + '  ' + settings.chars + '</span>');
		me.bind('keydown keypress keyup change', function(e) {
			if (me.val().length > settings.maxChars){
				me.val(me.val().substr(0, settings.maxChars));
				alert('Вы превысили допустимое количество символов ('+settings.maxChars+'), текст был обрезан!');
				}
			l = settings.maxChars - me.val().length;
			if (settings.show)
				me.next('span').html(
						settings.leftChars + '  ' + l + ' ' + settings.chars + '</span>');
		});
	});
};

jQuery.fn.center = function() {
	var w = $(window);
	this.css("position", "absolute");
	this.css("top", (w.height() - this.height()) / 2 + w.scrollTop() + "px");
	this.css("left", (w.width() - this.width()) / 2 + w.scrollLeft() + "px");
	return this;
};

jQuery.fn.unique = function(options) {
	var settings = jQuery.extend( {
		name : '',
		field : 'name',
		text : 'Такая запись уже существует! Вы хотите создать еще одну?'
	}, options);
	$(this).click(function(e) {
		var ob = $(this);
		e.preventDefault();
		$.post("/xajax/modules_ajax.php", {
			method : 'unique_' + settings.name,
			name : settings.field,
			str : $('input[name="' + settings.field + '"]').val()
		}, function(data) {
			if (data != '') {
				jConfirm(settings.text, 'Проверка на существование', function(r) {
					if (r) {
						ob.unbind();
						ob.click();
					}
				});

			} else {
				ob.unbind();
				ob.click();
			}
		});
	});
};

jQuery.fn.filter_city = function() {
	//alert(window.location);
	$(this).change(function() {
		search = window.location.search;
		sign = (search == "") ? "?" : "&";
		window.location += sign + 'CITY=' + this.value;
	});
};

