﻿/// <reference path="~/Scripts/jquery-1.4.4-vsdoc.js"/>

// page ready function
$(function() {
    initializeBehaviors();
});

// function that initializes the client side behaviors for all of the controls on the start page.
function initializeBehaviors() {

    //setup the modal for if the user can't find their school.  
    $('.cantFind .colorbox').colorbox({ width: "650px", height: "700px", iframe: true, overlayClose: false, href: 'start/findschoolmodal' });

    // wire up the zipcode input behaviors
    if ($('#ZipCode').val() == '') {
        $('#ZipCode').val('ex. 21703');
    }
    $('#ZipCode').focus(function() {
        if ($(this).val() == 'ex. 21703')
            $(this).val('');

        $(this).css('backgroundColor', '#ffffff');
    });
    $('#ZipCode').blur(function() {
        if ($(this).val() == '')
            $(this).val('ex. 21703');

        $(this).css('backgroundColor', '#f8f8f8');
    });

    $('#ZipCode').keypress(function() {
        if (event.keyCode == 13) {
            if (!$('#IsRentalForStudent input[type=checkbox]').attr('checked') || $('.selectedSchoolContainer').css('display') != 'none') {
                $('.start_continuebutton').click();
                return false;
            }
            else {
                performSearch(0);
                return false;
            }
        }

        return true;
    });

    // wire the search button to the async search functionality
    $('#btnSearch').click(function () { performSearch(0); return false; });

    // wire the changeSchoolButton to change the UI to allow for a new school search.
    $('#changeSchoolButton').click(function() {
        var myUrl = 'Start/ChangeCurrent';
        $.ajax({
            url: myUrl,
            success: searchCompleted,
            cache: false
        });

        return false;
    });

    // wire the child rental checkbox behavior to hide/show the distance and child grade inputs for adult rentals
    $('#IsRentalForStudent').click(function () {
        if (!$(this).attr('checked')) {
            $('.start_adultrentalhidden').hide();
            $('#resultsDiv').hide();
            $('#continueButton').show();
        }
        else {
            $('.start_adultrentalhidden').show();
            $('#resultsDiv').show();
            $('#continueButton').hide();
        }
    });

    // wire the results per page select behavior to research when changed
    $('#resultsPerPage').change(function() {
        return performSearch(0);
    });

    // in javascript mode, dont need to show the change result per page button
    $('#resultsPerPageChangeSubmit').hide();

    // wire the results per page change handler to perform a search
    $('#ResultsPerPage').change(function () { performSearch(0); return false; });

    // wire the previous link to search with a negative page offset
    $('#prevLink').click(function () {
        performSearch(-1);
        return false;
    });

    // wire the next link to search with a positive page offset
    $('#nextLink').click(function () {
        performSearch(1);
        return false;
    });

    //set the unavailable tooltip up for any displayed element that is inactive
    $('.notAvailable').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fixPNG: true,
        fade: 250,
        top: -50
    });

    // wire up the wait modals
    $(document).ajaxStart(blockUI).ajaxStop($.unblockUI);
}

// block the UI while waiting for ajax calls
function blockUI() {
    $.blockUI({ message: '<div style="height: 65px; line-height: 65px;"><h3><img src="content/images/loading.gif" style="vertical-align: middle;"/>  Loading, Please Wait...</h3></div>' });
}

// performs an async search for schools based on the selected criteria from the search form elements
function performSearch(offset) {

    //retrieve the values necessary to perform the search
    var zipValue = $('#ZipCode').val();
    var gradeValue = $('#Grade').val();
    var radius = $('#SearchRadius option:selected').val();
    var isRentalForStudent = $('#IsRentalForStudent').val();

    var rowsPerPage = 25;

    if ($('#ResultsPerPage').length != 0)
    {
        rowsPerPage = $('#ResultsPerPage option:selected').val();
    }

    var pageNumber = 0;

    if ($('#pageNumber').length != 0) {
        pageNumber = $('#pageNumber').html() - 1 + offset;
    }

    if ($('#totalPages').length != 0) {
        var maxPages = $('#totalPages').html();

        if (pageNumber >= maxPages) {
            pageNumber = maxPages-1;
        }
    }

    if (pageNumber < 0) {
        pageNumber = 0;
    }
        
    //do the search for the schools and show the results panel
    var myUrl = 'Start/Results?ZipCode=' + zipValue + "&Grade=" + gradeValue + "&SearchRadius=" + radius + "&ResultsPerPage=" + rowsPerPage + "&PageNumber=" + pageNumber + "&IsRentalForStudent=" + isRentalForStudent;
    $.ajax({
        url: myUrl,
        success: searchCompleted,
        cache: false
    });

    return true;
}

// callback function for when the school search has completed successfully.
function searchCompleted(data) {

    $("#resultsDiv").replaceWith(data);

    initializeBehaviors();
}

