function closePopup(popup,shadow){
	$(popup).fadeOut(400);
	$(shadow).fadeOut(400,function(){$(this).remove()});
	return false
}
var anim = false;
$(function(){
// webim button
	window.webimInit = { invitation: {"theme":"static","position":""} };
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "http://stomkom.pro-service.webim.ru/webim/js/button.js";
    document.getElementsByTagName("head")[0].appendChild(s);
// webim button

	var site = $("#layout")
	var node;
	node = $("#h_off");
	if (node.length){
		var head = $("#head");
		var h = node
		$(node).toggle(
		      function (e) {
				$(h).blur();
		        $(head).animate({top: "-=50", marginBottom : "-=50"},500,function(){$(h).text("Показать");})
		      },
		      function (e) {
				$(h).blur();
		        $(head).animate({top: "+=50", marginBottom : "+=50"},500,function(){$(h).text("Скрыть");})
		      }
		    );
		
	}

	// node = $("#chair");
	// 	if (node.length){
	// 		var ni = new Image();
	// 		ni.src = "/i/ind2.jpg";
	// 		var nInd = $("<div/>",{id:"index2"})
	// 		$($(node).parent()).append(nInd)
	// 		$(node).mouseover(function(){
	// 			$("#index2").fadeIn(500)
	// 		}).mouseout(function(){
	// 			$("#index2").fadeOut(500)
	// 		})
	// 	}
	
	node = $("#gallery")
	if (node.length){
		var gallery = new Gallery();
		gallery.init();
	}
	
	node = $("#fancy");
	if (node.length){
		$("a.fancy").fancybox({
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'speedIn'		:	600, 
				'speedOut'		:	200, 
				'overlayShow'	:	true,
				'padding'		: 	0
			});
		
	}
	
	var popup = $("#popup")
	if (!popup.length){
		popup = $("<div/>",{id:"popup"}).html("<div class='c_t'></div><div class='c_m'></div><div class='c_b'></div><a href='#close' id='close'></a>").appendTo(site);
	}
	var popupC = $("div:eq(1)",popup);
	
	node = $("#appoint")
	if (node.length){
		var appoint = $("div:eq(0)",$(node).parent()).html();
		$(node).click(function(){
			var shadow=$("<div/>",{id:"shadow"}).css("opacity","0.3")
			$(shadow).appendTo(site).fadeIn(400)
			$(popupC).html(appoint);
			$(popup).fadeIn(400);
			$("#close").live("click",function(){closePopup(popup,$("#shadow"));return false;}).focus(function(){$(this).blur()})
			$(shadow).click(function(){closePopup(popup,shadow)})
			$(".sub",popupC).click(function(){
				var input = $("input",popupC);
				var text = $("textarea",popupC);
				if (input[0].value=="" || input[2].value=="" ){
					alert("Не заполнены обязательные поля!")
				}
				else{
					$(this).attr("value","Отправляю...").attr("disabled","disabled")
					var post = "";
					$(input).each(function(){
						post+=$(this).attr("name")+"="+$(this).attr("value")+"&"
					})
					post+=$(text[0]).attr("name")+"="+$(text[0]).attr("value")
					$.ajax({
						  type: "POST",
						  url: "/webforms/send/",
						  cache: false,
						  data: post,
						  success: function(html){
							alert("Сообщение отправлено, в скором времени вам ответят!")
						    $(shadow).click();	
						  }
						});
				}
			})
		})
	}
	node = $("#appoint2")
	if (node.length){$(node).click(function(){
		$("#appoint").click();
	})}
	node = $("#question")
	if (node.length){
		var question = $("div:eq(0)",$(node).parent()).html();
		$(node).click(function(){
			var shadow=$("<div/>",{id:"shadow"}).css("opacity","0.3")
			$(shadow).appendTo(site).fadeIn(400)
			$(popupC).html(question);
			$(popup).fadeIn(400);
			$("#close").live("click",function(){closePopup(popup,$("#shadow"));return false;}).focus(function(){$(this).blur()})
			$(shadow).click(function(){closePopup(popup,shadow)})
			$(".sub",popupC).click(function(){
				var adr_pattern=/[0-9a-z_]+@[0-9a-z_]+\.[a-z]{2,5}/i; 
				var input = $("input",popupC);
				var text = $("textarea",popupC);
				if (input[0].value=="" || input[1].value=="" || text[0].value=="" ){
					alert("Не заполнены обязательные поля!")
				}
				else if (!adr_pattern.test(input[1].value)){
					alert("Не правильно введен e-mail");
				}
				else{
					$(this).attr("value","Отправляю...").attr("disabled","disabled")
					var post = "";
					$(input).each(function(){
						post+=$(this).attr("name")+"="+$(this).attr("value")+"&"
					})
					post+=$(text[0]).attr("name")+"="+$(text[0]).attr("value")
					$.ajax({
						  type: "POST",
						  url: "/webforms/send/",
						  cache: false,
						  data: post,
						  success: function(html){
							alert("Сообщение отправлено, в скором времени вам ответят!")
						    $(shadow).click();	
						  }
						});
				}
			})
		})
	}
	
	node = $(".layout_right")
	if (node.length){
		// function blink_text(f,node){
		// 	if (f) $(node).css("color","white");
		// 	else $(node).css("color","black");
		// 	setTimeout(function(){blink_text(!f,node)},800)
		// }
		function blink_image(f,node){
			if (f) {
				$(node[0]).fadeOut(1400)
				$(node[1]).fadeIn(2500)	
			}
			else {
				$(node[1]).fadeOut(1400)
				$(node[0]).fadeIn(3000)
			}
			setTimeout(function(){blink_image(!f,node)},4000)
		}
		var img;
		$(".rounda",node).each(function(i,val){
			img = $("img",val);
			
		 	blink_image(1,img)

		})
		
		
	}
	
	node = $("#popup2")
	if (node.length){
		function findActiv(pages){
			var r = "error"
			$(pages).each(function(i){
				if ($(this).attr("id")=="ap") {
					r = i
				}
			})
			return r;
		}
		var activ;
		var pages = $(".pagePop a",node)
		var back = pages[0];
		var next = pages[(pages.length-1)];
		var yes = $(".yes");
		pages = $(pages).not("a:first").not("a:last")
		$(pages).click(function(){
				anim = true	
				activ = $("#ap");
				var lastA = findActiv(pages);
				$(this).blur();
				$(activ).attr("id","");
				$(this).attr("id","ap")
				var act = findActiv(pages);
				$("#q"+lastA).fadeOut(500,function(){
					$("#q"+act).fadeIn(500,function(){anim=false;})})

		})
		$(next).click(function(){
			if (anim==false){
				anim = true
				$(this).blur();
				var act = findActiv(pages);
				if (act<(pages.length-1)) {
					$("#q"+act).fadeOut(500,function(){
						$("#q"+(act+1)).fadeIn(500,function(){anim=false;})})
					$(pages[act+1]).click()
					}
			}
		})
		
		$(back).click(function(){
			if (anim==false){
				anim = true
				$(this).blur();
				var act = findActiv(pages);
				if (act!=0) {
					$("#q"+act).fadeOut(500,function(){
						$("#q"+(act-1)).fadeIn(500,function(){anim=false;})})
					$(pages[act-1]).click()}
			}
		})
		
		$(yes).click(function(){$(next).click()})

	}
	
	node = $("#pop2")
	if (node.length){
		$(node).click(function(){
			var shadow=$("<div/>",{id:"shadow"}).css("opacity","0.3")
			$(shadow).appendTo(site).fadeIn(400)
			var popup2 = $("#popup2");
			$(popup2).fadeIn(400);
			$("#close").click(function(){closePopup(popup2,shadow);return false}).focus(function(){$(this).blur()})
			$(shadow).click(function(){closePopup(popup2,shadow)})
			
			$(".sub2",popup2).removeAttr("disabled").click(function(){
				var input = $("input",popup2);
				if (input[1].value=="" || input[2].value=="" || input[3].value=="" ){
					alert("Не заполнены обязательные поля!")
				}
				else{
					var msg = "Коллектив клиники ССК благодарит Вас за заполнение приложенной формы.\nНаши администраторы в ближайшее время свяжутся с Вами для уточнения всех подробностей приема."
					$(this).attr("value","Отправляю...").attr("disabled","disabled")
					var post = "";
					$(input).each(function(){
						post+=$(this).attr("name")+"="+$(this).attr("value")+"&"
					})
					$.ajax({
						  type: "POST",
						  url: "/webforms/send/",
						  cache: false,
						  data: post,
						  success: function(html){
							alert(msg)
						    $(shadow).click();	
						  }
						});
				}
			})
		
		})
	}

	var slidebox = $("#slider") 
	if (slidebox.length){
		var timer;
		var speed = 900;
		var speed2 = 5500;
		function slider(slidebox,speed,speed2){
			var node = $("li:eq(0)",slidebox);
			$(slidebox).append(node)
			var node_f = $("li:eq(0)",slidebox).css("display","block");
			$(node).fadeOut(speed,function(){
						timer = setTimeout(function(){
							slider(slidebox,speed,speed2)
						},speed2)}
					)

		}

		var first_node = $("li:eq(0)",slidebox).css("display", "block");
	 	timer = setTimeout(function(){slider(slidebox,speed,speed2)}, speed2)
	}

})

