var FilteringMgr = function() {
  this.init();
}
FilteringMgr.prototype = {
  init: function() {
    this.initYearFiltering();
    this.initEventTypeFiltering();
    this.initNewsFiltering();
  },
  
  initYearFiltering: function() {
    YAHOO.util.Dom.getElementsByClassName("year_filtering", "ul", false, function(s) {
      new YearFiltering(s);
    }); 
    
  },
  
  initEventTypeFiltering: function() {
    YAHOO.util.Dom.getElementsByClassName("event_type_filtering", "ul", false, function(s) {
      new EventTypeFiltering(s);
    });
  },
  initNewsFiltering: function() {  
    YAHOO.util.Dom.getElementsByClassName("news_releases_filtering", "ul", false, function(s) {
        new NewsFiltering(s);
    });
  }
  
}
var Filtering = function() {
    this.init();
}
Filtering.prototype = {
    convertUnorderedToSelect: function(unorderedEl, noChange) {
        var itemEls, linkEl;
        
        itemEls = YAHOO.util.Dom.getChildrenBy(unorderedEl, function(c) {
            return c.nodeName == "LI";    
        });        
        
        var i;
        var selectEl, optionEl;
        
        selectEl = document.createElement("select");
        if(!noChange) {
            YAHOO.util.Event.on(selectEl, "change", function() {
                window.location = selectEl.options[selectEl.selectedIndex].value;
            });    
        }
        
        for(i = 0; i < itemEls.length; i++) {
            linkEl = itemEls[i].getElementsByTagName("a")[0];
            
            if (linkEl) {
                optionEl = document.createElement("option");
                optionEl.value = linkEl.href;
                optionEl.innerHTML = linkEl.innerHTML;
                if(linkEl.href == window.location.href) {
                    optionEl.selected = "SELECTED";
                }
                
                selectEl.appendChild(optionEl);
            }
        }
        
        unorderedEl.parentNode.appendChild(selectEl);
        unorderedEl.parentNode.removeChild(unorderedEl);
        
        return selectEl;
    }
    
}
var EquipmentFiltering = function(el) {
    this.init(el);
} 
YAHOO.extend(EquipmentFiltering, Filtering);
EquipmentFiltering.prototype.init = function(el) {
    var el = YAHOO.util.Dom.get(el);
    this.convertUnorderedToSelect(el);
}
var QuarterlyReleaseFiltering = function(listEl) {
    this.init(listEl);
} 
YAHOO.extend(QuarterlyReleaseFiltering, Filtering);
QuarterlyReleaseFiltering.prototype.init = function(listEl) {
    var listEl = YAHOO.util.Dom.get(listEl);
    var tableEl, tbodyEl, trEl, tdEl;
    
    tableEl = document.createElement("table");
    listEl.parentNode.appendChild(tableEl);
    
    tbodyEl = document.createElement("tbody");
    tableEl.appendChild(tbodyEl);
    
    trEl = document.createElement("tr");
    tbodyEl.appendChild(trEl);
    
    tdEl = document.createElement("td");
    tdEl.appendChild(this.convertUnorderedToSelect(listEl));
    trEl.appendChild(tdEl);
    
    tdEl = document.createElement("td");
    trEl.appendChild(tdEl);                
}
var NewsFiltering = function(el) {
    this.init(el);
}
NewsFiltering.prototype = {
    init: function(el) {
      var el = YAHOO.util.Dom.get(el);
      var children, nested = false;
      
      children = YAHOO.util.Dom.getChildrenBy(el, function(c) {
        return c.nodeName == "LI";
      });
            
      nested = false;
      for(var i = 0; i < children.length && !nested; i++) {
        if (children[i].getElementsByTagName("UL").length > 0) {
            nested = true;
        }
      }
      
      if (nested) {
        new YearQuarterFiltering(el);
      }
      else {
        new YearOnlyFiltering(el);
      }
    }
}
var YearQuarterFiltering = function(el) {
    this.init(el);
}
YearQuarterFiltering.prototype = {
    
    init: function(el) {    
        var el = YAHOO.util.Dom.get(el);
        var pages = [];
        var years = YAHOO.util.Dom.getChildrenBy(el, function(c) {
            return c.nodeName == "LI";    
        });
        var quarters;
        var that = this;
        
        var year, quarter;
        for(var i = 0; i < years.length; i++) {            
            year = years[i].childNodes[0].nodeValue;
            if (year.substring(year.length - 2) == ": ") {
                year = year.substr(0, year.length - 2);
            }
            
            pages[pages.length] = [year, []];
            
            quarters = years[i].getElementsByTagName("LI");
            for(var j = 0; j < quarters.length; j++) {
                quarter = quarters[j].childNodes[0];
                pages[pages.length - 1][1][j] = [quarter.childNodes[0].nodeValue, quarter.href];            
            }
            
        }        
        
        this.getPages = function() {return pages;};
                
        var table = document.createElement("table");
        YAHOO.util.Dom.addClass(table, "filter");
        YAHOO.util.Dom.insertBefore(table, el);
        var tbody = document.createElement("tbody");
        table.appendChild(tbody);
        var tr = document.createElement("tr");
        tbody.appendChild(tr);        
        
        var td = document.createElement("td");
        tr.appendChild(td);
        
        var yearsSelect = document.createElement("select");
        td.appendChild(yearsSelect);
        YAHOO.util.Event.on(yearsSelect, "change", function() {
            that.populateQuarters();
        });
        this.getYearsSelect = function() {return yearsSelect;};
        
        for(var i = 0; i < pages.length; i++) {
            var option = document.createElement("option");
            option.value = pages[i][0];
            option.text = pages[i][0];
            yearsSelect.options[yearsSelect.options.length] = option;
        }
        
        var td = document.createElement("td");
        tr.appendChild(td);
        
        var quartersSelect = document.createElement("select");
        td.appendChild(quartersSelect);
        this.getQuartersSelect = function() {return quartersSelect;};
    
        this.populateQuarters();
        
        var td = document.createElement("td");
        tr.appendChild(td);
        
        var input = document.createElement("input");
        input.type = "image";
        input.src = lang.viewButtonImg;
        input.alt = lang.viewButtonAlt;
        YAHOO.util.Event.on(input, "click", function() {
            window.location = quartersSelect.options[quartersSelect.selectedIndex].value;
        });
        
        td.appendChild(input);
            
        yearsLoop: for(var i = 0; i < pages.length; i++) {    
            for(var j = 0; j < pages[i][1].length; j++) {
                if(window.location.href.indexOf(pages[i][1][j][1]) != -1) {
                    break yearsLoop;
                }
            }
        }
                
        if(i < pages.length && j < pages[i][1].length) {
            this.getYearsSelect().selectedIndex = i;
            this.populateQuarters();
            this.getQuartersSelect().selectedIndex = j;
        }
                
        el.parentNode.removeChild(el);
    },
    
    populateQuarters: function() {    
        while(this.getQuartersSelect().length) {
            this.getQuartersSelect().remove(0);
        }
        
        var year = this.getYearsSelect().options[this.getYearsSelect().selectedIndex].value;
        var yearIndex = -1;
        for(var i = 0; i < this.getPages().length && yearIndex == -1; i++) {
            if (this.getPages()[i][0] == year) {
                yearIndex = i;
            }
        }
                
        for(var i = 0; i < this.getPages()[yearIndex][1].length; i++) {
            var option = document.createElement("option");
            option.value = this.getPages()[yearIndex][1][i][1];
            option.text = this.getPages()[yearIndex][1][i][0];
            
            this.getQuartersSelect().options[this.getQuartersSelect().options.length] = option;
        }
        
    }
    
}
var YearOnlyFiltering = function(listEl) {
    this.init(listEl);
} 
YAHOO.extend(YearOnlyFiltering, Filtering);
YearOnlyFiltering.prototype.init = function(listEl) {    
    var listEl = YAHOO.util.Dom.get(listEl);
    var tableEl, tbodyEl, trEl, tdEl, input;
    
    tableEl = document.createElement("table");
    YAHOO.util.Dom.addClass(tableEl, "filter");
    listEl.parentNode.appendChild(tableEl);
    
    tbodyEl = document.createElement("tbody");
    tableEl.appendChild(tbodyEl);
    
    trEl = document.createElement("tr");
    tbodyEl.appendChild(trEl);
    
    var selectEl = this.convertUnorderedToSelect(listEl, true);
    
    tdEl = document.createElement("td");
    tdEl.appendChild(selectEl);
    trEl.appendChild(tdEl);
    
    tdEl = document.createElement("td");
    trEl.appendChild(tdEl);    
    
    input = document.createElement("input");
    input.type = "image";
    input.src = lang.viewButtonImg;
    input.alt = lang.viewButtonAlt;
    YAHOO.util.Event.on(input, "click", function() {
        if (selectEl.options[selectEl.selectedIndex].value.indexOf(".pdf") != -1) {    
            var popup = window.open(selectEl.options[selectEl.selectedIndex].value, YAHOO.util.Dom.generateId(null, "pdf_"), "menubar=1,toolbar=1,location=1,status=1,scrollbars=1,resizable=1");
            popup.focus();
            YAHOO.util.Event.on(popup, "unload", function(e) {
                window.focus();
            });            
        }
        else {
            window.location = selectEl.options[selectEl.selectedIndex].value;
        }
    });    
    tdEl.appendChild(input);
}
var MeasuresAndCarloadsFiltering = function(el) {
    this.init(el);
}
MeasuresAndCarloadsFiltering.prototype = {
    
    init: function(el) {
        var el = YAHOO.util.Dom.get(el);
        
        this.getEl = function() {return el;};
        
        this.loadDocuments();
        this.createDropdowns();
        this.addListeners();
        el.parentNode.removeChild(el);
    },
    
    loadDocuments: function() {
        var documents = [], years = [], weeks = [];
        
        var yearsEls, quartersEls, weeksEls;
        var i, j, k;
        
        yearsEls = YAHOO.util.Dom.getChildrenBy(this.getEl(), function(c) {
            return c.nodeName == "LI";
        });
        
        for(i = 0; i < yearsEls.length; i++) {
            years[i] = yearsEls[i].innerHTML.replace(/^\s+/,"").substr(0, 4);
            weeks[i] = [];
            documents[i] = [];
            
            quartersEls = yearsEls[i].getElementsByTagName("li");            
            for(j = 0; j < quartersEls.length; j++) {                
                weeksEls = quartersEls[j].getElementsByTagName("a");
                for(k = 0; k < weeksEls.length; k++) {
                    weeks[i][weeks[i].length] = weeksEls[k].getElementsByTagName("abbr")[0].title;
                    documents[i][documents[i].length] = weeksEls[k].href;
                }
            }
            
        }
        
        this.getDocuments = function() {return documents;};
        this.getYears = function() {return years;};
        this.getWeeks = function() {return weeks;};
    },
    
    createDropdowns: function() {
        var tableEl, tbodyEl, trEl, tdEl, labelEl, viewEl;
        var yearsEl, yearEl;
        var weeksEl, weekEl;
        var i;
                
        tableEl = document.createElement("table");
        YAHOO.util.Dom.addClass(tableEl, "filter");
        YAHOO.util.Dom.insertAfter(tableEl, this.getEl());
        tbodyEl = document.createElement("tbody");
        tableEl.appendChild(tbodyEl);
                
        trEl = document.createElement("tr");
        tbodyEl.appendChild(trEl);
        
        tdEl = document.createElement("td");
        trEl.appendChild(tdEl);
        
        labelEl = document.createElement("label");
        labelEl.htmlFor = this.getEl().id + "_key_year";
        labelEl.innerHTML = lang.archiveTxt + ":";
        tdEl.appendChild(labelEl);
        
        tdEl = document.createElement("td");
        trEl.appendChild(tdEl);
        
        yearsEl = document.createElement("select");
        yearsEl.id = this.getEl().id + "_key_year";
        tdEl.appendChild(yearsEl);
        this.getYearsEl = function() {return yearsEl;};
        for(i = 0; i < this.getYears().length; i++) {
            yearEl = document.createElement("option");
            yearEl.innerHTML = this.getYears()[i];
            yearsEl.appendChild(yearEl);
        }
        tdEl = document.createElement("td");
        trEl.appendChild(tdEl);
        
        weeksEl = document.createElement("select");
        weeksEl.id = this.getEl().id + "_key_week";
        tdEl.appendChild(weeksEl);
        this.getWeeksEl = function() {return weeksEl;};
        this.populateWeeks();
        
        tdEl = document.createElement("td");
        trEl.appendChild(tdEl);
        viewEl = document.createElement("input");
        viewEl.type = "image";
        viewEl.src = lang.viewButtonImg;
        viewEl.alt = lang.viewButtonAlt;
        tdEl.appendChild(viewEl);
        
        this.getViewEl = function() {return viewEl;};        
    },
    
    addListeners: function() {
        var that = this;
        YAHOO.util.Event.on(this.getYearsEl(), "change", function() {
            that.populateWeeks();
        });
        
        YAHOO.util.Event.on(this.getViewEl(), "click", function() {
            window.location.href = that.getWeeksEl().options[that.getWeeksEl().selectedIndex].value;
        });        
    },
    
    populateWeeks: function() {
        var yearIndex = this.getYearsEl().selectedIndex;
        var weeksEl = this.getWeeksEl(), weekEl;
        var i;
                                
        weeksEl.innerHTML = "";
        for(i = 0; i < this.getWeeks()[yearIndex].length; i++) {
            weekEl = document.createElement("option");
            weekEl.value = this.getDocuments()[yearIndex][i];
            weekEl.innerHTML = this.getWeeks()[yearIndex][i];
            weeksEl.appendChild(weekEl);
        }
        
    }
    
}
var YearFiltering = function(el) {
    this.init(el);
} 
YAHOO.extend(YearFiltering, Filtering);
YearFiltering.prototype.init = function(el) {
    var el = YAHOO.util.Dom.get(el);
    this.convertUnorderedToSelect(el);
}
var TabFiltering = function(el) {
    this.init(el);
}
var EventTypeFiltering = function(el) {
    this.init(el);
} 
YAHOO.extend(EventTypeFiltering, Filtering);
EventTypeFiltering.prototype.init = function(el) {
    var el = YAHOO.util.Dom.get(el);
    this.convertUnorderedToSelect(el);
}
var TabFiltering = function(el) {
    this.init(el);
}
YAHOO.extend(TabFiltering, Filtering);
TabFiltering.prototype.init = function(el) {
  var el = YAHOO.util.Dom.get(el);
  var headerEl = el.getElementsByTagName("h4")[0];
  var listEl = el.getElementsByTagName("ul")[0];
  var tableEl, tbodyEl, trEl, tdEl;
  var labelEl;
  
  tableEl = document.createElement("table");
  YAHOO.util.Dom.insertBefore(tableEl, listEl);
  
  tbodyEl = document.createElement("tbody");
  tableEl.appendChild(tbodyEl);
  
  trEl = document.createElement("tr");
  tbodyEl.appendChild(trEl);
  
  tdEl = document.createElement("td");
  trEl.appendChild(tdEl);
  labelEl = document.createElement("label");
  labelEl.htmlFor = "event_types_select";
  labelEl.innerHTML = headerEl.innerHTML + ":";
  tdEl.appendChild(labelEl);
  
  tdEl = document.createElement("td");
  trEl.appendChild(tdEl);
  
  tdEl.appendChild(listEl);
          
  YAHOO.util.Dom.addClass(el, "js");    
  el.removeChild(headerEl);
  this.convertUnorderedToSelect(listEl);
  YAHOO.util.Dom.setStyle(el.parentNode, "position", "relative");
  YAHOO.util.Dom.setStyle(el.parentNode, "border-top", "1px solid #fff");
}
 
new FilteringMgr();
