window.addEvent("domready", init);

function init() {

	cuone_config							= new Array();
	cuone_assets							= new Array();
	cuone_global							= new Array();

	/*
	* configuration / global variables
	*/
	cuone_config["external_link_expression"]	= "cuone"; "javascript:close()";
	cuone_config["speedbump_copy"]				= "You are now leaving the Credit Union ONE Web site. Credit Union ONE is not responsible for the products, services, or content of the site you are about to enter; and our Privacy Policy and Terms & Conditions do not apply. Please review the third-party site's Privacy Disclosures and Terms & Conditions for more information.  Thank you!";

	/*
	* configuration / global variables - IE-specific
	*/
	if (Browser.Engine.trident) {
		cuone_config["IEVariable"]			= "value";
	}

	/*
	* configuration / global variables - IE6-specific
	*/
	if (Browser.Engine.trident4) {
		cuone_config["IE6Variable"]			= "value";
	}

	/*
	* CUONE assets
	*/
	cuone_assets.push(new Request.HTML({async: false, evalResponse: true}).get("/common/js/classes/Overlay_c.js"));

	/*
	* global variables - for internal script use only, do not modify
	*/
	cuone_global["speedbump_href"]				= null;
	cuone_global["js_heightMatch_maxHeight"]	= 0;

	cuone_constructElements();
	cuone();

}

function cuone_constructElements() {

	/*
	* speedbump
	*/
	new Element("div", {'id': "speedbump", 'class': "mainTxt"}).setStyle("opacity", 0).inject(document.body, "top");
	new Element("div", {'class': "btn btn_ok"}).inject($("speedbump"), "top").addEvent("click", function() {
		window.location = cuone_global["speedbump_href"];
	});
	new Element("div", {'class': "btn btn_cancel"}).inject($("speedbump"), "top").addEvent("click", function() {
		dialog.fade("hide");
	});
	new Element("div", {'class': "icon"}).inject($("speedbump"), "top");
	new Element("div", {
		'class':	"copy",
		'html':		cuone_config["speedbump_copy"]
	}).inject($("speedbump"), "top");
	// end speedbump

}

