﻿/**
* Author:   Peter Wright
* Company:  Renishaw PLC
* Copy:     2010(c) 
* Title:    SearchController
* Desc:     An object that handles synchronisation between multiple instances of the RRC Search Control (ASCX)
**/
var SearchController = {

    Controls: [],
    LanguageContainers: [],
    FileLanguages: [],
    LanguageDropDowns: [],
    ButtonSeperators: [],
    ButtonRemoveMethod: [],
    FirstControlIdx: '',
    ClickToRemoveLang: '',
    version: '1.0',

    RegisterControl: function(jqObj) {
        var id = jqObj.get(0).id;
        if (this.ClickToRemoveLang.length == 0) {
            this.ClickToRemoveLang = $('input[type=hidden][id$=hfClickToRemoveText]').val();
        }
        if (this.Controls[id] != jqObj) {
            this.Controls[id] = jqObj;

            $('[id$=rblPurposes] input', this.Controls[id]).click(function() {
                SearchController.toggleCheckboxes($('[id$=rblPurposes] input:checked', $(this).parents('[id$=pnlLargeSearchBox]')).val(), "purpose", this)
            });

            $('[id$=rblDivisions] input', this.Controls[id]).click(function() {
                SearchController.toggleCheckboxes($('[id$=rblDivisions] input:checked', $(this).parents('[id$=pnlLargeSearchBox]')).val(), "division", this)
            });

            //            $('input[type=submit][id*=btnLargeControl],input[type=submit][id*=advSearchBtn]', this.Controls[id]).click(function() {
            //                SearchController.validate(this); 
            //            });

            this.LanguageContainers[id] = $('div[id$=cellLanguages]', this.Controls[id]);
            this.FileLanguages[id] = $('input[type=hidden][id$=hfFileLanguages]', this.Controls[id]);
            this.LanguageDropDowns[id] = $('select[id$=ddlFileLanguage]', this.Controls[id]).get(0);
            this.ButtonRemoveMethod[id] = "SearchController.removeSearchLanguage";
            this.ButtonSeperators[id] = $('input[type=hidden][id$=hfSeperator]', this.Controls[id]).val();

            if (this.FirstControlIdx.length == 0) {
                this.FirstControlIdx = id;
            }

            buttonRender(
                this.LanguageContainers[id],
                this.FileLanguages[id],
                this.LanguageDropDowns[id],
                this.ButtonRemoveMethod[id],
                this.ButtonSeperators[id],
                this.ClickToRemoveLang
            );

        }
    },
    validate: function(source) {
        //this = window.SearchController;
        //1. uncheck any boxes if the radio btns are set to all
        var s = $(source), activeSearchController = s.parents('div[id$=pnlLargeSearchBox]'), id = activeSearchController.get(0).id;
        if (this.Controls[id]) {
            var inputsDivision = $('input[type=radio][name$=rblDivisions][value=all]', activeSearchController);
            var inputPurposes = $('input[type=radio][name$=rblPurposes][value=all]', activeSearchController);
            //uncheck ALL divisions checkboxes from ALL search controls if 'ALL' is specified
            if (inputsDivision.val() == "all" && inputsDivision[0].checked) {
                //$('input[type=checkbox][id*=cblDivision]').removeAttr("checked");
                $('input[type=hidden][id*=hfFilterByProductLine]').val("false");
            } else {
                $('input[type=hidden][id*=hfFilterByProductLine]').val("true");
            }
            //uncheck ALL divisions checkboxes from ALL search controls if 'ALL' is specified
            if (inputPurposes.val() == "all" && inputPurposes[0].checked) {
                //$('input[type=checkbox][id*=cblDivision]').removeAttr("checked");
                $('input[type=hidden][id*=hfFilterByPurpose]').val("false");
            } else {
                $('input[type=hidden][id*=hfFilterByPurpose]').val("true");
            }
        }
        return true;
    },
    toggleCheckboxes: function(val, type, source) {
        var inputs, container; //, p = $(source).parents('[id$=pnlLargeSearchBox]');

        switch (type) {
            case 'division':
                inputs = $('input[type=radio][name$=rblDivisions]')
                container = '.divisions-individual-checkboxes';
                break;
            case 'purpose':
                inputs = $('input[type=radio][name$=rblPurposes]')
                container = '.purposes-individual-checkboxes';
                break;
        }

        for (i = 0; i < inputs.length; i++) {
            if (inputs[i].value == val) {
                inputs[i].checked = true;
            } else {
                inputs[i].checked = false;
            }
        }

        switch (val) {
            case "all":
                for (i in this.Controls) {
                    var j = this.Controls[i];
                    var k = $(container, j);
                    if ($('.more-search-options', j).is(':visible')) {
                        slideClosed(k);
                    } else {
                        k.hide();
                    }
                }                
                break;
            default:
                for (i in this.Controls) {
                    var j = this.Controls[i];
                    var k = $(container, j);
                    if ($('.more-search-options', j).is(':visible')) {
                        slideOpen(k);
                    } else {
                        k.show();
                    }
                }
                break;
        }
        SearchController.validate(source);
    },
    removeSearchLanguage: function(source, value) {
        var s = $(source);

        var ix = this.FirstControlIdx, fl = "";
        $('options[value=' + value + ']').show();

        var a = String(this.FileLanguages[ix].val()).split(";");

        for (var k = 0; k < a.length; k++) {
            if (a[k].length > 0 && a[k] != value) {
                fl += a[k] + ";";
            }
        }

        for (id in this.Controls) {
            this.FileLanguages[id].val(fl);
            buttonRender(
                this.LanguageContainers[id],
                this.FileLanguages[id],
                this.LanguageDropDowns[id],
                this.ButtonRemoveMethod[id],
                this.ButtonSeperators[id],
                this.ClickToRemoveLang
            );
        }


        return false;
    },
    addSearchLanguage: function(source) {
        //find the active drop down list
        var activeList = $(source).parents('div[id$=pnlLargeSearchBox]').find('select[id$=ddlFileLanguage]')[0];
        var selectedIndex = activeList.selectedIndex;
        var selectedValue = String(activeList.options[selectedIndex].value);
        var selectedText = String(activeList.options[selectedIndex].text);

        if (selectedIndex > 0) {
            //find all current selected languages
            var t = this.FileLanguages[this.FirstControlIdx].val();
            var a = String(t).split(";");
            var f = false;

            //look for the selected item already in the selected list
            for (var i = 0; i < a.length; i++) {
                if (String(a[i]) == selectedValue) {
                    f = true;
                }
            }

            //if we could not find it then add into the hidden field for all search controls
            if (!f) {
                for (ix in this.LanguageDropDowns) {
                    this.LanguageDropDowns[ix].options[selectedIndex].className = "hide-option";
                    this.LanguageContainers[ix].append("<a class=\"button-remove-language remove-filter\" href=\"#\" onclick=\"javascript: return SearchController.removeSearchLanguage(this, '" + selectedValue + "');\" title=\"" + String(this.ClickToRemoveLang).replace(/\{0\}/g, selectedText) + "\"><span>" + selectedText + " </span><span class=\"remove-cross\">&times;</span></a>");
                    this.FileLanguages[ix].val(this.FileLanguages[ix].val() + selectedValue + ";");
                    buttonSeperatorRender(this.LanguageContainers[ix], this.ButtonSeperators[ix]);
                }
            }

            activeList.selectedIndex = 0;
        }
        return false;
    }
};

