function prepareExternalLinks()
{
	$$('a[rel="external"]').invoke("observe", "click", function(e)
	{
		elem = e.findElement("a");
		window.open(elem.href);
	})
	$$('a[rel="external"]').each(function(e) {
		e.onclick = function() { return false;};
	});
}

function prepareKeyLinks()
{
	$$(".revlink").each(function(e)
	{
		e.href += e.getAttribute("rev")
	})
}

function prepareLanguageLink()
{
	document.getElementById("language_link").onclick = function()
	{
		oh = "humana.com";
		tsh = "espanol.humana.com/enes/";
		idx = location.href.indexOf(tsh);
		if (idx == -1) {
			idx = location.href.indexOf(oh) + oh.length;
			hname = tsh
		}
		else {
			idx = idx + tsh.length;
			hname = "www." + oh
		}
		path = location.href.substring(idx);
		hend = hname.charAt(hname.length - 1);
		pstart = path.charAt(0);
		if (hend == "/" && pstart == "/") {
			path = path.substring(path.indexOf("/") + 1)
		}
		if (hend != "/" && pstart != "/") {
			path = "/" + path
		}
		this.href = location.protocol + "//" + hname + path;
		return true
	}
}

function prepareFormAutoComplete(forms)
{
	if (!document.getElementById) {
		return false
	}
	var f, i = 0;
	for (; i < forms.length; ++i) {
		f = document.getElementById(forms[i]);
		if (f) {
			f.setAttribute("autocomplete", "off")
		}
	}
}

function prepareOverLabels()
{
	if (!document.getElementById) {
		return
	}
	var labels, id, field;
	labels = document.getElementsByTagName("label");
	for (var i = 0; i < labels.length; i++) {
		if (labels[i].className == "overlabel") {
			id = labels[i].htmlFor || labels[i].getAttribute("for");
			if (!id || !(field = document.getElementById(id))) {
				continue
			}
			labels[i].className = "overlabel-apply";
			if (field.value !== "") {
				hideLabel(field.getAttribute("id"), true)
			}
			field.onfocus = function()
			{
				hideLabel(this.getAttribute("id"), true)
			};
			field.onblur = function()
			{
				if (this.value === "") {
					hideLabel(this.getAttribute("id"), false)
				}
			};
			labels[i].onclick = function()
			{
				var id, field;
				id = this.getAttribute("for");
				if (id && (field = document.getElementById(id))) {
					field.focus()
				}
			}
		}
	}
}

function hideLabel(field_id, hide)
{
	var field_for;
	var labels = document.getElementsByTagName("label");
	for (var i = 0; i < labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute("for");
		if (field_for == field_id) {
			labels[i].style.textIndent = (hide) ? "-1000px" : "0px";
			return true
		}
	}
}

