﻿ejungle.runtime.controls.courseSearch = {};

ejungle.runtime.controls.courseSearch.filter = {
    timeOfDay: "all",
    isOnlineOnly: false,
    isCertificateProgram: false,
    daysOfWeek: ""
};

ejungle.runtime.controls.courseSearch.subjectListType = "TwoLevels";

ejungle.runtime.controls.courseSearch.updateFilter = function(data) {
    ejungle.runtime.controls.courseSearch.filter = $.extend(ejungle.runtime.controls.courseSearch.filter, data || {});
};


/* Initializes the page */
ejungle.runtime.controls.courseSearch.init = function() {
    this.initFilters();
    this.initCallOuts();
    this.initSubjectList();
    this.initProgramAndCourseList();
    $(".right-content-repository").html($(".right-content").html());

    var element = $(".course-search .subjects .subjects-list");
    element.find("> ul > li").each(function(index) {
        var onElement = $(this).find("a.on");
        if (onElement.length > 0) {
            element.accordion('activate', index);

            onElement.addClass("on").append(ejungle.settings.checkmark);
            onElement.find("img.bullet").attr("src", "/assets/images/course-search/bullet-red.gif");
            ejungle.runtime.controls.courseSearch.initProgramAndCourseList(true);
        }
    });

    element = $(".course-search .program-course-list");
    element.find("> ul > li").each(function(index) {
        var onElement = $(this).find("a.on");
        if (onElement.length > 0) {
            element.accordion('activate', index);

            onElement.addClass("on").append(ejungle.settings.checkmark);
            onElement.find("img.bullet.change").attr("src", "/assets/images/course-search/bullet-red.gif");
        }
    });
    this.initProgramCourseInfo();


    if ($(".sectionId-value").length > 0) {
        var sectionId = $(".sectionId-value").val();
        $(".program-course-info .section-table a").each(function() {
            var self = $(this);
            if (self.find(".sectionid").text() == sectionId) {
                self.addClass("on");
            }
        });
    }

};

