
/*
AM7kAEwBAAAA_S9gbgMAayHpg3Q1Ec1L_qPcqP76knRM9roAAAAAAAAAAAA8ww1XTdccGPPOZ8tQjfTDsCWkoA==
*/

$(document).ready(function () {
		initbanner();

			$('.txt label').each(function () {
				var url = $(this).attr('url');
				var info = $(this).attr('title');
				$(this).data('url', url);
				$(this).data('title', info);
				$(this).removeAttr('url');
				$(this).removeAttr('title');
				
			});
		
		
			if ($("#course_list").size() > 0) {
				$("#course_list").click(function () {
					
					$("#courseListHolder").slideToggle(400);
					
					return false;
				});
			} 
		
		
		if ($('#program-holder').size() > 0) {
			$('#program-holder').show(); 
			$('#cutdescription-holder').hide(); 
			$('#feed-holder').hide(); 
		}
		if ($('.normal-btn-state').size() > 0) {
		$('.normal-btn-state[rel="program-holder"]').addClass("active");
			var loaded = false;
			
			$('.normal-btn-state').click(function () {
			
				if ($(this).attr("rel") != "feed-holder") {
					
					$(".container-block").each(function () {
						if ($(this).css("display") == "block") {
							$(this).slideToggle(300);
						}
					});
					
					var id = $(this).attr("rel");
					$("#"+id).slideToggle(400);
					
					$('.normal-btn-state').removeClass('active');
					$(this).addClass('active');
				
				
				} else {
				
					var obj = $(this);
					var url = $(obj).attr("href");
					if (!loaded) {
						$.ajax({
								url: "/ajax/",
								type: 'POST',
								data: "act=get_feed_back&url="+url,
								dataType: 'JSON',
								timeout: 3000,
								beforeSend: function () {
									$("#waiter-ajax").fadeIn("slow");
								},
								complete: function () {
									$("#waiter-ajax").fadeOut("slow");
								},
								success: function (data) {
									loaded = true;
									$(".container-block").each(function () {
										if ($(this).css("display") == "block") {
											$(this).slideToggle(300);
										}
									});
						
									$("#feed-holder").html(data);
									$("#feed-holder").slideToggle(400);
									$('.normal-btn-state').removeClass('active');
									$(obj).addClass('active');								
									
								},
								error: function () {
									alert("Ошибка. Повторите ещё раз.");
								}
					    });					
					} else {
					
						$(".container-block").each(function () {
							if ($(this).css("display") == "block") {
								$(this).slideToggle(300);
							}
						});					
					
						$("#feed-holder").slideToggle(400);
						$('.normal-btn-state').removeClass('active');
						$(obj).addClass('active');	
					}
					
					
				
				}
				
				
				return false;
				
			});
						
		}
		
		

/*		
		$("#courseList").hide();
		$("#slideblock").show();
*/

		
		$(".smallhref").click(function () {
			var txt = ($("#courseList").css("display") != "none") ? "отобразить весь список курсов" : "отобразить акционный блок";
			$("#courseList").slideToggle("slow");
			$("#slideblock").slideToggle("slow");
			
			$(this).text(txt);
			
			return false;
		});
		var animating = false;	
		var time = 600;	
		$("#slideRight").click(function() {
			if (animating == false) {
				animating = true;
					var itemcount = $("#slider").children("ul").children("li").size();					
					var pos = $("#slider").css("left");
					pos = parseFloat(pos);		
					var shehe = 837;
					var shift = shehe * itemcount;
					var maxshift = 837 - shift;
					if (maxshift < pos) {
						$("#slider").animate({left: "-="+shehe+"px"}, time, function () {
							animating = false;
							var pos = $("#slider").css("left");
							pos = parseFloat(pos);
							if (maxshift > pos) {
								maxshift = maxshift - itemcount;
								$("#slider").animate({left: maxshift+"px"}, time);
							}
						});
					} else {
						animating = false;
					}
			}	
			return false;
		});
		
		
		$("#slideLeft").click(function() {
			if (animating == false) {
				animating = true;
				var pos = $("#slider").css("left");
				pos = parseFloat(pos);
				var shehe = 837;
				if (pos < 0) {
					$("#slider").animate({left: "+="+shehe+"px"}, time, function () {
						animating = false;
						var pos = $("#slider").css("left");
						pos = parseFloat(pos);
						if (pos > 0) {
							$("#slider").animate({left: "0px"}, time);
						}
					});
				} else {
					animating = false;
				}
			}	
			return false;
		});

					
			$('.txt label').click(function () {
				
					var url = $(this).data('url');
					var info = $(this).data('title');
					
				if (url || info) {
				
					var off = $(this).offset();
					var mt = off.top - 5;
					var ml = off.left + $(this).width() + 5;
					
					if ((ml + 250) > $(window).width()) {
						ml = ml - 5 - ( ml + 250 - $(window).width());
						var mt = off.top + 20;
					}
					
					var ident = Math.round(($(this).text().length + Math.random() * 1000), 0);
					ident = "tip"+ident;
					
					var more = ' <a href="'+ url +'" target="_blank" style="color:#1600CF; text-decoration:underline;">подробнее</a>';
					info = (url) ? info+more : info; 
					var hide = '<div style="text-align:right; padding-top:2px;"><a style="cursor:pointer; color:#ff6600; text-decoration:underline;" onClick="removeTip(\''+ ident +'\')">скрыть</a></div>';
					var c = '<div class="informerBlock" style="top:'+ mt +'px; left:'+ ml +'px;" id="'+ ident +'"><b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b><div class="bb"><div class="tipBlock">'+ info +' '+ hide +'</div></div><b class="b4"></b><b class="b3"></b><b class="b2"></b><b class="b1"></b></div>';
					$('body').append(c);
					$("#"+ident).fadeIn(400);
					
					setTimeout("removeTip('"+ident+"')", 8000);
				}	
			});

				/*
				$('.txt').children('label').hover(function(e) 
				{
					var bid = $(this).attr('rel');
					
					$('#'+bid)
					.css("position","absolute")
					.css("top",(e.pageY-50) + "px")
					.css("left",(e.pageX+0) + "px")
					.css("display","none")
					.fadeIn("fast");									
				}, 
				function()
				{
					var bid = $(this).attr('rel');
					$('#'+bid).fadeOut('fast');
				});
				
				$('.txt').children('label').mousemove(function(e){
				var bid = $(this).attr('rel');
					$("#"+bid)
						.css("top",(e.pageY-50) + "px")
						.css("left",(e.pageX + 5) + "px")					
				});			
				*/
				
				
				
		
		/*
		$(".has-nodes a").click(function () {
				if ($(this).hasClass("easingMenu")) {
					
					$(this).animate({fontSize: "10pt", borderBottomColor: "black"}, 300, function () {
						$(this).removeClass("easingMenu");
					});
					$(this).next("ul").fadeOut(200, function () {
						//$(this).parent().addClass("active");
					});
				} else {
					$(this).addClass("easingMenu");
					$(this).animate({fontSize: "16pt", borderBottomColor: "black"}, "slow", function () {});
					$(this).next("ul").slideToggle("slow", function () {
						//$(this).parent().addClass("active");
					});
				}
			return false;
		});
		
		$(".cmi").click(function () {
			var p = $(this).attr("rel");
			
			$(".pageBold").removeClass("pageBold");
			$(this).addClass("pageBold");
			
			slideTo(p);
			return false;
		});
		//initilize
		*/
		//$("body").addClass("JS");

		
		//$(window).bind("resize", initBlock);
		//$(window).bind("resize", initWidth);
		//initWidth();
			
			var options = { 
			beforeSubmit: function () { 
				$("#waiter").children("img").fadeIn(400);
				$("#submitForm").data("cont", $("#submitForm").children("span").children("span").text());
				$("#submitForm").children("span").children("span").text("Подождите");
			}, 
			complete: function () {
				$("#waiter").children("img").fadeOut(400);
				$("#submitForm").children("span").children("span").text($("#submitForm").data("cont"));
			},
			success: function (responseText, statusText) {
				$("#readyMessage").fadeIn("slow");
				setTimeout(function () { $("#popUpWindow").fadeOut(300, function () { $("#bgFeed").fadeOut(400); });}, 2000);
			},
			error: function (statusText) {
				alert("Превышено ожидание от сервера! Попробуйте ещё раз!");
			},
			timeout: 7000
		};		


		$("#submitForm").click(function () {			
				if (validateform()) {
					$("#formBlock").ajaxSubmit(options);
				}
			return false;
		});
		
		$("#formBlock").submit(function () {		
				if (validateform()) {
					$("#formBlock").ajaxSubmit(options);
				}
			return false;
		});
		
		
		$("#subscribeCourse").click(function () {
			$("#readyMessage").hide();
			document.formBlock.reset();
			$("#bgFeed").css({display:"block", opacity: "0"});
			$("#bgFeed").animate({opacity: "0.9"}, 300, function () {
				$("#popUpWindow").fadeIn(400);
			});
			return false;
		});
		
		
		$("#closePop").click(function () {
			$("#popUpWindow").fadeOut(300, function () {
				$("#bgFeed").fadeOut(400);
			});
			return false;
		});
		
		
		$(".expand_ico").click(function () {
			var obj = $(this);
			var ulObj = $(obj).parent().parent().children("ul");
			$(".expand_ico").not(obj).each(function () {
				var ulOb = $(this).parent().parent().children("ul");
				if ($(ulOb).css("display") == "block") {
					$(ulOb).slideToggle("300");
					$(this).removeClass("active");
				}
			});
			
			
			
			$(ulObj).slideToggle(300, function () {
				if ($(obj).hasClass("active")) {
					$(obj).removeClass("active");
				} else {
					$(obj).addClass("active");
				}
			});
			
		});
		$(".innerCourse").each(function () {
			if (!$(this).hasClass("active")) {
				$(this).hide();
			}
		});
		
		
});

