opsmile.history =
{
	"limit": 6,
	"offset": 0,
	"intervals": [],
	"mbs": null,
	"indInt": null,
	"indSect": null,
	"jqFeature": null,
	"tpl": new liltype.HTpl
	(
		["div", {"htpl:t": "main"}, [
			["div", {"class": "menubar clearfix"}, {"htpl:t": "menubar"}, [
				["a", {"onclick": "return opsmile.history.back(this, {mbInd});"}, {"href": "#"}, {"class": "back"}, "&lt; Back"],
				["div", {"class": "intervals clearfix"}, [
					["a", {"class": "interval"}, {"href": "#x"}, {"onclick": "return opsmile.history.select({ind}, null);"}, {"htpl:t": "intervals"}, "{title}"]
				]],
				["a", {"onclick": "return opsmile.history.fwd(this, {mbInd});"}, {"href": "#"}, {"class": "forward"}, "Forward &gt;"]
			]]
		]]
	),
	"init": function()
	{
		var jqc, jqcs = jQuery("#history_intervals div.history_interval"), i, c, vals, interval, ii, mbs, mbInd = 0;

		// START: Generate the nav bar
			vals  = {"intervals": ""};
			mbs = "";
			for (i = 0, c = jqcs.length; i < c; i++)
			{
				if ((i % this.limit) == 0)
				{
					if (vals.intervals.length)
					{
						vals["mbInd"] = mbInd++;
						mbs += this.tpl.sprint("menubar", vals)
					}
					vals  = {"intervals": ""};
				}
	
				jqc = jqcs.eq(i);
				interval =
				{
					"ind": 0,
					"vals":
					{
						"title": jQuery("div.title", jqc).html().replace(/<[^>]+>/g, ""),
						"ind": i
					},
					"sections_lower": jQuery("div.section_lower", jqc),
					"sections_upper": jQuery("div.section_upper", jqc),
					"jq": jqc
				};
				jQuery("a", jqc).addClass("history_interval_"+i);
				this.tpl.sprintEach("intervals", interval.vals, vals);
				this.intervals.push(interval);
			}
			if (vals.intervals.length)
			{
				vals["mbInd"] = mbInd++;
				mbs += this.tpl.sprint("menubar", vals)
			}
			jQuery("#history_interval_bar").html(mbs);
			
			// Deactivate the back/forward for first/last bars
			mbs = jQuery("#history_interval_bar div.menubar").css("display", "none");
			jQuery("a.back", mbs.eq(0).css("display", "block")).addClass("inactive");
			jQuery("a.forward", mbs.eq(mbs.length - 1)).addClass("inactive");
			this.mbs = mbs;
		// END: Generate the nav bar
		
		// START: Remove "view more" for no detail - fix various convio quirks
			for (i = 0, c = jqcs.length; i < c; i++)
			{
				jqc = jqcs.eq(i);
				//jQuery("div.title", jqc).html(jQuery("div.title a", jqc).html());
				for (ii = 1; ii <= 3; ii++)
				{
					if (!jQuery("div.section_upper div.image_"+ii+" img", jqc).length ||
					    !jQuery("div.section_upper div.descr_"+ii+" span", jqc).html().length)
					{
						jQuery("div.see_more_"+ii, jqc).css("display", "none");
					}
				}
			}
		// END: Remove "view more" for no detail

		// START: Set up the lower sections and the default banner content
			jqcs.css("display", "none").removeClass("hidden");
			this.jqFeature = jQuery("#history_feature");
			this.select(0, null);
			this.jqFeature.append(jQuery("#history_default_banner div.section_upper"));
		// END: Set up the lower sections and the default banner content
	},
	"select": function(indInt, indSect)
	{
		var interval, sect, img;
		if (this.indInt !== null)
		{
			(interval = this.intervals[this.indInt]).jq.css("display", "none");
			if (indSect !== null)
			{
				jQuery("div.section_upper", this.jqFeature).remove();
			}
		}
		
		this.intervals[indInt].jq.css("display", "block").removeClass("hidden");
		jQuery("#history_interval_bar div.menubar a.interval").removeClass("interval_selected").eq(indInt).addClass("interval_selected");

		if (indSect !== null)
		{
			this.jqFeature.append(sect = this.intervals[indInt].sections_upper.eq(indSect).removeClass("hidden").css("display", "block"));
			img = jQuery("div.section_left div.image img", sect);
			jQuery("div.section_left div.image", sect).html("").append(img);
		}

		this.indInt = indInt;
		this.indSect = indSect;
		return false;
	},
	"viewFullStory": function(el, ind)
	{
		var m;
		if (m = el.className.match(/(^|\s)history_interval_([0-9]+)(\s|$)/))
		{
			return this.select(new Number(m[2]), ind);
		}
		return false;
	},
	"fwd": function(el, mbInd)
	{
		if (jQuery(el).hasClass("inactive"))
		{
			return false;
		}
		this.mbs.eq(mbInd).css("display", "none");
		this.mbs.eq(mbInd + 1).css("display", "block");
		return this.select((mbInd + 1) * this.limit, null);
	},
	"back": function(el, mbInd)
	{
		if (jQuery(el).hasClass("inactive"))
		{
			return false;
		}
		this.mbs.eq(mbInd).css("display", "none");
		this.mbs.eq(mbInd - 1).css("display", "block");		
		return this.select((mbInd - 1) * this.limit, null);
	}
};
