var faq = {
    container: null,
    headings: null,
    heights: [],
    last_index: null,
    question_lists: null,
    interval_on: null,
    interval_off: null,
    step: 25,
    update_interval: 50,


    init: function() {

        if(!document.getElementById || !document.getElementsByTagName || !document.createElement) return;

        var bd = document.getElementsByTagName('body')[0];
        if(bd.className.indexOf('has_JS') == -1) { bd.className += ' has_JS';}

        this.container = $('questions');
        if(!this.container) return;
        this.headings = this.container.getElementsByTagName('h2');
        this.question_lists = this.container.getElementsByTagName('ul');
        var il = this.headings.length;
        for(var i = 0; i < il; i++) {
            var current_heading = this.headings[i];
            if(i == 0) {
                current_heading.className += ' first-child';
            }
            var current_heading_text = current_heading.firstChild;
            var current_a = document.createElement('a');
                current_a.appendChild(current_heading_text);
                current_a.setAttribute('href', '#');
            current_heading.appendChild(current_a);
            var current_list_height = $(this.question_lists[i]).getStyle('height');
            this.heights.push(parseInt(current_list_height));
            Event.observe(current_a, 'click', this.clickListener.bindAsEventListener(this), false);
            if(i == 0) {
                current_heading.className += ' active';
                this.last_index = i;
            } else {
                this.question_lists[i].style.display = 'none';
            }
        }
    },

    clickListener: function(e) {

        if(window.event) {
            window.event.returnValue = false;
        } else {
            e.preventDefault();
        }

        var event_target =  window.event ? window.event.srcElement : e.target;
        if(event_target.nodeName.toLowerCase() == 'img') {
            event_target = event_target.parentNode;
        }

        var item_index = this.getItemIndex(event_target);
        this.toggle(item_index);
    },

    toggle: function(item_index) {
        var new_list = this.question_lists[item_index];
        var new_heading = this.headings[item_index];
        if(!new_list || !new_heading) return;
        var o = this;

        if(new_list.className.indexOf('animating') != -1) return;

        if($(new_list).getStyle('display') != 'block') {
            if(this.interval_on != null) return;

            var target_height = this.heights[item_index];
            new_heading.className += ' active';
            new_list.style.height = '0px';
            this.interval_on = setInterval(function(){o.blind(new_list, 'on', target_height)}, o.update_interval);

        } else {

            if(this.interval_off != null) return;
            new_heading.className = new_heading.className.replace(/active/, '');
            this.interval_off = setInterval(function(){o.blind(new_list, 'off', 0)}, o.update_interval);

        }

        if(this.last_index != null && this.last_index != item_index) {
            if(this.interval_off != null) return;

            var last_list = this.question_lists[this.last_index];
            var last_heading = this.headings[this.last_index];
            last_heading.className = last_heading.className.replace(/active/, '');
            this.interval_off = setInterval(function(){o.blind(last_list, 'off', 0)}, o.update_interval);

        }

        if(this.last_index == item_index) {
            this.last_index = null;
        } else {
            this.last_index = item_index;
        }


    },

    blind: function(list, state, target_height) {
        if(list.getStyle('display') != 'block') {
            list.style.display = 'block';
        }

        if(list.className.indexOf('animating') == -1) {
            list.className += ' animating';
            list.style.overflow = 'hidden';
        }

        var current_heigth = list.getStyle('height');
        if(state == 'on') {
            var current_step = this.step;
        } else {
            var current_step = -this.step;
        }

        var new_height = parseInt(current_heigth) + current_step;

        if(target_height == 0 && new_height < 0) {
            new_height = 0;
            list.style.display = 'none';
            var the_end = true;
        } else if(target_height > 0 && new_height > target_height) {
            var new_height = target_height;
            var the_end = true;
        }
        if(the_end) {
            if(state == 'on') {
                clearInterval(this.interval_on);
                this.interval_on = null;
            } else {
                clearInterval(this.interval_off);
                this.interval_off = null;
            }
            list.className = list.className.replace(/animating/, '');
            list.style.overflow = 'visible';
        }

        list.style.height = new_height + 'px';
    },


    getItemIndex: function(event_target) {
        var related_heading = this.getParentByNodeName(event_target, 'h2');
        if(!related_heading) return;
        var item_index = null;

        var il = this.headings.length;
        for(var i = 0; i < il; i++) {
            var current_heading = this.headings[i];
            if(related_heading == current_heading) {
                item_index = i;
                break;
            }
        }
        return item_index;
    },

    getParentByNodeName: function(obj, nodeName) {

        while(obj.nodeName.toLowerCase() != nodeName) {
            obj = obj.parentNode;

            if(obj.nodeName.toLowerCase() == 'html') {
                obj = null;
                break;
            }
        }

        return obj;
    }

}