/*
 * video.js
 *
 * Author: Diego Perini
 * Update: 20070410
 *
 * Unobtrusive javascript video control
 *
 */
new function() {

	// customize your ID's...
	var CONTAINER_ID = 'nw_video_container';
	var ACTIVATOR_ID = 'nw_video_activator';

	document.onclick =
		function (e) {
			e = e || window.event;
			var object = e.target || e.srcElement;
			if (object.parentNode.nodeName.toLowerCase() == 'a') {
				object = object.parentNode;
			}
			if (object && object.id == ACTIVATOR_ID) {
				// insert video clip from href
				insertVideo(object.href);
				// prevent default click action
				if (e.preventDefault) {
					e.preventDefault();
				} else {
					e.returnValue = false;
				}
			}
		};

	window.onunload =
		function (e) {
			document.onclick = null;
			this.onunload = null;
		};

	function insertVideo(source) {
		var container = document.getElementById(CONTAINER_ID);
		var object = addObject({
//			'id':'nw_video_clip',
			'width':425,
			'height':350,
			'data':source,
			'parms':{
//				'menu':true,
//				'loop':true,
				'movie':source
//				'wmode':'opaque',
//				'allowScriptAccess':'sameDomain'
			},
			'type':'application/x-shockwave-flash'
		}, container);
		if (container) {
			container.style.visibility = 'visible';
			container.style.display = 'block';
		}
		return object;
	}

	// add object element to container
	// options is an object containing name/value pairs and a
	// nested object containing name/value pairs for param tags
	function addObject(options, container) {
		var d, i, j, param, object;
		// find document from container
		if (container) {
			d = container.ownerDocument || container.document || container;
		} else {
			d = document;
		}
		// create new object element
		object = d.createElement('object');
		if (container) {
			// cleanup previous content
			while (container.hasChildNodes()) {
				container.removeChild(container.lastChild);
			}
			// insert object element in container
			container.insertBefore(object, null);
		}
		for (i in options) {
			if (typeof options[i] == 'object') {
				// create param elements
				for (j in options[i]) {
					param = d.createElement('param');
					param.setAttribute('name', j);
					param.setAttribute('value', options[i][j]);
					object.insertBefore(param, null);
				}
			} else {
				// set element properties
				object.setAttribute(i, options[i]);
			}
		}
		return object;
	}

};

