function setPrintStyle(p) {
	var links = $('head link');
	if (p == true) {
		location.hash = 'print';
		var a = $('<a href="#" id="noPrint">Обычный вид страницы</a>');
		a.click( function() { setPrintStyle(false); return !1; })
		if ($('#header').children('a').size() > 0) {
			$('#header').children('a').replaceWith(a);
		}
		else {
			$('#header').append(a)
		}
	} else {
		location.hash = '';
		$('#noPrint').remove();
	}
	links.each(function() {
		if ($(this).attr('title') == 'printview') $(this).attr('disabled', !p);
	});
}
$('#printMe a').click( function() { setPrintStyle(true); return !1; });
if (document.location.hash == '#print') { setPrintStyle(true);}

function queryFieldFocus() {
	$(this).prev('label').hide();
}
function queryFieldBlur() {
	if ($(this).val() === '') {
		$(this).prev('label').show();
	} else if ($(this).val() === $(this).prev('label').text()) {
		$(this).val('');
		$(this).prev('label').show();
	}
}
function queryFieldChange() {
	if ($(this).val() != '') {
		$(this).prev('label').hide();
	}
}

var sf = $('#searchForm'),
	qFld = $('input:text', sf);
qFld.focus( queryFieldFocus).blur( queryFieldBlur).change( queryFieldChange);
qFld.change();

var loginForm = $('#loginForm'),
	inputFld = $('input:text, input:password', loginForm);
inputFld.focus( queryFieldFocus).blur( queryFieldBlur).change( queryFieldChange);
inputFld.change();

if ((window.opera) || '\v'==='v'/* detects IE */) {
	$('.rndCnrs, .b-gr-rnd').append('<i class="cnrs lt" /><i class="cnrs rt" /><i class="cnrs lb" /><i class="cnrs rb" />');
}
$('#authorsList p.more').append('<i class="cnrs lt" /><i class="cnrs rt" />');
$('.pollItms .votes, .pollItms .pct').append('<i class="cnrs" />');

$('#logIn').click(
	function() {
		loginForm.show('500');
		return !1;
	}
);
$('.closeIt', loginForm).click(
	function() {
		loginForm.hide('500');
		return !1;
	}
);

$('.tabs .tabItm a').click(
	function() {
		var prnt = $(this).parent();
		if (!prnt.hasClass('current')) {
			$('.tabs .tabItm').toggleClass('current');
			$('#bookshelf table, #bookshelf .rssLink').toggleClass('hidden');
		}
		return !1;
	}
);

$('#genderSel label').click(
	function() {
		$('#genderSel label').toggleClass('hidden');
	}
);

function openPopUp(url) {
	window.open(url,'','location=no,toolbar=no,resizable=no,scrollbars=yes,status=no,width=580,height=550');
}
$('#sendMe a').click(
	function() {
		var url = $(this).attr('href');
		$(this).attr('disabled','disabled');
		openPopUp(url);
		return !1;
	}
);
$('a #order').click(
	function() {
		var url = $(this).parent().attr('href');
		$(this).parent().attr('disabled','disabled');
		openPopUp(url);
		return !1;
	}
);

