﻿/// <reference path="../Scripts/jquery-1.2.6.js" />

// Global Variables
function AdvancedSearchFields() {
    this.Adv_ConditionType = '';
    this.Adv_SMRB = '';
    this.Adv_BC = '';
    this.Adv_CM = '';
    this.Adv_C = '';
    this.Adv_RC = '';
    this.Adv_LI = '';
    this.ControlsAreLoaded = false;
}
var ASF = new AdvancedSearchFields();

// DOM Startup
$(document).ready(function() {
    ConfigureSearchClient();//NOTE: May also need to call this when update panel refreshes
});

function ConfigureSearchClient() {


    //Wire Up Category Panel Display Events
    $('.tdCatSelector input').click(function() {
        //Reset: Hide all other panels and reset their selection
        $('.tdCatOptions div select').each(function() {
            $(this)[0].selectedIndex = 0;
        });
        $('.tdCatOptions div').hide();

        //Show the selected panel
        $('.' + this.value).show();
    });

    //Auto detect preloaded category (when refine search). Select Radio button and show panel
    $('.tdCatOptions select').each(function() {
        var nonDefaultIsSelected = $(this).val() > 0;
        if (nonDefaultIsSelected) {
            //show this panel
            var ddlContainer = $($(this)[0].parentNode);
            ddlContainer.show();
            var ddlContainerClass = ddlContainer.attr("class");
            //Set Radio Button selection
            $(".tdCatSelector input[value='" + ddlContainerClass + "']")[0].setAttribute("checked", "checked");
            
        }
    });
    

    /*-- Search Field Handles --*/
    LoadAdvancedSearchControlHandles();
}


//Load JQuery objects for each advanced search control if it is in the page
function LoadAdvancedSearchControlHandles() {

    if (typeof (BeefCuts) != "undefined") {
        ASF.ControlsAreLoaded = true;
    }

    if (ASF.ControlsAreLoaded) {
        //Load ConditionType
        if (ConditionType.length > 0) {
            var control = $('#' + ConditionType + " input[@value='all']");
            if (control != undefined) {
                ASF.Adv_ConditionType = control;
            }
        }
        
        //Load SearchMyRecipeBox
        if (SearchMyRecipeBox.length > 0) {
            var control = $('#' + SearchMyRecipeBox);
            if (control != undefined) {
                 ASF.Adv_SMRB = control; 
            }
        }

        //Load BeefCuts
        if (BeefCuts.length > 0) {
            var control = $('#' + BeefCuts);
            if (control != undefined) {
                ASF.Adv_BC = control;
            }
        }

        //Load Cooking Method
        if (CookingMethod.length > 0) {
            var control = $('#' + CookingMethod);
            if (control != undefined) {
                ASF.Adv_CM = control;
            }
        }

        //Load Cuisine
        if (Cuisine.length > 0) {
            var control = $('#' + Cuisine);
            if (control != undefined) {
                ASF.Adv_C = control;
            }
        }

        //Load Recipe Category
        if (RecipeCategory.length > 0) {
            var control = $('#' + RecipeCategory);
            if (control != undefined) {
                ASF.Adv_RC = control;
            }
        }

        //Load Ingredients
        if (RecipeIngredients.length > 0) {
            var controls = $('#' + RecipeIngredients + ' input');
            if (controls != undefined) {
                ASF.Adv_LI = controls;
            }
        }

        return true;
    }

    return false;
}

