$(document).ready(function(){
	$(".rounded").corner("bite cc:#e5d5ba");
	$(".rounded2").corner("bite cc:#e5d5ba");
	
	///////////////////////////////////////////
	//
	//	 валидатор
	///
	
	var validator_settings = {
		position: 'top center', 
		offset: [0, 0],
		lang: 'ru',
		message: '<div><em/></div>' // em element is the arrow
	};
	$.tools.validator.localize("ru", {
		'*'			: 'Пожалуйста, исправьте это значение',
		':email'  	: 'Введите корректный email',
		':number' 	: 'Только цифры',
		':url' 		: 'Введите корректный URL',
		'[max]'	 	: 'Введите значение меньше, чем $1',
		'[min]'		: 'Введите значение больше, чем $1',
		'[required]'	: 'Пожалуйста, заполните это поле'
	});
	$("form[name=question], form[name=order], form[name=ilostmypass], form[name=form_authorization], form[name=comment], form[faq]").validator(validator_settings);
	
	// Liturgy Calendar
	$('#calendar').datepicker({
		inline: true,
		beforeShowDay: setScheduledDays,
		onSelect: showEvent 
	});
	
	// клик на дату
	$("a.ui-state-default").live("click", function()
	{
		var _class	= $(this).parent().attr("class");
		var nodeURL	= classOption(_class, "go-to-").substr(6);
		//alert (nodeURL);
		document.location.href=nodeURL;
	});
	
	// наведение на дату
	$("a.ui-state-default").live("mouseover", function()
	{
		// вырезаем node-id-XXX из класса (могут быть лишние классы)
		var _class	= $(this).parent().attr("class");
		var nodeID 	= classOption(_class, "node-id-").substr(8);
		
		//alert (nodeURL);
		
		$.get("/php-templates/ajax/getCalendar.php", { 'id': nodeID },
			function(data){
				//alert("" + data);
				$("div.calendar-event div").html(data);
		});
	});
	
	
	$("#calendar").live("mouseout", function(e)
	{
		$("div.calendar-event div").html('<span class=cal-type-torzestvo><em> </em>торжество</span><span class=cal-type-prazdnik><em> </em>праздник</span><span class=cal-type-memorial><em> </em>память святого</span><br><span class="cal-type-triden"><em> </em>пасхальное триденствие</span><span class="cal-type-post"><em> </em>пост</span>');
	});
	
	$("#cal_showmode_cal").live("click", function(e)
	{
		e.preventDefault();
		$("#cal-by-list").hide();
		$("#cal-by-date").show();
	});
	
	$("#cal_showmode_list").live("click", function(e)
	{
		e.preventDefault();
		$("#cal-by-list").show();
		$("#cal-by-date").hide();
	});
	
	
	
	// Показать / скрыть
	$("a[show]").click(function(e){
		e.preventDefault();
		$(this).parent().parent().find("ul").toggle('fast');
	});
	
	
	// Корректировка под размер браузера
	function correctWindow() {
		//alert (0);
		if (windowWidth() < 1024)
		{
			//alert (1);
			$("#left").css("margin", "0 20px 0 70px");
			$("body").css("background", "#486f74");
		}
		else 
		{
			$("#left").css("margin", "0 20px 0 105px");
			$("body").css("background", "url(\"/design/img/bg.gif\") repeat-y scroll center top #3F8090");
			//alert (0);
		}
	};

	var resizeTimer = null;
	$(window).bind('resize', function() {
	if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(correctWindow, 100);
	});

	//alert (windowWidth());
	
		//alert (windowWidth());
	
	
	
	$("img").ifixpng(); 
	$("div.calendar").ifixpng(); 
	$("div.svitok .top").ifixpng(); 
	$("div.svitok .bottom").ifixpng(); 
	/*jQuery(function($) {
		$("img").pngfix();
		$("#calendar").pngfix();
	});*/

});
	
function showEvent (dateText, inst) 
{
	/*$this = $(this);

	var _class	= $(this).parent().attr("class");
	console.log($this);
	var nodeURL	= classOption(_class, "go-to-").substr(6);*/
	
	//alert (nodeURL);
	//var l = $("a.ui-state-active").parent().attr("class");
	//alert("Выбрано: " + dateText +	"\n\nid: " + inst.id +"\nselectedDay: " + inst.selectedDay +"\nselectedMonth: " + inst.selectedMonth +"\nselectedYear: " + inst.selectedYear);
	//console.log($(this));
}
	
// Разбор класса даты
// в классе кодируется параметры
// @prefix=node-id- айди ноды
// @prefix=node-url- урл ноды
function classOption(_class, prefix)
{
		var b_s		= _class.search(prefix);
		var b		= _class.substr(b_s);
		var a_n		= b.search(" ");
		var a		= (a_n > 0) ? b.substr(0, a_n) : b;
		return a;
}	

	
function windowWidth() {
	var de = document.documentElement;
	return self.innerWidth || ( de && de.clientWidth ) || document.body.clientWidth;
} 
	
	
	


