$(document).ready(function(){
	$("img").error(function(){
		$(this).hide();
	});
	
	$("textarea[limit]").livequery(function(){
		$(this).limit({
			"leftChars": "Осталось"
		});
	});
	$("input[limit]").livequery(function(){
		$(this).limit({
			"show": false
		});
	});
	
	$('div.popup').livequery(function(){
		$(this).center();
		$(this).fadeIn();
	});
	
	
	$('a.slide').click(function(){
		if ($(this).html() == '+') 
			$(this).html('&#8212;');
		else $(this).html('+');
		$(this).parent().next("div.slide").slideToggle();
		return false;
	});
	
	$('a.home').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();
	});
	*/
	
		$('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.advertise_link').click(function(){
		var id = $(this).attr('id');
		$.get("/__advertise", {
			id: id
		});
		
		
	});
	
	$('#login, #password').focus(function() {
		if ($(this).val()==$(this).attr('id')) {
			$(this).attr('value','');
		}
	});
	$('#login, #password').blur(function() {
		if ($(this).val()=='') {
			$(this).attr('value',$(this).attr('id'));
		}
	});
	
});


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
	});
}

function obj(obj) {
	var displayLine, out = '';
	for ( var prop in obj) {
		displayLine = obj.name + "." + prop + "=" + obj[prop];
		out += displayLine + "\n";
	}
	return (out);
}


