﻿Collapsor = function(elements, settings, callback) {
	this.settings = jQuery.extend({}, this.defaultSettings, settings);
	this.callback = callback;
	this.trigger = jQuery(elements.trigger).get(0);
	this.mainPanel = jQuery(elements.mainPanel).get(0);
	this.resizablePanel = jQuery(elements.resizablePanel).get(0);
	this.stateField = jQuery('#' + this.getStateFieldId()).get(0); //other ids where prefixed with the '#' on the server
};

Collapsor.prototype = {
	
	subscribeToClick : function() {
		jQuery(this.trigger).bind('click', Function.createDelegate(this, this.triggerClickHandler));
	},
	
	expand : function() {
		jQuery(this.trigger).addClass(this.settings.expandedLinkClass);
		jQuery(this.trigger).removeClass(this.settings.collapsedLinkClass);
		jQuery(this.resizablePanel).show();
		if (this.settings.dividerLocation) {
			jQuery(this.mainPanel).css({width: this.settings.dividerLocation + 'px'});
		}				
	},

	collapse : function() {
		jQuery(this.trigger).addClass(this.settings.collapsedLinkClass);
		jQuery(this.trigger).removeClass(this.settings.expandedLinkClass);
	  jQuery(this.resizablePanel).hide();
		if (this.settings.dividerLocation) {
			jQuery(this.mainPanel).css({width:''});
		}
	},
	
	update : function() {
		if (this.isExpanded()) {
			this.expand();
		}
		else {
			this.collapse();
		}
		if (this.callback) {
			this.callback.call(this);
		}
	},
	
	isExpanded : function() {
		return this.getStateFromForm();
	},
	
	setExpanded : function(value) {
	  if (value===undefined)
	  {
	    value = true;
	  }
		this.saveStateToForm(value);
		this.update();
	},
	
	getStateFieldId : function() {
		if (this.trigger && this.trigger.id) {
			return this.trigger.id + this.settings.statePrefix;
		}
		else {
			return jQuery.fn.collapsor.count + this.settings.statePrefix;
		}
	},
	
	getStateFromForm : function() {
		if (this.stateField) {
			return Boolean.parse(jQuery(this.stateField).val());
		}
		else {
			return this.settings.isExpanded;
		}		
	},
	
	saveStateToForm : function(value) {
		if (!this.stateField) {
			this.stateField = jQuery.create('input', {'type': 'hidden', 'id': this.getStateFieldId(), 'name': this.getStateFieldId()}).get(0);
			jQuery(this.resizablePanel).append(this.stateField);
			
		}
		jQuery(this.stateField).val(value);
	},
	
	triggerClickHandler : function() {
		this.setExpanded(!this.isExpanded());
	},
	
	defaultSettings : {
		collapsedLinkClass: '',
		expandedLinkClass: '',
		isExpanded: true,
		statePrefix : 'state'
	}
};

(function(jQuery) {
	jQuery.fn.makeCollapsable = function(elements, settings, callback) {

		return this.each(function() {
			var collapsor = new Collapsor(jQuery.extend({}, {trigger: this}, elements), settings, callback);
			collapsor.subscribeToClick();
			collapsor.update();
			jQuery.fn.makeCollapsable.count = jQuery.fn.makeCollapsable.count + 1;
		});
	};

	jQuery.fn.makeCollapsable.count = 0;

})(jQuery);
