/*
	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};
}

//Initialize link behavior based on class names or href.
function initLinks(parent) {
	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;
		
		//Video portal
		if(!behavior && result.href.indexOf("cfml/video/") != -1) {
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "large");
				e.preventDefault();
			});
			behavior = true;
		}
		
		//Photo gallery
		if(!behavior && result.href.indexOf("cfml/photo/") != -1) {
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "large");
				e.preventDefault();
			});
			behavior = true;
		}
	});
}

function initSite() {
	initLinks();
}

window.onload = initSite;