var ArtImgCarousel = function(cArr, cContainer) {
	this.cArr = cArr;
	this.cContainer = cContainer;
	this.total = cArr.length;
	this.curr = 0;
	this.ctrlWrpr = $('<p class="ctrl"/>');
}
$.extend(ArtImgCarousel.prototype, {
	init: function() {
		this.imgCont = $('div.b-wrpr', this.cContainer);
		this.showCntr();
		this.showCtrls();
	},
	showLoader: function() {
		if ($('i.loader', this.imgCont).length > 0) {
			$('i.loader', this.imgCont).show();
		} else {
			var loader = $('<i class="loader"/>');
			this.imgCont.append(loader);
		}
	},
	update: function(src, alt) {
		$('img', this.imgCont).remove();
		$('i.loader').hide();
		var nextImg = $('<img src="'+ src +'" lt="'+ alt +'"/>');
		nextImg.css('display', 'none');
		this.imgCont.append(nextImg);
		nextImg.fadeIn('600');
		this.showCntr();
		this.showDesc(alt);
	},
	createImg: function(src, alt) {
		var self = this, img = new Image();
		$(img).load(function() {
			$(img).unbind('load');
			self.update(src, alt);
		});
		img.src = src;
	},
	showCtrls: function() {
		var self = this;
		this.prev = $('<a class="arw l" href="#"></a>');
		this.prev.idx = -1;
		this.prev.bind('click', function() { self.showImg(-1); return !1; });
		this.next = $('<a class="arw r" href="#"></a>');
		this.next.idx = 1;
		this.next.bind('click', function() { self.showImg(1); return !1; })
		this.ctrlWrpr.append(this.prev, this.next);
		this.imgCont.after(this.ctrlWrpr);
	},
	showCntr: function() {
		var currNum = this.curr + 1,
			cntr = $('<span>'+ currNum + '/'+ this.total +'</span>');
		$('span', this.ctrlWrpr).remove();
		this.ctrlWrpr.append(cntr);
	},
	showImg: function(delta) {
		var self = this,
			idx = this.curr + delta < 0 ? this.total - 1 : this.curr + delta > this.total - 1 ? 0 : this.curr + delta;
		var nextImgSrc = this.cArr[idx].src,
			nextImgAlt = this.cArr[idx].desc;
			
		this.curr = idx;
		
		$('img', this.imgCont).fadeOut('550',
			function() {
				$(this).css({'display': 'block', 'visibility': 'hidden'});
				self.showLoader();
				self.createImg(nextImgSrc, nextImgAlt);
			}
		);
	},
	showDesc: function(desc) {
		$('small', this.cContainer).html(desc);
	}
	
});

var newsSlider = function(news, up, down, botIndent) {
	this.news = news;
	this.up = up;
	this.down = down;
	this.botIndent = botIndent;
	this.heights = [];
	this.deltas = [];
	this.counter = 0;
	this.init();
}
$.extend(newsSlider.prototype, {
	init: function() {
		var self = this;
		this.contHeight = this.news.parent().height();
		this.createHiddenItemsHeightArr();
		this.up.bind('click', function() { self.moveDown(); return !1; });
		this.down.bind('click', function() { self.moveUp(); return !1; });
		this.steps = this.heights.length;
		window.onresize = function() {
			self.heights = [];
			self.createHiddenItemsHeightArr();
		}
	},
	createHiddenItemsHeightArr: function() {
		var prntOffsetTop = this.news.offset().top, self = this;
		$('li', this.news).each(
			function(i) {
				if ($(this).offset().top - prntOffsetTop >= self.contHeight - self.botIndent) {
					var itmHeight = $('li:eq('+ i +')', self.news).outerHeight();
					self.heights.push(itmHeight);
				}
			}
		);
	},
	moveUp: function() {
		if (!this.down.hasClass('inactive')) {
			var moveTo = parseInt(this.news.css('top')) - this.heights[this.counter];
			this.news.animate({'top': moveTo}, 300);
			this.counter++;
			this.update();
		}
	},
	moveDown: function() {
		if (!this.up.hasClass('inactive')) {
			this.counter--;
			var moveTo = parseInt(this.news.css('top')) + this.heights[this.counter];
			this.news.animate({'top': moveTo}, 300);
			this.update();
		}
	},
	update: function() {
		if (this.counter === 0) {
			this.up.addClass('inactive');
		} else {
			this.up.removeClass('inactive');
		}
		
		if (this.counter === this.steps) {
			this.down.addClass('inactive');
		} else {
			this.down.removeClass('inactive');
		}
	}
});
/*
var datePickerFld = $('#eventsCal input'),
	localMonthNamesD = ['января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря'],
	localMonthNames = ['Январь','Февраль','Март','Апрель','Май','Июнь','Июдь','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
	localDayNames = ['Воскресенье','Понедельник','Вторник','Среда','Четверг','Пятница','Суббота'];
if (datePickerFld.length) {
	datePickerFld.datepicker({
		self: this,
		createButton: false,
		showAnim: 'slideDown',
		monthNames: localMonthNames,
		dayNames: localDayNames,
		dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
		dateFormat: 'd-mm-yy/DD/m',
		firstDay: 1,
		onSelect: function() {
			var preDate = $(this).val().split('/'),
				date = preDate[0].split('-'),
				dateStr = date[0] + ' ' + localMonthNamesD[preDate[2] - 1] + ' ' + date[2];
			window.setTimeout(
				function() {
					$('#eventsCal .date').html('<strong>'+ dateStr +'</strong>' + preDate[1].toLowerCase());
				}, 2000
			);
		},
		onClose: function () {
				var preDate = $(this).val().split('/'),
				date = preDate[0].split('-');
				reDraw(date);
		}
	});
	$('#datePicker').click(
		function() {
			datePickerFld.datepicker('show');
			return !1;
		}
	);
}
// coded by programmer
function reDraw(date) {
	var s = '';
	var year = parseInt(date[2], 10);
	var month = parseInt(date[1], 10);
	var day = parseInt(date[0], 10);
	var dt = new Date(year, month-1, 1);
	var dow = (parseInt(dt.getDay()) + 6)%7;
	s += "<tr>\n";
	for (i = 0; i < dow; i++) {
		s += "<td></td>\n";
	}
	dayscnt = 0;
	switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			dayscnt = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			dayscnt = 30;
			break;
		case 2:
			if (((year%4 == 0)&&(year%100 != 0)) || (year%400 == 0)) {
				dayscnt = 29;
			}
			else {
				dayscnt = 28;
			}
			break;
	}
	for (i = 1; i <= dayscnt; i++) {
		if ((i+dow)%7 == 1) s+="</tr>\n<tr>\n";
		cls = '';
		pos = 0;
		lnk = i;
		for (k in newsdates['d']) {
			if ((i == newsdates['d'][k]) && (newsdates['m'][k] == month)&&(newsdates['y'][k] == year)) {
				cls = 'class="hasNews"';
				lnk = '<a href="'+newsdates['url'][k]+'">'+i+'</a>';
				pos = k;
				break;
			}
		}
		if ((i+dow)%7 == 0) {
			cls = 'class="dayOff"';
			lnk = i;
		}
		if (i == day) {
			if (cls == 'class="hasNews"') {
				lnk = '<a href="'+newsdates['url'][pos]+'">'+i+'</a>';
			}
			else {
				lnk = '<a href="#">'+i+'</a>';
			}
			cls = 'class="today"';
		}
		s += "<td "+cls+">"+lnk+"</td>\n";
	}
	for (i = ((dayscnt+dow)%7); i < 7; i++) {
		if ((i%7) == 0) s+="</tr>\n<tr>\n";
		s += "<td></td>\n";
	}
	s += "</tr>\n";
	$('#eventsCal tbody').html(s);
}
*/
$('#regForm').submit(
	function() {
		if ($("#regForm .required:input[value='']").size() > 0) {
			$("#regForm .required:input[value='']:first").focus();
			$("#regForm .error").css('display','block');
			return false;
		}
		else {
			return true;
		}
	}
);
$('#forgotForm').submit(
	function() {
		if ($("#forgotForm .required:input[value='']").size() > 0) {
			$("#forgotForm .required:input[value='']:first").focus();
			$("#forgotForm .error").css('display','block');
			return false;
		}
		else {
			return true;
		}
	}
);

