jQuery(function($){
    $(document).ready(function(){
        var xmlObject;
        setSelects('#box1,#box2,#box2a,#box3');
        function setSelects(el){
            $.cyt_setMultiBox(el, 'cytSelBox', 12);
        }
        //JSONP
        var jsonObj;
        var startHersteller;
        $.getScript("/layout/cyt/js/xml_to_json.js", function(){
            $.get('/layout/cyt/data/plenty.xml', function(xml){
                jsonObj = $.xml2json(xml);
                __showBauteile();
                __showHersteller();
               __showBaujahre();
            });
        });
        
        //::: SELECTS BOXEN COOKIES :::::::::::::::::::::::::::::::::::::::::::::::::
        function __readCookie(cookieName, box){
            if ($.cookie(cookieName) != null) {
                $("#" + box + " option").eq($.cookie(cookieName)).attr('selected', 'selected');
            }
            setSelects("#" + box);
        }
        
        function __setCookie(cookieName, val){
            $.cookie(cookieName, val, {
                expires: 7
            });
        }
        
        $("#box1").change(function(){
            __setCookie('konfi_box1', $('#box1 :selected').index());
        });
        $("#box2").change(function(){
            __setCookie('konfi_box2', $('#box2 :selected').index());
        });
        $("#box2a").change(function(){
            __setCookie('konfi_box2a', $('#box2a :selected').index());
        });
        $("#box3").change(function(){
            __setCookie('konfi_box3', $('#box3 :selected').index());
        });
        
        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        
        function __showBauteile(){
        
            $('#box3').html("");
            $.each(jsonObj.kategorie, function(i, item){
                $.each(item, function(i, item2){
                    //$('<option value="' + item2.catId + '"></option>').html(item2.name).appendTo('#box3');
                    $('<option value="' + item2.name + '"></option>').html(item2.name).appendTo('#box3');
                });
            });
            
            //::: SetBox3 by Cookie :::::::::::::::::::::::::::::
            __readCookie('konfi_box3', 'box3');
            //:::::::::::::::::::::::::::::::::::::::::::::::::::

        }
        
        function __showHersteller(){
        
            $('#box1').html("");
            var ii = 0;
            $.each(jsonObj.hersteller, function(i, item){
                hersteller_name = item.name;
                var selected = ii <= 0 ? 'selected="selected"' : '';
                if (ii <= 0) {
                    startHersteller = hersteller_name;
                }
                $('<option value="' + hersteller_name + '" ' + selected + '></option>').html(hersteller_name).appendTo('#box1');
                ii++;
            });
            
            //::: SetBox1 by Cookie :::::::::::::::::::::::::::::
            __readCookie('konfi_box1', 'box1');
            //:::::::::::::::::::::::::::::::::::::::::::::::::::
            
            $('#box1').change(function(){
                __setCookie('konfi_box2', 0);
                __showModelle($('#box1 :selected').val());
                $("#box2").trigger("change");
            });
            __showModelle($('#box1 :selected').val());
        }
       
        function __showModelle(hersteller){
            $('#box2').html("");
            var ii = 0;
            $.each(jsonObj.hersteller, function(i, item){
                $.each(item, function(i, item2){
                    if (item2 == hersteller) {
                        $.each(item.modell, function(i, item3){
                            var selected = ii <= 0 ? 'selected="selected"' : '';
                            if (typeof(item3.modell) != 'undefined') {
                                $('<option value="' + item3.modell.name + '" ' + selected + '></option>').html(" " + item3.modell.name).appendTo('#box2');
                            }
                            else {
                                $('<option value="' + item3.name + '" ' + selected + '></option>').html(" " + item3.name).appendTo('#box2');
                            }
                            ii++;
                        });
                    }
                });
            });

            //::: SetBox2 by Cookie :::::::::::::::::::::::::::::
            __readCookie('konfi_box2', 'box2');
            //:::::::::::::::::::::::::::::::::::::::::::::::::::
            
            $("#box2").change(function(){
            	$('#box2a :selected').removeAttr("selected");
            	 __setCookie('konfi_box2a', 0);
            	 __showBaujahre();
            });
        }
        
        //Baujahre
        function __showBaujahre(){
	    $('#box2a').html("");	
            $.each(jsonObj.bj, function(i, item){
                $.each(item, function(i, item4){
                    $('<option value="' + item4.name + '"></option>').html(item4.name).appendTo('#box2a');
                });
            });
            
            //::: SetBox2a by Cookie :::::::::::::::::::::::::::::
            __readCookie('konfi_box2a', 'box2a');
            //:::::::::::::::::::::::::::::::::::::::::::::::::::

	}
        
        function sendSearchString(){
            //var param = 'c1=0&c2=0&c3=0&c4=0&c5=0&ActionCall=WebActionArticleSearch&Params[SearchInDescription]=1&Params[SearchParam]=' + $("#box1").val() + ' ' + $("#box2").val() + ' ' + $("#box2a").val() + '&Params[Cat]=' + $("#box3").val() + '&x=0&y=0';
            var param = 'c1=0&c2=0&c3=0&c4=0&c5=0&ActionCall=WebActionArticleSearch&Params[SearchInDescription]=1&Params[SearchParam]=' + $("#box1").val() + ' ' + $("#box2").val() + ' ' + $("#box2a").val() + ' ' + $("#box3").val() + '&x=0&y=0';

            document.location.href = 'http://' + window.location.host + '/index.php?' + param;
            return false;
        }
        
        $("#send").click(function(e){
            sendSearchString();
            e.preventDefault();
        });

    });
});


