$(document).ready(function(){
	$("img").error(function(){
		$(this).hide();
	});
	
	$("textarea[limit]").livequery(function(){
		$(this).limit({
			"leftChars": "Осталось"
		});
	});
	
	$("input[limit]").livequery(function(){
		$(this).limit({
			"show": false
		});
	});
	
	$('a.slide').click(function(){
		if ($(this).html() == '+') 
			$(this).html('&#8212;');
		else $(this).html('+');
		$(this).parent().next("div.slide").slideToggle();
		return false;
	});
	
	$('a.favorite').click(function(){
		if (document.all) 
			window.external.AddFavorite(location.href, document.title);
		else if (window.sidebar) window.sidebar.addPanel(document.title, location.href, "");
		return 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.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:
					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();
	}
	
	if (lightbx) {
		$('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
	});
}