function validateform() {
	
	var name = $("#name");
	var fname = $("#fname");
	var phone = $("#phone");
	var mail = $("#mail");
	var course = $("#course");
	
	$(".error_field").removeClass("error_field");
	$("#errorfield").html('&nbsp;');
	
	var text_name = $(name).val();
	if (!text_name) {
		$(name).addClass("error_field");
		$("#errorfield").text('Заполните поле "Имя"');
		return false;
	}	
	
	var text_fname = $(fname).val();
	if (!text_fname) {
		$(fname).addClass("error_field");
		$("#errorfield").text('Заполните поле "Фамилия"');
		return false;
	}
		
		
	var text_mail = $(mail).val();
	var text_phone = $(phone).val();
	
	if (!text_phone && !text_mail) {
	
		$(phone).addClass("error_field");
		$(mail).addClass("error_field");
		$("#errorfield").text('Укажите какие-то контактные данные');
		return false;
	}		
	
	var text_mail = $(mail).val();
	if (text_mail) {
		apos = text_mail.indexOf("@");
		dotpos = text_mail.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			$(mail).addClass("error_field");
			$("#errorfield").text('E-mail не верного формата');
			return false;
		}
	}	
	
	
	var text_phone = $(phone).val();
	if (text_phone) {
		var cphone = text_phone.replace('+', '');
		var stripped = cphone.replace(/[\(\)\.\-\ ]/g, '');
		if (isNaN(parseInt(stripped))) {
			$(phone).addClass("error_field");
			$("#errorfield").text('Телефон не верного формата (+380123456789)');
			return false;
		} else if (!(stripped.length == 12	)) {
			$(phone).addClass("error_field");
			$("#errorfield").text('Телефон не верного формата (+380123456789)');
			return false;
		}
	}
	return true;
}

