﻿/*

SearchBox: a styled box with switchable search forms
needs the following html structure:

<!-- SearchBox container, needs to have id SearchBox -->
<div id="SearchBox" class="Big">
    <form id="ActiveSearch" action="">
        <fieldset>
            
        </fieldset>
        <fieldset>
            <!-- selector for the forms to switch between, needs to have id SearchSelector -->
            <select id="SearchSelector">
                <option value="GoogleSearch">Google</option>
                <option value="ApaSearch">Vorarlberg</option>
            </select>               
        </fieldset>
    </form>
    
    <!-- these are the actual forms to switch between -->
	<form method="post" action="http://www.vol.at/search.aspx" id="GoogleSearch">
	    <fieldset>
            <input type="text" name="q" class="Text GoogleSearch" />
            <input type="hidden" name="cof" value="FORID:10"/>
            <input type="hidden" name="ie" value="ISO-8859-1"/>
            <input type="hidden" name="page" value="google-search"/>
            <input type="hidden" name="cx" value="001610385003410938437:lx2k7awo_la"/>
            <input type="hidden" name="sa" value="Suche" />
        </fieldset>
	</form>
	
	<form method="post" action="http://www.vol.at/engine.aspx" id="ApaSearch">
	    ...
	</form>
</div>

*/

//constructor expects an id of the default search for the initial setup:
// e.g. "ApaSearch"
function SearchBox(id) {
    SB = this;
    
    
    //set the selector to the given value
    $("#SearchSelector").val(id);
    	            
    //setup dropdown box with mcom.selector plugin
    $("#SearchSelector").mcomSelector({
        mode: "submit",
        selectCallback: SB.select,
        submitCallback: SB.submitCallback
    });
    
    //set the initial value for the mcom.selector
    SB.select({value:id});
    
}
//change the selected search, expects an named array vals for the selection
//e.g. {value:"ApaSearch"}
SearchBox.prototype.select = function(vals) {
    //remeber the searchphrase if already set	
    var searchPhrase = $("#ActiveSearch fieldset:first input:first").val();
            
    //set the action of the active form to the action of the hidden selected form
    $("#ActiveSearch").attr("action", $("#"+vals.value).attr("action"));
    
    //set the method of the active form to the method of the hidden selected form
    $("#ActiveSearch").attr("method", $("#"+vals.value).attr("method"));
    
    //remove all child elements in first fieldset of the active form
    $("#ActiveSearch fieldset:first").empty();
    
    //copy all child nodes of the hidden form to fieldset
    $("#"+vals.value+" fieldset > *").clone().prependTo($("#ActiveSearch fieldset:first"));
    
    
    if (typeof(searchPhrase)!= "undefined" && searchPhrase != "") {
        //set the searchphrase we remembered
        $("#ActiveSearch fieldset:first input:first").val(searchPhrase);
        //set the Focus class because there is text already typed in
        $("#ActiveSearch fieldset:first input:first").addClass("Focus");
    }
    
    //set an onfocus event for the input field: remove background
    $("#ActiveSearch fieldset:first input:first").focus(function () {
        $("#ActiveSearch fieldset:first input:first").addClass("Focus");
    });
    
    //set an onblur event for the input field: add background again
    $("#ActiveSearch fieldset:first input:first").blur(function () {
        if (typeof($("#ActiveSearch fieldset:first input:first").val())== "undefined" 
            || $("#ActiveSearch fieldset:first input:first").val() == "")
        $("#ActiveSearch fieldset:first input:first").removeClass("Focus");
    });
};

SearchBox.prototype.submitCallback = function(vals) {
    //remeber the searchphrase if already set	
    var searchPhrase = $("#ActiveSearch fieldset:first input:first").val();
    if (typeof(searchPhrase)== "undefined" || searchPhrase == '')
        return false;
    else
        return true;
}

//on startpage searchBox is used as TabBox
//adapted functions

		function initSearchBox(){
    //set an onfocus event for the input field: remove background
    $("#SearchBox.TabBox input.Text").focus(function () {
				$("#SearchBox.TabBox input.Text").addClass("Focus");
    });
    
    //set an onblur event for the input field: add background again
    $("#SearchBox.TabBox input.Text").blur(function () {

        if (typeof($(this).val())== "undefined" || $(this).val() == ""){
					$("#SearchBox.TabBox input.Text").removeClass("Focus");
        }
				$("#SearchBox.TabBox input.Text").val($(this).val()); 
        
    });
    
		}
