function ffGallerySlide(id, inc) {

	var galimages = window["ffGallery" + id + "Images"];
	var descs = window["ffGallery" + id + "Descs"];
	var newimage = window["ffGallery" + id + "Image"] + inc;
	var size = window["ffGallery" + id + "Size"] * 100;
	if (newimage < 0)
		newimage = galimages.length - 1;
	if (newimage >= galimages.length)
		newimage = 0;
	document.images["ffGallery" + id + "Slide"].src = galimages[newimage] + "&thumb=" + size;
	if (xBrowserGetElement("ffGallery" + id + "SlideTop"))
		xBrowserGetElement("ffGallery" + id + "SlideTop").innerHTML = "Image " + (newimage+1) + "/" + galimages.length;
	if (descs.length > 0)
		xBrowserGetElement("ffGallery" + id + "SlideDesc").innerHTML = descs[newimage];
	window["ffGallery" + id + "Image"] = newimage;
	
	if (newimage < galimages.length - 1)
		ffGalleryPreload(galimages[newimage+1] + "&thumb=" + size);

	if (newimage < galimages.length - 2)
		ffGalleryPreload(galimages[newimage+2] + "&thumb=" + size);	

}

function ffGalleryUpdateTimer(id, time) {
	if (window["ffGalleryTimer" + id])
		window.clearInterval(window["ffGalleryTimer" + id]);
	if (time > 0)
		window["ffGalleryTimer" + id] = window.setInterval("ffGallerySlide(" + id + ",1)", 1000*time);
}

function ffGalleryPreload(src) {
	var imagePL = new Image();
	imagePL.src = src;
}

function ffGalleryPagePreload(id, page) {

	/*if (page < 0) return;
	var galimages = window["ffGallery" + id + "Images"];
	var cols = window["ffGallery" + id + "Cols"];
	var rows = window["ffGallery" + id + "Rows"];
	var size = window["ffGallery" + id + "Size"] * 50;
	var pages = Math.ceil(galimages.length/(cols*rows));
	if (page >= pages) return;
	var startrow = page*rows;
		
	for (var i = startrow; i < startrow+rows; i++) {
		for (var j = 0; j < cols; j++) {
		
			if (galimages[(cols*i)+j] != null) {
				ffGalleryPreload(galimages[(cols*i)+j]);
			}
		}
	}*/

}

function ffGalleryLayoutTable(id, cols, rows, size, galimages, descs, controls) {

	size *= 50;
	var pages = Math.ceil(galimages.length/(cols*rows));
	document.write("<table width='100%' class='thumbnailtable' cellpadding=5 cellspacing=3>");
	if ((pages > 1) && controls) {
		document.write("<tr><td colspan=" + cols + " class='thumbnailstop'><center><span id='ffGallery" + id + "_top'>Page 1/" + pages + "</span></center></td></tr>");
	}
	for (var i = 0; i < rows; i++) {
		document.write("<tr>");
		var stop = false;
		for (var j = 0; j < cols; j++) {
		
				if (galimages[(cols*i)+j] == null) {
					document.write("<td>&nbsp;</td>");
					stop = true;
				}
				else {
					document.write("<td><center><a href=\"" + galimages[(cols*i)+j] + "\" target='_blank'><img width=" + size + "height=" + size + " border=0 id='ffGallery" + id + "_" + ((cols*i)+j) + "_img' src=\"" + galimages[(cols*i)+j] + "&thumb=" + size + "\"></a>");
					if (descs.length > 0)
						document.write("<div id='ffGallery" + id + "_" + ((cols*i)+j) + "_div' class='thumbnaildesc' style='text-align: center'>" + descs[(cols*i)+j] + "</div>");
					document.write("</center></td>");
				}
		}
		document.write("</tr>");
		if (stop) break;
	}
	if ((pages > 1) && (controls)) {
		document.write("<tr><td colspan=" + cols + "><center><a href='javascript:ffGalleryRelativePage(" + id + ",-1)'>&lt; Back</a> ");
		for (var k = 0; k < pages; k++) {
			document.write("<a href='javascript:ffGalleryPage(" + id + "," + (k) + ")'>" + (k+1) + "</a> ");
		}
		document.write("<a href='javascript:ffGalleryRelativePage(" + id + ",1)'>Forward &gt;</a></center></td></tr>");
	}
	document.write("</table>");
	if (pages > 1)
		ffGalleryPagePreload(id, 1);
	
}

