/* ---------------------------------- */
/*  WITT - In Page JS Code  		  */
/*  Ver          : 1.8                */
/*  Created      : 21 October 2011    */
/*  Last Updated : 9 January 2012     */
/* ---------------------------------- */

/*------------------*/
/* Table of Content */
/*------------------*/

/*
1. Global Variables

2. Functions
    
3. Document Ready Functions
    3.1. Activate JS Elements	    
    3.2. Top Nav CSS Adjustment
    3.3. Study Pathway Hover Code
	3.4. Arron's Find a course Code
	3.5. Home Anything Slider Trigger

4. Window Load Functions 
    4.1. Top Nav Init
    4.2. Top Nav Hover State
    4.3. Top Nav Sub Hover State
    4.4. Find Course App Slide Down
    
*/

/*---------------------*/
/* 1. Global Variables */
/*---------------------*/

var timer;

/*--------------*/		
/* 2. Functions */
/*--------------*/

/*-----------------------------*/		
/* 3. Document Ready Functions */
/*-----------------------------*/

$(document).ready(function () {
	
    $(".youtube-video").fancybox();

    /* 3.1. Activate JS Elements */
    $("body").addClass("js");

    /* 3.2. Top Nav CSS Adjustment */

    $("div.top-nav-area>ul li:first-child").children().addClass("first");
    $("div.top-nav-area>ul li:last-child").
    children().
    addClass("last").
    children().
    children().
    append("<span class='top-nav-padding'></span>");

    /* 3.3. Study Pathway Hover Code */

    $("ul.subject-list>li a").hover(function () {
        $a = $("." + $(this).attr("class"));
        $a.css("color", "#000");
        $a.css("background-color", "#fff");
    },
    function () {
        $a = $("." + $(this).attr("class"));
        $a.css("color", "");
        $a.css("background-color", "");
    });

    /* 3.4. Arron's Find a course Code */

    $(".result").hide();
    $("#loadingimage").hide();
    $("#coursecount").hide();

    $(".checkbox").click(function () {
        $(".result").show();
        $("#loadingimage").show();
        var departmentids = "",
			levelids = "",
			semesterids = "";
        $('.checkbox').each(function () {
            if (this.checked) {
                if ($(this).hasClass("department_checkbox")) {
                    departmentids += $(this).attr("data-value") + "#";
                }
                else if ($(this).hasClass("level_checkbox")) {
                    levelids += $(this).attr("data-value") + "#";
                }
                else if ($(this).hasClass("semester_checkbox")) {
                    semesterids += $(this).attr("data-value") + "#";
                }
            }
        });
        $.ajax(
            {
                url: 'DataProvider.asmx/GetCourseCount',
                type: "POST",
                data: "{departmentids: '" + departmentids + "',levelids: '" + levelids + "' ,semesterids: '" + semesterids + "' }",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.d == "1") {
                        $("#coursecount").html("1 course matches your search").show();
                    }
                    else {
                        $("#coursecount").html(data.d + " courses match your search").show();
                    }
                    $("#loadingimage").hide();
                }
            });
    });

    $("#search-courses").click(function () {
        var departmentids = "",
			levelids = "",
			semesterids = "";
        $('.checkbox').each(function () {
            if (this.checked) {
                if ($(this).hasClass("department_checkbox")) {
                    departmentids += $(this).attr("data-value") + "#";
                }
                else if ($(this).hasClass("level_checkbox")) {
                    levelids += $(this).attr("data-value") + "#";
                }
                else if ($(this).hasClass("semester_checkbox")) {
                    semesterids += $(this).attr("data-value") + "#";
                }
            }
        });
        $.ajax(
            {
                url: 'DataProvider.asmx/SetCourseSearchParameters',
                type: "POST",
                data: "{departmentids: '" + departmentids + "',levelids: '" + levelids + "' ,semesterids: '" + semesterids + "' }",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (window.location.href.indexOf("CourseSearch") >= 0 && navigator.appName == 'Microsoft Internet Explorer') {
                        // url contained CourseSearch
                        window.location = "http://www.witt.ac.nz/CourseSearch/";
                    } else {
                        // url did not..
                        window.location = "http://www.witt.ac.nz/CourseSearch/";
                    }
                }
            });
    });
    /* 4.1. Top Nav Init & Adjustment */

    var $topNavArea = $("div.top-nav-area"),
        $courseListArea = $("div.course-list"),
        defaultTopNavHeight = $topNavArea.height();

    // 4.2. Top Nav Hover State
    $("div.top-nav-area>ul>li").hover(function () {

        var $e = $(this),
            $currDdContent = $e.find("div.dd-content"),
            elementHeight = $currDdContent.outerHeight(),
            heightAdjustment = defaultTopNavHeight + elementHeight;

        // Close any open drop down content
        $("div.dd-content").css("display", "none");

        // Truncate Current Animation
        $topNavArea.stop(false, false);

        // Adjust the Height of div.top-nav-area
        if ($(this).find("a").hasClass('no-children').length > 0) {
            heightAdjustment = 0;
        }
        // dont show menu if it doesnt have any options
        if ((heightAdjustment == 55) || (heightAdjustment == 68)) {
            heightAdjustment = 35;
        }

        if ($e.find("input").val() == "29|0|0") {
            // Display the selected drop down content
            $currDdContent.css("display", "block");
        } else {
            heightAdjustment = 35;
        }

        $topNavArea.animate({
            height: heightAdjustment + "px"
        }, 400);

    },
        function () {

            // Truncate Current Animation
            $topNavArea.stop(false, true);

            // Adjust the Height of div.top-nav-area
            $topNavArea.animate({
                height: defaultTopNavHeight + "px"
            }, 400, function () {
                // Display the selected drop down content
                $("div.dd-content").css("display", "none");
            });
        });

    // 4.3. Top Nav Sub Hover State
    $("div.col-major>ul>li").hover(function () {

        var $e = $(this),
            majorColHeight = $e.parent().parent().height(),
            courseColHeight = $e.find("div.course-list").height(),
            tallestCol = 0,
            courseColHeightMod = 0;

        // Set the height settings to the col
        if (majorColHeight > courseColHeight) {
            tallestCol = majorColHeight;
        }
        else {
            tallestCol = courseColHeight;
        }

        courseColHeightMod = tallestCol + 10;

        $e.parent().parent().css("height", tallestCol + "px");
        $e.find("div.col-1").css("height", courseColHeightMod + "px");
        $e.find("div.col-2").css("height", courseColHeightMod + "px");


        // Hide any displayed the course list - if any
        $courseListArea.css("display", "none");

        // Truncate Current Animation
        $courseListArea.stop(false, true);

        // Display the course list
        $e.find("div.course-list").fadeIn();

    },
    function () {

        var $e = $(this);

        // Truncate Current Animation
        $courseListArea.stop(false, true);

        // Hide the course list		
        $e.find("div.course-list").fadeOut();

        // Set the height settings to the col
        $e.parent().parent().css("height", "auto");
        $e.find("div.col-1").css("height", "auto");
        $e.find("div.col-2").css("height", "auto");
    });

});

/* 3.5. Home Anything Slider Trigger */

$(function () {				
	if ($("div.home-slider-area").length) {
		$('#home-slider').anythingSlider({						
			autoPlay			: true,
			resumeOnVisible     : false,
			resumeOnVideoEnd    : true,						
			autoPlayLocked      : true,
			delay				: 7000,
			resumeDelay         : 7000
		});
	}
});

/*--------------------------*/		
/* 4. Window Load Functions */
/*--------------------------*/

$(window).load(function () {

    // 4.4. Find Course App Slide Down

    var $findCourseApp = $("div.find-course-app");

    $findCourseApp.addClass("hidden");

    $("div.find-course-btn>a").live("click", function () {

        var $e = $(this);

        if ($findCourseApp.hasClass("hidden") == true) {
            $findCourseApp.removeClass("hidden").addClass("active").slideDown();

            $e.addClass("btn-right-dd-arrow-up");
        }
        else {
            $findCourseApp.removeClass("active").addClass("hidden").slideUp();

            $e.removeClass("btn-right-dd-arrow-up");
        }
    });

});
