$(document).ready(function()
{
	
});

function printMe()
{
	window.open(document.location.href + '-print/', 'Версия для печати', '');
}

function openTeam()
{
	$("#lightbox-overlay").show();
	$("#popup-splash").css("width", "1004px");
	$("#popup-splash").css("left", ((document.body.clientWidth - 1004) / 2) + "px");
	$("#popup-splash").css("top", (getScrollY() + 50) + "px");
	$("#popup-splash").show();
	$.get("/team/ajax/", function(html){
		$("#popup-splash-content").html('<span class="close" onclick="closeTeam();">&times;</span>' + html);
	});
}

function closeTeam()
{
	$("#popup-splash").hide();
	$("#lightbox-overlay").hide();
}

function getScrollY() 
{
        scrollY = 0;    
        if (typeof window.pageYOffset == "number") {
                scrollY = window.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
                scrollY = document.documentElement.scrollTop;
        }  else if (document.body && document.body.scrollTop) {
                scrollY = document.body.scrollTop; 
        } else if (window.scrollY) {
                scrollY = window.scrollY;
        }
        return scrollY;
}

function switchFooterTab(openTab)
{
	//var tabs = ["partnership","guestbook","vacancy"];
	var tabs = ["guestbook","vacancy"];
	for (i = 0; i < tabs.length; i++) {
		if (tabs[i] == openTab && $("#" + tabs[i])[0].offsetHeight == 0) {
			$("#" + tabs[i]).show();
			$("#" + tabs[i] + "-tail").show();
		} else {
			$("#" + tabs[i]).hide();
			$("#" + tabs[i] + "-tail").hide();
		}
	}
}

function switchIndexTab(openTab)
{
	var tabs = ["about","mission","principles","targets","team"];
	for (i = 0; i < tabs.length; i++) {
		$("#main-link-" + tabs[i]).addClass("aslink");
		$("#main-text-" + tabs[i]).hide("normal");
	}
	for (i = 0; i < tabs.length; i++) {
		if (tabs[i] == openTab) {
			if ($("#main-text-" + openTab)[0].offsetHeight == 0) {
				$("#main-link-" + openTab).removeClass("aslink");
				$("#main-text-" + openTab).show("normal");
				$("#main-people-text").hide();
			} else {
				$("#main-people-text").show();
			}
		}
	}	
}

function submitGuestbook()
{
	$.post("/index/guestbook/", $(".guestbook-form:last").serialize(), function(status)
	{
		if (status == 1) {
			$("#guestbook-form-message").html("<strong>Сообщение успешно отправлено</strong><br /><p align=\"center\"><button type=\"button\" onclick=\"switchFooterTab('guestbook');\">Ok</button>");
		} else {
			$("#guestbook-form-message").html("<strong style='color:red;'>Ошибка: Вы ввели неверный проверочный код</strong>");
			$("#guestbook-form-fields").show();
		}
		
	}, "text");
	$("#guestbook-form-fields").hide();
	$("#guestbook-form-message").html("<em>Пожалуйста, оставайтесь на линии&hellip;</em>");
}

function submitFeedback()
{
	$.post("/index/guestbook/", $("#feedback-form").serialize(), function(status)
	{
		if (status == 1) {
			if ($('#feedback-form').attr('rel') == 'en') {
				$("#feedback-form-message").html("Thank you for your feedback.");
			} else {
				$("#feedback-form-message").html("Благодарим Вас за то, что уделили нам время и поделились своим мнением. Это позволит нам делать материалы для вас еще более интересными.");
			}
		} else {
			$("#feedback-form-message").html("<strong style='color:red;'>Ошибка: Вы ввели неверный проверочный код</strong>");
			$("#feedback-form-fields").show();
		}
		
	}, "text");
	$("#feedback-form-fields").hide();
	$("#feedback-form-message").html("<em>Пожалуйста, оставайтесь на линии&hellip;</em>");
}