function ffGalleryPage(id, page) {

	if (page < 0) return;
	var galimages = window["ffGallery" + id + "Images"];
	var descs = window["ffGallery" + id + "Descs"];
	var cols = window["ffGallery" + id + "Cols"];
	var rows = window["ffGallery" + id + "Rows"];
	var size = window["ffGallery" + id + "Size"] * 50;
	var pages = Math.ceil(galimages.length/(cols*rows));
	if (page >= pages) return;
	var startrow = page*rows;
		
	for (var i = startrow; i < startrow+rows; i++) {
		for (var j = 0; j < cols; j++) {
		
			if (galimages[(cols*i)+j] != null) {
				if (descs.length > 0)
					xBrowserGetElement("ffGallery" + id + "_" + ((cols*(i-startrow))+j) + "_div").innerHTML = descs[(cols*i)+j];
				xBrowserGetElement("ffGallery" + id + "_" + ((cols*(i-startrow))+j) + "_img").src = "Templates/pixel.gif";
				xBrowserGetElement("ffGallery" + id + "_" + ((cols*(i-startrow))+j) + "_img").src = galimages[(cols*i)+j] + "&thumb=" + size;
				xBrowserGetElement("ffGallery" + id + "_" + ((cols*(i-startrow))+j) + "_img").parentNode.href = galimages[(cols*i)+j];
				xBrowserGetElement("ffGallery" + id + "_" + ((cols*(i-startrow))+j) + "_img").parentNode.style.visibility = "";
			}
			else {
				if (descs.length > 0)
					xBrowserGetElement("ffGallery" + id + "_" + ((cols*(i-startrow))+j) + "_div").innerHTML = "";
				xBrowserGetElement("ffGallery" + id + "_" + ((cols*(i-startrow))+j) + "_img").parentNode.style.visibility = "hidden";
			}
		}
	}
	
	window["ffGallery" + id + "Page"] = page;
	xBrowserGetElement("ffGallery" + id + "_top").innerHTML = "Page " + (page+1) + "/" + pages;
	if (page < (pages - 1))
		ffGalleryPagePreload(id, page + 1);

}

function ffGalleryRelativePage(id, relpage) {
	ffGalleryPage(id, window["ffGallery" + id + "Page"] + relpage);
}