$('#commentForm').submit(
	function() {
		if ($("#commentForm .required:input[value='']").size() > 0) {
			$("#commentForm .required:input[value='']:first").focus();
			$("#commentForm .error").css('display','block');
			return false;
		}
		else {
			return true;
		}
	}
);

$('#sendLinkForm').submit(
	function() {
		if ($("#sendLinkForm .required:input[value='']").size() > 0) {
			$("#sendLinkForm .required:input[value='']:first").focus();
			$("#sendLinkForm .error").eq(0).show();
			$("#sendLinkForm .error").eq(1).remove();
			$("#sendLinkForm .success").eq(0).remove();
			return false;
		}
		else {
			return true;
		}
	}
);
$('#orderForm').submit(
	function() {
		if ($("#orderForm .required:input[value='']").size() > 0) {
			$("#orderForm .required:input[value='']:first").focus();
			$("#orderForm .error").eq(0).show();
			$("#orderForm .error").eq(1).remove();
			$("#orderForm .success").eq(0).remove();
			return false;
		}
		else {
			return true;
		}
	}
);
$('#sendArtForm .dataForm').submit(
	function() {
		if ($("#sendArtForm form :input[value='']").size() > 0) {
			$("#sendArtForm form :input[value='']:first").focus();
			$("#sendArtForm form .error").eq(0).show();
			$("#sendArtForm form .error").eq(1).remove();
			$("#sendArtForm form .success").eq(0).remove();
			return false;
		}
		else {
			return true;
		}
	}
);
$('#profileForm .dataForm').submit(
	function() {
		if ($("#profileForm form .required:input[value='']").size() > 0) {
			$("#profileForm form .required:input[value='']:first").focus();
			$("#profileForm form .error").eq(0).show();
			$("#profileForm form .error").eq(1).remove();
			$("#profileForm form .success").eq(0).remove();
			return false;
		}
		else {
			return true;
		}
	}
);
$('#commentForm').submit(
	function() {
		if ($("#commentForm :input[value='']").size() > 0) {
			$("#commentForm :input[value='']:first").focus();
			$("#commentForm .error").eq(0).show();
			$("#commentForm .error").eq(1).remove();
			$("#commentForm .success").eq(0).remove();
			return false;
		}
		else {
			return true;
		}
	}
);
$('#changePassForm').submit(
	function() {
		if ($("#changePassForm :input[value='']").size() > 0) {
			$("#changePassForm :input[value='']:first").focus();
			$("#changePassForm .error").eq(0).show();
			$("#changePassForm .error").eq(1).remove();
			$("#changePassForm .success").eq(0).remove();
			return false;
		}
		else {
			return true;
		}
	}
);
/*
$.ready(function($) {
	$('#bgcolor').colorPicker();
	$('#bbgcolor').colorPicker();
	$('#hdrcolor').colorPicker();
	$('#txtcolor').colorPicker();
	$('#dtcolor').colorPicker();
});
*/
$('#eventsCal form').submit(
	function() {
		var s = '';
		year = parseInt($('#eventsCal :input[name=year]').val());
		month = parseInt($('#eventsCal :input[name=month]').val());
		var dt = new Date(year, month-1, 1);
		var dow = (dt.getDay() + 6)%7;
		s += "<tr>\n";
		for (i = 0; i < dow; i++) {
			s += "<td></td>\n";
		}
		dayscnt = 0;
		switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				dayscnt = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				dayscnt = 30;
				break;
			case 2:
				if (((year%4 == 0)&&(year%100 != 0)) || (year%400 == 0)) {
					dayscnt = 29;
				}
				else {
					dayscnt = 28;
				}
				break;
		}
		for (i = 1; i <= dayscnt; i++) {
			if ((i+dow)%7 == 1) s+="</tr>\n<tr>\n";
			cls = '';
			pos = 0;
			lnk = i;
			for (k in newsdates['d']) {
				if ((newsdates['d'][k] == i)&&(newsdates['m'][k] == month)&&(newsdates['y'][k] == year)) {
					cls = 'class="hasNews"';
					lnk = '<a href="'+newsdates['url'][k]+'">'+i+'</a>';
					pos = k;
					break;
				}
			}
			if ((i+dow)%7 == 0) {
				cls = 'class="dayOff"';
			}
			s += "<td "+cls+">"+lnk+"</td>\n";
		}
		for (i = ((dayscnt+dow)%7); i < 7; i++) {
			if ((i%7) == 0) s+="</tr>\n<tr>\n";
			s += "<td></td>\n";
		}
		s += "</tr>\n";
		$('#eventsCal tbody').html(s);
		return false;
	}
);
$('#comnts .commentEdit a').click(
	function() {
		id = $(this).attr('href').slice(1);
		sect = $("#commentForm input:hidden[name='sect']").val();
		txt = $.ajax({ 
			type: "GET", 
			url: "/ru/"+sect+"/detail.php?cmd=editcomment&COMID="+id, 
			async: false,
			cache: false
		}).responseText;
		idelem = $("#commentForm input:hidden[name='COMID']");
		if (idelem.size() > 0) {
			idelem.val(id);
		}
		else {
			$('#commentForm').prepend("<input type='hidden' name='COMID' value='"+id+"'/>");
		}
		$("#commentForm input:hidden[name='cmd']").val('savecomment');
		$("#commentForm :textarea[name='comment']").val(txt);
		if ($("#commentForm #commentActs .cstmBtn").size() < 2) {
			$("#commentForm #commentActs :submit").attr('value', 'Отредактировать');
			$("#commentForm #commentActs").append("<input class='cstmBtn' type='button' value='Отправить'/>");
		}
		$("#commentForm #commentActs :button").click(function(){
			$("#commentForm input:hidden[name='cmd']").val('postcomment');
			$("#commentForm").submit();
		});
		$("#commentForm :textarea[name='comment']").focus();
	}
);