var Slideshow = Class.create({
	initialize: function(options)
	{
		this.slideshow = $(options.id);
		if (!this.slideshow) {
			throw ("Attempted to initalize slideshow with undefined element: " + options.id)
		}
		this.slideClass = options.slideClass || "slide";
		this.duration = options.duration || 6;
		options.navigation = options.navigation || false;
		this.slides = this.slideshow.select("." + this.slideClass);
		this.current = 1;
		this.isAnimating = false;
		this.toExpand = null;
		if (options.navigation) {
			options.event = options.event || "click";
			var nav = new Element("p", {
				id: "slideshow_nav"
			});
			this.slideshow.insert({
				bottom: nav
			});
			var e = null;
			for (var i = 0; i < this.slides.length;) {
				++i;
				e = new Element("a", {
					id: "slide" + i + "_link",
					href: "#" + this.slideshow.id
				}).update(i);
				if (i == this.current) {
					e.addClassName("active")
				}
				e.observe(options.event, this.control.bindAsEventListener(this, i));
				nav.insert({
					bottom: e
				})
			}
			e = new Element("span", {
				id: "control_link",
				href: "#" + this.slideshow.id
			}).update("&gt;");
			e.observe(options.event, this.control.bindAsEventListener(this, 0));
			nav.insert({
				bottom: e
			})
		}
		this.pe = new PeriodicalExecuter(this.animate.bind(this), this.duration)
	},
	control: function(e, i)
	{
		if (this.isAnimating || this.current == i) {
			return false
		}
		e.stop();
		this.pe.stop();
		if (i == 0) {
			if ($("control_link").hasClassName("active")) {
				$("control_link").removeClassName("active");
				this.pe = new PeriodicalExecuter(this.animate.bind(this), this.duration)
			}
			else {
				$("control_link").addClassName("active")
			}
		}
		else {
			$("control_link").addClassName("active");
			this.animate(i)
		}
	},
	animate: function(slide)
	{
		var effects = [];
		var next = 0;
		if (typeof slide == "number") {
			next = slide
		}
		else {
			next = (this.current == this.slides.length) ? 1 : this.current + 1
		}
		effects.push(new Effect.Fade(this.slides[this.current - 1], {
			sync: true
		}));
		effects.push(new Effect.Appear(this.slides[next - 1], {
			sync: true
		}));
		new Effect.Parallel(effects, {
			duration: 1,
			queue: {
				position: "end",
				scope: this.slideshow.id + "Animation"
			},
			beforeStart: function()
			{
				this.isAnimating = true;
				$("slide" + this.current + "_link").removeClassName("active");
				$("slide" + next + "_link").addClassName("active")
			}
.bind(this)			,
			afterFinish: function()
			{
				this.current = next;
				this.isAnimating = false
			}
.bind(this)
		})
	}
});
var Cookie = {
	data: {},
	options: {
		expires: 1,
		domain: "",
		path: "",
		secure: false
	},
	init: function(options, data)
	{
		Cookie.options = Object.extend(Cookie.options, options ||
		{});
		var payload = Cookie.retrieve();
		if (payload) {
			Cookie.data = payload.evalJSON()
		}
		else {
			Cookie.data = data ||
			{}
		}
		Cookie.store()
	},
	getData: function(key)
	{
		return Cookie.data[key]
	},
	setData: function(key, value)
	{
		Cookie.data[key] = value;
		Cookie.store()
	},
	removeData: function(key)
	{
		delete Cookie.data[key];
		Cookie.store()
	},
	retrieve: function()
	{
		var start = document.cookie.indexOf(Cookie.options.name + "=");
		if (start == -1) {
			return null
		}
		if (Cookie.options.name != document.cookie.substr(start, Cookie.options.name.length)) {
			return null
		}
		var len = start + Cookie.options.name.length + 1;
		var end = document.cookie.indexOf(";", len);
		if (end == -1) {
			end = document.cookie.length
		}
		return unescape(document.cookie.substring(len, end))
	},
	store: function()
	{
		var expires = "";
		if (Cookie.options.expires) {
			var today = new Date();
			expires = Cookie.options.expires * 86400000;
			expires = ";expires=" + new Date(today.getTime() + expires)
		}
		document.cookie = Cookie.options.name + "=" + escape(Object.toJSON(Cookie.data)) + Cookie.getOptions() + expires
	},
	erase: function()
	{
		document.cookie = Cookie.options.name + "=" + Cookie.getOptions() + ";expires=Thu, 01-Jan-1970 00:00:01 GMT"
	},
	getOptions: function()
	{
		return (Cookie.options.path ? ";path=" + Cookie.options.path : "") + (Cookie.options.domain ? ";domain=" + Cookie.options.domain : "") + (Cookie.options.secure ? ";secure" : "")
	}
};
var Tab = Class.create({
	initialize: function(options)
	{
		this.tab = $(options.id);
		if (!this.tab) {
			throw ("Attempted to initalize tab with undefined element: " + options.id)
		}
		var styles = {
			display: "none",
			opacity: 0
		};
		this.toggleClass = "tabset_tabs";
		this.activeClass = "active";
		this.contentClass = "tabset_content";
		this.current = 0;
		this.tabs = this.tab.select("." + this.toggleClass + " li");
		this.contents = this.tab.select("." + this.contentClass);
		this.isAnimating = false;
		options.event = options.event || "click";
		this.tabs.each(function(e, i)
		{
			if (e.hasClassName(this.activeClass)) {
				this.current = i
			}
			e.observe(options.event, this.toggle.bindAsEventListener(this, i))
		}
.bind(this));
		hash = Cookie.getData("tab");
		if (hash) {
			this.tabs[this.current].removeClassName(this.activeClass);
			this.current = hash
		}
		this.tabs[this.current].addClassName(this.activeClass);
		if (options.height == true) {
			styles.height = this.contents.max(function(e)
			{
				return e.getHeight()
			}) + "px";
			this.contents[this.current].setStyle({
				height: styles.height
			})
		}
		if (options.rounded == true) {
			var elem = this.tab.select("." + this.contentClass + "_container")[0];
			if (elem) {
				elem.setStyle({
					paddingTop: 0,
					paddingBottom: 0
				});
				elem.insert({
					top: '<div class="tr"></div>'
				});
				elem.insert('<div class="bl"><div class="br"></div></div>')
			}
		}
		this.contents.each(function(section, i)
		{
			if (this.current != i) {
				section.setStyle(styles)
			}
		}
.bind(this))
	},
	toggle: function(e, i)
	{
		e.stop();
		if (this.isAnimating || this.current == i) {
			return false
		}
		this.tabs[i].addClassName(this.activeClass);
		this.tabs[this.current].removeClassName(this.activeClass);
		new Effect.Parallel([new Effect.Fade(this.contents[this.current], {
			sync: true
		}), new Effect.Appear(this.contents[i], {
			sync: true
		})], {
			duration: 0.4,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: "end",
				scope: this.tab.id + "Animation"
			},
			beforeStart: function()
			{
				this.isAnimating = true
			}
.bind(this)			,
			afterFinish: function()
			{
				this.isAnimating = false;
				this.current = i
			}
.bind(this)
		});
		Cookie.setData("tab", i)
	}
});