/* Initializes the call outs */
ejungle.runtime.controls.courseSearch.initFilters = function() {
    $(".btn-all-courses").click(function() {
        $(this).parent().find("li").removeClass("bold").removeClass("on");
        $(this).addClass("bold").addClass("on");

        $(".filter-dropdown").css("display", "none");
        ejungle.runtime.controls.courseSearch.updateFilter({
            timeOfDay: "all",
            isOnlineOnly: false,
            isCertificateProgram: false,
            daysOfWeek: ""
        });

        $(".secondary-nav span.title").text($(this).find("span").text());

        ejungle.runtime.ajax.loadSubjects(ejungle.runtime.controls.courseSearch.loadSubjectList);
        return false;
    });

    $(".btn-online-courses").click(function() {
        $(this).parent().find("li").removeClass("bold").removeClass("on");
        $(this).addClass("bold").addClass("on");

        $(".filter-dropdown").css("display", "none");
        ejungle.runtime.controls.courseSearch.updateFilter({
            timeOfDay: "all",
            isOnlineOnly: true,
            isCertificateProgram: false,
            daysOfWeek: ""
        });

        $(".secondary-nav span.title").text($(this).find("span").text());

        ejungle.runtime.ajax.loadSubjects(ejungle.runtime.controls.courseSearch.loadSubjectList);
        return false;
    });

    $(".btn-certificate-programs").click(function() {
        $(this).parent().find("li").removeClass("bold").removeClass("on");
        $(this).addClass("bold").addClass("on");

        $(".filter-dropdown").css("display", "none");
        ejungle.runtime.controls.courseSearch.updateFilter({
            timeOfDay: "all",
            isOnlineOnly: false,
            isCertificateProgram: true,
            daysOfWeek: ""
        });

        $(".secondary-nav span.title").text($(this).find("span").text());

        ejungle.runtime.ajax.loadSubjects(ejungle.runtime.controls.courseSearch.loadSubjectList);
        return false;
    });

    $(".btn-evening-courses").click(function() {
        $(this).parent().find("li").removeClass("bold").removeClass("on");
        $(this).addClass("bold").addClass("on");

        $(".filter-dropdown").css("display", "none");
        ejungle.runtime.controls.courseSearch.updateFilter({
            timeOfDay: "evening",
            isOnlineOnly: false,
            isCertificateProgram: false,
            daysOfWeek: ""
        });

        $(".secondary-nav span.title").text($(this).find("span").text());

        ejungle.runtime.ajax.loadSubjects(ejungle.runtime.controls.courseSearch.loadSubjectList);
        return false;
    });

    $(".btn-weekend-courses").click(function() {
        $(this).parent().find("li").removeClass("bold").removeClass("on");
        $(this).addClass("bold").addClass("on");

        $(".filter-dropdown").css("display", "none");
        ejungle.runtime.controls.courseSearch.updateFilter({
            timeOfDay: "all",
            isOnlineOnly: false,
            isCertificateProgram: false,
            daysOfWeek: "Saturday|Sunday"
        });

        $(".secondary-nav span.title").text($(this).find("span").text());

        ejungle.runtime.ajax.loadSubjects(ejungle.runtime.controls.courseSearch.loadSubjectList);
        return false;
    });

    $(".btn-advanced-search").click(function() {
        $(this).parent().find("li").removeClass("bold").removeClass("on");
        $(this).addClass("bold").addClass("on");

        $(".filter-dropdown").css("display", "block");

        $(".secondary-nav span.title").text($(this).find("span").text());

        //        document.onclick = function() {
        //            alert("here");
        //            $(".filter-dropdown").css("display", "none");
        //        };


        return false;
    });

//    $("body").click(function() {
//        //alert("close");
//        $(".filter-dropdown").css("display", "none");
//    });

//    $(".filter-dropdown, .filter-dropdown *,.btn-advanced-search, .btn-advanced-search *").click(function() {
//        $(".filter-dropdown").css("display", "block");
//    });

    var filterdropdown = $(".filter-dropdown");

    filterdropdown.find(".instruction-format").click(function() {
        if ($(this).attr("value") == "online") {
            filterdropdown.find(".days[value='']").attr('checked', true);
            filterdropdown.find(".days[value!='']").attr('disabled', true).attr('checked', false);
        }
        else {
            filterdropdown.find(".days[value!='']").removeAttr('disabled');
        }
    });

    filterdropdown.find(".days").click(function() {
        var self = $(this);

        if (self.attr("value") == "") {
            if (self.is(':checked')) {
                filterdropdown.find(".days[value!='']").attr('checked', false);
            }
        }
        else {
            filterdropdown.find(".days[value='']").attr('checked', false);
        }

        if (filterdropdown.find(".days:checked").length == 0) {
            self.attr('checked', true);
        }
    });



    filterdropdown.find(".btn-advanced-search-submit").click(function() {
        var timeOfDay = filterdropdown.find(".time-of-day:checked").val();
        var isCertificateProgram = filterdropdown.find(".education-format:checked").val() == "certificate";
        var isOnlineOnly = filterdropdown.find(".instruction-format:checked").val() == "online";

        var daysOfWeek = "";
        filterdropdown.find(".days:checked").each(function() {
            daysOfWeek += "|" + $(this).val();
        });
        daysOfWeek = daysOfWeek.substring(1);


        ejungle.runtime.controls.courseSearch.updateFilter({
            timeOfDay: timeOfDay,
            isOnlineOnly: isOnlineOnly,
            isCertificateProgram: isCertificateProgram,
            daysOfWeek: daysOfWeek
        });

        ejungle.runtime.ajax.loadSubjects(ejungle.runtime.controls.courseSearch.loadSubjectList);

        filterdropdown.css("display", "none");

        return false;
    });
};


