jQuery(document).ready(function() {
        /*remove duplicate entries from a passed array*/
        function dme(a1){ // delete multiple entries (array)
            var a2 = new Array();
            var a3 = new Array();
            for(var i = 0; i < a1.length; i++){
                if(typeof(a2[a1[i]]) == "undefined"){
                    a2[a1[i]] = true;
                    a3[a3.length] = a1[i];
                }
            }
            var a4 = new Array();
            for(var i = 0; i < a3.length; i++){
                a4[a4.length] = a3[i]
            }
            return a4;
        }
        
        /*depends on the global array a new entry (year month link)*/
        function updateGlobalArray(entryYear, entryMonth, entryHref){
            var newEntry = new Array(entryYear, entryMonth, entryHref);
            globalArray = globalArray.concat(newEntry);
        }
        
        /*creates an array with years as the content*/
        function makeArrayOfYears(){
            var globalArrLength = globalArray.length;
            for(i=0; i < globalArrLength; i+=3){           
               yearsArray.push(globalArray[i])
            }
            yearsArray = dme(yearsArray);
        }
        
        /*created the dropdown elements for years or months, depending on which option was set*/
        function getSelectBox(option, selectetYear){
            var html = '';
            if(option){
                makeArrayOfYears();
                html += '<select class="select-year"><option>Jahr</option>';
                for(var i=0; i < yearsArray.length; i++){           
                    html += '<option>'+yearsArray[i]+'</option>';
                }
                html += '</select>';
            }else{
                html += '<select class="select-month"><option>Monat</option>';
                for(var j=0; j < globalArray.length; j++){
                    if(globalArray[j] == selectetYear){
                        html += '<option>'+globalArray[j+1]+'</option>';
                    }
                }
                html += '</select>';
            }
            return html;
        }
        
        /*the old month selection is replaced by a new*/
        function constructNewMonth(){
            var selectetYear = jQuery('.select-year option:selected').text();
            var selectMonth;
            if(selectetYear == 'Jahr'){
                selectMonth = '<select class="select-month"><option>Monat</option></select>';
                
            }else{
                selectMonth = getSelectBox(0, selectetYear);
            }
            jQuery('.select-month-container').html(selectMonth);
        }
        
        var yearsArray = new Array();
        var globalArray = new Array();
        var menueContainer = jQuery('.news-amenu-container');
        
        if(menueContainer){
            menueContainer.hide();
            var entry    = jQuery('.news-amenu-container .news-amenu-entry');
            var year     = jQuery('.news-amenu-container .year');
            var month  = jQuery('.news-amenu-container .month');
            
             /*build an global erray with all years, month and hrefs*/
            jQuery.each(entry, function() {
                var entryYear    = jQuery(this).find('.year').text();
                var entryMonth = jQuery(this).find('.month').text();
                var entryHref     = jQuery(this).find('a').attr('href');
                updateGlobalArray(entryYear, entryMonth, entryHref);
            });  
            
            /*initial construct for year and month*/
            var selectYear =  getSelectBox(1);
            var selectMonth = '<select class="select-month"><option>Monat</option></select>';
            
            jQuery('.select-year-container').html(selectYear);
            jQuery('.select-month-container').html(selectMonth);
            
            /*selects the appropriate part of the following year and the month before*/
            var loadedYear    = jQuery('.amenu-act .year').text();
            var yearOption    = jQuery('.select-year option');

            if(loadedYear){//if a selection of year and month was done
                jQuery.each(yearOption, function() {
                    if(loadedYear == jQuery(this).text()){
                        jQuery(this).attr('selected',true);
                    }
                });
                constructNewMonth();//after the replacement of the old selectbox, the eventhandler get a new binding
                jQuery('.select-month').change(monthChangeHandler);
                
                var loadedMonth = jQuery('.amenu-act .month').text();
                var monthOption = jQuery('.select-month option');
                jQuery.each(monthOption, function() {
                    if(loadedMonth == jQuery(this).text()){
                        jQuery(this).attr('selected',true);
                    }
                });
            }
        }
        
        /*change the selected option in year drop-down*/
        jQuery('.select-year').change(function() {
            constructNewMonth();
            jQuery('.select-month').change(monthChangeHandler);
        });
        
        jQuery('.select-month').change(monthChangeHandler);
        
        /*change the selected option in month drop-down*/
        function monthChangeHandler(){
            var selectetYear    = jQuery('.select-year option:selected').text();
            var selectedMonth = jQuery('.select-month option:selected').text();
            var thebase = document.getElementsByTagName("base"); 
            var baseHref = thebase[0].href;
            
            if(selectedMonth != 'Monat'){
                for(var i=0; i < globalArray.length; i++){
                    if(globalArray[i] == selectetYear && globalArray[i+1] == selectedMonth){
                        jQuery('#loadingdiv').show();
                        window.location.href = baseHref + globalArray[i+2] + '#amenujump';
                        return false;
                    }
                }
            }
        }
});
