﻿// javascript functions common to all TCUL project designs

var _rotatingSidebarGroups = []; // only the groups
var _rotatingSidebars = []; // all rotating sidebars on this page

$(document).ready(function() {
	// suppress the devexpress "trial mode" warning - although we should probably just figure out why this is showing up [jj 09Oct07]
	$("body > div:first").hide();

	// remove last (errant) sidebar divider [jj 10Jul21]
	// $(".attachmentDiv:last").css("border", "solid 1px #ffffff");
	// $("div#rightColumnInnerDiv div#attachmentDiv:last div.attachmentDiv:last").css("border", "solid 1px #ffffff");
	$(".sidebar_bottom_div:last").css("border", "solid 1px #ffffff");

	$("#bannerNav .navItem:last").css("padding-right", "0px");
	$("#bannerNav .navItem:last").css("border", "solid 0px #ffffff");

	$("span#viewDebug").click(function() {
		//$(this).hide();
		$("div.debug").toggle();
	});

	// rotate sidebars
	_rotatingSidebarGroups.every(function(group) {
		_rotatingSidebars.every(function(sidebar) {
			if (sidebar.group == group) {
				ShowRotatingSidebar(sidebar);
				return false;
			}
			return true;
		});
		return true;
	});

	CheckLinks();
});

// checks all links on the page - if one is "external", pipe it through an interstitial page (_internalDomains is defined in main.Master)
function CheckLinks() { // rewritten [jj 07Mar6]
	for (var i = 0; i < document.links.length; i++) {
		var linkUrl = document.links[i].toString();
		var isInternal = false;
		if (linkUrl.substr(0, 11) == "javascript:") isInternal = true;
		for (var j = 0; j < _internalDomains.length; j++)
			if (linkUrl.indexOf("//" + _internalDomains[j]) != -1) isInternal = true;

		// exception: "/sites/25043ee8-a7e7-4aeb-8304-a91b5e7553c9/uploads/" // PCR [jj 10Feb26]
		if (linkUrl.indexOf("/sites/") != -1) isInternal = false;

		if (!isInternal) {
			document.links[i].target = "_blank";
			// removed interstitial page PCR [jj 10Feb12]
			// document.links[i].href = "/UPcms/designs/CUPassport/interstitial.aspx?url=" + document.links[i].href;
			document.links[i].href = document.links[i].href;
		}
	}
}

function ShowRotatingSidebar(currentSidebar) {
	// turn all the ones in this group off
	_rotatingSidebars.every(function(sidebar) {
		if (sidebar.group == currentSidebar.group) $("div#" + sidebar.sidebarId).hide();
		return true;
	});

	$("div#" + currentSidebar.sidebarId).show();

	var nextSidebar = "";
	var nextOne = false;
	_rotatingSidebars.every(function(sidebar) {
		if (sidebar.group == currentSidebar.group) {
			if (nextSidebar == "") nextSidebar = sidebar;
			if (nextOne) {
				nextSidebar = sidebar;
				return false;
			}
			if (sidebar.sidebarId == currentSidebar.sidebarId) nextOne = true;
		}
		return true;
	});

	setTimeout(function() { ShowRotatingSidebar(nextSidebar) }, currentSidebar.delay * 1000);
}

function CookiesTest() {
	// set a cookie
	$.cookie('cookie_test', 'the_value');
	// then retreive it
	return ($.cookie('cookie_test') == 'the_value');
}

// necessary because IE is not a very good browser [jj 10Jan28] (from http://www.hunlock.com/blogs/Mastering_Javascript_Arrays)
if (!Array.prototype.every) { Array.prototype.every = function(fun /*, thisp*/) { var len = this.length >>> 0; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this && !fun.call(thisp, this[i], i, this)) return false; } return true; }; }
if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/) { var len = this.length; var from = Number(arguments[1]) || 0; from = (from < 0) ? Math.ceil(from) : Math.floor(from); if (from < 0) from += len; for (; from < len; from++) { if (from in this && this[from] === elt) return from; } return -1; }; }
