﻿$(document).ready(function() {


    var iTotalNumberOfJobs = 0;

    $("#ctl00_ContentPlaceHolder1_applyOnlineSearchButton").click(keyWordSearchClick);
    $("#ctl00_ContentPlaceHolder1_applyOnlineSearchButton").css("cursor", "pointer");



    //set the click event when the enter key is pressed
    $("#search-field").keyup(function(e) {
        if (e.which == "13") {
            // update now
            keyWordSearchClick();
            // Don't post back
            return (false);
        } else {
            //do nothing
        }
    });

    var iPreSetPageIndex = $.query.get('preSetPageIndex');
    if (iPreSetPageIndex == "") {
        iPreSetPageIndex = 0;
    }

    var oSelectedCategory = $("*[id$='DdlCategory'] option:selected");
    var iCategoryID = oSelectedCategory.val();

    var oSelectedDepartment;
    var iTaleoDepartmentID;


    //show or hide departments

    if (iCategoryID == 1) {
        $("#departments").show();
        $("#internshipDepartments").hide();
        oSelectedDepartment = $("*[id$='DdlDepartments'] option:selected");
        iTaleoDepartmentID = oSelectedDepartment.val();
    }
    else if (iCategoryID == 5) {
        $("#internshipDepartments").show();
        $("#departments").hide();
        oSelectedInternshipDepartment = $("*[id$='DdlInternshipDepartments'] option:selected");
        iTaleoDepartmentID = oSelectedInternshipDepartment.val();

    }
    else {
        oSelectedDepartment = $("*[id$='DdlDepartments'] option:selected");
        iTaleoDepartmentID = oSelectedDepartment.val();
        $("#departments").hide();
        $("#internshipDepartments").hide();
    }


    var oSelectedLocation = $("*[id$='DdlLocations'] option:selected");
    var iLocationID = oSelectedLocation.val();

    //hide this until the user does a keyword search. this is the div that says what the user search for
    $("#search-specs").hide();

    //this is the method that gets everything going. default search is a filtered search
    getTotalNumberOfJobs(iCategoryID, iTaleoDepartmentID, iLocationID);

    //this gets the total number of jobs when doing a filtered search result
    function getTotalNumberOfJobs(iCategoryID, iTaleoDepartmentID, iLocationID) {

        //hide and show by divs, if we are in this method then the search type was by filter. so...
        $("#TryKeyword").show();
        $("#TryFilter").hide();
        $("#search-specs").hide();
        $("#SearchResultCount").show();
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetTotalNumberOfJobs",
            data: "{'iCategoryID':" + iCategoryID + ", 'iTaleoDepartmentID':" + iTaleoDepartmentID + ", 'iLocationID':" + iLocationID + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: displayPagination
        });
    }

    function displayPagination(data) {

        iTotalNumberOfJobs = data.d;
        //only display pagination if needed
        if (data.d > 10) {
            $("#job-pages").css("display", "inline");
            $("#Jobs-Pagination").pagination(data.d, {
                items_per_page: 10,
                num_display_entries: 10,
                next_text: "",
                prev_text: "",
                current_page: iPreSetPageIndex,
                callback: displayJobListings
            });
        }
        else {

            $("#job-pages").css("display", "none");
        }
        //once the pagination is loaded, we can display the job listings
        displayJobListings(iPreSetPageIndex);
    }

    //this displays the filtered search results, not the keyword
    function displayJobListings(iCurrentPage, jq) {
        var iResultsPerPage = 10;

        if (iTotalNumberOfJobs < 10) {
            $('#SearchResultCount').html("Showing jobs <span class='bold'>" + ((iCurrentPage * 10) + 1) + "</span>-<span class='bold'>" + iTotalNumberOfJobs + "</span> of <span class='bold'>" + iTotalNumberOfJobs + "</span>");
        }
        else {
            var intCurrentRangeMax = ((iCurrentPage * 10) + 10);
            if (intCurrentRangeMax > iTotalNumberOfJobs) {
                intCurrentRangeMax = iTotalNumberOfJobs;
            }
            $('#SearchResultCount').html("Showing jobs <span class='bold'>" + ((iCurrentPage * 10) + 1) + "</span>-<span class='bold'>" + intCurrentRangeMax + "</span> of <span class='bold'>" + iTotalNumberOfJobs + "</span>");
        }

        $.ajax({
            type: "POST",
            url: "Default.aspx/GetFilteredJobResults",
            data: "{'iCategoryID':" + iCategoryID + ", 'iTaleoDepartmentID':" + iTaleoDepartmentID + ", 'iLocationID':" + iLocationID + ", 'iCurrentPageIndex':" + iCurrentPage + ", 'iResultsPerPage':" + iResultsPerPage + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                // Replace the div's content with the page method's return.
                $("#ResultListing").hide();
                $("#ResultListing").html(msg.d);
                $("#ResultListing").fadeIn("normal");
            }
        });
    }

    //this handles the onchange event of the category dropdown
    $("*[id$='DdlCategory']").change(function() {
        $("#search-field").val("");
        var oSelectedItem = $("*[id$='DdlCategory'] option:selected");
        //get the categoryID
        iCategoryID = oSelectedItem.val();
        //determine if the category is Corporate in order to hide or showe the department drop down
        if (iCategoryID == 1) {
            $("*[id$='DdlInternshipDepartments']").val('-1');
            $("#departments").show();
            $("#internshipDepartments").hide();
        }
        else if (iCategoryID == 5) {
            $("*[id$='DdlDepartments']").val('-1');
            $("#internshipDepartments").show();
            $("#departments").hide();
        }
        else {
            $("*[id$='DdlDepartments']").val('-1');
            $("*[id$='DdlInternshipDepartments']").val('-1');
            iTaleoDepartmentID = -1;
            $("#departments").hide();
            $("#internshipDepartments").hide();
        }
        iTaleoDepartmentID = -1;
        //make ajax request to get jobs for this location
        getTotalNumberOfJobs(iCategoryID, iTaleoDepartmentID, iLocationID);
    })

    //this handles the onchange event of the locations dropdown
    $("*[id$='DdlLocations']").change(function() {
        $("#search-field").val("");
        var oSelectedItem = $("*[id$='DdlLocations'] option:selected");
        //get the locationID
        iLocationID = oSelectedItem.val();
        //make ajax request to get jobs for this location
        getTotalNumberOfJobs(iCategoryID, iTaleoDepartmentID, iLocationID);
    })

    //this handles the onchange event of the department dropdown
    $("*[id$='DdlDepartments']").change(function() {
        $("#search-field").val("");
        var oSelectedItem = $("*[id$='DdlDepartments'] option:selected");
        //get the locationID
        iTaleoDepartmentID = oSelectedItem.val();
        //make ajax request to get jobs for this location
        getTotalNumberOfJobs(iCategoryID, iTaleoDepartmentID, iLocationID);
    })

    //this handles the onchange event of the internship department dropdown
    $("*[id$='DdlInternshipDepartments']").change(function() {
        $("#search-field").val("");
        var oSelectedItem = $("*[id$='DdlInternshipDepartments'] option:selected");
        //get the departmentID
        iTaleoDepartmentID = oSelectedItem.val();
        //make ajax request to get jobs for this location
        getTotalNumberOfJobs(iCategoryID, iTaleoDepartmentID, iLocationID);
    })

    //this function is fired when the user clicks to search for a keyword
    function keyWordSearchClick() {

        var sKeyWord = $("#search-field").val();
        sKeyWord = jQuery.trim(sKeyWord);
        $("#search-field").val(sKeyWord);

        if (sKeyWord == "") {
            $("#search-field").stop();
            $("#search-field").css("background-color", "#ffa347");
            $("#search-field").animate({ backgroundColor: "#ffffff" }, 1300);
        } else {

            $.ajax({
                type: "POST",
                url: "Default.aspx/GetJobsByKeyword",
                data: '{"sSearchKeyword":"' + sKeyWord + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    //first we are going to split the response to get the html vs the total job count
                    var sResponse = msg.d;
                    var msgArray = sResponse.split("~|~");
                    var sHtml = msgArray[0];
                    var iTotalJobResults = msgArray[1];
                    //hide the pagination
                    $("#job-pages").hide();
                    //reset the dropdowns
                    $("*[id$='DdlCategory']").val('-1');
                    $("*[id$='DdlDepartments']").val('-1');
                    $("*[id$='DdlLocations']").val('-1');
                    //show/hide stuff
                    $("#search-specs").show();
                    $("#departments").hide();
                    $("#SearchResultCount").hide();
                    $("#TryKeyword").hide();
                    $("#TryFilter").show();

                    //display search result info about search term and number of results
                    $("#apply-search-results").html("Your search for <span class=\"dgray\">\"" + sKeyWord + "\"</span> found <span class=\"bold dgray\">" + iTotalJobResults + " jobs</span>");

                    // Replace the div's content with the page method's return.
                    $("#ResultListing").hide();
                    $("#ResultListing").html(sHtml);
                    $("#ResultListing").fadeIn("normal");

                }
            });
        }
    }

});