$(function() {
    setTimeout(function() {
        $("#radiation").hide();
    }, 60000);

//    $("#flash").flash({
//        swf: "/img/photo_test4.swf",
//        width: 1200,
//        height: 350,
//        wmode: 'transparent'
//    });

    $(".blockOverlay").live("click", $.unblockUI);

    //login
    (function() {
        $("#login-link a").click(function() {
            $.blockUI({ message: $('#login') });
            $(".blockMsg").center();
            return false;
        });

        $("#login-form").submit(function() {
            $.post($(this).attr("action"), $(this).serialize(), function(res) {
                if (res.success) {
                    location.reload();
                } else {
                    $("#login-error").fadeIn();
                    $("#login-error").oneTime("3s", function() { $(this).fadeOut(); });
                }
            });
            return false;
        });

        $("#login-inner-form").submit(function() {
            $.post($(this).attr("action"), $(this).serialize(), function(res) {
                if (res.success) {
                    location.reload();
                } else {
                    $("#login-inner-error").fadeIn();
                    $("#login-inner-error").oneTime("3s", function() { $(this).fadeOut(); });
                }
            });
            return false;
        });

    })();

    //find salons
    (function() {
        $("#find-salon form").submit(function() {
            var city = $("#find-salon input[name='city']").val();
//            document.location = "/salons#city-" + city;
            document.location = "/salons/" + city;
            return false;
        });

        $("#find-salon a").click(function() {
            $.blockUI({ message: $('#cities') });
            $(".blockMsg").center();
            return false;
        });

        $("#cities a").click(function() {
            var id = $(this).attr("href").substring(1);
            $("#find-salon input[name='city']").val(id);
            $("#find-salon a").text($(this).text());
            $.unblockUI();
            return false;
        })
    })();

    //mainmenu
    (function() {
        var l1 = $("#mainmenu > ul");
        l1.find("li:has(ul)").hover(function() {
            var l2 = $(this).find("ul");
            l2.stop().css("visibility", "visible").animate({"opacity" : 1});
        }, function() {
            var l2 = $(this).find("ul");
            l2.stop().css("visibility", "visible").animate({"opacity" : 0}, 1, function() {
                l2.css("visibility", "hidden");
            });
        });
        //Чтобы не юзать метакласс hover для li тега
        l1.find("li ul.l2").hover(function() {
            $(this.parentNode.firstChild).addClass("hovered");
        }, function() {
            $(this.parentNode.firstChild).removeClass("hovered");
        });
    })();

    //right images
    (function() {
        var vp = $("#content .thumbs .viewport");
        var max = 0;
        vp.find("img").each(function() {
            max += $(this).width();
        });
        var next = $("#content .thumbs a[rel='next']");
        /*next.hover(function() {
            vp.stop(false, false).animate({scrollLeft: max }, 2000);
        }, function() {
            vp.stop(false, false);
        });*/
        next.click(function() {
            vp.stop(false, false).animate({scrollLeft: "+=125"}, 500);
            return false;
        });

        var prev = $("#content .thumbs a[rel='prev']");
        /*prev.hover(function() {
            vp.stop(false, false).animate({scrollLeft: 0}, 2000);
        }, function() {
            vp.stop(false, false);
        });*/
        prev.click(function() {
            vp.stop(false, false).animate({scrollLeft: "-=125"}, 500);
            return false;
        })
    })();


    //news
    var loc = "" + document.location;
    if (loc.indexOf("/news") != -1) //if news page
        (function() {
            //common
            function getRandomInt(min, max) {
                return Math.floor(Math.random() * (max - min + 1)) + min;
            }

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

            var scrollToGroups = function() {
                $.scrollTo($(".news-details"), 500, {offset: {top:-50, left:0} });
            };

            var scrollToItems = function() {
                $.scrollTo($(".news-items"), 500, {offset: {top:-50, left:0} });
            };

            var nd = $(".news-details");
            var dd = nd.find(".news-details-date");
            var dc = nd.find(".news-details-content");
            var ddd = dd.find(".day");
            var ddm = dd.find(".month");
            var ddy = dd.find(".year");

            var loadingAnim = function() {
                ddd.html(getRandomInt(1, 31));
                ddm.html(months[getRandomInt(1, 12)]);
                ddy.html(getRandomInt(1999, 2020));
            };

            var startLoading = function() {
                loadingInterval = setInterval(loadingAnim, 100);
                nd.fadeTo("fast", 0.3);
            };

            var stopLoading = function() {
                if (loadingInterval)
                    clearInterval(loadingInterval);
                nd.fadeTo("fast", 1);
            };

            var renderNews = function(news) {
                var date = new Date(news.date * 1000);
                var d = date.getDate();
                ddd.html(d >= 10 ? d : "0" + d);
                var m = date.getMonth();
                ddm.html(months[m]);
                ddy.html(date.getFullYear());
                dc.html(news.text);
                nd.attr("class", "news-details month" + (m + 1));
            };

            var showGroup = function(g, scroll) {
                $("a[href!='#" + g + "']").removeClass("current");
                $("a[href='#" + g + "']").addClass("current");

                if (g == 'all') {
                    $("a.news-item").fadeIn();
                    if (scroll && $("a.news-item").length)
                        scrollToItems();
                } else {
                    var left = $("a.news-item[rel!='" + g + "']");
                    var right = $("a.news-item[rel='" + g + "']");
                    if (left.length) {
                        left.fadeOut(function() {
                            right.fadeIn();
                            if (scroll && right.length)
                                scrollToItems();
                        });
                    }
                    else
                        right.fadeIn();
                }
            };

            var showNews = function(id) {
                if (!id) return;
                scrollToGroups();
                startLoading();
                $.ajax({
                    url: "/news/item/" + id,
                    success: function(res) {
                        stopLoading();
                        if (res.success) renderNews(res.news);
                    },
                    error: function() {
                        stopLoading();
                    }
                });
            };

            $(".news-category a").click(function() {
                var g = $(this).attr("href").substring(1);
                showGroup(g, true);
            });

            $(".news-item").click(function() {
                var id = $(this).attr("href").substring(6);
                showNews(id);
            });

            $(".to-news-categories").click(function() {
                scrollToGroups();
                return false;
            });

            //hash as current group store
            var group = 'all';
            var news = false;
            var h = location.hash;
            if (h) h = h.substring(1);
            if (h && h.indexOf("news") !== 0) {
                group = h;
            } else {
                news = h.substring(5);
            }
            showGroup(group);
            showNews(news)
        })();


    //salons page
    if (loc.indexOf("/salons") != -1 && false)
        (function() {
            var scrollToCities = function() {
				//$(".salon-list").slideUp();
				$.scrollTo($(".cities"), 500, {offset: {top:-50, left:0} });
            };

            var scrollToCity = function(id) {
				$(".salon-list").slideUp();
				$("#sl-" + id).slideDown(function(){
                    $.scrollTo($("#c-" + id), 500, {offset: {top:-20, left:0} });
                });
            };

            $("a.up").click(function() {
                scrollToCities();
            });

            $(".cities .names a").click(function() {
                var href = $(this).attr("href");
                var id = href.substring(6);
                if (id) scrollToCity(id);
            });

            var h = document.location.hash;
            if (h.indexOf("city") === 1)
                var id = h.substring(6);
            if (id) scrollToCity(id);
        })();

    if(loc.indexOf("/order") != -1)
        (function(){

            var scrollToOldUser = function(){
                $.scrollTo($("#order-auth-viewport"), 200);
                $("#order-auth-viewport").scrollTo($("#order-old"), 200);
            };               
            var scrollToNewUser = function(){
                $.scrollTo($("#order-auth-viewport"), 200);
                $("#order-auth-viewport").scrollTo($("#order-new"), 200);
            };
            $("#order-auth-viewport a[href='#yes']").click(scrollToOldUser);
            $("#order-auth-viewport a[href='#no']").click(scrollToNewUser);

            var h = document.location.hash;
            if(h)
                h = h.substring(1);

            if(h == 'yes') scrollToOldUser();
            else if(h=='no')  scrollToNewUser();

			$("#nonauth-order-submit").click(function() {
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if ($("input[name=fio]").attr("value").length > 0 &&
						$("input[name=phone]").attr("value").length > 0 &&
						$("input[name=email]").attr("value").length > 0 &&
						reg.test($("input[name=email]").attr("value")) != false) {
					$("#nonauth-order-form").submit();
				} else {
					$("#nonauth-error").fadeIn();
                    $("#nonauth-error").oneTime("3s", function() { $(this).fadeOut(); });
				}
			});
        })();
});

