//---------------------------------------- Windows ----------------------------------------
/*
	Attaches the onclick behavior to elements with specific class names.  The dimensions of the window depend on the class name.
	
	Parameters:
	Name	Type			Default		Description
	parent	object						(Optional) Object to initialize child elements.
	
	Returns:
	Type	Description
	void
*/
function initWindows(parent) {
	if(parent) {
		var popup_small = dojo.query(".LinkPopupSmall", parent);
		var popup_medium = dojo.query(".LinkPopupMedium", parent);
		var popup_large = dojo.query(".LinkPopupLarge", parent);
		var new_window = dojo.query(".LinkNewWindow", parent);
	} else {
		var popup_small = dojo.query(".LinkPopupSmall");
		var popup_medium = dojo.query(".LinkPopupMedium");
		var popup_large = dojo.query(".LinkPopupLarge");
		var new_window = dojo.query(".LinkNewWindow");
	}
	
	//LinkPopupSmall
	dojo.forEach(popup_small, function (result) {
		dojo.connect(result, "onclick", function (e) {openWindow(e.currentTarget.href, "small"); e.preventDefault();});
	});
	
	//LinkPopupMedium
	dojo.forEach(popup_medium, function (result) {
		dojo.connect(result, "onclick", function (e) {openWindow(e.currentTarget.href, "medium"); e.preventDefault();});
	});
	
	//LinkPopupMedium
	dojo.forEach(popup_large, function (result) {
		dojo.connect(result, "onclick", function (e) {openWindow(e.currentTarget.href, "large"); e.preventDefault();});
	});
	
	//LinkNewWindow
	dojo.forEach(new_window, function (result) {
		dojo.connect(result, "onclick", function (e) {openWindow(e.currentTarget.href, "new"); e.preventDefault();});
	});
}

/*
	Opens a new browser window with dimensions based on "type."  Custom dimensions may be used if "type" is an object containing width and height parameters.
	
	Parameters:
	Name	Type			Default		Description
	url		string						The URL location
	type	string/object				A string containing the class name, or an object containing specific width and height
	name	string						The name to assign to the window
	
	Returns:
	Type	Description
	void
*/
function openWindow(url, type, name) {
	if(!name) {
		name = "";
	}
	
	if(type&& type.width&& type.height) {
		window.open(url, name, "width=" + type.width + ", height=" + type.height + ", scrollbars, resizable");
	} else if(type) {
		var width = 800;
		var height = 600;
		switch(type) {
			case "small":
				width = 320;
				height = 240;
				break;
			case "medium":
				width = 640;
				height = 480;
				break;
			case "large":
				width = 800;
				height = 600;
				break;
		}
		if(type == "new") {
			var viewport = getViewport();
			var mod_width = Math.round(viewport.w * 0.8);
			var mod_height = Math.round(viewport.h * 0.8);
			window.open(url, name, "width=" + mod_width + ", height=" + mod_height + ", scrollbars, resizable, menubar, toolbar, location, status");
		} else {
			window.open(url, name, "width=" + width + ", height=" + height + ", scrollbars, resizable");
		}
	} else {
		window.open(url, name);
	}
}

//Return the width and height of the viewport
function getViewport() {
	var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	var height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	return {w: width, h: height};
}

//Request error handler.
function dataError(response, ioArgs) {
	console.error("HTTP status code: ", ioArgs.xhr.status);
	return response;
}

