var IS_EXPLORER = false;
if (navigator.userAgent.indexOf('MSIE') !=-1) {
        IS_EXPLORER=true;
}

function showLatestArticles() {
        
        // Lets get all ULs for the page
        var candidates = document.getElementsByTagName('ul');
        
        for(var i=0;i<candidates.length;i++) {
                
                // Check if the is a newsTree present
                if(candidates[i].id && candidates[i].id.indexOf('newsTree') != -1) {
                        
                        var tree = candidates[i];
                        // Now sent it to the showLastYear funcion, where it will determine the last year to display
                        showLastYear(tree);
                }
        }
}

function showLastYear (tree) {
        flag = false;
        
        //Non Ie browsers has different ways of finding a dom elements last child
        if (!IS_EXPLORER){
                
                var years = [];
                var cnt = 0;
                
                //loop through all the tree's childnodes
                for(var i=0;i<tree.childNodes.length;i++) {
                                
                                // Check if the is a LI present ie, if there is a year
                                if(tree.childNodes[i].tagName && tree.childNodes[i].tagName.indexOf('LI') != -1) {
                                        
                                        // add all the years to an array
                                        years[cnt] = tree.childNodes[i];
                                        cnt++;
                                }
                }
                
                //now we have there last year in the tree
                var lastYear = years[(years.length-1)];
                
        } else { //Ie browsers has different ways of finding a dom elements last child
                
                //now we get the last year in the tree
                var lastYear = tree.lastChild;
                
        }
                
        //loop through all the child nodes to find the ul
        for(var i=0;i<lastYear.childNodes.length;i++) {
                
                // Check if the is a UL present
                if(lastYear.childNodes[i].tagName && lastYear.childNodes[i].tagName.indexOf('UL') != -1) {
                        
                        // now we have found a the UL of all the months for the last year
                        lastYearsMonthsTree = lastYear.childNodes[i];
                        //set the flag to inndicate that we have found a the UL of all the months for the last year
                        flag = true;
                }
        }
        
        if (flag) {
                //lets display and all the months for this year
                lastYearsMonthsTree.style.display = "block";
        
                // Now sent the active year to the showLastMonth funcion, where it will determine the last month to display
                showLastMonth(lastYearsMonthsTree);
        }
}

function showLastMonth (activeMonthsTree) {
        //Ie and other browsers has different ways of finding a dom elements last child
        if (IS_EXPLORER){
                //now we get the last year in the tree
                var allMonths = activeMonthsTree.lastChild;
                
                //loop through all the child nodes to find the ul
                for(var i=0;i<allMonths.childNodes.length;i++) {
                        
                        // Check if the is a UL present
                        if(allMonths.childNodes[i].tagName && allMonths.childNodes[i].tagName.indexOf('UL') != -1) {
                                activeMonth = allMonths.childNodes[i];
                        }
                }
        } else {
                //now we get the last month in the tree
                var activeMonth = activeMonthsTree.select("li:last-child ul")[0];
        }
        
        //lets display this year
        activeMonth.style.display = "block";
        
}

function changeMonth (obj) {
        
        var newMonth = obj.parentNode; // This is the LI of the month we want to display
        var months = newMonth.parentNode; // This is the UL of the all he months we need to hide before displaying the month we want to display ie. the month's siblings
        var monthsToClose = [];
        var cnt = 0;
        
        //loop through all this nodes siblings
        for(var i=0;i<months.childNodes.length;i++) {
                        
                        // Check if the is a UL present
                        if(months.childNodes[i].tagName && months.childNodes[i].tagName.indexOf('LI') != -1) {
                                
                                // add all the months o an array
                                monthsToClose[cnt] = months.childNodes[i];
                                cnt++;
                        }
        }
        
        //lop through all the month/monthsArray so we can hide them
        for(var i=0;i<monthsToClose.length;i++) {
                
                //loop through the months to find the UL to hide, this will hide the months
                for(var t=0;t<monthsToClose[i].childNodes.length;t++) {
                        
                        if(monthsToClose[i].childNodes[t].tagName && monthsToClose[i].childNodes[t].tagName.indexOf('UL') != -1) {
                                
                                //hide this month
                                monthsToClose[i].childNodes[t].style.display = "none";
                        }
                }
        }
        
        //Loop throught the month we want to display, to find the ul to show, this will display the month we want to show
        for(var i=0;i<newMonth.childNodes.length;i++) {
                        
                // Check if the is a UL present
                if(newMonth.childNodes[i].tagName && newMonth.childNodes[i].tagName.indexOf('UL') != -1) {
                        //finally display the month we wand to show
                        activeMonth = newMonth.childNodes[i];
                        activeMonth.style.display = "block";
                }
        }
}

function changeYear (obj) {
        //logic still to come
        var newYear = obj.parentNode; // This is the LI of the month we want to display
        var years = newYear.parentNode; // This is the UL of the all he years we need to hide before displaying the year we want to display ie. the month's siblings
        var yearsToClose = [];
        var cnt = 0;
        
        //loop through all this nodes siblings
        for(var i=0;i<years.childNodes.length;i++) {
                        
                        // Check if the is a UL present
                        if(years.childNodes[i].tagName && years.childNodes[i].tagName.indexOf('LI') != -1) {
                                
                                // add all the months o an array
                                yearsToClose[cnt] = years.childNodes[i];
                                cnt++;
                        }
        }
        
        //lop through all the month/monthsArray so we can hide them
        for(var i=0;i<yearsToClose.length;i++) {
                
                //loop through the months to find the UL to hide, this will hide the months
                for(var t=0;t<yearsToClose[i].childNodes.length;t++) {
                        
                        if(yearsToClose[i].childNodes[t].tagName && yearsToClose[i].childNodes[t].tagName.indexOf('UL') != -1) {
                                
                                //hide this month
                                yearsToClose[i].childNodes[t].style.display = "none";
                        }
                }
        }
        
        //Loop throught the month we want to display, to find the ul to show, this will display the month we want to show
        for(var i=0;i<newYear.childNodes.length;i++) {
                        
                // Check if the is a UL present
                if(newYear.childNodes[i].tagName && newYear.childNodes[i].tagName.indexOf('UL') != -1) {
                        
                        //finally display the month we wand to show
                        activeYear = newYear.childNodes[i];
                        activeYear.style.display = "block";
                        
                        // Now sent the active year to the showLastMonth funcion, where it will determine the last month to display
                        showLastMonth(activeYear);
                }
        }
}