YAHOO.namespace("OPS");

var opsCollapsableGroup = {};

YAHOO.OPS.collapsableGroup = function(opts) {
    opsDebugDump('opts', opts, 1);
    this.createEvent('expand');
    this.createEvent('collapse');
    opsCollapsableGroup[opts.groupid] = this;

    YAHOO.util.Event.onDOMReady(this.init, opts, this, true);
};

YAHOO.OPS.collapsableGroup.prototype = {
    init: function(ev, other, opts) {
	myopts = {
	    groupid: ''
	    , btnCollapseURL: ''
	    , btnExpandURL: ''
	    , clickAllHeader: false
	    , collapseClass: ''
	    , expandClass: 'ops-collapsable-expanded'
	    , headerBlurClass: ''
	    , headerFocusClass: 'ops-collapsable-inhead'
	};

	var debugout = '';
	for(var opt in myopts) {
	    if(typeof opts[opt] != 'undefined') {
		debugout += opt+'='+opts[opt]+"\n";
		myopts[opt] = opts[opt];
	    }
	}
	this.opts = myopts;
	opsDebug(debugout);

	this.groupEl = YAHOO.util.Dom.get(this.opts.groupid);
	this.btnEl = YAHOO.util.Dom.get(this.opts.groupid+'_btn');
	this.headerEl = YAHOO.util.Dom.get(this.opts.groupid+'_h');
	this.headerExtraEl = YAHOO.util.Dom.get(this.opts.groupid+'_hextra');
	this.contentEl = YAHOO.util.Dom.get(this.opts.groupid+'_c');

	if(this.opts.expanded)
	    this.expand();
	else
	    this.collapse();

	if(this.opts.clickAllHeader) {
	    YAHOO.util.Dom.setStyle(this.headerEl, 'cursor', 'pointer');
	    YAHOO.util.Event.on(this.headerEl, 'click', this.toggle, this, true);
	    YAHOO.util.Event.on(this.headerEl, 'mouseover', this.headerFocus, this, true);
	    YAHOO.util.Event.on(this.headerEl, 'mouseout', this.headerBlur, this, true);
	} else {
	    YAHOO.util.Event.on(this.btnEl, 'click', this.toggle, this, true);
	}

	return;

    },

    headerBlur: function() {
	if(this.opts.headerFocusClass)
	    YAHOO.util.Dom.removeClass(this.headerEl, this.opts.headerFocusClass);
	if(this.opts.headerBlurClass)
	    YAHOO.util.Dom.addClass(this.headerEl, this.opts.headerBlurClass);
    },

    headerFocus: function() {
	if(this.opts.headerBlurClass)
	    YAHOO.util.Dom.removeClass(this.headerEl, this.opts.headerBlurClass);
	if(this.opts.headerFocusClass)
	    YAHOO.util.Dom.addClass(this.headerEl, this.opts.headerFocusClass);
    },

    toggle: function() {
	var displayState = YAHOO.util.Dom.getStyle(this.contentEl, 'display');
	if(displayState == 'none')
	    this.expand();
	else
	    this.collapse();
    },

    collapse: function() {
        YAHOO.util.Dom.setStyle(this.contentEl, 'display', 'none');
	if(this.headerExtraEl)
	    YAHOO.util.Dom.setStyle(this.headerExtraEl, 'display', 'block');
	if(this.opts.expandClass)
	    YAHOO.util.Dom.removeClass(this.groupEl, this.opts.expandClass);
	if(this.opts.collapseClass)
	    YAHOO.util.Dom.addClass(this.groupEl, this.opts.collapseClass);
	if(this.opts.btnCollapseURL && this.opts.btnExpandURL)
	    this.btnEl.src = this.opts.btnExpandURL;
	this.fireEvent('collapse');
    },

    expand: function() {
        YAHOO.util.Dom.setStyle(this.contentEl, 'display', 'block');
	if(this.headerExtraEl)
	    YAHOO.util.Dom.setStyle(this.headerExtraEl, 'display', 'none');
	if(this.opts.collapseClass)
	    YAHOO.util.Dom.removeClass(this.groupEl, this.opts.collapseClass);
	if(this.opts.expandClass)
	    YAHOO.util.Dom.addClass(this.groupEl, this.opts.expandClass);
	if(this.opts.btnCollapseURL && this.opts.btnExpandURL)
	    this.btnEl.src = this.opts.btnCollapseURL;
	this.fireEvent('expand');
    },

    endofclass: ''
};

YAHOO.lang.augmentProto(YAHOO.OPS.collapsableGroup, YAHOO.util.EventProvider);