/* Initializes the call outs */
ejungle.runtime.controls.courseSearch.loadSubjectList = function(msg) {
    $(".course-search .subjects .subjects-list").html(msg.d)
    ejungle.runtime.controls.courseSearch.initSubjectList();
    $(".right-content").html($(".right-content-repository").html());
    ejungle.runtime.controls.courseSearch.initCallOuts();

    // Fix the height
    ejungle.runtime.controls.courseSearch.setHeight();
};

/* Initializes the call outs */
ejungle.runtime.controls.courseSearch.initCallOuts = function() {
    $(".call-out-list li").each(function() {
        var self = $(this);

        self.hover(function() {
            var color = $(".course-search").attr("ejColor") || "red";

            $(this).find(".border-default").attr("src", "/assets/images/course-search/border-call-out-" + color + ".png");
        }, function() {
            $(this).find(".border-default").attr("src", "/assets/images/course-search/border-call-out-default.png");
        });

        self.click(function() {
            var url = $(this).find(".link a").attr("href");
            document.location = url;
        });
    });
};

/* Loads the subjects */
ejungle.runtime.controls.courseSearch.initSubjectList = function() {
    var self = ejungle.runtime.controls.courseSearch;
    var element = $(".course-search .subjects .subjects-list");
    //self.fixIEHeight($(".subjects-list"));


    var levelOnes = element.find("ul.level-0 > li").addClass("level-one");
    var levelTwoUl = levelOnes.find("ul");
    var levelTwos = levelTwoUl.find("> li").addClass("level-two");

    var openers = levelTwos;

    if (ejungle.runtime.controls.courseSearch.subjectListType == "OneLevel") {
        openers = levelOnes;
    } else {
        levelTwoUl.each(function() {
            if ($(this).find("a.on").length == 0) {
                $(this).hide();
            } else {
                $(this).parent().addClass("open");
            }
        });

        levelOnes.find("> a").click(function() {
            var li = $(this).parent();
            var ul = li.find("ul");

            if (!li.hasClass("open")) {
                levelTwoUl.slideUp(500);
                levelOnes.removeClass("open");
                li.addClass("open");
                ul.slideDown(500, ejungle.runtime.controls.courseSearch.setHeight);
            }

            return false;
        });
    }



    // For all items that do not have children attach AJAX event
    openers.find("a").click(function() {
        ejungle.runtime.ajax.loadProgramsAndCourses($(this).metadata().id, function(msg) {
            $(".course-search .body .right-content").html(msg.d);
            self.initProgramAndCourseList();
            scroll(0, 0);
        });

        // Add the checkmark
        $(".subjects ul a").removeClass("on").find("img.checkmark").remove().end().find("img.bullet.change").attr("src", "/assets/images/course-search/bullet-black.gif");
        $(this).addClass("on").append(ejungle.settings.checkmark);
        $(this).find("img.bullet.change").attr("src", "/assets/images/course-search/bullet-red.gif");
        return false;
    });
};

ejungle.runtime.controls.courseSearch.fixIEHeight = function(element) {
    if ($.browser.msie) {
        var liHeight = 39;

        element.find("ul.level-0 li").css("height", jQuery.PxToEm(liHeight));
        var listItems = element.find("ul.level-0 li.selected ul.level-1 li");
        var ulHeight = liHeight * (listItems.length + 1);
        element.find("ul.level-0 li.selected").css("height", jQuery.PxToEm(ulHeight));
    }
};


