var ds = (function () {

// privates
var fading = hash();
var rolling_reset = hash();
var visible_pan = hash();
var mouse_hover = hash();

// publics
var that = {

"init" : function (params) {
	params = params || {};
	var prefix = params.prefix || "";
	var rolling = params.rolling || 5000;

	fading.set(prefix, false);

	var index = -1;
	if (location.hash.length) {
		index = jQuery("." + prefix + "btn").index($("." + prefix + "btn[href$=" + location.hash + "]"));
	}

	if (index == -1)
		index = 0;

	visible_pan.set(prefix, index);

	jQuery("." + prefix + "pan").hide();
	jQuery("." + prefix + "pan:eq(" + index + ")").show();
	jQuery("." + prefix + "btn:eq(" + index + ")").addClass("selected");

	jQuery("." + prefix + "btn").each(function(index, domElement) {
		jQuery(this).bind("click", {"index" : index, "prefix" : prefix}, that.showPan);
	});

	if (rolling) {
		mouse_hover.set(prefix, false);
		jQuery("." + prefix + "btn").hover(function () {mouse_hover.set(prefix, true);}, function () {mouse_hover.set(prefix, false);});
		jQuery("." + prefix + "pan").hover(function () {mouse_hover.set(prefix, true);}, function () {mouse_hover.set(prefix, false);});


		rolling_reset.set(prefix, false);
		var next_pan = index + 1;
		if (next_pan >= jQuery("." + prefix + "pan").length)
			next_pan = 0;
		setTimeout("ds.showPan({'data' : {'rolling' : " + rolling + ", 'index' : " + next_pan + ", 'prefix' : '" + prefix + "'} })", rolling);
	}
},

"showPan" : function (ev) {
	var prefix = ev.data.prefix;

	if (ev.data.rolling) {
		if (rolling_reset.get(prefix) || mouse_hover.get(prefix)) {
			rolling_reset.set(prefix, false);
			var next_pan = visible_pan.get(prefix) + 1;
			if (next_pan >= jQuery("." + prefix + "pan").length)
				next_pan = 0;
			setTimeout("ds.showPan({'data' : { 'rolling' : " + ev.data.rolling + ", 'index' : " + next_pan + ", 'prefix' : '" + prefix + "'} })", ev.data.rolling);
			return;
		}
	} else {
		rolling_reset.set(prefix, true);
	}
	
	if (!fading.get(prefix)) {
		fading.set(prefix, true);
		visible_pan.set(prefix, ev.data.index);
		jQuery("." + prefix + "btn.selected").removeClass("selected");
		jQuery("." + prefix + "btn:eq(" + ev.data.index + ")").toggleClass("selected");
		jQuery("." + prefix + "pan:visible").fadeOut("fast", function() {
			jQuery("." + prefix + "pan:eq(" + ev.data.index + ")").fadeIn("slow", function() {
					fading.set(prefix, false);
					if (ev.data.rolling) {
						rolling_reset.set(prefix, false);
						var next_pan = ev.data.index + 1;
						if (next_pan >= jQuery("." + prefix + "pan").length)
							next_pan = 0;
						setTimeout("ds.showPan({'data' : {'rolling' : " + ev.data.rolling + ", 'index' : " + next_pan + ", 'prefix' : '" + prefix + "'} })", ev.data.rolling);
					}
			   });
		});
	}
} // public's end

};

// ******************************
//  Hash Table - version 1
//  Imported from Forms Framework

function hash() {
	// privates

	// publics
	var that = {

	"keys"		: [],
	"values"	: [],
	"length"	: 0,

	//functions
	"set" : function (key, value) {
		var index = that.keys.indexOf(key);
		if (index > -1) {
			that.values[index] = value;
			return index;
		} else {
			that.keys.push(key);
			that.values.push(value);
			that.length++;
			return that.length - 1;
		}
	},

	"get" : function (key) {
		var index = that.keys.indexOf(key);
		if (index > -1)
			return that.values[index];
		else
			return undefined;
	},

	"indexget" : function (index) {
		if (index > 0 && index < that.length)
			return that.values[index];
		else
			return undefined;
	},

	"isset" : function (key) {
		var index = that.keys.indexOf(key);
		if (index > -1)
			return index;
		else
			return undefined;
	},

	"find" : function (value, offset) {
		var index = that.values.indexOf.apply(that.values, arguments);
		if (index > -1) {
			return {"index" : index, "key" : that.keys[index]};
		} else {
			return undefined;
		}
	},

	"keyfind" : function (value, key) {
		var index;

		if (key === undefined)
			index = that.values.indexOf(value);
		else {
			index = that.keys.indexOf(key);
			if (index > -1)
				index = that.values.indexOf(value, index);
			else
				return undefined;
		}

		if (index > -1) {
			return {"index" : index, "key" : that.keys[index]};
		} else {
			return undefined;
		}
	},

	"unset" : function (key) {
		var index = that.keys.indexOf(key);
		if (index > -1) {
			that.keys.splice(index, 1);
			that.values.splice(index, 1);
			that.length--;
			return that.length;
		} else {
			return undefined;
		}
	},

	"indexunset" : function (index) {
		if (index > 0 && index < that.length)
			return undefined;
		else {
			that.keys.splice(index, 1);
			that.values.splice(index, 1);
			that.length--;
			return that.length;
		}
	},

	"each" : function (func) {
		var tmp_keys	= that.keys.slice();
		var tmp_values	= that.values.slice();
		var l = tmp_keys.length;

		for (var i = 0; i < l; i++) {
			func(tmp_keys[i], tmp_values[i], i);
		}

		return l;
	},

	"clear" : function () {
		that.keys.length = 0;
		that.values.length = 0;
		that.length = 0;
	}
	
	}; // public's end

	return that;
}
// ********* Hash Table *********

return that;

})();

