﻿/// <reference path="jquery-1.4.2.min.js" />

(function ($) {

    $.fn.FAQSlider = function (options) {

        //        debug(this);

        var opts = $.extend({}, $.fn.FAQSlider.defaults, options);

        return this.each(function () {

            $(opts.boxes).each(function () {
                $(this).hide();
            });

            var $this = $(this);

            var openedItems;

            var container = $(".LevelBox");
            var initialHeight = 20;
            var additionalHeight = 20;



            $(opts.slider, $this).click(function () {
                // debugger;

                if ($("#hind").hasClass("hindVisible")) {
                    $("#hind").removeClass("hindVisible").addClass("hindInvisible");
                }
                var level = $(this).attr("level");
                var firstLevelActive = $(".arrowWhite");
                var secondLevelActive = $(".arrowBlueBottomFAQ");
                var thirdLevelActive = $(".arrowBlackBottomFAQ");

                if (level == "1") {

                    $(this).toggleClass("arrowBlueFAQ");
                    $(this).toggleClass("arrowWhite");
                    $(this).parent().toggleClass("no_bg");
                    $(this).parent().toggleClass("bluebg");
                    var id = $(this).attr("id");
                    $(".container_" + id).slideToggle(500);

                    $("a.LevelBox", $(".container_" + id).children("div.LevelBox")).each(function () {
                        var actualHeight = $(this).height();
                        if (actualHeight > initialHeight && actualHeight < initialHeight + additionalHeight) {
                            $(this).height(initialHeight + additionalHeight);
                            $(this).children(".LevelBoxText").css("margin-top", "10px");
                            $(this).css("background-position", "left 11px");

                        }

                        else if (actualHeight < initialHeight) {
                            $(this).height(initialHeight);
                        }
                    });

                    thirdLevelActive.next(".box").slideToggle(500);
                    thirdLevelActive.toggleClass("arrowBlackBottomFAQ");
                    thirdLevelActive.toggleClass("arrowBlueFAQ_SubcategoriesThird");

                    secondLevelActive.parent().next(".subItemsInvisible").slideToggle(500);
                    if (secondLevelActive.parent().next().hasClass("blueborder")) {
                        secondLevelActive.parent().next(".blueborder").toggleClass("ContainerVisible");
                        secondLevelActive.parent().next(".blueborder").toggleClass("ContainerInvisible");
                        secondLevelActive.parent().next(".blueborder").next(".subItemsInvisible").slideToggle(500);
                    }
                    secondLevelActive.toggleClass("arrowBlueBottomFAQ");
                    secondLevelActive.toggleClass("arrowBlueFAQ_Subcategories");

                    var activeId = firstLevelActive.not($(this)).attr("id");
                    $(".container_" + activeId).slideToggle(500);
                    firstLevelActive.not($(this)).toggleClass("arrowWhite");
                    firstLevelActive.not($(this)).toggleClass("arrowBlueFAQ");
                    firstLevelActive.not($(this)).parent().toggleClass("bluebg");
                    firstLevelActive.not($(this)).parent().toggleClass("no_bg");

                    Cufon.refresh();
                }
                else if (level == "2") {

                    var backgroundPosition = $(this).css("background-position");
                    var marginTop = $(this).css("margin-top");

                    $(this).parent().next(".subItemsInvisible").slideToggle(500);
                    if ($(this).parent().next().hasClass("blueborder")) {
                        $(this).parent().next(".blueborder").toggleClass("ContainerVisible");
                        $(this).parent().next(".blueborder").toggleClass("ContainerInvisible");
                        $(this).parent().next(".blueborder").next(".subItemsInvisible").slideToggle(500);

                        $("a.LevelBox", $(this).parent().next(".blueborder").next(".subItemsInvisible")).each(function () {
                            var actualHeight = $(this).height();
                            if (actualHeight > initialHeight && actualHeight < initialHeight + additionalHeight) {
                                $(this).height(initialHeight + additionalHeight);

                                $(this).children(".LevelBoxText").css("margin-top", "10px");
                                $(this).css("background-position", "left 11px");

                            }


                            else if (actualHeight < initialHeight) {
                                $(this).height(initialHeight);
                            }
                        });
                    }
                    $(this).toggleClass("arrowBlueFAQ_Subcategories");
                    $(this).toggleClass("arrowBlueBottomFAQ");
                    $(this).css("background-position", backgroundPosition);
                    $(this).css("margin-top", marginTop);


                    $("a.LevelBox", $(this).parent().next(".subItemsInvisible")).each(function () {
                        var actualHeight = $(this).height();
                        if (actualHeight > initialHeight && actualHeight < initialHeight + additionalHeight) {
                            $(this).height(initialHeight + additionalHeight);

                            $(this).children(".LevelBoxText").css("margin-top", "10px");
                            $(this).css("background-position", "left 11px");

                        }


                        else if (actualHeight < initialHeight) {
                            $(this).height(initialHeight);
                        }
                    });

                    thirdLevelActive.next(".box").slideToggle(500);
                    thirdLevelActive.toggleClass("arrowBlackBottomFAQ");
                    thirdLevelActive.toggleClass("arrowBlueFAQ_SubcategoriesThird");

                    secondLevelActive.not($(this)).parent().next(".subItemsInvisible").slideToggle(500);
                    if (secondLevelActive.not($(this)).parent().next().hasClass("blueborder")) {
                        secondLevelActive.parent().next(".blueborder").toggleClass("ContainerVisible");
                        secondLevelActive.parent().next(".blueborder").toggleClass("ContainerInvisible");
                        secondLevelActive.not($(this)).parent().next(".blueborder").next(".subItemsInvisible").slideToggle(500);
                    }
                    secondLevelActive.not($(this)).toggleClass("arrowBlueBottomFAQ");
                    secondLevelActive.not($(this)).toggleClass("arrowBlueFAQ_Subcategories");

                    Cufon.refresh();

                }
                else if (level == "3") {

                    var backgroundPosition = $(this).css("background-position");
                    var marginTop = $(this).css("margin-top");
                    $(this).next(".box").slideToggle(500);

                    $(this).toggleClass("arrowBlueFAQ_SubcategoriesThird");
                    $(this).toggleClass("arrowBlackBottomFAQ");
                    $(this).css("background-position", backgroundPosition);
                    $(this).css("margin-top", marginTop);

                    thirdLevelActive.not($(this)).next(".box").slideToggle(500);
                    thirdLevelActive.not($(this)).toggleClass("arrowBlackBottomFAQ");
                    thirdLevelActive.not($(this)).toggleClass("arrowBlueFAQ_SubcategoriesThird");

                    Cufon.refresh();
                }
                //                    if ($(this).hasClass("arrowBlueFAQ")) {
                //                        var Categories = $(".firstLevel.arrowWhite");
                //                        Categories.stop().removeClass("arrowWhite").addClass("arrowBlueFAQ");
                //                        Categories.parent().stop().removeClass("bluebg").addClass("no_bg");
                //                        $(this).stop().removeClass("arrowBlueFAQ").addClass("arrowWhite");
                //                        $(this).parent().stop().removeClass("no_bg").addClass("bluebg");
                //                    }
                //                    else {
                //                        $(this).stop().removeClass("arrowWhite").addClass("arrowBlueFAQ");
                //                        $(this).parent().removeClass("bluebg").addClass("no_bg");

                //                    }
                //                    var visibleContainers = $(".ContainerVisible");
                //                    if (visibleContainers.length > 0) {
                //                        var elementIdToShow = $(this).attr("id");
                //                        visibleContainers.stop().animate({ height: "0px" }, 200, function () {
                //                            $(this).removeClass("ContainerVisible").addClass("ContainerInvisible").css("display", "none");
                //                            $(".subItemsVisible", $(this)).stop().removeClass("subItemsVisible").addClass("subItemsInvisible").removeAttr("style");
                //                            $(".arrowBlueBottomFAQ", $(this)).stop().removeClass("arrowBlueBottomFAQ").addClass("arrowBlueFAQ");
                //                            $(".answersVisible", $(this)).stop().removeClass("answersVisible").addClass("answersInvisible").removeAttr("style");
                //                            $(".arrowBlackBottomFAQ", $(this)).stop().removeClass("arrowBlackBottomFAQ").addClass("arrowBlueFAQ");

                //                            var myObj = $(".ContainerInvisible.container_" + elementIdToShow);
                //                            var myObjSubCount = $(".secondLevel", myObj).length;

                //                            $("#HeightTest").css("line-height", "1em").html(myObj.html());
                //                            myObj.stop().removeClass("ContainerInvisible").addClass("ContainerVisible").css("height", "1px").animate({ height: $("#HeightTest").height() }, 200, function () {
                //                                $(this).removeAttr("style");
                //                                Cufon.refresh();
                //                            });
                //                        });

                //                    }
                //                    else {
                //                        $(".hindVisible").stop().removeClass("hindVisible").addClass("hindInvisible");
                //                        var myObj = $(".ContainerInvisible.container_" + $(this).attr("id"));
                //                        var myObjSubCount = $(".secondLevel", myObj).length;

                //                        $("#HeightTest").css("line-height", "1em").html(myObj.html());
                //                        myObj.stop().removeClass("ContainerInvisible").addClass("ContainerVisible").css("height", "1px").animate({ height: $("#HeightTest").height() }, 200, function () {
                //                            $(this).removeAttr("style");
                //                            Cufon.refresh();
                //                        });
                //                    }
                //                    if (!$(".ContainerVisible").length > 0) {
                //                        $(".hindInvisible").stop().removeClass("hindInvisible").addClass("hindVisible");
                //                    }

                //                }
                //                else if (level == "2") {
                //                    if ($(this).hasClass("arrowBlueFAQ")) {
                //                        var openCategories = $(".secondLevel.arrowBlueBottomFAQ");
                //                        openCategories.stop().removeClass("arrowBlueBottomFAQ").addClass("arrowBlueFAQ");
                //                        $(this).stop().removeClass("arrowBlueFAQ").addClass("arrowBlueBottomFAQ");
                //                    }
                //                    else {
                //                        $(this).stop().removeClass("arrowBlueBottomFAQ").addClass("arrowBlueFAQ");
                //                    }
                //                    //debugger;
                //                    var visibleSubItems = $(".subItemsVisible");
                //                    var elementIdToShow = $(this).attr("id");
                //                    if (visibleSubItems.length > 0) {

                //                        visibleSubItems.stop().animate({ height: "0px" }, 200, function () {
                //                            $(this).removeClass("subItemsVisible").addClass("subItemsInvisible").css("display", "none");
                //                            $(".answersVisible", $(this)).stop().removeClass("answersVisible").addClass("answersInvisible").removeAttr("style");
                //                            $(".arrowBlackBottomFAQ", $(this)).stop().removeClass("arrowBlackBottomFAQ").addClass("arrowBlueFAQ");

                //                            var myObj = $(".subItemsInvisible.box_" + elementIdToShow);
                //                            var myObjSubCount = $(".thirdLevel", myObj).length;

                //                            $("#HeightTest").css("line-height", "1em").html(myObj.html());
                //                            myObj.stop().removeClass("subItemsInvisible").addClass("subItemsVisible").css("height", "1px").animate({ height: $("#HeightTest").height() }, 200);
                //                            Cufon.refresh();
                //                        });
                //                    }
                //                    else {
                //                        var myObj = $(".subItemsInvisible.box_" + elementIdToShow);
                //                        var myObjSubCount = $(".thirdLevel", myObj).length;

                //                        $("#HeightTest").css("line-height", "1em").html(myObj.html());
                //                        myObj.stop().removeClass("subItemsInvisible").addClass("subItemsVisible").css("height", "1px").animate({ height: $("#HeightTest").height() }, 200);
                //                        Cufon.refresh();
                //                    }

                //                }

                //                else if (level == "3") {
                //                    //debugger;
                //                    var visibleAnswers = $(".answersVisible");
                //                    var subItem = $(this).parents(".subItemsVisible");
                //                    if ($(this).hasClass("arrowBlueFAQ")) {
                //                        var openAnswers = $(".arrowBlackBottomFAQ");
                //                        openAnswers.stop().removeClass("arrowBlackBottomFAQ").addClass("arrowBlueFAQ");
                //                        $(this).stop().removeClass("arrowBlueFAQ").addClass("arrowBlackBottomFAQ");
                //                    }
                //                    else {
                //                        $(this).stop().removeClass("arrowBlackBottomFAQ").addClass("arrowBlueFAQ");
                //                    }
                //                    var myObj = $(this).next(".box");
                //                    if (visibleAnswers.length > 0) {

                //                        visibleAnswers.stop().animate({ heigth: "0px" }, 200, function () {
                //                            $(this).removeClass("answersVisible").addClass("answersInvisible").css("display", "none");

                //                            subItem.removeAttr("style");
                //                            $("#HeightTest").css("line-height", "1.5em").css("width", "340px").html(myObj.html());
                //                            myObj.stop().removeClass("answersInvisible").addClass("answersVisible").css("height", "1px").animate({ height: $("#HeightTest").height() }, 200);
                //                            Cufon.refresh();
                //                        });
                //                    }
                //                    else {

                //                        subItem.removeAttr("style");
                //                        $("#HeightTest").css("line-height", "1.5em").css("width", "340px").html(myObj.html());
                //                        myObj.stop().removeClass("answersInvisible").addClass("answersVisible").css("height", "1px").animate({ height: $("#HeightTest").height() }, 200);
                //                        Cufon.refresh();

                //                    }

                //                }


            });

            //            markup = $.fn.hilight.format(markup);

        });

    };

    function debug($obj) {
        if (window.console && window.console.log)
            window.console.log($obj);
    };

    //    $.fn.pager.format = function (txt) {
    //        return '<strong>' + txt + '</strong>';
    //    };

    $.fn.FAQSlider.defaults = {
        boxes: '.box',
        slider: '.sliderItemFAQ',
        toggleClass: 'arrowBlue',
        toggleClassActiveThird: 'arrowBlackBottom',
        toggleClassActiveSecond: 'arrowBlueBottom',
        toggleClassActiveFirst: 'arrowWhite',
        speed: 1000
    };

})(jQuery);
