jQuery.extend({
    accordionItems: []
});

function accordion(obj, gn){
    this.item = obj;
    this.body = this.item.find('.accordionBody, .showHideInfoContent').eq(0).css({overflow:'hidden'});
    this.groupNum = gn || null;
    this.titleContainer = this.item.find('[class^=accordionTitle],[class^=sliderTitle],[class^=showHideToggler]').eq(0);
    this.closedTitle = this.titleContainer.children().eq(0);
    this.openedTitle = this.closedTitle.next('.openTitle');
    this.bodyHeight = this.body.height();
    this.duration = 500;
}

accordion.prototype = {
    init: function(){
        var self = this;
        this.switchTitle();
        if (!this.titleContainer.hasClass('opened') && this.body.find('.formErrors').length==0) {
            this.body.css({
                height: 0,
                opacity: 0
            });
        }
        this.titleContainer.hover(function(){
            jQuery(this).addClass('over');
        }, function(){
            jQuery(this).removeClass('over');
        });
        this.titleContainer.click(function(e){
            self.toggle();
            e.preventDefault();
        });
    },
    
    switchTitle: function(){
         if (this.openedTitle.length) {
            if (this.titleContainer.hasClass('opened')){
                this.closedTitle.hide();
                this.openedTitle.show();
            }
            else {
                this.closedTitle.show();
                this.openedTitle.hide();
            }
        }    
    },
    
    open: function(){
        this.bodyHeight = this.body.children().outerHeight();
        if (this.groupNum) {
            for (var i = 0; i < jQuery.accordionItems[this.groupNum].length; i++) {
                var ai=jQuery.accordionItems[this.groupNum][i];
                if (ai.titleContainer.hasClass('opened') && ai != this) ai.close();
            }
        }
        this.titleContainer.addClass('opened');
        this.switchTitle();
        this.body.animate({
            opacity: 1,
            height: this.bodyHeight + 'px'
        }, {
            queue: false,
            duration: this.duration,
            complete: function(){
                jQuery(this).css({
                    height: 'auto'
                });
            }
        });
    },
    
    close: function(){
        var self = this;
        this.bodyHeight = this.body.height();
        this.body.animate({
            opacity: 0,
            height: 0
        }, {
            queue: false,
            duration: this.duration,
            complete: function(){
                self.titleContainer.removeClass('opened');
                self.switchTitle();
            }
        });
    },
    
    toggle: function(){
        if (this.titleContainer.hasClass('opened')) 
            this.close();
        else 
            this.open();
    }
}

function accordions(){
    jQuery(".accordionNode, .showHideInfoControl").filter(":not('.existingItem')").each(function(){
        var groupNum = 0;
        var tmp = jQuery(this).attr('class').split(" ");
        for (var i = 0; i < tmp.length; i++) {
            if (tmp[i].indexOf('accordionGroup') != -1) {
                groupClass = tmp[i];
                groupNum = groupClass.split('accordionGroup-')[1];
            }
        }
        
        if (jQuery.accordionItems[groupNum] == undefined) jQuery.accordionItems[groupNum] = [];
        var n = jQuery.accordionItems[groupNum].length;
        jQuery.accordionItems[groupNum][n] = new accordion(jQuery(this), groupNum);
        jQuery.accordionItems[groupNum][n].init();
        jQuery.accordionItems[groupNum][n].item.addClass('existingItem');
       
    });
    jQuery('.accordionLink').each(function(){
        jQuery(this).click(function(e){
            var tmp = jQuery(this).attr('rel').split(',');
            var group = tmp[0];
            var item = tmp[1] - 1;
            jQuery.accordionItems[group][item].toggle();
            e.preventDefault();
        })
    });
}


jQuery(document).ready(function(){

    accordions();
    
});

