$(document).ready(function() {
	$("img").error(function() {
		$(this).hide();
	});
	// $(document).bind('keydown', 'Ctrl+return', function() {
		// if (window.getSelection) {
		// alert(window.getSelection().toString());
		// }
		// });

		if (isLogin) {
			$(document).everyTime(200000, function() {
				$.post("/xajax/xajax.php", { func : "online"
				});
			});
		}
		$(document).bind('keydown', 'Ctrl+right', function() {
			if ($('li a#next').html())
				window.location = $('li a#next').attr('href');
		});
		$(document).bind('keydown', 'Ctrl+left', function() {
			if ($('li a#prev').html())
				window.location = $('li a#prev').attr('href');
		});

		$("a#print_button").click(function() {
			$("div.cont").printArea();
			return false;
		});

		$("textarea[limit]").livequery(function() {
			$(this).limit( { "leftChars" : "Осталось"
			});
		});
		$("input[limit]").livequery(function() {
			$(this).limit( { "show" : false
			});
		});

		$('div.popup').livequery(function() {
			$(this).center();
			$(this).fadeIn();
		});

		$('a.feedback').click(function() {
			$.post("/xajax/xajax.php", { func : "feedback",
			cmd : 'form',
			responseType : 'html'
			}, function(data) {
				$("div.popup").remove();
				$('div.all').prepend(data);
			});
			return false;
		});

		$('a.feederror, a.feederror2').click(function() {
			$.post("/xajax/xajax.php", { func : "feederror",
			cmd : 'form',
			responseType : 'html'
			}, function(data) {
				$("div.popup").remove();
				$('div.all').prepend(data);
			});
			return false;
		});

		$.post("/xajax/xajax.php", { func : "showCalendar",
		responseType : 'html'
		}, function(data) {
			$('div.calendar').html(data);
		});

		$('div.mounth a.rght').live('click', function() {
			$('div.calendar').fadeOut('fast', function() {

				$.post("/xajax/xajax.php", { func : "showCalendar",
				mn : '1',
				responseType : 'html'
				}, function(data) {
					$('div.calendar').html(data);
					$('div.calendar').fadeIn('fast');
				});
			});
			return false;
		});
		$('div.mounth a.lft').live('click', function() {
			$('div.calendar').fadeOut('fast', function() {
				$.post("/xajax/xajax.php", { func : "showCalendar",
				mn : '-1',
				responseType : 'html'
				}, function(data) {
					$('div.calendar').html(data);
					$('div.calendar').fadeIn('fast');
				});
			});
			return false;
		});

		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#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).attachDatepicker();
		});

		// $('a.delete_ajax').live('click', function () {
		// var ob = $(this);
		// var title = ob.attr('title');
		// if (title == '') title = 'Удаление';
		// jConfirm('Вы уверены, что хотите удалить?', title, function (r) {
		// if (r) {
		// $.get(ob.attr('href'));
		// ob.parent().parent().remove();
		// }
		// });
		// 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.add_date").click(
				function() {
					$(this).parent().before(
							'<div>' + '<div style="width:200px;float:left;">Дата начала:<br/>' + '<input name="date_start[]" class="date"/>' + '</div>' + '<div style="width:200px;float:left;">Дата окончания:<br/>' + '<input name="date_end[]"  class="date"/>' + '</div>'
									+ '<div style="float:left;padding-top:10px;"><a href="" class="delete_date"> удалить </a></div>' + '</div>');
					return false;
				});

		$("a.delete_date").live('click', function() {
			var obj = $(this);
			jConfirm('Вы уверены, что хотите удалить?', 'Удаление даты', function(r) {
				if (r) {
					obj.parent().parent().remove();
				}
			});
			return false;
		});

		$('.tooltip').tooltip( { delay : 0,
		showURL : false
		});

		$('.fotka').mouseover(function() {
			$(this).addClass('fotka_hover').removeClass('fotka');
		});
		$('.fotka').mouseout(function() {
			$(this).addClass('fotka').removeClass('fotka_hover');
		});

		$('a.reload_captcha').live('click', function() {
			$(this).prev('img.captcha').attr('src', $(this).prev('img.captcha').attr('src') + '?reload');
			return false;

		});

		if ($.mask) {
			// $.mask.masks.integer = { mask : '9',
			// type : 'repeat'
			// };

			$('input:text').setMask();
		}
		
		 $('#advertise_temp script').remove();
     $('#advertise').html($('#advertise_temp').html());
		 $('#advertise_temp').remove();

	});


function autocomplete(func, where, from) {
	if (!from) {
		var str = '/xajax/xajax.php?func=' + func + '&from=' + $('#' + from + ' option:selected').val() + '&responseType=html&' + Math.random();
		$('div.ac_results').remove();
		ac = $('#' + where).autocomplete(str);
	} else {
		$('#' + from).change(function() {
			var str = '/xajax/xajax.php?func=' + func + '&from=' + $('#' + from + ' option:selected').val() + '&responseType=html&' + Math.random();
			$('div.ac_results').remove();
			$('#' + where).autocomplete(str);
		});
	}
}

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
	});
}
/*
 * $.gritter.add({ // (string | mandatory) the heading of the notification
 * title: 'This is a regular notice!', // (string | mandatory) the text inside
 * the notification text: txt, // (string | optional) the image to display on
 * the left image:
 * 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png', //
 * (bool | optional) if you want it to fade out on its own or just sit there
 * sticky: false, // (int | optional) the time you want it to be alive for
 * before fading out (milliseconds) time: 3000, // (string | optional) the class
 * name you want to apply directly to the notification for custom styling
 * class_name: 'my-class', // (function | optional) function called before it
 * opens before_open: function(){ //alert('I am a sticky called before it
 * opens'); }, // (function | optional) function called after it opens
 * after_open: function(e){ //alert("I am a sticky called after it opens: \nI am
 * passed the jQuery object for the created Gritter element...\n" + e); }, //
 * (function | optional) function called before it closes before_close:
 * function(e){ //alert("I am a sticky called before it closes: I am passed the
 * jQuery object for the Gritter element... \n" + e); }, // (function |
 * optional) function called after it closes after_close: function(){ //alert('I
 * am a sticky called after it closes'); } });
 * 
 */