function Gallery (){
	this.gallery = $("#gallery");
	this.bImg = $("#bigImg")
	this.listImg = $("#listImg")
	this.mass = new Array($("div:eq(0)",this.bImg)); //массив всех больших картинок
	this.massL; // количество элементов
	this.speed = 5000 // скорость переключения
	this.timer; // ссылка на таймер
}

Gallery.prototype.init = function(){
	var _t = this;
	$(this.listImg).css("display","block");
	// получаем все ссылки на большие изображения
	$("a",this.listImg).each(function(i,val){
		$(this).click(function(){
			$(val).blur();
			clearTimeout(_t.timer);
			var curent = $("div:visible",_t.bImg);
			var next = $(_t.mass[i]);
			$("a",_t.listImg).removeClass();
			$("a:eq("+i+")",_t.listImg).addClass("act");
			curent.fadeOut(500);
			next.fadeIn(500);
			return false
		})
		if (i==0) return 1
		var href = $(this).attr("href")
		var div = $("<div/>",{"style":"background-image:url("+href+")"})
		_t.mass[i] = div;
		$(_t.bImg).append(div)
	})
	
	this.massL = this.mass.length;
	this.timer = setTimeout(function(){_t.start(0)}, _t.speed);
}

Gallery.prototype.start = function(i){
	var i = i,k=i;
	var _t = this;
	 if (i==(_t.massL-1)){k = 0;}
	else {k++;}

	$("a:eq("+i+")",this.listImg).removeClass();
	$("a:eq("+k+")",this.listImg).addClass("act");
	$(_t.mass[i]).fadeOut(500);
	$(_t.mass[k]).fadeIn(500);
	this.timer = setTimeout(function(){_t.start(k)}, _t.speed);
}
