function newsSlider(arr, height) {
	i = -1;
	$('#slider #list .news').each(function() {
		i++;
		$('#slider #list').wait(4000).animate({ top: -i * height}, 2000);
	});
}

CampOnline = {
	waitAjax: function()
	{
		$("#progressBar").html("<center><div style='margin:5px;color:#fff;font-weight:bold;'>Загрузка...<br><img src='/dev/images/decoration/ajax-loader.gif'></div></center>");
	},
	endAjax: function()
	{
		$("#progressBar").html("");
	},
	autorisation: function()
	{
		CampOnline.waitAjax();
		ln = $("#loginOrganization").val();
		pwd = $("#passwordOrganization").val();
		$.get("/dev/camp-online-xmlhttp.php", { action: "autorisation", login: ln, password: pwd },
			function(data){
				CampOnline.endAjax();
				if (data.indexOf("success") != -1) {
					$("#campOnlineMain").html(data);
					$("#errorOnline").html("");
					$.cookie('login', ln);
					$.cookie('password', pwd);
				} else {
					$("#errorOnline").html(data);
				}
			});
	},
	getCampList: function(getType, getCurrency)
	{
		CampOnline.waitAjax();
		$.get("/dev/camp-online-xmlhttp.php", { action: "getSession" },
			function(data){
				CampOnline.endAjax();
				$.cookie('onlineSession', data);
				if ($.cookie('onlineSession')) {
					$.get("/dev/camp-online-xmlhttp.php", { action: "campList", type: getType, currency: getCurrency },
					function(data){
						$("#campOnlineMain").html(data);
					});
				} else {
					$("#campOnlineMain").html('<div class="error">В вашем браузере не включены cookie.</div>');
				}
			});
	},
	getCampTourList: function(campIndex)
	{
		CampOnline.waitAjax();
		$.get("/dev/camp-online-xmlhttp.php", { camp: campIndex },
			function(data){
				CampOnline.endAjax();
				$("#campTourList").html(data);
			});
	},
	refreshPriceCurrency: function() //Обновление прайсовой таблицы
	{
		CampOnline.waitAjax();
		getCurrency = $("#peopleCurrency").val();
		campIndex = $("#campList").val();
		$.get("/dev/camp-online-xmlhttp.php", { action: "refreshPrice", camp: campIndex, currency: getCurrency},
			function(data){
				CampOnline.endAjax();
				$("#camp" + campIndex).html(data);
			});
	},
	getSummPrice: function()
	{
		CampOnline.waitAjax();
		campIndex = $("#campList").val();
		currencyIndex = $("#peopleCurrency").val();
		selectInd = "";
		
		$("#camp" +campIndex + " input:checkbox:checked").each(
			function(intIndex) {
				
				if (intIndex != 0) {
					selectInd += ",";
				}
				selectInd += $(this).val();
			}
		);
		checkedRailway = 0;
		$("#showRailwayPackage input:radio:checked").each(
			function(intIndex) {
				checkedRailway = $(this).attr("id");
			}
		);
		$.get("/dev/camp-online-xmlhttp.php", { periods: selectInd, camp: campIndex, currency: currencyIndex, railway: checkedRailway },
			function(data){
				$("#showSummPrice").html(data);
				if ($("#submitChildData").hasClass('isHidden')) {
					$("#submitChildData").removeClass('isHidden');
				}
			});
		$.get("/dev/camp-online-xmlhttp.php", { action: "getRailwayPackage", currency: currencyIndex, current: checkedRailway },
			function(data){
				CampOnline.endAjax();
				$("#showRailwayPackage").html(data);
		});
		if ($("#showRailwayPackage").hasClass("isHidden")) {
			$("#showRailwayPackage").removeClass("isHidden");
		}
	},
	checkDataBeforeQuest: function() /* Проверяем данные перед открытием анкеты */
	{
		CampOnline.waitAjax();
		var childFio         = $("#childFio").val();
		var childBirthDate   = $("#childBirthDate").val();
		var childGender      = $("#childGender").val();
		var parentsFio       = $("#parentsFio").val();
		var parentsEmail     = $("#parentsEmail").val();
		campIndex = $("#campList").val();
		
		selectInd = "";
		$("#camp" +campIndex + " input:checkbox:checked").each(
			function(intIndex) {
				
				if (intIndex != 0) {
					selectInd += ",";
				}
				selectInd += $(this).val();
			}
		);
		$.get("/dev/camp-online-xmlhttp.php",
			{
				verify: "quest",
				childName: childFio,
				childBirthSend: childBirthDate,
				childGenderSend: childGender,
				parentsName: parentsFio,
				parentsMail: parentsEmail,
				periods: selectInd
			}, function(data) {
				CampOnline.endAjax();
				if (data != 'ok') {
					if ($("#checkData").hasClass("isHidden")) {
						$("#checkData").removeClass("isHidden");
					}
					$("#checkData").html(data);
				} else {
					CampOnline.showQuest();
					$("#checkData").html("");
				}
			}
		);
	},
	sendData: function(real)
	{
		CampOnline.waitAjax();
		campIndex            = $("#campList").val(); //Лагерь
		currencyIndex        = $("#peopleCurrency").val(); //Валюта для расчета
		//For IE 0_o. Так как данные переменные уже определены (id)
		var childFio         = $("#childFio").val();
		var childBirthDate   = $("#childBirthDate").val();
		var childGender      = $("#childGender").val();
		var parentsFio       = $("#parentsFio").val();
		var parentsTelephone = $("#parentsTelephone").val();
		var parentsEmail     = $("#parentsEmail").val();
		var childCity        = $("#childCity").val();
		var countrySend      = $("#country").val();
		/* Необходимые смены */
		selectInd = "";
		$("input:checkbox:checked").each(
			function(intIndex) {
				if (intIndex != 0) {
					selectInd += ",";
				}
				selectInd += $(this).val();
			}
		);
		/* Железнодорожный пакет */
		checkedRailway = 0;
		$("#showRailwayPackage input:radio:checked").each(
			function(intIndex) {
				checkedRailway = $(this).attr("id");
			}
		);
		$.post("/dev/camp-online-xmlhttp.php", 
			{ 
					status: 3,
					periods: selectInd, 
					camp: campIndex, 
					currency: currencyIndex,
					childName: childFio,
					childBirthSend: childBirthDate,
					childGenderSend: childGender,
					parentsName: parentsFio,
					telephone: parentsTelephone,
					email: parentsEmail,
					city: childCity,
					country: countrySend,
					sendOut: real,
					sendPassNumber: $("#questPassNumber").val(),
					sendLivePlace: $("#questLivePlace").val(),
					sendPayerPassportSerial: $("#questPayerPassportSerial").val(),
					sendPayerPassportNumber: $("#questPayerPassportNumber").val(),
					sendPayerPassportGiven: $("#questPayerPassportGiven").val(),
					sendPayerAddress: $("#questPayerAddress").val(),
					sendPayerKinship: $("#questPayerKinship").val(),
					sendService: $("#questService").val(),
					sendAddress: $("#questAddress").val(),
					sendGivenKinship: $("#questGivenKinship").val(),
					sendContacts: $("#questContacts").val(),
					sendAdditionalInfo: $("#questAdditionalInfo").val(),
					sendRailway: checkedRailway
			},
			function(data){
				CampOnline.endAjax();
				if (data.indexOf("Проверьте") != -1) {
					$("#campOnlineMain").addClass("isHidden");
				}
					//$("#campOnlineMain").addClass("isHidden");
					if ($("#checkData").hasClass("isHidden")) {
						$("#checkData").removeClass("isHidden");
					}
					$("#checkData").html(data);
				/*} else {
					$("#showSummPrice").html(data);
				}*/
			});
	},
	goBack: function()
	{
		$("#checkData").html("");
		$("#campOnlineMain").removeClass("isHidden");
		if ($("#checkData").hasClass("isVisible")) {
			$("#checkData").removeClass("isVisible");
		}
		$("#checkData").addClass("isHidden");
	},
	changeCamp: function()
	{
		selectValue = $("#campList").val();
		
		$("input").each( 
			function(){
				$(this).removeAttr("checked");
			}
		);
		
		$("#uniqueCamp div").each(
			function( intIndex ){
				if ($(this).hasClass("isVisible")) {
					$(this).removeClass("isVisible");
				}
				$(this).addClass("isHidden");
			}
		);
		if (selectValue != 0) {
			$("#camp" + selectValue).addClass("isVisible");
		}
	},
	showQuest: function()
	{
		document.getElementById('bugContent').innerHTML = document.getElementById('bufferQuest').innerHTML;
		
		showPopup('bug');
		$("#popup").css({'margin-left':'0px'});
		document.getElementById('bugContent').innerHTML = document.getElementById('bufferQuest').innerHTML;
		//ФИО ребенка
		$("#questFio").html($("#childFio").val());
		$("#questBirthDate").html($("#childBirthDate").val());
		$("#questGender").html($("#childGender option:selected").text());
		$("#questPayerFio").html($("#parentsFio").val());
		$("#questPayerEmail").html($("#parentsEmail").val());
	},
	validateQuest: function()
	{
		CampOnline.waitAjax();
		$.get("/dev/camp-online-xmlhttp.php", 
		{
			verify: "afterQuest",
			sendPassNumber: $("#questPassNumber").val(),
			sendLivePlace: $("#questLivePlace").val(),
			sendPayerPassportSerial: $("#questPayerPassportSerial").val(),
			sendPayerPassportNumber: $("#questPayerPassportNumber").val(),
			sendPayerPassportGiven: $("#questPayerPassportGiven").val(),
			sendPayerAddress: $("#questPayerAddress").val(),
			sendPayerKinship: $("#questPayerKinship").val(),
			sendService: $("#questService").val(),
			sendAddress: $("#questAddress").val(),
			sendGivenKinship: $("#questGivenKinship").val(),
			sendContacts: $("#questContacts").val(),
			sendAdditionalInfo: $("#questAdditionalInfo").val()
		},
		function(data) {
			CampOnline.endAjax();
			if (data == 'ok') {
				CampOnline.sendData(1);
				closePopup();
			} else {
				$("#questError").html(data);
			}
		}
		);
	}
}

function showInnerContent(caseId, elementId) {
	if ($("#" + elementId).hasClass('isVisible')) {
		$("#" + elementId).removeClass('isVisible');
		$("#" + elementId).addClass('isHidden');
		$("#" + caseId).removeClass('controlCaseOpen');
		$("#" + caseId).addClass('controlCaseClose');
	} else {
		$("#" + elementId).addClass('isVisible');
		$("#" + elementId).removeClass('isHidden');
		$("#" + caseId).removeClass('controlCaseClose');
		$("#" + caseId).addClass('controlCaseOpen');
	}
}

$(document).ready(function() {
	
	var height = $('#slider').height()+2;
	arr = $('#slider #list .news').size();
	arr = parseInt(arr);
	size = $('#slider #list .news').size();
	newsSlider(arr, height);
	setInterval(function() { newsSlider(arr, height); }, (6000 * size));
	$.cookie('login', '');
	$.cookie('password', '');
});