function OrganizerStack() {
    this.openDuration=300;
    this.closeDuration=300;

    this.elementReference;



    this.toggleOpenButton;
    this.toggleOpenButtonContent;


    this.width;
    this.height;

    this.currentState=null;
    
    this.isOpen = false;
    this.isClosing=false;

    this.stackContentStates = null;

    this.bullet={over:"graphics/plusIconOver.jpg"
    			,active:"graphics/plusIconActive.jpg"
    			,normal:"graphics/plusIcon.jpg"};
    
    this.contentContainer=null;
    this.content = null;
    
    this.createBody();
}

OrganizerStack.prototype.setBullet=function(newStates){
	this.bullet=newStates;
	if(this.currentState!=null)
		this.changeStackContentState(this.currentState);
};

OrganizerStack.prototype.setBarContent = function (newContent) {
    this.stackContentStates = newContent;
    this.toggleOpenButton.find('.stackText').html(this.stackContentStates);
    this.changeStackContentState('normal');
};

OrganizerStack.prototype.changeStackContentState = function (newStateName) {
    if (this.stackContentStates != null) {
        this.toggleOpenButton.find('.stackText').css('display', 'none');
        if (newStateName == "over") {
        	this.currentState=newStateName;
            this.toggleOpenButton.find('img').attr('src', this.bullet.over)
  /*          								.removeClass('plusIconNormal')
								            .removeClass('plusIconActive')
            								.addClass('plusIconOver')*/;
            this.toggleOpenButton.find('.barContentOver').css('display', 'inline');

        } else if (newStateName == 'normal') {
        	this.currentState=newStateName;
            this.toggleOpenButton.find('img').attr('src', this.bullet.normal)
            								/*.removeClass('plusIconOver')
								            .removeClass('plusIconActive')
            								.addClass('plusIconNormal')*/;
            this.toggleOpenButton.find('.barContent').css('display', 'inline');

        } else if (newStateName == "active") {
        	this.currentState=newStateName;
            this.toggleOpenButton.find('img').attr('src', this.bullet.active)
            								/*.removeClass('plusIconNormal')
								            .removeClass('plusIconOver')
            								.addClass('plusIconActive')*/;
            this.toggleOpenButton.find('.barContentActive').css('display', 'inline');
        }
    }
};

OrganizerStack.prototype.getBodyReference = function () {
    return this.elementReference;
};



OrganizerStack.prototype.createBody = function () {
    var object = this;
    this.toggleOpenButton = $('<div class="stack"></div>');
    var stackContent=$('<div style="display:inline"></div>');
    var plusSign=$('<img class="plusSign" />');
    var stackTextNormal=$('<div class="stackText barContent" style="display:none"></div>');
    var stackTextActive=$('<div class="stackText barContentActive" style="display:none"></div>');
    var stackTextOver=$('<div class="stackText barContentOver" style="display:none"></div>');
    this.toggleOpenButton.append(plusSign)
    				.append(stackContent
    						.append(stackTextNormal)
    						.append(stackTextActive)
    						.append(stackTextOver));
    
    this.elementReference = $('<div class="stackContainer"></div>');
    stackContent.mousedown(function (event) {
        object.toggleContentOpen();
    });
    stackContent.hover(function (event) { if (!object.isOpen) object.changeStackContentState('over');}
            , function (event) { if (!object.isOpen) object.changeStackContentState('normal');});

    this.elementReference.append(this.toggleOpenButton);
};

OrganizerStack.prototype.setWidth = function (newValue) {
    this.width = newValue;
};

OrganizerStack.prototype.setHeight = function (newValue) {
    this.height = newValue;
};

OrganizerStack.prototype.refreshBody = function () {
    this.toggleOpenButton.width(this.width);
    this.toggleOpenButton.height(this.height);

    this.elementReference.width(this.width);

    if (this.content != null) {
        this.content.width(this.width);
        this.content.refreshBody();
    }
};



