﻿//array for sorting through the selected categories on service search
var _selectedServiceCat = [];
var _all

$(document).ready(function () {

    // primarynav search watermark
    $("div#primarynav div.search input.text").val("keyword search");
    
    $("div#primarynav div.search input.text").click(function () {
        if ($(this).val() == "keyword search")
            $(this).val("");
    });
    $("div#primarynav div.search input.text").blur(function () {
        if ($(this).val() == "")
            $(this).val("keyword search");
    });

    // zip code watermark
    $("div#body-content-center input.zipcode").val("Enter Zip Code");
    $("div#body-content-center input.zipcode").click(function () {
        if ($(this).val() == "Enter Zip Code")
            $(this).val("");
    });
    $("div#body-content-center input.zipcode").blur(function () {
        if ($(this).val() == "")
            $(this).val("Enter Zip Code");
    });

    //organization watemark
    $("div#body-content-center input.organization").val("Enter Organization Name");
    $("div#body-content-center input.organization").click(function () {
        if ($(this).val() == "Enter Organization Name")
            $(this).val("");
    });
    $("div#body-content-center input.organization").blur(function () {
        if ($(this).val() == "")
            $(this).val("Enter Organization Name");
    });

    //calendat watemark
    $("div#body-content-center input.from-date").val("From Date");
    $("div#body-content-center input.from-date").click(function () {
        if ($(this).val() == "From Date")
            $(this).val("");
    });
    $("div#body-content-center input.from-date").blur(function () {
        if ($(this).val() == "")
            $(this).val("From Date");
    });

    $("div#body-content-center input.to-date").val("To Date");
    $("div#body-content-center input.to-date").click(function () {
        if ($(this).val() == "To Date")
            $(this).val("");
    });
    $("div#body-content-center input.to-date").blur(function () {
        if ($(this).val() == "")
            $(this).val("To Date");
    });

    // secondary nav accordion
    //$("ul#accordion-nav").accordion({ collapsible: true, active: false, autoHeight: false });

    // primary / secondary nav flyout menus
    var config = {
        sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
        interval: 200, // number = milliseconds for onMouseOver polling interval    
        over: flyoutDown, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: flyoutUp // function = onMouseOut callback (REQUIRED)
    };
    $('#primarynav ul > li').hoverIntent(config);
    $('#secondary-nav ul > li:not(.current)').hoverIntent(config);

    //calendar search
    $(".sub_category_link").click(function () {
       var checkBox = $(this).parent().parent().find("input");

       if (checkBox.attr('disabled') == false)
            checkBox.attr('disabled', true);
        else
            checkBox.removeAttr('disabled');
    });

    //save off the service categories from the search page
    $('.service_types_list > option').each(function (i) {
        objService(i, $(this).val(), $(this).text());
    });

    //service search toggle services by selected categories
    $('.service_group').click(function () {

        var selectedIDs = "";

        $(".service_group").each(function () {
            var cbService = $(this).find("input");
            if ($(cbService).is(':checked')) {
                selectedIDs += (selectedIDs == "") ? $(cbService).parent().parent().next("div.service_group_type_ids").html() : "|" + $(cbService).parent().parent().next("div.service_group_type_ids").html();
            }
        });

        displayServiceOptions(selectedIDs);
    });

    //text resizing handlers
    $('#text_decrease').click(function () {
        var currentFont = $("body").css("font-size");
        updateFontSize(parseInt($("body").css("font-size")) - 4, parseInt($("#body-content-center h2").css("font-size")) - 4, parseInt($("h3").css("font-size")) - 4);
    });

    $('#text_increase').click(function () {
        updateFontSize(parseInt($("body").css("font-size")) + 4, parseInt($("#body-content-center h2").css("font-size")) + 4, parseInt($("h3").css("font-size")) + 4);
    });

});

function updateFontSize(fontSize, fontH2, fontH3) {
    $("body").css("font-size", fontSize + "px");
    $("#body-content-center h2").css("font-size", fontH2 + "px");
    $("h3").css("font-size", fontH3 + "px");
}

function displayServiceOptions(serviceIDs) {
   //first remove all the options
    $('.service_types_list').empty();
    var serviceOptions = $(".service_types_list").attr('options');
    
    if (serviceIDs != "") {
        serviceOptions[serviceOptions.length] = new Option("Select a Service", "");
        //now add back selected ones
        var ids = serviceIDs.split('|');

        for (service in objService) {
            for (var i = 0; i < ids.length; i++) {

                if (objService[service].id == ids[i]) {
                    serviceOptions[serviceOptions.length] = new Option(objService[service].text, objService[service].id);
                }
            }
        }
    } else {
        //when nothing is selected display all options
        for (service in objService) {
            serviceOptions[serviceOptions.length] = new Option(objService[service].text, objService[service].id);
        }
    }
}

// primary navigation drop-down menu functions
function flyoutDown() {
    $(this).find('div.nav-wrapper').slideDown();
}

function flyoutUp() {
    $(this).find('div.nav-wrapper').hide();
}

function objService(index, id, text) {
    return objService[index] = {
        index: index,
        id: id,
        text: text
    }
}
