;(function($, document, document, undefined) {
    window.container_bfa_openclose = {
        
        execute_actions : function (data,no) {
            if(typeof no == 'undefined') {
                no = '';
            }
            var _this = window.container_bfa_openclose;
            _this.container = window.container;
            _this.goon = 1;
            switch (data.type) {
                case 'status' :
                    return _this.openclose_status(data,no);
                    break;
                case 'multi' :
                    return _this.openclose_multi(data,no);
                    break;
            }
            
            return _this.goon;
        },
        
        openclose_status : function (data,no) {
            var _this = window.container_bfa_openclose;
            
            if (!_this.container.exists(data,'valuetarget')) {
                alert ('setting: this_id not found or is incorrect');
                return false;
            }
            if (!_this.container.exists(data,'status')) {
                alert ('setting: bfa_openclose_status not found');
                return false;
            }
            if (!_this.container.exists(data,'target')) {
                alert ('setting: bfa_openclose_target not found  or is incorrect');
                return false;
            }
            if (!_this.container.exists(data,'speed')) {
                alert ('setting: bfa_openclose_speed not found');
                return false;
            }
            
            if (typeof data.setclass != 'undefined') {
                if (!_this.container.exists(data.setclass,'target')) {
                    alert ('setting: openClose_setClass_target not found');
                    return false;
                }
                
            }
            if (_this.container.isNumeric(no)) {
                var status_id = 'bfa_' + no + '_openclose_status';
            } else {
                var status_id = 'bfa_openclose_status';
            }
            var status = $(data.valuetarget).data(status_id);
            var do_blur = 0;
            if (typeof data.blurtarget != 'undefined') {
                do_blur = 1;
            } 
            if (status == 'closed') {
                $(data.valuetarget).data(status_id,'open');
                $(data.target).show(data.speed);
                if (do_blur) {
                    $(data.blurtarget).addClass('in');
                }
                
                if (typeof data.setclass.open != 'undefined') {
                    $(data.setclass.target).addClass(data.setclass.open);
                }
                if (typeof data.setclass.closed != 'undefined') {
                    $(data.setclass.target).removeClass(data.setclass.closed);
                }
            } else {
                $(data.valuetarget).data(status_id,'closed');
                $(data.target).hide(data.speed);
                if (do_blur) {
                    $(data.blurtarget).removeClass('in');
                }
                if (typeof data.setclass.open != 'undefined') {
                    $(data.setclass.target).removeClass(data.setclass.open);
                }
                if (typeof data.setclass.closed != 'undefined') {
                    $(data.setclass.target).addClass(data.setclass.closed);
                }
            }
            if (_this.container.exists(data,'runfunction')) {
                var list = data.runfunction.split('|');
                if (list.length == 2) {
                    if (data.status == 'closed') {
                        data.status = 'open';
                    } else {
                        data.status = 'closed';
                    }
                    var f_obj = list[0];
                    var f_fn = list[1];
                    window[f_obj][f_fn](data);
                }
            }
            
        },
        
        
        openclose_multi : function (data,no) {
            var _this = window.container_bfa_openclose;
            
            if (!_this.container.exists(data,'valuetarget')) {
                alert ('setting: this_id not found or is incorrect');
                return false;
            }
            if (!_this.container.exists(data,'status')) {
                alert ('setting: bfa_openclose_status not found');
                return false;
            }
            if (!_this.container.exists(data,'target')) {
                alert ('setting: bfa_openclose_target not found  or is incorrect');
                return false;
            }
            if (!_this.container.exists(data,'grouplinkitems')) {
                alert ('setting: grouplinkitems not found  or is incorrect');
                return false;
            }
            if (!_this.container.exists(data,'groupitems')) {
                alert ('setting: groupitems not found  or is incorrect');
                return false;
            }
            if (!_this.container.exists(data,'speed')) {
                alert ('setting: bfa_openclose_speed not found');
                return false;
            }
            
            if (typeof data.setclass != 'undefined') {
                if (!_this.container.exists(data.setclass,'target')) {
                    alert ('setting: openClose_setClass_target not found');
                    return false;
                }
                
            }
            if (_this.container.isNumeric(no)) {
                var status_id = 'bfa_' + no + '_openclose_status';
                var target_id = 'bfa_' + no + '_openclose_target';
                
            } else {
                var status_id = 'bfa_openclose_status';
                var target_id = 'bfa_openclose_target';
            }
            _this.data = data;
            _this.status_id = status_id;
            _this.target_id = target_id;
            $(data.grouplinkitems).each (function(){
                var _this = window.container_bfa_openclose;
                var data = _this.data;
                var this_data = $(this).data();
                if (this_data[_this.target_id] == data.target) {
                    if (data.status == 'closed') {
                        $(this).data(_this.status_id,'open');
                        
                        $(this).removeClass(data.setclass.closed);
                        $(this).addClass(data.setclass.open);
                    } else {
                        $(this).data(_this.status_id,'closed');
                        $(this).addClass(data.setclass.closed);
                        $(this).removeClass(data.setclass.open);
                    }
                } else {
                    $(this).data(_this.status_id,'closed');
                    $(this).addClass(data.setclass.closed);
                    $(this).removeClass(data.setclass.open);
                }
            });
            
            $(data.groupitems).each (function(){
                var _this = window.container_bfa_openclose;
                var data = _this.data;
                var this_id = '#' + $(this).attr('id');
                var this_data = $(this).data();
                
                if (this_id == data.target) {
                    if (data.status == 'closed') {
                        $(this).show(data.speed);
                    } else {
                        $(this).hide(data.speed);
                    }
                } else {
                    $(this).hide(data.speed);
                }
            });
            if (_this.container.exists(data,'runfunction')) {
                var list = data.runfunction.split('|');
                if (list.length == 2) {
                    if (data.status == 'closed') {
                        data.status = 'open';
                    } else {
                        data.status = 'closed';
                    }
                    
                    var f_obj = list[0];
                    var f_fn = list[1];
                    window[f_obj][f_fn](data);
                }
            }
            
           
//            $(data.valuetarget).data(status_id,'open');
//            $(data.target).show(data.speed);
//            if (typeof data.setclass.open != 'undefined') {
//                $(data.setclass.target).addClass(data.setclass.open);
//            }
//            if (typeof data.setclass.closed != 'undefined') {
//                $(data.setclass.target).removeClass(data.setclass.closed);
//            }
            
        }
    }
    
})(jQuery, window, document);