// =========================================
// UPDATE IMAGEBANK MENUS CLASS
// =========================================
// Actually updates the $('menus') element.
// Populates with folders.

var updateDropDowns    = 
{
    pageNumber : 1,
    request: function(url, parentId)
    {
        this.parentId        = parentId;
        this.loading();
        var parentObject    = this;    
        if (!this.cleanUrl)
            this.cleanUrl        = url;    
        var myXHR = new XHR({
            method: 'get',
            onSuccess: function() {
                eval("parentObject.init("+this.response.text+");");
            }
        }).send(url);
        if ($('debug'))
            $('debug').innerHTML    = url;
    },
    
    init: function(jsonElement)
    {
        this.jsonElement    = jsonElement;
        this.pageNumber     = (this.jsonElement.get_vars.page) ? this.jsonElement.get_vars.page : 1;
        this.years            = (this.jsonElement.years.length > 0) ? this.jsonElement.years : false;
        this.filters        = (this.jsonElement.filters.length > 0) ? this.jsonElement.filters : false;
        this.categories        = (this.jsonElement.categories.length > 0) ? this.jsonElement.categories : false;
        this.yearVal        = (this.yearVal) ? this.yearVal : this.jsonElement.years[0].id;
        this.filterVal        = (this.filterVal) ? this.filterVal : this.jsonElement.filters[0].id;
        if (this.jsonElement.categories.length > 0 && !this.categoryVal)
        {
            this.categoryVal = this.jsonElement.categories[0].id;
        }
        this.categoryVal    = (this.categoryVal) ? this.categoryVal : false;
        this.createSelects();
        this.updateYears();
        this.updateFilters();
        this.updateCategories();
        this.finishedLoading();
        this.requestImages();
    },
    
    loading: function()
    {
        if (!this.parentElement)
            this.parentElement    = (this.parentId && $(this.parentId)) ? $(this.parentId) : document.getElementsByTagName('body')[0]; 
        if (!this.loadingElement)
        {
            this.loadingElement    = document.createElement('div');
            this.loadingElement.className    = 'loading';
            this.loadingElement.appendChild(document.createTextNode('Loading Menus...'));
            this.loadingElement.style.display    = "none";
            this.parentElement.appendChild(this.loadingElement);
        }
        this.loadingElement.style.display    = "block";
        if (this.menuElement)
            this.menuElement.style.display    = "none";
    },
    
    finishedLoading: function() 
    {
        this.menuElement.style.display        = "block";
        this.loadingElement.style.display    = "none";
    },
    
    createSelects: function()
    {
        var selectIds    = new Array('year', 'filter', 'category');
        if (!this.menuElement) 
        {
            this.menuElement    = document.createElement('div');
            this.menuElement.id             = "filterMenu";
            this.parentElement.appendChild(this.menuElement);
        }
        for (i=0; i < selectIds.length; i++)
        {
            if (!$(selectIds[i])) 
            {
                newSelect                        = document.createElement('select');
                newSelect.name, newSelect.id    = selectIds[i]
                this.menuElement.appendChild(newSelect);;
                
                parentSelectObject                    = this;
                $(selectIds[i]).addEvent('change', function() {
                    parentSelectObject.retrieveNewValues();
                });
            }
        }
        if (!this.searchElement)
        {
            this.searchElement            = document.createElement('input');
            this.searchElement.type        = "text";
            this.searchElement.value    = "search here...";
            this.searchElement.id        = "image-search";
            this.menuElement.appendChild(this.searchElement);
            this.searchElement            = $('image-search');
            this.searchElement.addEvent("click", function() {
                this.value    = "";
                this.removeEvents("click");
                
            });
            parentObject        = this;
            this.searchElement.style.display    = "none";
            this.searchElement.addEvent('keyup', function() {
                parentObject.startTimer    = new Date().getTime();                setTimeout(function() {                    currentTime = new Date().getTime();                    if ((currentTime - parentObject.startTimer) > 375) {
                        parentObject.searchString   = $('image-search').value.replace(/[^a-z^A-Z^0-9]/, "");                        parentObject.pageNumber     = 1;
                        parentObject.oldPageNumber  = 1;
                        parentObject.requestImages();
                    } else {
                        return;                    }
                }, 400);
            });
        }
    },
    
    updateYears: function()
    {
        yearsList            = $('year');
        yearsSelectedIndex    = 0
        while (yearsList.hasChildNodes())
            yearsList.removeChild(yearsList.firstChild);
        for (i=0; i<this.years.length; i++)
        {
            if (this.years[i])
            {
                yearListItem            = document.createElement('option');
                yearListItem.value        = this.years[i].id;
                if (this.years[i].id == this.yearVal)
                    yearsSelectedIndex    = i;
                yearListItem.appendChild(document.createTextNode(this.years[i].year));
                yearsList.appendChild(yearListItem);
            }
        }
        $('year').selectedIndex    = yearsSelectedIndex;
    },
    
    updateFilters: function()
    {
        filtersList                = $('filter');
        filtersSelectedIndex    = 0;
        while (filtersList.hasChildNodes())
            filtersList.removeChild(filtersList.firstChild);
        for (i=0; i<this.filters.length; i++)
        {
            if (this.filters[i])
            {
                filterListItem                = document.createElement('option');
                filterListItem.value        = this.filters[i].id;
                if (this.filters[i].id == this.filterVal)
                    filtersSelectedIndex    = i;
                filterListItem.appendChild(document.createTextNode(this.filters[i].filter));
                filtersList.appendChild(filterListItem);
            }
        }
        $('filter').selectedIndex    = filtersSelectedIndex;
    },
    
    updateCategories: function()
    {
        categoriesList            = $('category');
        categoriesSelectedIndex    = 0;
        while (categoriesList.hasChildNodes())
            categoriesList.removeChild(categoriesList.firstChild);
        for (i=0; i<this.categories.length; i++)
        {
            if (this.categories[i])
            {
                categoryListItem                = document.createElement('option');
                categoryListItem.value            = this.categories[i].id;
                if (this.categories[i].id == this.categoryVal)
                    categoriesSelectedIndex    = i;
                categoryListItem.appendChild(document.createTextNode(this.categories[i].category));
                categoriesList.appendChild(categoryListItem);
            }
        }
        $('category').selectedIndex    = categoriesSelectedIndex;
    },
    
    retrieveNewValues: function()
    {
        resetArray    = new Array();
        if ($('year').value != this.yearVal)
        {
            resetArray[resetArray.length]    = 'filterVal';
            resetArray[resetArray.length]    = 'categoryVal';
        }
        if ($('filter').value != this.filterVal)
            resetArray[resetArray.length]    = 'categoryVal';
            
        if (resetArray || ($('category').value != this.categoryVal))
            this.pageNumber = 1;
            
        this.yearVal        = ($('year').value) ? $('year').value : false;
        this.filterVal        = ($('filter')) ? $('filter').value : false;
        this.categoryVal    = ($('category')) ? $('category').value : false;
        
        for (i=0; i<resetArray.length; i++)
            this[resetArray[i]]            = false;
            
        ajaxURL                = this.cleanUrl+"?";
        ajaxURL                += (this.yearVal) ? "year="+this.yearVal+"&" : "";
        ajaxURL                += (this.filterVal) ? "filter="+this.filterVal+"&" : "";
        ajaxURL                += (this.categoryVal) ? "category="+this.categoryVal : "";
        
        if (resetArray && (resetArray.length > 0))
        {
            this.request(ajaxURL, this.parentId);
        } else {
            this.requestImages();
        }
        
    },
    
    requestImages : function()
    {        
        imageRequestUrl     = "http://icpc.baylor.edu/dmt/v2/ImageBank/images.asp?";
        imageRequestUrl        += (this.searchString) ? "search="+escape(this.searchString)+"&" : "";
        imageRequestUrl        += (this.yearVal) ? "year="+this.yearVal+"&" : "";
        imageRequestUrl        += (this.filterVal) ? "filter="+this.filterVal+"&" : "";
        imageRequestUrl        += (this.categoryVal) ? "category="+this.categoryVal+"&" : "";
        if (!this.imageRequest)
            this.imageRequest        = updateImages;
        this.imageRequest.pageNumber = (this.pageNumber) ? this.pageNumber : 1;
        this.imageRequest.request(imageRequestUrl, 'images', 'image-menus');
    }
}
