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).closest('li.upload_li').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' : '14097152',
				'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"><span class="photo-item"><a href="#"><img src="/200x100' + path + '" alt="/200x100' + path + '" /></a> <a href="' + path + '" class="del_img">Удалить</a> <span class="adjustment"><a href="' + path + '" class="note_img"><img src="/img/u_note_gray.png" /><input type="hidden" name="note[]" value=""/></a> <i class="empty"></i> <i class="empty"></i> <a href="' + path + '" class="ccw"><img src="/img/u_rotate_ccw.png" /></a>  <a href="' + path + '" class="cw"><img src="/img/u_rotate_cw.png" /></a><input type="hidden" name="' + field	+ '[]" value="' + path + '"/></span></span>');
					} else {

						deleteFile($('#' + field + '_show' + ' a.del_img').attr('href'), table, field, settings.multi);
						$('#' + field + '_show').html('<span class="photo-item"><a href="#"><img src="/200x100' + path + '" alt="/200x100' + path + '" /></a> <a href="' + path + '" class="del_img">Удалить</a> <span class="adjustment"><i class="empty"></i><i class="empty"></i> <i class="empty"></i> <a href="' + path + '" class="ccw"><img src="/img/u_rotate_ccw.png" /></a>  <a href="' + path + '" class="cw"><img src="/img/u_rotate_cw.png" /></a><input type="hidden" name="' + field + '" value="' + path + '"/></span></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 style="display:block;">' + 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));
			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();
			}
		});
	});
};

function intval(val) {
	tmp = parseInt(val);
	return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;

}

function trim(sInString) {
	sInString = sInString.replace(/&nbsp;/g, ' ');
	return sInString.replace(/(^\s+)|(\s+$)/g, '');
}

function notice(title, text, options) {
	var settings = jQuery.extend( { show : true,
	timer : false
	}, options);
	var ID = $('div.popup').length + 1;
	var iden = '#p_' + ID + '.popup';
	$('body').append('<div class="popup" style="display: none;" id="p_' + ID + '">' + '<div class="popup-fon">' + '<div class="popup-in link-friend">' + '<h2>' + title + '</h2>' + '<h3>' + text + '</h3>' + '</div>' + '<i class="closes"></i>' + '</div>' + '</div>');
	$(iden).center();
	if (settings.show)
		$(iden).fadeIn();
	$(iden + ' i.closes').live("click", function() {
		$(iden).fadeOut(function() {
			$(iden).remove();
		});
	});
	if (settings.timer) {
		$(iden).oneTime(settings.timer * 1000, function(i) {
			$(this).fadeOut(function() {
				$(this).remove();
			});
		});
		$(iden).mouseover(function() {
			$(this).stopTime();
		});
		$(iden).mouseout(function() {
			$(iden).oneTime(settings.timer * 1000, function(i) {
				$(this).fadeOut(function() {
					$(this).remove();
				});
			});
		});
	}

}

function loader(cmd) {
	var cmd = cmd || false;
	if (!cmd) {
		$('body')
				.append(
						'<div id="mLoader" style="opacity:0; width: 100%; height: ' + $(document).height() + 'px; position: absolute; top:0px; left:0px; z-index: 9998;   background-color: #fff;"></div><div id="dLoader" style="z-index: 9999; position:absolute; opacity:0;"><img src="/img/mloader.gif" /></div>');
		$('#dLoader').center();
		$('#mLoader').animate({opacity:0.5}, 300, function(){
			$('#dLoader').animate({opacity:1}, 300);
		});
	} else {
		$('#dLoader').animate({opacity:0}, 300, function(){
			$('#mLoader').animate({opacity:0}, 300, function(){
				$('#dLoader img').remove();
				$('#dLoader').remove();
				$('#mLoader').remove();
				
			});
		});
	}

}