function ShowDialog(url) {
	showDialog(url, "no");
}
function ShowDialog(url, scrollbars) {
	// increased because of SP2 and its insistence on status bar
	var height = 470;
	var width = 350;
	var windowname = "ffDialog";
	windowname += Math.round(Math.random() * 10000) + 1;
	var top = Math.round((screen.availHeight/2) - (height/2));
	var left = Math.round((screen.availWidth/2) - (width/2));
	var newWindow = window.open(url, windowname, "status=no,directories=no,toolbar=no,scrollbars=" + scrollbars + ",resizable=no,location=no,menu=no,width=" + width + ",height=" + height + ",screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left);
	newWindow.focus();
}
function ModifyComponent(id, type) {

	if (type == "html") {
		var url = "Admin/modifyHtml.aspx?id=" + id;
		var width = 700;
		var height = 500;
		var windowname = "ffModify";
		windowname += Math.round(Math.random() * 10000) + 1;
		var top = Math.round((screen.availHeight/2) - (height/2));
		var left = Math.round((screen.availWidth/2) - (width/2));
		var newWindow = window.open(url, windowname, "status=no,directories=no,toolbar=no,resizable=yes,location=no,menu=no,width=" + width + ",height=" + height + ",screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left);
		newWindow.focus();
	}

	else if (type == "RSS") {
		ShowDialog("Admin/modifyNewsSummary.aspx?id=" + id);
	}

	else if (type == "progress") {
		ShowDialog("Admin/modifyProgressCheck.aspx?id=" + id);
	}

	else if (type == "question") {
		var url = "Admin/modifyQuestion.aspx?id=" + id;
		var width = 600;
		var height = 380;
		var windowname = "ffModify";
		windowname += Math.round(Math.random() * 10000) + 1;
		var top = Math.round((screen.availHeight/2) - (height/2));
		var left = Math.round((screen.availWidth/2) - (width/2));
		var newWindow = window.open(url, windowname, "status=no,directories=no,toolbar=no,resizable=no,location=no,menu=no,width=" + width + ",height=" + height + ",screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left);
		newWindow.focus();
	}

	else if (type == "calendar") {

		var url = "Admin/modifyCalendar.aspx?id=" + id;
		var width = 600;
		var height = 400;
		var windowname = "ffModify";
		windowname += Math.round(Math.random() * 10000) + 1;
		var top = Math.round((screen.availHeight/2) - (height/2));
		var left = Math.round((screen.availWidth/2) - (width/2));
		var newWindow = window.open(url, windowname, "status=no,directories=no,toolbar=no,resizable=no,location=no,menu=no,width=" + width + ",height=" + height + ",screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left);
		newWindow.focus();

	}
	
	else if (type == "gallery" || type == "resourcelist") {

		var url = "Admin/modifyGallery.aspx?id=" + id;
		var width = 450;
		var height = 340;
		var windowname = "ffModify";
		windowname += Math.round(Math.random() * 10000) + 1;
		var top = Math.round((screen.availHeight/2) - (height/2));
		var left = Math.round((screen.availWidth/2) - (width/2));
		var newWindow = window.open(url, windowname, "status=no,directories=no,toolbar=no,resizable=no,location=no,menu=no,width=" + width + ",height=" + height + ",screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left);
		newWindow.focus();

	}
	
	else if (type == "plugin") {

		var url = "Admin/modifyPluginComponent.aspx?id=" + id;
		var width = 450;
		var height = 340;
		var windowname = "ffModify";
		windowname += Math.round(Math.random() * 10000) + 1;
		var top = Math.round((screen.availHeight/2) - (height/2));
		var left = Math.round((screen.availWidth/2) - (width/2));
		var newWindow = window.open(url, windowname, "status=no,directories=no,toolbar=no,resizable=no,location=no,menu=no,width=" + width + ",height=" + height + ",screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left);
		newWindow.focus();

	}
	
}
function PageMakeupDelete(page, component) {
	if (window.confirm("Are you sure you want to delete the selected component?"))
		DoHiddenDialog("Admin/pageMakeupDelete.aspx?page_id=" + page + "&component_id="+ component);
}
function DoHiddenDialog(url) {
	window.open(url, "ffHidden", "status=no,directories=no,toolbar=no,resize=no,location=no,menu=no,width=10,height=10");
	window.focus();
}
function ControlPanel() {
	ShowDialog("Admin/controlpanel.aspx", "no", "440");
}
function ResourceManager(id) {
	var url = "Admin/editor/resourcesManager.aspx?section_id=" + id;
		var width = 700;
		var height = 450;
		var windowname = "ffRM";
		windowname += Math.round(Math.random() * 10000) + 1;
		var top = Math.round((screen.availHeight/2) - (height/2));
		var left = Math.round((screen.availWidth/2) - (width/2));
		var newWindow = window.open(url, windowname, "status=no,directories=no,toolbar=no,resizable=yes,location=no,menu=no,width=" + width + ",height=" + height + ",screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left);
		newWindow.focus();
}
function SiteManager(id) {
	ShowDialog("Admin/sitemanager.aspx?id=" + id);
}
function ChangePassword() {
	ShowDialog("Admin/changePassword.aspx");
}
function ShowStatus(text) {
	window.status = text;
	return true;
}

function ModifyPage(id) {
	ShowDialog("Admin/pagesModify.aspx?id=" + id);
}

function InsertPage(section_id) {
	ShowDialog("Admin/pagesModify.aspx?section_id=" + section_id);
}

function ImportExport(page_id) {
	ShowDialog("Admin/importExport.aspx?page_id=" + page_id);
}

function NewsManager(section_id) {
	ShowDialog("Admin/newsManager.aspx?section_id=" + section_id);
}

function ChooseHtml(page_id) {
	var url = "Admin/chooseHtml.aspx?page_id=" + page_id;
	var width = 700;
	var height = 500;
	var windowname = "ffModify";
	windowname += Math.round(Math.random() * 10000) + 1;
	var top = Math.round((screen.availHeight/2) - (height/2));
	var left = Math.round((screen.availWidth/2) - (width/2));
	var newWindow = window.open(url, windowname, "status=no,directories=no,toolbar=no,resizable=yes,location=no,menu=no,width=" + width + ",height=" + height + ",screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left);
	newWindow.focus();
}


function DeletePage(id) {
	ShowDialog("Admin/pagesDelete.aspx?id=" + id, "auto");
}

function UndeletePage(id) {
	ShowDialog("Admin/pagesUndelete.aspx?id= " + id, "auto");
}

function ConfirmURL(url, text) {
	if (window.confirm(text))
		DoHiddenDialog(url);
}

function getCookie(strName) {
	var cookies, bits;
	cookies = document.cookie.split(";");
	for (i = 0; i < cookies.length; i++) {
		cookies[i] = cookies[i].replace(" ", "");
		bits = cookies[i].split("=");
		if (bits[0] == strName) { return bits[1]; }
	}
	return false;
}

function setTempCookie(strName, strValue) {
	document.cookie = strName + "=" + strValue + "; ";
}

function ffRedirect(url, referrer) {
	location.href = url;
}

function xBrowserGetElement(elename) {
	// support for ie4/ie5/ie6/moz5+
	if (document.all) { return document.all[elename]; }
	else { return document.getElementById(elename); }
}

function ffLinkHelper() {
	var contentDiv = xBrowserGetElement("content");
	var leftBar = xBrowserGetElement("ffLB");
	var networkOnlyLinks = 0;

	window.defaultStatus = "Building page links...";

	networkOnlyLinks += ffLinkHelperDoObj(contentDiv);
	networkOnlyLinks += ffLinkHelperDoObj(leftBar);

	if (networkOnlyLinks > 0) {
		contentDiv.innerHTML = "<div style='border: 2px solid red; padding: 3px; margin-top: 3px'><b>This page contains links that will only function correctly when using the intranet from within the school network.  Such links are marked with this icon: <img src='Templates/linkfile.gif'>.</div>" + contentDiv.innerHTML;
	}

	window.defaultStatus = "";
	ffImageHelper(contentDiv);
}
function ffImageHelper(container) {
	var images = container.getElementsByTagName("IMG");
	for (i = 0; i < images.length; i++) {
		if (images[i].style.height && (images[i].src.indexOf("image.aspx") != -1)) {
			images[i].src += "&height=" + parseInt(images[i].style.height) + "&width=" + parseInt(images[i].style.width);
		}
	}
}
function ffLinkHelperDoObj(node) {

	contentLinks = node.getElementsByTagName("A");
	var thisServer = location.protocol + "//" + location.host + location.port + location.pathname;
	var networkOnlyLinks = 0;

	for (i = 0; i < contentLinks.length; i++) {

			if (contentLinks[i].href && contentLinks[i].innerText != "") {
				tsrc = ""; talt = ""; ttarget="";
				if (contentLinks[i].href.length > 7) {
					if (contentLinks[i].href.substring(0,7) == "mailto:") { tsrc = 'Templates/linkmail.gif'; talt='Link to e-mail address'; }
					if (contentLinks[i].href.substring(0,7) == "http:\/\/") { 
						// check no same server link
						if (contentLinks[i].href.length >= thisServer.length) {
							if (contentLinks[i].href.substring(0, thisServer.length) != thisServer) {
								tsrc='Templates/linkext.gif'; talt='Link to external web site (opens in a new window)'; ttarget = "_new";
							}
						} else {
							tsrc= 'Templates/linkext.gif'; talt='Link to external web site (opens in a new window)'; ttarget = "_new";
						}
					}
					if (contentLinks[i].href.substring(0,7) == "file:\/\/") {  networkOnlyLinks++; tsrc='Templates/linkfile.gif'; talt='Link to file on network share (only available on internal network)';  }

				}
				if (tsrc != "") {
					// we have a glyph to add
				if (!document.all) {
					var newglyph = document.createElement("img");
					var src = document.createAttribute("src"); src.value = tsrc; newglyph.setAttributeNode(src);
					var border = document.createAttribute("border"); border.value = "0"; newglyph.setAttributeNode(border);
					var alt = document.createAttribute("alt"); alt.value = talt; newglyph.setAttributeNode(alt);
					contentLinks[i].appendChild(newglyph);
					newglyph.style.marginLeft = "3px";
				}
				else {
					contentLinks[i].innerHTML += "<img src='" + tsrc + "' alt='" + talt + "' border=0 style='margin-left: 3px'>";
				}
					if (ttarget != "") { contentLinks[i].target = ttarget; }
				}
			}

	}
	return networkOnlyLinks;
}


function getTop(obj) {

	var top = obj.offsetTop;

	if (obj.tagName.toLowerCase() != "body") {
		top += getTop(obj.offsetParent);
	}

    return top;
}

function getLeft(obj) {

    var left = obj.offsetLeft;

	if (obj.tagName.toLowerCase() != "body") {
		left += getLeft(obj.offsetParent);
	}

    return left;
}

function getScrollTop(obj) {

	var top = 0
	if (obj.tagName)
		top = obj.scrollTop;
	
	if (obj.parentElement)
		top += getScrollTop(obj.parentElement);

	else if (obj.parentNode)
		top += getScrollTop(obj.parentNode);

    return top;
}

function getScrollLeft(obj) {

	var left = 0
	if (obj.tagName)
		left = obj.scrollLeft;

	if (obj.parentElement)
		left += getScrollLeft(obj.parentElement);

	else if (obj.parentNode)
		left += getScrollLeft(obj.parentNode);

    return left;
}

function jsError() {

	return true;

}

window.onerror = jsError;
