$(document).ready(function() {
	$("img").error(function() {
		$(this).hide();
	});

	$('div.popup').livequery(function(){
		$(this).center();
		$(this).fadeIn();
	});

	$("textarea[limit]").livequery(function() {
		$(this).limit( { "leftChars" : "Осталось"
		});
	});
	$("input[limit]").livequery(function() {
		$(this).limit( { "show" : false
		});
	});



	$('input.date').livequery(function() {
		$(this).datepicker( { dateFormat : 'dd.mm.yy',
		gotoCurrent : true,
		changeMonth : true,
		changeYear : true,
		firstDay : 1,
		dayNamesMin : [ 'Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб' ],
		monthNamesShort : [ 'Янв', 'Фев', 'Март', 'Апр', 'Май', 'Июнь', 'Июль', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек' ],
		yearRange : '2008:2015'
		});
	});

	$('a.pay_ajax').click(function() {
		var ob = $(this);

		$.get(ob.attr('href'), function(data) {
			switch (data) {
				case '1':
					if (ob.html() == 'оплачено') {
						ob.html('оплатить');
						ob.parent().parent().attr('bgcolor', '#b7ffc4');
					} else {
						ob.html('оплачено');
						ob.parent().parent().attr('bgcolor', '#fac1bf');
						if ($('div.mcontainer'))
							$('div.mcontainer').remove();
					}
				break;
				default:
					// alert(data);
			break;
		}
	}	);
		return false;
	});

	$('a.publish_ajax').click(function() {
		var ob = $(this);

		$.get(ob.attr('href'), function(data) {
			switch (data) {
				case '1':
					if (ob.html() == 'скрыть') {
						ob.html('отобразить');
					} else {
						ob.html('скрыть');
						if ($('div.mcontainer'))
							$('div.mcontainer').remove();
					}
				break;
				default:
					// alert(data);
			break;
		}
	}	);
		return false;
	});

	$('a.delete, a.delete_ajax').live('click', function() {
		var href = $(this).attr('href');
		var title = $(this).attr('title');
		if (title == '')
			title = 'Удаление';
		jConfirm('Вы уверены, что хотите удалить?', title, function(r) {
			if (r)
				window.location = href;
		});
		return false;
	});

	$('a.reload_captcha').live('click', function() {
		$('img.captcha').attr('src', $('img.captcha').attr('src') + '?reload');
		return false;
	});

	if ($.mask) {
		$('input:text').setMask();
	}

	$('a.popupOpenBtn, a.popupCloseBtn').click(function() {
		$(this).prev().slideToggle('slow');
		$(this).toggleClass('popupCloseBtn popupOpenBtn');
	});

	if ($('a.lb').length) {
		$('head').append('<link href="/css/jquery.lightbox.css" rel="stylesheet" type="text/css" />');

		$.getScript('/engine/js/jquery.lightbox.js', function() {

			$('a.lb').lightBox( {
			// overlayBgColor : '#FFF',
				overlayOpacity : 0.8,
				imageLoading : '/engine/images/loading.gif',
				imageBtnClose : '/engine/images/close.gif',
				imageBtnPrev : '/engine/images/prev.gif',
				imageBtnNext : '/engine/images/next.gif',
				containerResizeSpeed : 350,
				txtImage : 'фото',
				txtOf : 'из'
			});

		});

	}

	$('a.note_img').live('click', function() {
		var href = $(this).attr('href');
		jPrompt('', $('a[href="' + href + '"].note_img input').attr('value'), 'Введите описание файла', function(r) {
			if (r) {
				$('a[href="' + href + '"].note_img input').attr('value', r);
				$('a[href="' + href + '"].note_img img').attr('src', '/img/note.png');
			} else {
				if (r == '') {
					$('a[href="' + href + '"].note_img input').attr('value', '');
					$('a[href="' + href + '"].note_img img').attr('src', '/img/note_none.png');
				}
			}
		});
		return false;
	});

});

function deleteFile(file, table, field, multi, id_parent) {
	$.post("/xajax/xajax.php", { func : "deleteFile",
	file : file,
	table : table,
	field : field,
	multi : multi,
	id_parent : id_parent
	});
}