OrganizerStack.prototype.toggleContentOpen = function () {
    if (this.isOpen) {
        this.changeStackContentState('over');
        this.close();
    } else {
        this.open();
    }
};

OrganizerStack.prototype.setContent = function (newContent) {
    this.content = newContent;
    newContent.height(0);
    this.contentContainer = $('<div style="overflow:hidden; height:0"></div>');
};

OrganizerStack.prototype.open = function () {
    var object = this;
    if (this.content != null && !this.isOpen) {
        this.isOpen = true;

        this.getBodyReference().append(this.contentContainer.append(this.content.getBodyReference()));
        this.getBodyReference().trigger({
            type: "stackOpenStart",
            stack: this
        });
        
        this.contentContainer.stop();
        var closing = this.isClosing;

        object.changeStackContentState('active');
        object.isClosing = false;
        this.contentContainer.animate({ height: this.content.height() },{
        	duration:this.openDuration,
        	easing:'swing',
        	complete:function () {
	            if (!closing) {
	                object.getBodyReference().trigger({
	                    type: "stackOpenFinish",
	                    stack: object
	                });
	            }
          	},
            step:function () {
                object.getBodyReference().trigger({
                    type: "stackOpenStep",
                    stack: object
                });
            }
        });
        // this.contentContainer.animate({ height: this.content.height() }, this.openDuration, 'swing', function () {
            // if (!closing) {
                // object.getBodyReference().trigger({
                    // type: "stackOpenFinish",
                    // stack: object
                // });
            // }
        // });
    }
};

// OrganizerStack.prototype.open = function () {
    // var object = this;
    // if (this.content != null && !this.isOpen) {
        // this.isOpen = true;
// 
        // this.getBodyReference().append(this.contentContainer.append(this.content.getBodyReference()));
        // this.getBodyReference().trigger({
            // type: "stackOpenStart",
            // stack: this
        // });
//         
        // this.contentContainer.stop();
        // var closing = this.isClosing;
// 
        // object.changeStackContentState('active');
        // object.isClosing = false;
        // this.contentContainer.animate({ height: this.content.height() }, this.openDuration, 'swing', function () {
            // if (!closing) {
                // object.getBodyReference().trigger({
                    // type: "stackOpenFinish",
                    // stack: object
                // });
            // }
        // });
    // }
// };

OrganizerStack.prototype.close = function () {
    var object = this;

    this.getBodyReference().trigger({
        type: "stackCloseStart",
        stack: this
    });
    if (this.content != null && this.isOpen) {
        this.isOpen = false;
        this.isClosing = true;
        this.content.getBodyReference().trigger({
            type: "contentCloseStart",
            stack: this
        });

        this.contentContainer.stop();
        this.contentContainer.animate({ height: 0 }, this.closeDuration, 'swing', function () {
            object.content.getBodyReference().detach();
            object.isClosing = false;
            object.getBodyReference().trigger({
                type: "stackCloseFinish",
                stack: object
            });
        });
    }
};

OrganizerStack.prototype.resizeToContent = function () {
    var object = this;
    object.getBodyReference().trigger({
        type: "stackResizeStart",
        stack: object
    });
    if (this.content != null && this.isOpen) {
        //this.contentContainer.stop();
        //alert(this.content.height());
        //alert(this.content.height());
        //this.contentContainer.css('backg');
        this.contentContainer.animate({height: this.content.height()}, this.openDuration, 'swing', function () {
        	//alert('--');
            object.getBodyReference().trigger({
                type: "stackResizeFinish",
                stack: object
            });
        });
        //this.contentContainer.height(this.content.height());
    }
};

OrganizerStack.prototype.unload = function () {
    if (this.content != null) {
        this.content.getBodyReference().stop();
        this.content.unload();
    }
    this.toggleOpenButton.unbind('mousedown');
    this.toggleOpenButton.unbind('mouseover');
    this.toggleOpenButton.unbind('mouseout');
    this.getBodyReference().remove();
};