window.SearchController = SearchController;

//function AdjustOptionsWindowSize() {
//    if ($('.more-search-options').is(':visible') && $('.more-search-options').is(':not(:animated)')) {
//        $('.more-search-options').animate({ height: ($('.more-search-options *:visible').height()) }, 50, function() { });
//    }
//};

function toggleOptions(source) {
    //locate the parent element for the search control
    var x = $(source).parents('div.large-search-area-container');

    switch (x.children('div.more-search-options').is(':hidden')) {
        case true:
            if (!x.children('div.more-search-options').is(':animated')) {

                /* look at the value for divisions and see if we need to open the specific section */
                if ($('[id$=rblDivisions] input:checked', x).val() != "all") {
                    $('.divisions-individual-checkboxes', x).show();
                } else {
                    $('.divisions-individual-checkboxes', x).hide();
                }

                /* look at the value for purposes and see if we need to open the specific section */
                if ($('[id$=rblPurposes] input:checked', x).val() != "all") {
                    $('.purposes-individual-checkboxes', x).show();
                } else {
                    $('.purposes-individual-checkboxes', x).hide();
                }

                /* update the advanced search options */
                $('input[type=hidden][id$=hfAdvanced]', x).val("1");

                /* open the andvanced search options */
                $('a.options-link-more', x).hide();
                $('a.options-link-less', x).show();
                slideOpen(x.children('div.more-search-options'));
            }
            break;
        case false:
            if (!x.children('div.more-search-options').is(':animated')) {
                $('a.options-link-less, a.options-link-more, div.more-search-options', x).stop();
                $('a.options-link-less', x).hide();
                $('a.options-link-more', x).show();
                jqObj = x.children('div.more-search-options');
                if (jqObj.is(':visible') && jqObj.is(':not(:animated)')) {
                    slideClosed(jqObj);
                }
                $('input[type=hidden][id$=hfAdvanced]', x).val("0");
            }
            break;
    }
    return false; //to stop the browser from processing the href
};


//function addSearchLanguage(source) {
//    if (ddlLang.selectedIndex > 0) {
//        var t = hfLang.val();
//        var a = String(t).split(";");
//        var f = false;
//        for (var i = 0; i < a.length; i++) {
//            if (String(a[i]) == String(ddlLang.options[ddlLang.selectedIndex].value)) {
//                f = true;
//            }
//        }
//        if (!f) {
//            ddlLang.options[ddlLang.selectedIndex].className = "hide-option";
//            cell.append("<a class=\"button-remove-language remove-filter\" href=\"#\" onclick=\"javascript: return removeSearchLanguage(this, '" + ddlLang.options[ddlLang.selectedIndex].value + "');\" title=\"" + String($('input[type=hidden][id$=hfClickToRemoveText]').val()).replace(/\{0\}/g, ddlLang.options[ddlLang.selectedIndex].text) + "\"><span>" + ddlLang.options[ddlLang.selectedIndex].text + " </span><span class=\"remove-cross\">&times;</span></a>");
//            hfLang.val(hfLang.val() + ddlLang.options[ddlLang.selectedIndex].value + ";");
//            buttonSeperatorRender(cell, seperator);
//        }
//    }
//    ddlLang.selectedIndex = 0;
//    return false;
//};



//function removeSearchLanguage(source, id) {
//    var s = $(source);
//    for (var j = 0; j < ddlLang.options.length; j++) {
//        if (ddlLang.options[j].value == id) {
//            var a = String(hfLang.val()).split(";");
//            hfLang.val("");
//            for (var k = 0; k < a.length; k++) {
//                if (a[k].length > 0 && a[k] != id) {
//                    hfLang.val(hfLang.val() + a[k] + ";");
//                }
//            }
//            ddlLang.options[j].className = "";
//            s.parent().get(0).removeChild(source);
//        }
//    }
//    buttonSeperatorRender(cell, seperator);
//    return false;
//}
