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

//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(result.href.indexOf("/video/") != -1) {
			dojo.addClass(result, "LinkVideo");
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "large");
				e.preventDefault();
			});
			behavior = true;
		}
		
		//Pop-up windows
		if(dojo.hasClass(result, "LinkPopupMedium") || dojo.hasClass(result, "LinkPopup")) {
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "medium");
				e.preventDefault();
			});
			behavior = true;
		}
		if(dojo.hasClass(result, "LinkNewWindow")) {
			dojo.connect(result, "onclick", function (e) {
				openWindow(e.currentTarget.href, "new");
				e.preventDefault();
			});
			behavior = true;
		}
	});
}

function initBroadcast() {
	var home_page = dojo.byId("Home");
	var broadcast = dojo.byId("Broadcast");
	var featured_video = dojo.byId("FeaturedVideo");
	
	var href = "/";
	if(document.location.href.indexOf("STAGE-SNWA") != -1) {
		href = "/STAGE-SNWA/";
	}
	
	if(home_page) {
		if(broadcast) {
			dojo.xhrGet({
				url: href + "cfml/video/broadcast.cfml",
				load: broadcastCallback,
				preventCache: true,
				error: dataError
			});
		}
		
		if(featured_video) {
			dojo.xhrGet({
				url: href + "cfml/video/broadcast.cfml?notice",
				load: broadcastNoticeCallback,
				preventCache: true,
				error: dataError
			});
		}
	} else {
		if(featured_video) {
			dojo.xhrGet({
				url: href + "cfml/video/broadcast.cfml?notice_sub",
				load: broadcastNoticeCallback,
				preventCache: true,
				error: dataError
			});
		}
	}
}

function broadcastNoticeCallback(response, ioArgs) {
	if(response.length > 0 && response != "false") {
		var featured_video = dojo.byId("FeaturedVideo");
		featured_video.innerHTML = response;
		
		initLinks(featured_video);
	}
	return response;
}

function broadcastCallback(response, ioArgs) {
	if(response.length > 0 && response != "false") {
		var broadcast = dojo.byId("Broadcast");
		broadcast.innerHTML = response;
		
		initLinks(broadcast);
		
		initVideoPlayer(dojo.byId("VideoBroadcast"), dojo.byId("VideoBroadcastInfo").value, true);
	}
	return response;
}

function initVideoPlayer() {
	var video_container = dojo.byId("VideoContent");
	
	var href = "/";
	if(document.location.href.indexOf("STAGE-SNWA") != -1) {
		href = "/STAGE-SNWA/";
	}
	
	if(video_container) {
		var video_player = new SWFObject(href + "cfml/video/swf/player.swf", "VideoPlayer", "380", "234", "10", "");
		var video_params = dojo.byId("VideoBroadcastInfo").value;
		video_player.addParam("allowscriptaccess", "always");
		video_player.addParam("allowfullscreen", "true");
		video_player.addParam("flashvars", video_params + "&linktarget=_top&link=javascript: openBroadcast('VideoPlayer');");
		video_player.write("Video");
	}
}

function openBroadcast(video_id) {
	var href = "/";
	if(document.location.href.indexOf("STAGE-SNWA") != -1) {
		href = "/STAGE-SNWA/";
	}
	
	dojo.byId(video_id).sendEvent("STOP", null);
	openWindow(href + "cfml/video/broadcast_view.cfml", {width: 575, height: 352});
}

function initSite() {
	initLinks();
	initBroadcast();
}

dojo.addOnLoad(initSite);
