var productSearch = {
    pages:{
        'search':'/ajax/search/search/'
    },
    inputs:{},
    types:[],
    inputType:'tickselection',
    noPerPage:12,
    or:false,
    submit:false,
    //Performs a search based on input values.
    s:function(page){
        if(window.mytimeout) window.clearTimeout(window.mytimeout);
        window.mytimeout = window.setTimeout(function(){
            var values = {};
            var searchTypes = {};
            $.each(productSearch.inputs,function(name){
                
                //Gets values for a tickselection.
                if(this.type=='tickselection'){
                    values[name] = productSearch.inputValues.tickselection(name);
                }else if(this.type=='linkselection'){
                    values[name] = productSearch.inputValues.linkselection(name);
                }else if(this.type=='textbox'){
                    values[name] = productSearch.inputValues.textbox(name);
                }else if(this.type=='tickbox'){
                    values[name] = productSearch.inputValues.tickbox(name);
                }else if(this.type=='slider'){
                    values[name] = productSearch.inputValues.slider(name);
                }else if(this.type=='dropdown'){
                    values[name] = productSearch.inputValues.dropdown(name);
                }else if(this.value!=undefined && this.value!=''){
                    values[name] = this.value;
                }
                //alert($.toJSON(values));
                //This sets the search type for each search input. E.G. exact.
                if(this.searchType!=undefined){
                    searchTypes[name] = this.searchType;
                }
                if(name=='keywords'){
                    var keywords = productSearch.inputValues.textbox(name);
                    if(keywords!=''){
                        values[name] = keywords.split(' ');
                    }
                }
            });
            
            if(productSearch.inputs.keywords!=undefined){
                var attributes = productSearch.inputs.keywords.attributes
            }
            if($('#searchOrder').val() != undefined){
                var order = $('#searchOrder').val();
            }
            var types = $.toJSON((productSearch.types.length>0?productSearch.types:productSearch.getTypes()));
            if(productSearch.submit==true){
                var values2 = {};
                $.each(values,function(key){
                    if(this!='' && this!=0){
                        values2[key] = this;
                    }
                });
                if(order){
                    values2['order'] = order;
                }
                values2['noPerPage'] = productSearch.noPerPage;
                if(page){
                    values2['page'] = page;
                }
                var string = '';
                $.each(values2,function(name){
                	string += name+'='+this+'&';
                });
                string = rtrim(string,'&');
                redirect(url.u(['search'])+'?'+string);
            }else{
                $.post(
                    productSearch.pages['search'],
                    {
                        'inputs':$.toJSON(values),
                        'searchTypes':$.toJSON(searchTypes),
                        'types':types,
                        'keywordsAttributes':$.toJSON(attributes),
                        'order':order,
                        'page':page,
                        'noPerPage':productSearch.noPerPage,
                        'or':productSearch.or?1:0
                    },
                    function(data){
                        html.productSearch.clear();
                        if(data.results.length>0){
                            $.each(data.results,function(){
                                html.productSearch.results(this);
                            })
                        }else{
                            html.productSearch.noResults();
                        }
                        html.productSearch.pagination(data.pageDetails);
                        if(typeof(html.productSearch.aftersearch) == 'function'){
                            html.productSearch.aftersearch(data);
                        }
                    },
                    'json'
                );
            }
        },1000);
    },
    inputValues:{
        /**
        * Gets all the values from the a tick selection.
        */
        tickselection:function(name){
            var values = [];
            var i = 0;
            //Loops throigh each tick box and get the checked ones.
            $('#ts-'+name+' input').each(function(){
                if($(this).attr('checked')){
                    var name = $(this).attr('name').split('-');
                    values[i] = name[2];
                    i++;
                }
            });
            return values;
        },
        textbox:function(name){
            return $('#tb-'+name).val();
        },
        tickbox:function(name){
            if($('#cb-'+name).attr('checked')==true){
                return 1;
            }else{
                return 0;
            }
        },
        dropdown:function(name){
            return $('#dd-'+name).val();
        },
        slider:function(name){
            var values = $('#s-'+name+'-value').val().split('-');
            values[0] = $.trim(values[0].replace(/([^0-9]*)/,''));
            values[1] = $.trim(values[1].replace(/([^0-9]*)/,''));
            return values;
        },
        linkselection:function(name){
            return $('#ls-'+name).val();
        }
    },
    updateLinkSelection:function(name,value){
        $('#ls-'+name+'-holder a').removeClass('active');
        if(value==''){
            $('#ls-'+name+'-all').addClass('active');
        }else{
            $('#ls-'+name+'-'+value).addClass('active');
        }
        $('#ls-'+name).val(value);
        this.s();
    },
    getTypes:function(){
        var types = [];
        if(this.inputType=='tickselection'){
            types = this.inputValues.tickselection('type');
        }else if(this.inputType=='linkselection'){
            var value = this.inputValues.linkselection('type')
            if(value!=''){
                types.push(value);
            }
        }
        if(types.length>0){
            return types;
        }
    },
    changeNoPerPage:function(no){
        this.noPerPage = no;
        this.s();
    },
    submit:function(id){
        var values = formFunctions.getFormValues(id);
        $.each(values,function(key){
            if(this=='' || key==''){
                delete values[key];
            }
        });
        redirect('/search/'+encodeURI($.toJSON(values))+'/');
    }
}
