$(document).ready(
		function() {
			$("#screen").click( function(el) {
				$("#enterdiv").hide();
				$('#screen').css('height', '0');
				$('#screen').css('width', '0');
				$("#screen").hide();

			});
			$("#login").focus( function() {
				if ($(this).val() == 'логин')
					$(this).val("");
			});
			$("#login").blur(
					function() {
						if ($(this).val() == '' || $(this).val() == null
								|| $(this).val() == undefined)
							$(this).val("логин");
					});
			$("#password").focus( function() {
				if ($(this).val() == 'пароль')
					$(this).val("");
			});
			$("#password").blur(
					function() {
						if ($(this).val() == '' || $(this).val() == null
								|| $(this).val() == undefined)
							$(this).val("пароль");
					});

			// @todo it's not a good idea to put here but i haven't enouth time
			$("#voteForm").bind('submit', submitVote);
			jQuery('[@class$=fixPng]').ifixpng();
			jQuery('input[@src$=.png]').ifixpng();
		});

function submitVote() {
	// @todo just for radio polls
	for ( var i = 0; i < this.vote_answer.length; i++) {
		if (this.vote_answer[i].checked) {
			var answer = this.vote_answer[i].value;
		}
	}
	if (!answer) {
		my_tb_show('Пожалуйста введите ответ');
	} else {
		var id_question = this.id_question.value;
		var id_vote = this.id_vote.value;
		var time = new Date();
		time = time.getTime();
		var params = {
			time :time,
			id_vote :id_vote,
			id_question :id_question,
			answer :answer
		};

		jQuery.getJSON("/ajaxvote.php", params, backSubmitVote);
	}

	return false;
}

function backSubmitVote(data) {
	if (data.message && data.message != "2") {
		var voteDivs = jQuery(".leftVoteBlockDiv");
		jQuery(voteDivs.get(0)).css('display', 'none');
		jQuery(voteDivs.get(1)).css('display', 'block');

		var totalPercents = 0;
		for ( var i = 0; i < data.response.length; i++) {
			var curDiv = jQuery("#vote_" + data.response[i][0], voteDivs.get(1));
			var percent = ( Math.round(data.response[i][1] / data.maxCnt * 100) );
			jQuery(".Bar", curDiv).css('width', (percent * 188 / 100) + "px");
			totalPercents = totalPercents + percent;
			if(i ==  data.response.length - 1){
				if( totalPercents > 100 ){
					percent = percent - ( totalPercents - 100  );
				} else if ( totalPercents < 100 ){
					percent = percent + 100 - totalPercents;
				}
			}
			jQuery(".percent", curDiv).text(percent + "%");
		}
	}
	switch (data.message) {
	case 1:
		var message = 'Спасибо, Ваш голос учтён!';
		break;
	case 2:
		var message = 'Поля не заполнены';
		break;
	case 3:
		var message = 'Вы уже участвовали в этом опросе';
		break;
	default:
		var message = 'Произошла ошибка';
		break;
	}
	my_tb_show(message);
}
function jumpToEnter() {
	ShowEnter();
	var href = window.location.href;
	window.location.href += href.lastIndexOf('#') != -1 ? '' : '#';
}

function ShowEnter() {

	$('#screen').css('display', 'block');
	var y = getyScroll('y');
	var x = getyScroll('x');
	$('#screen').css('height', y + 'px');
	$('#screen').css('width', x + 'px');

	$("#enterdiv").toggle();
	$("#enteraction").text("");
	$("#password").val('пароль');
	$("#login").val('логин');
	$('#password').bind("keyup", callB).bind("keypress", callB).bind("keydown",
			callB);
}
function callB(event) {
	if (event.keyCode == 13) {
		ClickEnter();
		return false;
	}
}
function ClickEnter() {
	var error = 0;
	var login = $("#login").val();
	var pas = $("#password").val();
	var rem = 0;

	if ($("#remember").attr('checked') == true) {
		rem = $("#remember").attr('value') || 1;
	}

	$("#enteraction").text("");

	if (!login) {
		$("#enteraction").text("Введите логин!");
		$("#login").focus();
		error++;
	}
	if (!pas && error == 0) {
		$("#enteraction").text("Введите пароль!");
		$("#password").focus();
		error++;
	}

	if (error == 0) {
		$.post('http://'+registry.serverName+"/ajaxenter.php", {
			login :login,
			password :pas,
			remember :rem
		}, backAjaxEnter);
	}
}

function backAjaxEnter(data) {
	if (parseInt(data) == 1) {
		window.location.reload();
	} else {
		$("#enteraction").text("Данные не верны!");
		$("#password").val("");
		$("#password").focus();
	}
}

function Exit() {
	$.post('http://'+registry.serverName+"/ajaxexit.php", {}, function() {
		window.location.reload();
	});
}
function getyScroll(fl) {
	yScroll = 0;
	xScroll = 0;
	var deff = 0;
	var wff = 0
	var hff = 0;
	if (window.innerHeight && window.scrollMaxY || window.innerWidth
			&& window.scrollMaxX) {
		yScroll = parseInt(window.innerHeight) + parseInt(window.scrollMaxY);
		xScroll = parseInt(window.innerWidth) + parseInt(window.scrollMaxX);

		var deff = document.documentElement;
		var wff = (deff && deff.clientWidth) || document.body.clientWidth
				|| window.innerWidth || self.innerWidth;
		var hff = (deff && deff.clientHeight) || document.body.clientHeight
				|| window.innerHeight || self.innerHeight;
		xScroll = parseInt(xScroll)
				- (parseInt(window.innerWidth) - parseInt(wff));
		yScroll = parseInt(yScroll)
				- (parseInt(window.innerHeight) - parseInt(hff));
	} else if (document.body.scrollHeight > document.body.offsetHeight
			|| document.body.scrollWidth > document.body.offsetWidth) { // all
																		// but
																		// Explorer
																		// Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
		// alert(yScroll+"--2");
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla
				// and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
		// alert(yScroll+"--3");
	}
	// alert(yScroll);
	if (fl == 'y') {
		// alert(yScroll);
		return yScroll;
	} else
		return xScroll;
}