function removeTip(id) {
	if ($("#"+id).length) {
		$("#"+id).fadeOut(400, function () {
			$(this).remove();
		});
	}
}

var rotatorInterval;
var divIndex = 0;
var rotating = true;
var started = false;
var ctime = 500;
var intTime = 4000;

var rotor = function () {
	hider();
	var rotatorInterval = setInterval(hider, intTime);
}

var initbanner = function () {
	var h = $('#pointers');
	$('#banns').find('div.bcont').hover(function () {stopRotator()}, function () {restartRotator()});
	$('#banns').find('div.bcont').each(function () {
		$(this).css({display: "none", position: "absolute", top: "0px", left: "0px"});
		var id = $(this).attr('id');
		if (id) $(h).append('<a href="#'+id+'" class="point" onClick="nextBanner(this);return false" onMouseOver="nextBanner(this);" onMouseOut="restartRotator();"></a>');
	});
	rotor();
}

var nextBanner = function (obj) {
	stopRotator();
	var cIndex = $(obj).parents('div:first').find('a').index(obj);
	var list = $('#banns').find('div');
	var hList = $('#pointers').find('a');
	var listActive = $('#banns').find('div.bcont.active');
	var hListactive = $('#pointers').find('a.active');
	
	$(listActive).fadeOut(ctime);
	$(hListactive).removeClass('active');
	$(listActive).removeClass('active');
	
	var t = $(obj).attr('href');
	
	$(t).fadeIn(ctime);
	$(t).addClass('active');
	$(obj).addClass('active');	
	
	divIndex = cIndex;
}

