﻿function goToByScroll(id) {
    $('html,body').animate({ scrollTop: $("#" + id).offset().top }, 'slow');
    return false;
}

$(document).ready(function () {


    // Placeholder for searchfield
    function hint(elementId) {
        var el = $("#" + elementId);
        var sval = el.attr("placeholder");
        if (sval == "") return;

        el.val(sval);
        el.focus(function () {
            if (el.val() == sval) {
                el.val("");
            }
        }).blur(function () {
            if (el.val() == "") {
                el.val(sval);
            }
        });
    }
    hint("quicksearch");

    // Scroll to anchor
    var baseLocation = window.location.href;
    var getLocationEnd = baseLocation.split("#");
    $(".scroll a").click(function () {
        var target = $(this).attr("href");
        $('html, body').animate({
            scrollTop: $($(this).attr("href")).offset().top + 8
        }, 1000, function () {
            if (getLocationEnd[1]) {
                window.location.href = getLocationEnd[0] + target;
            }
            else {
                window.location.href = baseLocation + target;
            }
        });
        // $("#navEternal li").removeClass("currentStage");
        return false;
    });

    if ($("#bmStage0").length) {

        // Fixed headers for in focus vote results
        var navPanel = $("#navEternal"),
        	topScroll = $("#topScroll"),
        	eternalShadow = $("#eternalShadow");

        $(window).bind("scroll load resize", function () {
            var currentScrollPos = $(document).scrollTop();

            if (currentScrollPos > Math.round($("#navPanel").offset().top)) {
                navPanel.addClass("fixed");
                eternalShadow.show();
            }
            else {
                navPanel.removeClass("fixed");
                eternalShadow.hide();
            }

            if ((currentScrollPos > Math.round($("#bmStage0").offset().top)) && (currentScrollPos < Math.round($("#bmStage1").offset().top))) {
                $("#navEternal li").removeClass("currentStage");
                $("#navEternal li:nth(0)").addClass("currentStage");
                eternalShadow.show();
            }
            else if ((currentScrollPos > Math.round($("#bmStage1").offset().top)) && (currentScrollPos < Math.round($("#bmStage2").offset().top))) {
                $("#navEternal li").removeClass("currentStage");
                $("#navEternal li:nth(1)").addClass("currentStage");
            }
            else if (currentScrollPos > Math.round($("#bmStage2").offset().top)) {
                $("#navEternal li").removeClass("currentStage");
                $("#navEternal li:nth(2)").addClass("currentStage");
            }
            else {
                $("#navEternal li").removeClass("clickedStage").removeClass("currentStage");
            }
        });
        navPanel.find("li a").click(function () {
            $(this).parent().siblings().removeClass("clickedStage");
            $(this).parent().addClass("clickedStage");
        });

        // Cosmetic.. on load
        if (getLocationEnd[1] == "bmStage0") {
            $("#navEternal li:nth(0)").addClass("currentStage");
        }
        else if (getLocationEnd[1] == "bmStage1") {
            $("#navEternal li:nth(1)").addClass("currentStage");
        }
        else if (getLocationEnd[1] == "bmStage2") {
            $("#navEternal li:nth(2)").addClass("currentStage");
        }
    }

    // Append Twitter url to Twitter list items
    $("#tweets .list li").each(function () {
        var tweet = $(this).data('url');
        $(this).click(function () {
            window.location = tweet;
        });
    });

    // News listing
    var listHeight = $(".primary .listings .list").height();
    $(".primary .listings .list .story .inner").each(function () {
        if ($(this).height() > listHeight) {
            listHeight = $(this).height();
        }
    });
    $("#productList").css("min-height", listHeight + 90)
    $(".primary .listings .list .story .inner").css("min-height", listHeight - 40);
    $("#productList .list li:first-child").addClass("hovered");
    $(".listings #inShort .list li").each(function () {
        $(this).mouseenter(function () {
            $(this).siblings().removeClass("hovered");
            $(this).addClass("hovered");
        }).mouseleave(function () {
            $(this).removeClass("hovered");
        });
    });
    $(".primary .listings .list li").each(function () {
        $(this).bind("mouseenter focus", function () {
            $(this).siblings().removeClass("hovered");
            $(this).addClass("hovered");
        });
    });
    $(".primary .listings ul.list .story .arrow").each(function () {
        var arrowPos = Math.round($(this).parent().parent().offset().top - $(this).parent().parent().parent().offset().top) + 25;
        $(this).css("top", arrowPos);
    });

    // Link box listing with hover story (secondary column)
    $(".secondary .linkBox ul li").each(function () {
        $(this).mouseenter(function () {
            $(this).addClass("hovered");
        }).mouseleave(function () {
            $(this).removeClass("hovered");
        });
    });

    // Align height on product line items
    $(".productLine").each(function () {
        var headerHeight = 0;
        var textHeight = 0;

        $(this).find("> li h2").each(function () {
            if ($(this).height() > headerHeight) {
                headerHeight = $(this).height();
            }
        });
        $(this).find("> li .dotline.text").each(function () {
            if ($(this).outerHeight() > textHeight) {
                textHeight = $(this).outerHeight();
            }
        });

        $(this).find("> li h2").css({ "height": headerHeight });
        $(this).find("> li .dotline.text").css({ "height": textHeight, "padding-bottom": "1em" });
    });

    // Startpage gallery
    var startpagegallery = $('#startPageGallery');
    if (startpagegallery.length > 0) {
        startpagegallery.cycle({
            prev: '#startPageGalleryPrev',
            next: '#startPageGalleryNext'
        });

        startpagegallery.mouseover(function () { startpagegallery.cycle('pause'); });
        startpagegallery.mouseout(function () { startpagegallery.cycle('resume'); });
    }

    // Product variants
    $("#productVariants ul li h3 a").toggle(function () {
        $(this).parent().parent().addClass("expanded");
        $(this).parent().parent().find(".details").stop(true, true).slideDown();
    }, function () {
        $(this).parent().parent().find(".details").stop(true, true)
    		.slideUp("normal", function () {
    		    $(this).parent().removeClass("expanded");
    		});
    });

    // Product dropdown
    var dropDownState = 0;
    $(".dropDown > p > a").click(function () {
        if (dropDownState == 0) {
            $(this).parent().addClass("open")
				.next("ul").removeClass("accessibility").fadeIn("fast");
            dropDownState = 1;
        }
        else {
            $(this).parent().removeClass("open")
				.next("ul").fadeOut("fast");
            dropDownState = 0;
        }
        return false;
    }).mouseover(function () {
        if (dropDownState == 1) {
            $(this).parent().next("ul")
				.stop(true, true)
				.fadeIn("fast");
        }
    });
    $(".dropDown").mouseleave(function () {
        $(this).find("ul")
			.stop(true, true)
			.delay(500)
			.fadeOut(300, function () {
			    $(this).prev("p").removeClass("open");
			});
        dropDownState = 0;
    });
    $(".dropDown ul").mouseenter(function () {
        $(this)
			.stop(true, true)
			.fadeIn("fast");
        $(this).prev("p").addClass("open");
        dropDownState = 1;
    });
    if ($(".productSelectBox") && (dropDownState == 1)) {
        $("body").click(function () {
            $(".dropDown ul")
				.stop(true, true)
				.delay(500)
				.fadeOut(300, function () {
				    $(".dropDown p").removeClass("open");
				});
            dropDownState = 0;
        });
    }

    // Nav main (top) hovered effect and dropdown control
    if ($("#navMain")) {
        var drpdwn = $("#navDropdown");
        var drpdwnMaxHeight = 0;
        // Find tallest inner list
        $("#navMain .inner > ul").each(function () {
            if ($(this).height() > drpdwnMaxHeight) {
                drpdwnMaxHeight = $(this).height();
            }
        });
        $("#navMain .inner > ul:first-child").each(function () {
            $(this).css('margin-left', $(this).parent().siblings('a').position().left + 10);
        });
        // Set dropdown position and height according to tallest list
        drpdwn.css({
            width: $("#navMain > ul").width() - 7,
            height: drpdwnMaxHeight + 35,
            top: Math.round($("#navMain").height() + 1)
        });
        $("#navMain .inner").css({
            width: ($("#navMain > ul").width() - 5),
            height: drpdwnMaxHeight + 30
        }).mouseenter(function () {
            $(this)
				.stop(true, true)
				.animate({ opacity: 1 }, 0, function () {
				    $(this).removeClass("accessibility");
				    drpdwn.stop(true, true).show();
				    $(this).stop(true, true).parent().removeClass("accessibility");
				});
        }).mouseleave(function () {
            $(this)
				.stop(true, true)
				.animate({ opacity: 1 }, 500, function () {
				    $(this).addClass("accessibility");
				});
            drpdwn
				.animate({ opacity: 1 }, 500, function () {
				    $(this).hide();
				});
        });
        $("#navMain > ul > li > a").bind("mouseenter focus", function () {
            $(this).next(".inner")
				.stop(true, true)
				.animate({ opacity: 1 }, 0, function () {
				    $(this).parent().siblings().find(".inner").addClass("accessibility");
				    $(this).removeClass("accessibility");
				});
            drpdwn
				.stop(true, true)
				.animate({ opacity: 1 }, 0, function () {
				    $(this).show();
				});
        }).mouseleave(function () {
            $(this).next(".inner")
				.stop(true, true)
				.animate({ opacity: 1 }, 500, function () {
				    $(this).parent().find(".inner").addClass("accessibility");
				});
            drpdwn
				.stop(true, true)
				.animate({ opacity: 1 }, 500, function () {
				    $(this).hide();
				});
        });
    }

    // FAQ listing
    $(".faq .longAnswer").each(function () {
        var elem = $(this);
        var state = 0;
        $(this).parent().find(".viewLongAnswer a").click(function (e) {
            e.preventDefault();
            if (state == 0) {
                elem.hide()
					.removeClass("accessibility")
					.fadeIn();
                $(this).text($(this).data('hidelonganswer'));
                state = 1;
                return false;
            }
            else {
                elem.fadeOut("normal", function () {
                    elem.addClass("accessibility")
						.removeAttr("style");
                });
                $(this).text($(this).data('showlonganswer'));
                state = 0;
                return false;
            }
        });
    });

    // Product gallery
    //     $("#productGallery ul img").reflect({
    //         startOpacity: 0.2, 	// The starting opacity of the gradient for the reflection (value 0 to 1)
    //         reflectionScale: 0.2	// Size of the reflection relative to the size of the image (value 0 to 1)
    //     });

    var productGalleryItems = $("#productGallery li").length,
		currentItem = 1,
		galleryWidth = 160;

    if (productGalleryItems < 2) {
        $("#productGalleryNav").remove();
    }
    else {

        // Get previous item
        $("#productGalleryNav .prev a").click(function () {
            if (currentItem == 1) {
                currentItem = productGalleryItems,
				lastSetPos = (productGalleryItems - 1) * galleryWidth;
                $("#productGallery ul").stop(true, true).animate({
                    opacity: 0
                }, 500, function () {
                    $(this).animate({
                        left: '-=' + lastSetPos
                    }, 0, function () {
                        $(this).animate({
                            opacity: 1
                        }, 500);
                    });
                });
            }
            else {
                currentItem = currentItem - 1;
                $("#productGallery ul").stop(true, true).animate({
                    opacity: 0
                }, 500, function () {
                    $(this).animate({
                        left: '+=' + galleryWidth
                    }, 0, function () {
                        $(this).animate({
                            opacity: 1
                        }, 500);
                    });
                });
            }
            return false;
        });

        // Get next item
        $("#productGalleryNav .next a").click(function () {
            if (currentItem < productGalleryItems) {
                currentItem = currentItem + 1;
                $("#productGallery ul").stop(true, true).animate({
                    opacity: 0
                }, 500, function () {
                    $(this).animate({
                        left: '-=' + galleryWidth
                    }, 0, function () {
                        $(this).animate({
                            opacity: 1
                        }, 500);
                    });
                });
            }
            else {
                currentItem = 1;
                $("#productGallery ul").stop(true, true).animate({
                    opacity: 0
                }, 500, function () {
                    $(this).animate({
                        left: '0'
                    }, 0, function () {
                        $(this).animate({
                            opacity: 1
                        }, 500);
                    });
                });
            }
            return false;
        });
    }

    // Restricted pages
    if (restricted && $.cookies.test()) {
        var cookiename = "weifahealthpro";
        var cookie = $.cookies.get(cookiename);

        if (!cookie) {
            var shade = $("#restrictedPopup");
            var dialog = $("#restrictedPopup .restrictedProduct");

            dialog.css({
                top: Math.round($(window).height() / 2),
                left: Math.round(($(window).width() / 2) - 215)
            });
            shade.fadeIn(0);

            $('#stay').click(function (e) {
                e.preventDefault();
                // 5 minutes = (5*60*1000)
                var exptime = (new Date().getTime() + (5 * 60 * 1000));
                var now = new Date().getTime();
                $.cookies.set(cookiename, cookiename, { expiresAt: new Date(exptime) });
                shade.fadeOut();
                $("#top").focus();
            });

            $('#leave').click(function () {
                if (document.referrer.indexOf(document.domain) < 0) {
                    window.location = 'http://' + document.domain;
                }
                else {
                    window.location = document.referrer;
                }
            });
        }
    }
});

