$(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
		});
	});
	if ($('input.date').length) {
		$('head').append('<link href="/css/ui.custom.css" rel="stylesheet" type="text/css" />');
		
		$('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/u_note.png');
			} else {
				if (r == '') {
					$('a[href="' + href + '"].note_img input').attr('value', '');
					$('a[href="' + href + '"].note_img img').attr('src', '/img/u_note_gray.png');
				}
			}
		});
		return false;
	});

	$('a.ccw, a.cw').live('click', function() {
		im = $(this).parents('.photo-item').find('img').get(0);
		var src = $(im).attr('alt');
		switch ($(this).attr('class')) {
			case 'ccw':
				deg = 90;
			break;
			case 'cw':
				deg = -90;
			break;

			default:
				deg = 0;
			break;
		}

		$.post("/xajax/xajax.php", { func : "rotateImg",
		file : $(this).attr('href'),
		rot : deg
		}, function(dat) {
			$(im).attr('src', src + '?' + Math.random());
		}, 'html');
		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
	});
}