var restartRotator = function () {
	
	rotating = true;

}
var stopRotator = function () {
	
	rotating = false;

}

var hider = function () {
	if (!started && rotating) {
			var list = $('#banns').find('div');
			var hList = $('#pointers').find('a');		
			$(list[divIndex]).addClass('active');
			$(list[divIndex]).fadeIn(ctime);
			$(hList[divIndex]).addClass('active');
			started = true;
		}
	else if (rotating) {			
			var aaa = $('#pointers').find('a');
			var cIndex = $(aaa).index($('#pointers').find('a.active'));
			var nIndex = cIndex + 1;
			nIndex = (nIndex > ($(aaa).size() - 1)) ? 0 : nIndex;
			var list = $('#banns').find('div.bcont');
			var hList = $('#pointers').find('a');
			var listActive = $('#banns').find('div.bcont.active');
			var hListactive = $('#pointers').find('a.active');
			$(listActive).fadeOut(ctime);
			$(hListactive).removeClass('active');
			$(listActive).removeClass('active');
			$(list[nIndex]).fadeIn(ctime);
			$(list[nIndex]).addClass('active');
			$(hList[nIndex]).addClass('active');
	}
}
 
function initWidth() {
	var he = $(window).height();
	var wi = $(window).width();
	$(".letter").children("img").each(function () {
		$(this).height(he);
		//var cl = $(this).width();
		//$(this).parent().next("div.letter_c").css("left", cl+"px");
	});	
	$(".letter_bg").children("img").each(function () {
		$(this).height(he);
		$(this).width(wi);
		//var cl = $(this).width();
		//$(this).parent().next("div.letter_c").css("left", cl+"px");
	});
	initBlock();
}

function slideTo(p) {
	var pl = $("#page-content").children("li");
	var cl = $("#page-content").children("li#"+p);
	
	var pos = $(pl).index(cl);
	var shift = pos * $(cl).width();
	$("#sb").animate({left: -shift+"px"}, "slow");
}

function initBlock() {
		var w = $(window).width();
		var h = $(window).height();
		
		var pages = $("#page-content").children("li");

		$(pages).each(function () {
			$(this).width(w);
			$(this).height(h);
		});
		
		$("#sb").height(h);
		$("#fb").height(h);
		
	var pl = $(".pageBold").parent().parent().children("li");
	var cl = $(".pageBold").parent();
	
	var pos = $(pl).index(cl);
	
	if (pos != -1) {
		var shift = pos * w;
		$("#sb").css("left", -shift+"px");	
	}
}

