// File:        admin.js
// Version:     1.0
// Description: Manage display of 
// Author:      Henry Woodbury (www.henrywoodbury.com)
// Requires:	jQuery.js (tested with jQuery 1.4.2
// Created:     
// Language:    Javascript
// License:     
// Copyright 2010 Henry Woodbury, all rights reserved.

// Create an "indexOf" method for MSIE
if (!Array.indexOf){
	Array.prototype.indexOf = function(obj) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == obj) { return i; }
		}
	return -1;
	}
} // End if



// Returns:	object:showHide
var ShowHide = function(oInit) {

// Sanity check
	if (typeof this.fnInit != 'function') {
		alert("ManageDisplay must be initialised with the 'new' keyword.");
		return;
	}

// Default settings
	var oSettings = {
		"source" : '.showHideSource',
		"parent" : '.showHideSet',
		"label" : '.showHideText',
		"labelOpen" : 'Collapse',
		"labelClosed" : 'Expand',
		"icon" : ".icon-showHide",
		"iconOpenClass" : "icon-showHideOpen",
		"iconClosedClass" : "icon-showHideClosed",
		"targetType" : 'next', // alternate target types not yet defined
		"target" : '.showHideTarget'
	};

// Function: fnGetSettings
// Purpose:	 Return instance settings for the object to the Prototype functions
	this.fnGetSettings = function () {
		return oSettings;
	};
	
// Initialize the object
	this.fnInit(oInit);

}; // ShowHide

// ShowHide prototype
ShowHide.prototype = {
	
// The "constructor" - see inputs above
	fnInit: function (oInit) {
		var s = this.fnGetSettings();
		$.extend(s, oInit);
// Bind Events
		this.fnBindEvents();
	},
	
	fnBindEvents: function() {
		var s = this.fnGetSettings();
		$(s.source).click(function(event) {
			event.preventDefault();
			var nLabel = $(event.target).closest(s.parent).find(s.label);
			var nIcon = $(event.target).closest(s.parent).find(s.icon);
			var nTarget = $(event.target).closest(s.parent).find(s.target);
			if ($(nTarget).first().is(":visible")) {
				$(nTarget).hide();
				$(nLabel).text(s.labelClosed);
				$(nIcon).removeClass(s.iconOpenClass);		
				$(nIcon).addClass(s.iconClosedClass);		
			} else {
				$(nTarget).show();
				$(nLabel).text(s.labelOpen);
				$(nIcon).removeClass(s.iconClosedClass);		
				$(nIcon).addClass(s.iconOpenClass);		
			}
		});
	}

} // End ShowHide.prototype