/* Loads courses and programs */
ejungle.runtime.controls.courseSearch.initProgramAndCourseList = function(doNotLoadCourseInfo) {
    doNotLoadCourseInfo = doNotLoadCourseInfo || false;

    var self = ejungle.runtime.controls.courseSearch;
    var element = $(".course-search .program-course-list");
    //self.fixIEHeight($(".program-course-list"));

//    element.find("ul.level-0 > li > a").addClass("ryan");
//    var animated = $.browser.msie ? false : "slide";

//    element.accordion({
//        autoHeight: false,
//        alwaysOpen: false,
//        active: false,
//        animated: animated,
//        event: "click",
//        header: "a.ryan",
//        onClick: function(element) {
//            $(".program-course-list a").removeClass("on").find("img.checkmark").remove().end().find("img.bg").remove().end().find("img.bullet").attr("src", "/assets/images/course-search/bullet-black.gif");
//            $(element).addClass("on").append("<img src='/assets/images/course-search/bg-left-nav-level-0.jpg' class='bg' alt='' />");

//            if (!doNotLoadCourseInfo) {
//                self.loadProgramCourseInfo(element);
//            }
//        },
//        onComplete: function() {
//            //self.fixIEHeight($(".program-course-list"));
//        }
//    });


    $(".program-course-list ul a.on").append(ejungle.settings.checkmark).find("img.bullet").attr("src", "/assets/images/course-search/bullet-red.gif");


    // For all items that do not have children attach AJAX event
    element.find("ul a").click(function() {
        var url = new String(document.location);
        if (url.indexOf("?") >= 0) {
            url = url.substring(0, url.indexOf("?"));
        }

        ejungle.currentUrl = url + $(this).attr("href");
        ejungle.runtime.controls.courseSearch.loadProgramCourseInfo(this);

        // Add the checkmark
        $(".program-course-list ul a").removeClass("on").find("img.checkmark").remove().end().find("img.bullet.change").attr("src", "/assets/images/course-search/bullet-black.gif");
        $(this).addClass("on").append(ejungle.settings.checkmark);
        $(this).find("img.bullet.change").attr("src", "/assets/images/course-search/bullet-red.gif");
        scroll(0, 0);
        return false;
    });
    // Fix the height
    ejungle.runtime.controls.courseSearch.setHeight();

};

/* Loads course/program information */
ejungle.runtime.controls.courseSearch.loadProgramCourseInfo = function(element) {
    var self = this;
    var metadata = $(element).metadata();

    ejungle.runtime.ajax.loadProgramCourseInfo(metadata.id, metadata.type, function(msg) {
        $(".program-course-info").removeClass("program-course-info-empty").html(msg.d);
        ejungle.runtime.controls.courseSearch.initProgramCourseInfo();
    });
};

/* Inits course/program information */
ejungle.runtime.controls.courseSearch.initProgramCourseInfo = function() {
    var self = this;
    $(".program-course-info .section-table a").click(function() {
        $(".program-course-info .section-table a").removeClass("on");

        if ($(".program-course-info .section-table a").length > 1) {
            $(this).addClass("on");
        }
        var sectionId = $(this).find(".sectionid").text();
        self.loadSectionInfo(sectionId, false);
        return false;
    });
    ejungle.runtime.controls.courseSearch.initSectionInfo();
    // Fix the height
    ejungle.runtime.controls.courseSearch.setHeight();
};

ejungle.runtime.controls.courseSearch.loadSectionInfo = function(sectionId, isBeingApproved) {
    var self = ejungle.runtime.controls.courseSearch;

    ejungle.runtime.ajax.loadSectionInfo(sectionId, isBeingApproved, function(msg) {
        // Display the section data
        $(".section-info-div").html(msg.d);

        self.initSectionInfo();
    });
};