function initFeatured() {
	var rotation_interval = 8000;
	var featured = dojo.byId("Featured");
	if(featured) {
		var tab_content = dojo.query("#Featured .TabContent");
		if(tab_content[0]) {
			dojo.addClass(tab_content[0], "Active");
			
			var tabs = dojo.query("#Featured .Tab");
			dojo.forEach(tabs, function (result) {
				dojo.connect(result, "onclick", function (e) {
					var tab_content = dojo.query("#Featured .TabContent");
					dojo.forEach(tab_content, function (result) {
						dojo.removeClass(result, "Active");
					});
					var tab_content_id = "TabContent" + e.currentTarget.id.split("Tab")[1];
					dojo.addClass(dojo.byId(tab_content_id), "Active");
					
					var tabs = dojo.query("#Featured .Tab");
					dojo.forEach(tabs, function (result) {
						dojo.removeClass(result, "Active");
					});
					dojo.addClass(e.currentTarget, "Active");
				});
			});
			
			var rotate_tabs = setInterval(function () {
				// Rotate to the next tab if the mouse is not on the featured area
				if(!featured.hold) {
					var tabs = dojo.query(".Tab", dojo.byId("FeaturedTabs"));
					var active_tab_id;
					var next_tab_id;
					var tab_content_id;
					
					// Find the active tab index
					dojo.forEach(tabs, function (result, index) {
						if(dojo.hasClass(result, "Active")) {
							active_tab_id = index;
						}
					});
					
					// Calculate the next tab in sequence and restart at the first tab when the end is reached
					next_tab_id = active_tab_id + 1;
					if(tabs[active_tab_id + 1]) {
						next_tab_id = active_tab_id + 1;
					} else {
						next_tab_id = 0;
					}
					
					// Activate the next tab
					dojo.addClass(tabs[next_tab_id], "Active");
					dojo.removeClass(tabs[active_tab_id], "Active");
					
					// Show tab content
					var tab_content = dojo.query("#Featured .TabContent");
					dojo.forEach(tab_content, function (result) {
						dojo.removeClass(result, "Active");
					});
					tab_content_id = "TabContent" + tabs[next_tab_id].id.split("Tab")[1];
					dojo.addClass(tab_content_id, "Active");
				}
			}, rotation_interval);
			
			// Set flag to stop rotation on mouseover
			dojo.connect(featured, "onmouseover", function (e) {
				e.currentTarget.hold = true;
			});
			dojo.connect(featured, "onmouseout", function (e) {
				e.currentTarget.hold = false;
			});
		}
	}
}

function initStriping() {
	var stripe = dojo.query(".Stripe");
	dojo.forEach(stripe, function (result) {
		var rows = result.getElementsByTagName("tr"); 
		dojo.forEach(rows, function (result, index) {
			if(index % 2 != 1) {
				dojo.addClass(result, "StripeColor");
			}
		});
	});
}