function submitPersonal()
{
	$.post("/index/personal/", $("#personal-form").serialize(), function(status)
	{
		if (status == 1) {
			$("#personal-form-message").html("<strong>Сообщение успешно отправлено</strong><br /><p align=\"center\"><button type=\"button\" onclick=\"$('.splash-letter').hide();\">Ok</button>");
		} else {
			$("#personal-form-message").html("<strong style='color:red;'>Ошибка: Вы ввели неверный проверочный код</strong>");
			$("#personal-form-fields").show();
		}
		
	}, "text");
	$("#personal-form-fields").hide();
	$("#personal-form-message").html("<em>Пожалуйста, оставайтесь на линии&hellip;</em>");
}

function submitPolls(id)
{
	$.post("/index/polls/vote/", $("#polls-form-" + id).serialize(), function(results)
	{
		
		html = '<h2 style="margin:0 0 20px 0; font-size:160%;">' + results.name + '<!--Опрос--></h2><!--div class="question">' + results.name + '</div-->';
		
		total = 0;
		if (results.answer1 != '') total += parseInt(results.answer1count);
		if (results.answer2 != '') total += parseInt(results.answer2count);
		if (results.answer3 != '') total += parseInt(results.answer3count);
		if (results.answer4 != '') total += parseInt(results.answer4count);
		if (results.answer5 != '') total += parseInt(results.answer5count);
		if (results.answer6 != '') total += parseInt(results.answer6count);
		if (results.answer7 != '') total += parseInt(results.answer7count);
		if (results.answer8 != '') total += parseInt(results.answer8count);
		if (results.answer9 != '') total += parseInt(results.answer9count);
		
		if (results.answer1 != '') {
			p = Math.round(results.answer1count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer1 + '</label>: ' + results.answer1count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		if (results.answer2 != '') {
			p = Math.round(results.answer2count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer2 + '</label>: ' + results.answer2count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		if (results.answer3 != '') {
			p = Math.round(results.answer3count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer3 + '</label>: ' + results.answer3count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		if (results.answer4 != '') {
			p = Math.round(results.answer4count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer4 + '</label>: ' + results.answer4count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		if (results.answer5 != '') {
			p = Math.round(results.answer5count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer5 + '</label>: ' + results.answer5count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		if (results.answer6 != '') {
			p = Math.round(results.answer6count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer6 + '</label>: ' + results.answer6count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		if (results.answer7 != '') {
			p = Math.round(results.answer7count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer7 + '</label>: ' + results.answer7count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		if (results.answer8 != '') {
			p = Math.round(results.answer8count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer8 + '</label>: ' + results.answer8count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		if (results.answer9 != '') {	
			p = Math.round(results.answer9count / total * 100);
			html += '<p><label for="poll-answer-1">' + results.answer9 + '</label>: ' + results.answer9count + ' (' + p + '%) <span class="percent"><i style="width:' + p + '%"></i></span></p>';
		}
		$("#polls-form-" + id).html(html);
	}, "json");
	$("#polls-form-" + id).html("<em>Пожалуйста, оставайтесь на линии&hellip;</em>");
}

function showSubscribeReply()
{
	if (processIFrameEvents) {
		alert($("#service").contents());
		processIFrameEvents = false;
	}
}

var processIFrameEvents = false;
var subscribeReply = 0;

function submitSubscribe()
{
	error = false;
	$("#sign-form-fields input").each(function(){
		this.className = "";
		if (this.getAttribute("rel") == 1 && $.trim(this.value) == "") {
			this.className = "error";
			error = true;
		}
	});
	email = $("#sign-email").val();
	if (!email.match(/[\w\d\.\-]+@[\w\d\.\-]{2,}\.[a-z]{2,3}/i)) {
		error = true;
		$("#sign-email")[0].className = "error";
	}
	if (error) {
		$("#sign-form-fields")[0].scrollIntoView(true);
		return false;
	}
	if ($("#sign-online")[0].checked) {
		$("#subs-pro-form")[0].submit();
		//processIFrameEvents = true;
	}
	$.post("/index/subscribe/", $("#sign-form1").serialize(), function(status)
	{
		if (status == 1) {
			$("#main-text").hide();
			if ($("#sign-form-message-ok").html() == "") {
				//$("#sign-form-message").html("<strong>Запрос на оформление подписки успешно отправлен</strong>");
				$(".page-content").append("<strong>Запрос на оформление подписки успешно отправлен</strong>");
			} else {
				//$("#sign-form-message").html($("#sign-form-message-ok").html());
				$(".page-content").append("<p>" + $("#sign-form-message-ok").html() + "</p>");
			}
			if (subscribeReply == 1) {
				$(".page-content").append("<p>" + $("#sign-form-message-subs1").html() + "</p>");
			} else if (subscribeReply == 2) {
				$(".page-content").append("<p>" + $("#sign-form-message-subs2").html() + "</p>");
			}
			$(".page-content")[0].scrollIntoView(true);
		} else {
			$("#sign-form-message").html("<strong style='color:red;'>Ошибка: Вы ввели неверный проверочный код</strong>");
			$("#sign-form-fields").show();
		}
	}, "text");
	$("#sign-form-fields").hide();
	$("#sign-form-message").html("<em>Пожалуйста, оставайтесь на линии&hellip;</em>");
}

function submitSubscribe3()
{
	error = false;
	$("#sign-form-fields input").each(function(){
		this.className = "";
		if (this.getAttribute("rel") == 1 && $.trim(this.value) == "") {
			this.className = "error";
			error = true;
		}
	});
	email = $("#sign-email").val();
	if (!email.match(/[\w\d\.\-]+@[\w\d\.\-]{2,}\.[a-z]{2,3}/i)) {
		error = true;
		$("#sign-email")[0].className = "error";
	}
	if (error) {
		$("#sign-form-fields")[0].scrollIntoView(true);
		return false;
	}
	$.post("/index/subscribe3/", $("#sign-form1").serialize(), function(status)
	{
		if (status == 1) {
			$("#main-text").hide();
			if ($("#sign-form-message-ok").html() == "") {
				//$("#sign-form-message").html("<strong>Запрос на оформление подписки успешно отправлен</strong>");
				$(".page-content").append("<strong>Запрос на оформление подписки успешно отправлен</strong>");
			} else {
				//$("#sign-form-message").html($("#sign-form-message-ok").html());
				$(".page-content").append($("#sign-form-message-ok").html());
			}
			$(".page-content")[0].scrollIntoView(true);
		} else {
			$("#sign-form-message").html("<strong style='color:red;'>Ошибка: Вы ввели неверный проверочный код</strong>");
			$("#sign-form-fields").show();
		}
	}, "text");
	$("#sign-form-fields").hide();
	$("#sign-form-message").html("<em>Пожалуйста, оставайтесь на линии&hellip;</em>");
}

function submitSubscribe2()
{
	$.post("/index/subscribe2/", $("#sign-form2").serialize(), function(status)
	{
		if (status == 1) {
			if ($("#sign-form2-message-ok").html() == "") {
				$("#sign-form2-message").html("<strong>Запрос на оформление подписки успешно отправлен</strong>");
			} else {
				$("#sign-form2-message").html($("#sign-form2-message-ok").html());
			}
		} else {
			$("#sign-form2-message").html("<strong style='color:red;'>Ошибка: Вы ввели неверный проверочный код</strong>");
			$("#sign-form2-fields").show();
		}
	}, "text");
	$("#sign-form2-fields").hide();
	$("#sign-form2-message").html("<em>Пожалуйста, оставайтесь на линии&hellip;</em>");
}

function updateSubscribeContent(cbName, cbVal)
{
	$("#subs-content").val("");
	$("#subs-pro-form-cb").html("");
	$("#subs-cb input[type=checkbox]").each(function(){
		if (this.checked) {
			$("#subs-content").val($("#subs-content").val() == "" ? this.name : $("#subs-content").val() + ", " + this.name);
			if (this.value != "") {
				cb = this.value.split("|");
				$("#subs-pro-form-cb").append($("<input type='checkbox' name='"+cb[0]+"[]' value='"+cb[1]+"' checked='checked' />"));
			}
		}
	});
}
