/*****************************   anDeRan   ************************************/

jQuery(function($) {

	/*$("h3.name a").each(function () {
		var text = $(this).html();
		$(this).html(text.replace(/([ \(\"\.])(.)/g, '$1<span class="caps">$2</span>').replace(/^([ \(\"\.]*)(.)/g, '$1<span class="caps">$2</span>'));
	});*/

	Cufon.replace("#nav, .partner-std", {fontFamily: 'PartnerLightUltraCondensed', fontSize: '19px', letterSpacing: '2px', hover: true});
	Cufon.replace(".partner", {fontFamily: 'PartnerLightUltraCondensed'});
	Cufon.replace("h3.name, h4.subname, .form label, .fedra", {fontFamily: 'Fedra Serif A Pro Medium', letterSpacing: '1px'});

	if (jQuery().fancybox) {
		$("a.fancybox").fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'overlayColor'  : '#000',
			'overlayOpacity': 0.6,
			'onStart'       : function (links, index) {
				$(links).css("visibility", "visible");
				$(links[index]).css("visibility", "hidden");
			},
			'onClosed'      : function (links, index) {
				$(links[index]).css("visibility", "visible");
			}
		});
	}

	$(".exblock h4 a").click(function () {
		$(this).blur();
		$(this).parent().next("ul").stop(true, true).animate({height: "toggle", opacity: "toggle"}, "fast", function (a) {
			$(this).parent().toggleClass("exopened");
		}).find("img").stop(true, true).animate({opacity: "toggle"}, "fast");
		return false;
	});

	window.currentImage = [];
	$(".gallery").each(function (i) {
		currentImage[i] = 1;
		var container = $(this).find(".container");
		$("<A>").attr({href: '#'}).click(function () {
			$(this).blur();

			var total = parseInt(container.width(), 10) / 476;
			if (currentImage[i] > 1) {
				currentImage[i]--;

				var prev = $(this);
				container.stop(true, true).animate({left: '+=476px'}, "fast", function () {
					container.parent().find("span.current strong").html(currentImage[i]);
					Cufon.refresh();
					if (currentImage[i] == 1) {
						prev.fadeOut(200);
					}
				});
				prev.next("a.next").fadeIn("fast");
			} else {
				$(this).fadeOut("fast")
			}

			return false;
		}).addClass("prev").appendTo(this);

		$("<A>").attr({href: '#'}).click(function () {
			$(this).blur();

			var total = parseInt(container.width(), 10) / 476;
			if (currentImage[i] < total) {
				currentImage[i]++;

				var next = $(this);
				container.stop(true, true).animate({left: '-=476px'}, "fast", function () {
					container.parent().find("span.current strong").html(currentImage[i]);
					Cufon.refresh();
					if (currentImage[i] == total) {
						next.fadeOut(200);
					}
				});
				next.prev("a.prev").fadeIn("fast");
			}

			return false;
		}).addClass("next").appendTo(this);
	});

	window.currentAarch = [];
	$("#aarch").each(function (i) {
		currentAarch[i] = 1;
		var container = $(this).find(".container");
		var total = parseInt(container.width(), 10) / 227;

		$("<A>").attr({href: '#'}).click(function () {
			$(this).blur();

			if (currentAarch[i] > 1) {
				currentAarch[i]--;

				var prev = $(this);
				container.stop(true, true).animate({left: '+=227px'}, "fast", function () {
	
					if (currentAarch[i] == 1) {
						prev.fadeOut(200);
					}
				});
				prev.next("a.next").fadeIn("fast");
			} else {
				$(this).fadeOut("fast")
			}

			return false;
		}).hover(function () {
			$(this).addClass('prev-on');
		}, function () {
			$(this).removeClass('prev-on');
		}).addClass("prev").appendTo(this);

		var next = $("<A>").attr({href: '#'}).click(function () {
			$(this).blur();

			if (currentAarch[i] < total) {
				currentAarch[i]++;

				var next = $(this);
				container.stop(true, true).animate({left: '-=227px'}, "fast", function () {
					container.parent().find("span.current strong").html(currentAarch[i]);
		
					if (currentAarch[i] == total) {
						next.fadeOut(200);
					}
				});
				next.prev("a.prev").fadeIn("fast");
			}

			return false;
		}).hover(function () {
			$(this).addClass('next-on');
		}, function () {
			$(this).removeClass('next-on');
		}).addClass("next").appendTo(this);

		if (total > 1) {
			next.fadeIn();
		}
	});

	$(".banners-top a, .banners-left a, .banners-right a").mouseover();

	(function () {
		for (var i = 0; i < arguments.length; i++) {
			(new Image()).src = arguments[i];
		}
	})('/images/common/dark_bg.png', '/images/common/btn_close.png', '/images/common/mini_red.png', '/images/common/a_left_on.png', '/images/common/a_right_on.png');

	$("#authorize a[href='#login'], #comments .no-comments a[href='#login']").click(function () {
		$(this).blur();

		var win = $("<DIV>").addClass("window-login").prependTo("body");
		var inner = $("<DIV>").addClass("inner").prependTo(win);
		var form = $("<FORM>").attr({action: '#'}).addClass('form').prependTo(inner);
		form.html('<h3 class="t-login"></h3><p class="field"><label for="lbl-username">Имя пользователя</label> <input type="text" name="username" maxlength="60" class="field" id="lbl-username" /></p><p class="field"><label for="lbl-password">Пароль</label> <input type="password" name="password" maxlength="60" class="field" id="lbl-password" /></p><p class="checkbox"><input type="checkbox" name="remember" class="checkbox" id="lbl-remember" /> <label for="lbl-remember">Запомнить меня</label></p><p class="button"><button type="submit" class="btn-red"><img src="/images/common/mini_red.png" width="41" height="41" alt="" /></button></p><p class="remind"><a href="#">Забыли пароль?</a></p>');
		var close = $("<SPAN>").addClass("btn-close").prependTo(inner).click(function () {
			win.fadeOut(function () {
				$(this).remove();
			});
		});
		form.find('p.remind a').click(function () {
			$(this).blur();
			form.find("p.field").stop(true,true).fadeOut("fast", function () {
				$(this).remove();
			});
			$(this).fadeOut("fast", function () {
				form.addClass("form-remind");
				$("<P>").addClass("field").hide().html('<label for="lbl-email">E-mail</label> <input type="text" name="email" maxlength="60" class="field" id="lbl-email" />').insertAfter("h3.t-login").fadeIn("fast");
				Cufon.refresh();
				$(this).remove();
			});

			return false;
		});
		form.find('button').click(function () {
			$(this).blur();

			if (form.hasClass("form-remind")) {
				$.post("/scripts/requests.php?action=remind", { email:this.form.email.value }, function (data) {
					if (data.status == 200) {
							form.find("p.field, p.button").stop(true,true).fadeOut("fast", function () {
								form.addClass("remind-sent");

								var p = form.find("p.msg-error");
								if (!p.length) {
										p = $("<P>").addClass("msg-error").html("Письмо отправлено на указанный e-mail!").insertAfter(form.find("h3.t-login")).animate({height: "toggle", opacity: "toggle"}, "fast");
								}
								$(this).remove();
							});

							setTimeout(function () {
								close.click();
							}, 4000);
					} else {
						var p = form.find("p.msg-error");
						if (!p.length) {
							p = $("<P>").addClass("msg-error").html("Указанный e-mail не существует в базе данных!").insertAfter(form.find("h3.t-login")).animate({height: "toggle", opacity: "toggle"}, "fast");
							setTimeout(function () {
								p.slideDown().animate({height: "toggle", opacity: "toggle"}, "fast", function () {
									$(this).remove();
								});
							}, 2000);
						}
					}
				}, "json");
			} else {
				$.post("/scripts/requests.php?action=login", { username:this.form.username.value, password:this.form.password.value, remember:(this.form.remember.checked ? 1 : 0) }, function (data) {
					if (data.status == 200) {
						/*$("#authorize").remove();
						var logged = $("<P>").attr("id", "logged").appendTo("#header");
						$("<IMG>").attr({src:data.icon, width:40, height:40, alt:data.username}).appendTo(logged);
						$("<A>").attr("href", "/profil/").addClass("username").html(data.username).appendTo(logged);
						$("<A>").attr("href", "?logout").addClass("logout").html("Выйти").appendTo(logged);
						Cufon.refresh();
						close.click();*/
						location.href = location.pathname;
					} else {
						var p = form.find("p.msg-error");
						if (!p.length) {
							p = $("<P>").addClass("msg-error").html("Неверное имя пользователя или пароль!").insertAfter(form.find("h3.t-login")).animate({height: "toggle", opacity: "toggle"}, "fast");
							setTimeout(function () {
								p.slideDown().animate({height: "toggle", opacity: "toggle"}, "fast", function () {
									$(this).remove();
								});
							}, 2000);
						}
					}
				}, "json");
			}

			return false;
		});
		Cufon.refresh();
		win.fadeIn();

		return false;
	});

	$(".add-comment textarea").keypress(function(e) {
		if (e.keyCode == 13 && e.ctrlKey) {
			$(".add-comment p.button a").click();
		}		
	});

	$(".add-comment p.button a").click(function () {
		$(this).blur();

		var add  = $(this).parents(".add-comment");
		var page = add.find("input:hidden").val();
		var area = add.find("textarea");
		var text = area.val();
		area.val('');

		if (text.length > 2) {
			$.post("/scripts/requests.php?action=comments", {comment:text,page_id:page}, function (data) {
				$("#comments").html(data);
				Cufon.refresh();
			});
		}

		return false;
	});

	$("#comments .remove a").click(function () {
		$(this).blur();
		var comment = $(this).parents("div.comment");
		$.post("/scripts/requests.php?action=remove", {id:this.href.replace(/.+(\d+)$/, '$1')}, function (data) {
			if (data == "OK") {
				comment.animate({height: "toggle"}, function () {
					$(this).remove();
				});
			}
		});
		return false;
	});

	$(".sblock").hover(function () {
		var sub = $(this).find(".sb-inner");
		sub.stop(true, true).animate({height: "toggle", opacity: "toggle"}, 0);
	}, function () {
		var sub = $(this).find(".sb-inner");
		sub.stop(true, true).animate({height: "toggle", opacity: "toggle"}, 0);
	});

	$("#time").each(function () {
		var timer = $(this);
		Number.prototype.zeroFill = function () {
			return this >= 10 ? this : "0" + this;
		};

		var months = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'];

		setInterval(function () {
			var text = '<em>сегодня</em> ', date = new Date();
			text += "<strong>" + date.getDate() + " " + months[date.getMonth()] + "</strong> | <strong>" + date.getHours().zeroFill() + ":" + date.getMinutes().zeroFill() + "</strong>";
			timer.html(text);
		}, 1000);
	});

	if ($.browser.msie && $.browser.version < 7) {
		$("body").hide();
		location = "http://windows.microsoft.com/ru-RU/internet-explorer/products/ie/home";
	}
});