//Initialize link behavior based on class names or href.
function initLinks(parent) {
	var site_domain = "springspreserve.org";
	var certificate_domain = "digicert.com";
	
	var anchor_links = [];
	
	if(parent) {
		anchor_links = dojo.query("a", parent).concat(dojo.query("area", parent));
	} else {
		anchor_links = dojo.query("a").concat(dojo.query("area"));
	}
	
	dojo.forEach(anchor_links, function (result) {
		var behavior = false;
		
		//Normalize hrefs for secure page
		if(document.location.href.indexOf("https://") == 0) {
			var current_anchor_text = result.innerHTML;
			result.href = result.href.replace("https://", "http://");
			result.innerHTML = current_anchor_text;
		}
		
		//Video portal
		if(!behavior && result.href.indexOf("apps/video/") != -1) {
			dojo.addClass(result, "LinkVideo");
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "large");
				e.preventDefault();
			});
			behavior = true;
		}
		
		//Photo gallery
		if(!behavior && result.href.indexOf("apps/photo_gallery/") != -1) {
			dojo.addClass(result, "LinkPhoto");
			dojo.connect(result, "onclick", function (e) {
				//openWindow(e.currentTarget.href, "large");
				window.open(e.currentTarget.href, "", "width=650, height=500, scrollbars, resizable");
				e.preventDefault();
			});
			behavior = true;
		}
		
		//Contact
		if(result.href.indexOf("apps/contact/") != -1) {
			dojo.addClass(result, "LinkEmail");
		}
		
		//Subscription
		if(result.href.indexOf("apps/subscription/") != -1) {
			dojo.addClass(result, "LinkSubscribe");
		}
		
		//Pop-up windows
		if(!behavior && dojo.hasClass(result, "LinkPopupSmall")) {
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "small");
				e.preventDefault();
			});
			behavior = true;
		}
		if(!behavior && dojo.hasClass(result, "LinkPopupMedium")) {
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "medium");
				e.preventDefault();
			});
			behavior = true;
		}
		if(!behavior && dojo.hasClass(result, "LinkPopupLarge")) {
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "large");
				e.preventDefault();
			});
			behavior = true;
		}
		if(!behavior && dojo.hasClass(result, "LinkNewWindow")) {
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "new");
				e.preventDefault();
			});
			behavior = true;
		}
		
		//Links
		var ext_pdf = ".pdf";
		if(result.href.indexOf(ext_pdf) != -1) {
			dojo.addClass(result, "LinkPDF");
		}
		
		var ext_doc = ".doc";
		if(result.href.indexOf(ext_doc) != -1) {
			dojo.addClass(result, "LinkDOC");
		}
		
		var ext_xls = ".xls";
		if(result.href.indexOf(ext_xls) != -1) {
			dojo.addClass(result, "LinkXLS");
		}
		
		var ext_jpg = ".jpg";
		if(result.href.indexOf(ext_jpg) != -1) {
			dojo.addClass(result, "LinkImage");
		}
		
		var ext_gif = ".gif";
		if(result.href.indexOf(ext_gif) != -1) {
			dojo.addClass(result, "LinkImage");
		}
		
		var ext_png = ".png";
		if(result.href.indexOf(ext_png) != -1) {
			dojo.addClass(result, "LinkImage");
		}
		
		var ext_zip = ".zip";
		if(result.href.indexOf(ext_zip) != -1) {
			dojo.addClass(result, "LinkArchive");
		}
		
		if(!behavior && ((result.href.indexOf(site_domain) == -1&& result.href.length > 0&& result.href.indexOf(certificate_domain) == -1) || dojo.hasClass(result, "LinkExternal"))) {
			dojo.addClass(result, "LinkExternal");
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "new");
				e.preventDefault();
			});
		}
	});
}

//Initialize close and print buttons for pop-up windows
function initPopup() {
	var popup_close = dojo.query(".PopupClose");
	dojo.forEach(popup_close, function (result) {
		dojo.connect(result, "onclick", function (e) {
			top.close();
			e.preventDefault();
		});
	});
	
	var popup_print = dojo.byId("PopupPrint");
	if(popup_print) {
		dojo.connect(popup_print, "onclick", function (e) {
			window.print();
			e.preventDefault();
		});
	}
}

//Initialize Dojo widgets
function initWidgets() {
	dojo.require("dojo.parser");
	dojo.addOnLoad(function () {dojo.parser.parse();});
	
	dojo.require("custom.Date");
	dojo.addOnLoad(initDatePicker);
}

//Initialize helper text behavior for date widgets
function initDatePicker() {
	dojo.query(".FormDateSelector input.dijitReset").forEach(function (date) {
		if(date.value == "") {
			dojo.addClass(date, "Default");
			dojo.connect(date, "onclick", function (e) {
				if(date.value == "mm/dd/yyyy") {
					dojo.removeClass(date, "Default");
					date.value = "";
				}
			});
			dojo.connect(date, "onfocus", function (e) {
				if(dojo.hasClass(date, "Default")) {
					dojo.removeClass(date, "Default");
					date.value = "";
				}
			});
			date.value = "mm/dd/yyyy";
		}
	});
}

// Google Analytics
function initGa() {
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	var head = document.getElementsByTagName("head")[0];
	var ga_script = document.createElement("script");
	ga_script.src = gaJsHost + "google-analytics.com/ga.js";
	ga_script.type = "text/javascript";
	head.appendChild(ga_script);
	dojo.addOnLoad(gaTrack);
}

function gaTrack() {
	try {
		var pageTracker = _gat._getTracker("UA-8961185-9");
		pageTracker._trackPageview();
	} catch(err) {}
}

function initSite() {
	initStriping();
	initLinks();
	initFeatured();
	initPopup();
	initGa();
}

dojo.addOnLoad(initSite);