ejungle.runtime.controls.courseSearch.initSectionInfo = function() {
    var self = ejungle.runtime.controls.courseSearch;

    $(".section-info-div .btn-enroll").hover(function() {
        $(this).find("img").attr("src", "/assets/images/course-search/btn-on-enroll_now.gif");
    }, function() {
        $(this).find("img").attr("src", "/assets/images/course-search/btn-off-enroll_now.gif");
    });

    $(".base-fee-table input:radio:first").attr("checked", true);
    $(".optional-fee-table input:radio:first").attr("checked", true);

    $(".instructor-anchor").click(function() {
        var $this = $(this);
        $(".popup1 h2").text($this.find("span").text());
        $(".popup1 p").html($this.find("p").text());

        ejungle.functions.dialog($(".popup1"), "popup1");
        return false;
    });

    $(".discount-code-submit").unbind("click");
    $(".discount-code-submit").click(function() {
        $(".discount-code-error").hide();
        $(".discount-fee").remove();
        var sectionId = $(".sectionId-value").val();
        var code = $("#dicount-code-input").val();
        ejungle.runtime.ajax.validateDiscountCode(sectionId, code, function(msg) {
            var result = msg.d;

            var dest = $(".fees-table tbody").length > 0 ? $(".fees-table tbody") : $(".fees-table");

            if (result.length > 0) {
                dest.append(result);
                $(".discount-code-input").hide();
                $(".discount-fee input:radio:first").attr("checked", true);
            }
            else {
                $(".discount-code-error").show();
            }
        });

        return false;
    });

    $(".approval-code-submit").unbind("click");
    $(".approval-code-submit").click(function() {
        $(".approval-code-error").hide();
        $(".section-number").hide();

        var sectionId = $(".sectionId-value").val();
        var code = $("#approval-code-textbox").val();
        var btn = $(".enroll-btn-row");
        ejungle.runtime.ajax.validateApprovalCode(sectionId, code, function(msg) {
            var result = msg.d;
            if (msg.d == "true") {
                btn.show();
                $(".section-number").show();
                $(".approval-code-input").hide();
            } else {
            $(".approval-code-error").show();
                btn.hide();
            }
        });

        return false;
    });

    // Adjust the height to ensure that the page looks correct
    ejungle.runtime.controls.courseSearch.setHeight();
};

/* Ensures that all of the columns are the same height */
ejungle.runtime.controls.courseSearch.setHeight = function() {
    var courseSearch = $(".course-search");
    var subjectList = courseSearch.find(".subjects");
    var rightColumn = courseSearch.find(".right-content");
    var programCourseList = courseSearch.find(".program-course-list");
    var programCourseInfo = courseSearch.find(".program-course-info");
    var programCourseInfoEmpty = courseSearch.find(".program-course-info-empty");
    var body = courseSearch.find(".body");
    var rightContent = body.find(".right-content");
    var mainImage = body.find(".main-image");
    var rightContentColumns = rightContent.find(".columns");
    var rightColumn = rightContentColumns.find(".right-column");

    var columnTitleHeight = 55;

    var maxHeight = 520;

    rightContent.css({ height: "auto" });
    subjectList.css({ height: "auto" });
    programCourseList.css({ height: "auto" });
    rightColumn.css({ height: "auto" });
    programCourseInfo.css({ height: "auto" });
    programCourseInfoEmpty.css({ height: "auto" });

    maxHeight = Math.max(maxHeight, rightContent.height());
    maxHeight = Math.max(maxHeight, $(".course-search .subjects .subjects-list > ul").height() + columnTitleHeight);
    maxHeight = Math.max(maxHeight, $(".program-course-list > ul").height() + columnTitleHeight);
    maxHeight = Math.max(maxHeight, ($(".program-course-info .description").outerHeight() || 0) + ($(".program-course-info .title").outerHeight() || 0) + 20);

    maxHeight = maxHeight + 30;

    //console.log("body.height(): " + body.height());
    body.css({ height: $.PxToEm(maxHeight) });


    subjectList.css({ height: $.PxToEm(body.height()) });
    subjectList.find("ul").css({ borderBottom: (subjectList.height() < body.height() ? "solid 0.1em #8a8a8a" : "none") });

    programCourseList.css({ height: $.PxToEm(body.height()) });
    programCourseList.find("ul").css({ borderBottom: (subjectList.height() < body.height() ? "solid 0.1em #8a8a8a" : "none") });

    rightContentColumns.css({ height: $.PxToEm(body.height() - mainImage.height()) });

    rightColumn.css({ height: $.PxToEm(rightContentColumns.height()) });
    rightColumn.find("ul.level-1").css({ borderBottom: (rightColumn.height() < rightContent.height() ? "solid 0.1em #c1c0c2" : "none") });
    //console.log("body.height(): " + body.height());


    programCourseInfo.css({ height: $.PxToEm(body.height()) });
    programCourseInfoEmpty.css({ height: $.PxToEm(body.height()) });
};
