/**
 * Functions for displaying a video in a div popup
 */
function initVideo(anchors)
{
	var dim;
	var overlay = null;
	var videoDiv = null;
	var playerDiv = null;
	var body;
	var details;
	var buttons;
	var img;
	var currentIndex;
	var currentWidth;
	var currentHeight;
	var bottom = null;
	var showFile;
	var title;
	var videos = [];
	var borders;
	
	var options = typeof VideoOptions != "undefined" ? VideoOptions : {};
	if (!options.buttonImageClose)
		options.buttonImageClose = common_resource_path + "images/gallery/close.gif";
	if (!options.css)
		options.css = "css/videoPopup.css";
	if (!options.overlayColor)
		options.overlayColor = "black";
	if (!options.overlayOpacity)
		options.overlayOpacity = 40;
	if (!options.volume)
		options.volume = "100";
	if (!options.previewImage)
		options.previewImage = true;
	if (!options.showFullscreenButton)
		options.showFullscreenButton = "1";
	if (!options.allowFullScreen)
		options.allowFullScreen = "true";
	if (!options.autoRepeat)
		options.autoRepeat = "0";
	if (!options.autoPlay)
		options.autoPlay = "0";
	if (!options.hideControls)
		options.hideControls = "0";
	if (!options.ignoreAspectRatio)
		options.ignoreAspectRatio = "0";
	if (!options.playerSkin)
		options.playerSkin = "simple";
	
	var bottomHeight = 40;
	include(options.css);
	
	var getIndex = function(anchor)
	{
		if (typeof anchor.index != "undefined")
			return anchor.index;
		
		var i = 0;
		var found = false;
		while(i<anchors.length && !found)
		{
			if (anchors[i] == anchor)
				found = true;
			else
				i++;
		}
		
		return found ? i : -1;
	};
	
	var resize = function()
	{
		var dim = getScreenDimensions();
		overlay.style.top = dim.scrollTop + "px";
		overlay.style.left = dim.scrollLeft + "px";
		overlay.style.width = "100%";
		overlay.style.height = dim.availHeight + "px";
		overlay.style.zIndex = "300000";
		
		videoDiv.style.left = dim.scrollLeft + Math.floor((dim.availWidth - videoDiv.offsetWidth) / 2) + "px";
		videoDiv.style.top = dim.scrollTop + Math.floor((dim.availHeight - videoDiv.offsetHeight) / 2) + "px";
	};
	
	var fadeFromTo = function(obj, from, to, onDone)
	{
		var opacity = from;
		var handler = window.setInterval(function()
		{
			opacity -= from > to ? 10 : -10;
			if ((from > to && opacity <= to) || (from < to && opacity >= to))
			{
				opacity = to;
			}
				
			setOpacity(obj, opacity);
			if (opacity == to)
			{
				window.clearInterval(handler);
				if (onDone)
				{
					onDone();
				}
			}
		}, 40);
		return handler;
	};
	
	var hide = function()
	{
		fadeFromTo(overlay, options.overlayOpacity, 0, function()
		{
			body.removeChild(overlay); ///
		});
		
		var flashChild = null;
		flashChild = getFlash(playerDiv);
		if (flashChild != null)
		{
			flashChild.parentNode.removeChild(flashChild);
		}
		
		videoDiv.removeChild(playerDiv); ///
		videoDiv.parentNode.removeChild(videoDiv);

		window_detachEvent("onresize", resize);
		window_detachEvent("onscroll", resize);
	};
	
	var resizeFromTo = function(w1, h1, w2, h2, onDone)
	{
		var w = w1;
		var h = h1;
		videoDiv.style.width = w  + "px";
		videoDiv.style.height = h + "px";

		var handler = window.setInterval(function()
		{
			w = w + (w2 > w1 ? Math.ceil((w2 - w)/2) : Math.floor((w2 - w)/2));
			h = h + (h2 > h1 ? Math.ceil((h2 - h)/2) : Math.floor((h2 - h)/2));
			if ((w2 > w1 && w > w2) || (w2 < w1 && w < w2))
				w = w2;
			if ((h2 > h1 && h > h2) || (h2 < h1 && h < h2))
				h = h2;
			
			videoDiv.style.width = w  + "px";
			videoDiv.style.height = h + "px";
			videoDiv.style.left = dim.scrollLeft + Math.floor((dim.availWidth - videoDiv.offsetWidth) / 2) + "px";
			videoDiv.style.top = dim.scrollTop + Math.floor((dim.availHeight - videoDiv.offsetHeight) / 2) + "px";
			
			if (w == w2 && h == h2)
			{
				window.clearInterval(handler);
				if (onDone)
					onDone();
			}
		}, 40);
	};

/*	var tempImage = document.createElement("img");
	var counter = 0;
	
	var fadeImage = function()
	{
		fadeFromTo(img, 0, 100);
	};

	var onloadImg = function()
	{
		img.detachEvent("onload", onloadImg);
		window.setTimeout(function()
		{
			fadeImage();
		}, 250);
	};
	
	var replaceImage = function()
	{
	};
	
	var updateButtons = function()
	{
		goLeft.style.display = currentIndex > 0 ? "" : "none";
		goRight.style.display = currentIndex < images.length - 1 ? "" : "none";
	};
	
	var previous = function()
	{
		if (currentIndex > 0)
		{
	 		currentIndex--;
			showFile(images[currentIndex]);
			updateButtons();
		}
	};
	
	var next = function()
	{
		if (currentIndex < images.length - 1)
		{
			currentIndex++;
			showFile(images[currentIndex]);
			updateButtons();
		}
	};
	
	var resizeButtons = function(width, height)
	{
		goLeft.style.width = Math.floor(width / 2) + "px";
		goLeft.style.height = height + "px";
		goRight.style.width = Math.floor(width / 2) + "px";
		goRight.style.height = height + "px";
		goRight.style.marginLeft = (width - Math.floor(width / 2) + 10) + "px";
	};*/

	var showVideo = function(video, file, width, height)
	{
		currentWidth = width;
		currentHeight = height;
		
		// add overlay
		overlay = document.createElement("div");
		overlay.style.overflow = "hidden";
		overlay.style.backgroundColor = options.overlayColor;
		overlay.style.position = "absolute";
		setOpacity(overlay, options.overlayOpacity);
		overlay.attachEvent("onclick", hide);
		
		body = document.getElementsByTagName("body")[0];
		body.appendChild(overlay);
		
		// add popup div
		videoDiv = document.createElement("div");
		videoDiv.className = "smi_video";
		if (options.loadingImage)
			videoDiv.style.backgroundImage = "url(" + options.loadingImage + ")";
		
		// add video player
		playerDiv = document.createElement("div");
		videoDiv.appendChild(playerDiv);
		
		var extension = file ? file.extension : false;
		var url = dircat(g_baseDir, "cms_file.php?fromDB=" + file.id + (in_array(extension,["mp4", "flv"]) ? ("&ext=" + extension) : ("&processed=" + videoProcessor)));
		
		if (options.previewImage)
			previewImg = dircat(g_baseDir, "cms_file.php?fromDB=" + file.id + "&forcePNG&ext=.png");
		
		var volume = options.volume;
		if (getCookie("movieplayer_volume"))
		{
			volume = getCookie("movieplayer_volume");
		}
		
		switch(options.playerSkin)
		{
			case "dark":
				var skin = "movieplayer2";
				break;
			default:
				var skin = "movieplayer_simple";
		}
		
		createFlash(playerDiv, dircat(common_resource_path, "flash/" + skin + ".swf"), width, height,
		{
			common_resource_path: common_resource_path,
			url: url,
			autoPlay: options.autoPlay,
			autoRepeat: options.autoRepeat,
			volume: volume,
			showFullscreenButton: options.showFullscreenButton,
			allowFullScreen: options.allowFullScreen,
			previewImg: options.previewImage ? previewImg : "false",
			ignoreAspectRatio: options.ignoreAspectRatio,
			hideControls: options.hideControls
		},
		function()
		{
			playerDiv.innerHTML = "Video niet beschikbaar. Download de laatste versie van de Flash speler a.u.b.";
		}, 
		extension == "mp4" ? 9 : 8,
		{
			onload: function(flash)
			{
				videoDiv.style.backgroundImage = "none";
			},
			volume: function(flash, args)
			{
				setCookie("movieplayer_volume", args[0]);
			}
		});
		
		// add resize events
		window_attachEvent("onresize", resize);
		window_attachEvent("onscroll", resize);
		resize();
		
		// add bottom bar
		bottom = document.createElement("div");
		bottom.className = "video_bottom";
		videoDiv.appendChild(bottom);
		
		// add details
		details = document.createElement("div");
		details.className = "video_details";
		bottom.appendChild(details);
		
		// add title
		title = document.createElement("div");
		title.appendChild(document.createTextNode(video.title));
		title.className = "video_title";
		details.appendChild(title);
		
		// add buttons
		buttons = document.createElement("div");
		buttons.className = "video_buttons";
		bottom.appendChild(buttons);
		
		var close = document.createElement("img");
		close.src = options.buttonImageClose;
		close.attachEvent("onclick", hide);
		close.style.cursor = "pointer";
		buttons.appendChild(close);
		
		// finally resize into view
		videoDiv.style.width = "40px";
		videoDiv.style.height = "40px";
		
		body.appendChild(videoDiv);
		borders = getBorders(videoDiv);
		playerDiv.style.display = "none";
		setOpacity(details, 0);
		setOpacity(buttons, 0);
		resizeFromTo(40, 40, width, height + bottomHeight, function()
		{
			playerDiv.style.display = "";
			fadeFromTo(buttons, 0, 100);
			fadeFromTo(details, 0, 100);
		});
	};
	
	showYoutube = function(anchor)
	{
		currentIndex = getIndex(anchor);
		var video = videos[currentIndex];
		
		dim = getScreenDimensions();
		var width = 480;
		var height = 385;
		currentWidth = width;
		currentHeight = height;
		
		// add overlay
		overlay = document.createElement("div");
		overlay.style.overflow = "hidden";
		overlay.style.backgroundColor = options.overlayColor;
		overlay.style.position = "absolute";
		setOpacity(overlay, options.overlayOpacity);
		overlay.attachEvent("onclick", hide);
		
		body = document.getElementsByTagName("body")[0];
		body.appendChild(overlay);
		
		// add popup div
		videoDiv = document.createElement("div");
		videoDiv.className = "smi_video";

		// add video player
		playerDiv = document.createElement("div");
		videoDiv.appendChild(playerDiv);

		var result = /http:\/\/www\.youtube\.com\/watch\?v=([^&]+)/.exec(anchor.href);
		var videoId = result ? result[1] : "";
		var url = "http://www.youtube.com/v/" + videoId + "&hl=nl_NL&fs=1&";
		
		createFlash(playerDiv, url, width, height,
		{
			movie: url,
			allowFullScreen: "true",
			allowscriptaccess: "always"
		},
		function()
		{
			playerDiv.innerHTML = "Video niet beschikbaar. Download de laatste versie van de Flash speler a.u.b.";
		},
		9,
		{
			onload: function(flash)
			{
				videoDiv.style.backgroundImage = "none";
			}
		});
		
		// add resize events
		window_attachEvent("onresize", resize);
		window_attachEvent("onscroll", resize);
		resize();
		
		// add bottom bar
		bottom = document.createElement("div");
		bottom.className = "video_bottom";
		videoDiv.appendChild(bottom);
		
		// add details
		details = document.createElement("div");
		details.className = "video_details";
		bottom.appendChild(details);
		
		// add title
		title = document.createElement("div");
		title.appendChild(document.createTextNode(video.title));
		title.className = "video_title";
		details.appendChild(title);
		
		// add buttons
		buttons = document.createElement("div");
		buttons.className = "video_buttons";
		bottom.appendChild(buttons);
		
		var close = document.createElement("img");
		close.src = options.buttonImageClose;
		close.attachEvent("onclick", hide);
		close.style.cursor = "pointer";
		buttons.appendChild(close);
		
		// finally resize into view
		videoDiv.style.width = "40px";
		videoDiv.style.height = "40px";
		
		body.appendChild(videoDiv);
		borders = getBorders(videoDiv);
		setOpacity(details, 0);
		setOpacity(buttons, 0);
		resizeFromTo(40, 40, width, height + bottomHeight, function()
		{
			fadeFromTo(buttons, 0, 100);
			fadeFromTo(details, 0, 100);
		});
	};
	
	showFile = function(anchor)
	{
		var result = /cms_file\.php\?fromDB=([^&]+)(&|$)/.exec(anchor.href);
		var fileId = result ? result[1] : -1;
		
		currentIndex = getIndex(anchor);
		dim = getScreenDimensions();
		var video = videos[currentIndex];

		getItem("item_tree_file", fileId, function(item)
		{
			var resolutions = [[1580, 1000], [1240, 880], [1000, 630], [780, 450], [780, 230]];
			var found = false;
			for(var i=0; i<resolutions.length && !found; i++)
			{
				var res = resolutions[i];
				if (dim.availWidth >= res[0] && dim.availHeight - bottomHeight >= res[1])
					found = res;
			}
			if (!found)
				found = resolutions[resolutions.length-1];
			var width = item.width || 4096;
			
			if (item.height == 0)
				var itemHeight = 0;
			else
				var itemHeight = item.height + 30;
			var height = itemHeight || 4096;
			
			var ratio = width / height;
			if (width > found[0])
			{
				width = found[0];
				height = Math.floor(width / ratio);
			}
			if (height > found[1])
			{
				height = found[1];
				width = Math.floor(ratio * height);
			}
			
			showVideo(video, item, width, height);
		});
	};
	
	var onclick = function(_e)
	{
		var e = _e || window.event;
		var target = e.target ? e.target : e.srcElement;
		cancelEvent(e);
		
		var anchor = target;
		while(anchor && anchor.nodeName != "A")
		{
			anchor = anchor.parentNode;
		}
		
		if (/^http:\/\/www\.youtube\.com\/watch\?v=([^&]+)$/.test(anchor.href))
		{
			showYoutube(anchor);
		}
		else
		{
			showFile(anchor);
		}
		
		return false;
	};
	
	for(var i=0; i<anchors.length; i++)
	{
		videos[i] = {
			title: anchors[i].title,
			href: anchors[i].href,
			index: i
		};
		anchors[i].attachEvent("onclick", onclick);
	}
}