function cuone() {

	/*
	* Accordion on home page
	*/
	
	if ($defined($('accordion'))) {
		var accordion = new Accordion("h5.toggler", "div.stretch", {show: $random(0, $$("div.stretch").length-1)});
	}
	
	if ($defined($('accordion1'))) {
		var accordion = new Accordion("h5.toggler1", "div.stretch1", {show: $(0, $$("div.stretch").length-1)});
	}

	if ($defined($('accordion2'))) {
		var accordion = new Accordion("h5.toggler2", "div.stretch2", {show: $(0, $$("div.stretch2").length-1)});
	}
	
	if ($defined($('accordion3'))) {
		var accordion = new Accordion("h5.toggler3", "div.stretch3", {show: $(0, $$("div.stretch3").length-1)});
	}
	
	/*
	* Overlay & dialog (modal)
	*/
	try
	{
	        dialog = new Overlay.Dialog($("speedbump"));
	        overlay = new Overlay.Screen({
		        'opacity':	0.6,
		        'styles': {
			        'min-width': $("mainWrap").getSize().x
		        }
	        });
	}
	catch(e)
	{
	    return;
	}

	overlay.addEvent("visible", function() {
		dialog.fade("in");
	});
	dialog.addEvent("hidden", function() {
		overlay.fade("out");
	});

	/*
	* External link hook
	*/
	$$("a").addEvents({
		'click': function(e) {
			if (!this.hasClass("nospeedbump") && this.hostname.length > 0 && this.hostname != location.hostname && !this.hostname.test(cuone_config["external_link_expression"])) {
				new Event(e).stop();
				cuone_global["speedbump_href"] = this.href;
				this.blur();
				overlay.fade("in");
			}
		}
	});

	/*
	* "hero" rotating content
	*/
	$$("a.heroThumb").each(
		function(element, i) {
			element.addEvent("click", function(e) {
				new Event(e).stop();
				$("hero").rotate(i);
			});
		}
	);

	$$("#hero").each(
		function(element, i) {

			element.store("divs", $$(".randomContent .randomItem"));

			element.rotate = function(index) {
				var container	= $E("div.rotate", this);
				var pool		= $E("div.randomContentPool", this);

				if (!$defined(container) || !$defined(pool))
					return;

				var div_old = $E("div.randomItem", container);

				var index = $numeric(index) ? index : $random(0, this.retrieve("divs").length-1);

				var div_new = this.retrieve("divs")[index];

				if (!$defined(div_new))
					return;

				$$(".heroThumbWrap .heroThumb").removeClass("heroThumbSelected");

				$$(".heroThumbWrap .heroThumb")[index].addClass("heroThumbSelected");

				pool.adopt(div_old);
				container.adopt(div_new);

			}
			element.rotate();
		}
	);

	/*
	* tabs
	*/
	$$(".js_tabSwitch").addEvents({
		'click': function() {
			this.className = this.className.replace("TSTabSel", "TSTab");
			this.className = this.className.replace("TSTab", "TSTabSel");

			$(this.id + "Contents").setStyle("display", "block");

			$$(".js_tabSwitch").each(
				function(item, n) {
					if (item != this) {
						item.className = item.className.replace("TSTabSel", "TSTab");
						$(item.id + "Contents").setStyle("display", "none");
					}
				},
				this
			);

			var defaultTab = Cookie.read("defaultTab");
			if ($defined(defaultTab) && $defined($("defaultTab")))
				$("defaultTab").checked = defaultTab == this.id ? true : false;
		}
	});
	
		/*
	* tabs2
	*/
	$$(".js_tabSwitch2").addEvents({
		'click': function() {
			this.className = this.className.replace("TSTabSel", "TSTab");
			this.className = this.className.replace("TSTab", "TSTabSel");

			$(this.id + "Contents").setStyle("display", "block");

			$$(".js_tabSwitch2").each(
				function(item, n) {
					if (item != this) {
						item.className = item.className.replace("TSTabSel", "TSTab");
						$(item.id + "Contents").setStyle("display", "none");
					}
				},
				this
			);

			var defaultTab = Cookie.read("defaultTab");
			if ($defined(defaultTab) && $defined($("defaultTab")))
				$("defaultTab").checked = defaultTab == this.id ? true : false;
		}
	});

	/*
	* default tab checkbox
	*/
	$$("#defaultTab").addEvents({
		'click': function() {
			if (this.checked) {
				Cookie.write("defaultTab", $E(".TSTabSelBrown").id, {duration: 365});
			}
			else {
				Cookie.dispose("defaultTab");
			}
		}
	});

	/*
	* default tab selection
	*/
	{
		var defaultTab = Cookie.read("defaultTab");
		if ($defined(defaultTab) && $defined($(defaultTab)))
			$(defaultTab).fireEvent("click");
	}

	/*
	* Matching columns
	*/
	$$(".column").each(function(el, i) {
		cuone_global["js_heightMatch_maxHeight"] = Math.max(cuone_global["js_heightMatch_maxHeight"], el.getSize().y);
	});
	$$(".column").setStyle("height", cuone_global["js_heightMatch_maxHeight"]);

	$$(".equals").each(function(el, i) {
		cuone_global["js_heightMatch_maxHeight"] = Math.max(cuone_global["js_heightMatch_maxHeight"], el.getSize().y);
	});
	$$(".equals").each(function(el, i) {
		if (el.getSize().y < cuone_global["js_heightMatch_maxHeight"])
			new Element("div", {'class': "dummyExtender",'styles': {'height': cuone_global["js_heightMatch_maxHeight"] - el.getSize().y}}).inject(el, "bottom");
	});
}

function $numeric(i) { return (!isNaN(i*1) && i.toString().trim().length > 0); }

/*
* Redefine useful (but deprecated) MooTools functions.
*/
function $E(selector, element) {
	element = $defined(element) ? element : document;
	return element.getElement(selector);
}

function $ES(selector, element) {
	element = $defined(element) ? element : document;
	return element.getElements(selector);
}

