/*
 * Speichert und liest die Einstellungen aus der Searchbox
 *
 */
(function( $ ){

    var settings = {
           'prefix' : 'sb'
        }

    var methods = {
        init : function( options ) {

            if ( options ) {
               $.extend( settings, options );
            }

            if (Modernizr.localstorage){
                $('div#container ul li').each(function(index) {
                    $(this).click(function() {
                       jQuery.fn.Searchbox('save');
                    });
                });
            }
        },


        save : function( ) {
            if (Modernizr.localstorage){
                $('select.searchbox,input.searchbox').each(function(index) {
                    name = $(this).attr('name');
                    value = $(this).attr('value');
                    placeholder = $(this).attr('placeholder');
                    if ( value != placeholder ) {
                        localStorage.setItem(settings.prefix + '.' + name, value);
                        if ((name == 'zo') && ($(this).context.tagName == 'SELECT')) {
                            DestinationDesc = $(this).context[$(this).context.selectedIndex].text.replace('--', '');
                            localStorage.setItem(settings.prefix + '.' + 'zo_autocomplete', DestinationDesc);
                        }
                    }
                });
            }
        },


        load : function( ) {
            if (Modernizr.localstorage){
                var show_ext = false;
                var show_child = false;
                $('select.searchbox,input.searchbox').each(function(index) {
                    name = $(this).attr('name');
                    value = localStorage.getItem(settings.prefix + '.' + name);
                    if (value == 'xl-change') {
                        value = '';
                    }
                    if ((value != null) && (value != '[beliebig]') && (value != '[bitte auswählen]') ) {
                        isDate = $(this).hasClass('sb_date');
                        if (((!isDate) && (value != '')) || ((isDate) && (value != '') && (value != '0'))) {
                            $(this).attr('value', value);
                            if ($(this).hasClass('sb_ext')) {
                                show_ext = true;
                            }
                            if ($(this).hasClass('sb_child')) {
                                show_child = true;
                            }
                        }
                    }
                });
                if (show_ext) {
                    $('.sb_ext_button').click();
                }
                if (show_child) {
                    $('.sb_child_button').click();
                    childClick();
                }
            }
        }

    };



    $.fn.Searchbox = function( method ) {
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
        }
    };


})( jQuery );
