vimeoMovieLoaded = function(objectId) {
	document.getElementById(objectId).movieLoaded();
}

function getStyle(el,styleProp) {
	var x = document.getElementById(el);
	if (!x && el) {
		x = el;
	}
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function hex( c ) {
	var m = /rgba?\((\d+), (\d+), (\d+)/.exec( c );
	return m
		? '#' + ( m[1] << 16 | m[2] << 8 | m[3] ).toString(16)
		: c;
};

movieLibrary = function(vimeoUserName, albumId) {
	
	// Include SWFObject
	var e = document.createElement('script');
	e.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js');
	document.body.appendChild(e);

	getMovieLibraryDOMObject = function() {

		// Not defined, create new
		if (document.getElementById('movielibrary') == null) {

			var oMovieLibrary = document.createElement('div');
			oMovieLibrary.id = 'movielibrary';
			oMovieLibrary.style.display = 'none';
			oMovieLibrary.className = 'movie-library';

			oMovieLibrary.player = document.createElement('div');
			oMovieLibrary.player.className = 'player';
			oMovieLibrary.appendChild(oMovieLibrary.player);

			oMovieLibrary.player.view = document.createElement('div');
			oMovieLibrary.player.view.className = 'view';
			oMovieLibrary.player.appendChild(oMovieLibrary.player.view);

			oMovieLibrary.player.view.object = document.createElement('div');
			oMovieLibrary.player.view.object.id = oMovieLibrary.id + '_player';
			oMovieLibrary.player.view.appendChild(oMovieLibrary.player.view.object);

			oMovieLibrary.library = document.createElement('div');
			oMovieLibrary.library.className = 'library';
			oMovieLibrary.appendChild(oMovieLibrary.library);

			oMovieLibrary.library.header = document.createElement('h1');
			oMovieLibrary.library.header.innerHTML = 'Filmoverzicht';
			oMovieLibrary.library.appendChild(oMovieLibrary.library.header);

			oMovieLibrary.library.list = document.createElement('ul');
			oMovieLibrary.library.appendChild(oMovieLibrary.library.list);

			document.body.appendChild(oMovieLibrary);
		}

		var oMovieLibrary = document.getElementById('movielibrary');

		return oMovieLibrary;
	}

	var oMovieLibrary = getMovieLibraryDOMObject();

	oMovieLibrary.selectVideo = function(videoId, autoplay) {

		this.hidePlayer();

		var color = hex(getStyle(this.player, 'color')).substr(1);

	 	var flashvars = {
	        clip_id: videoId,
			color: color ? color : '0099cc',
	        show_portrait: 1,
	        show_byline: 1,
	        show_title: 1,
			js_api: 1, // required in order to use the Javascript API
			js_onLoad: 'vimeoMovieLoaded', // moogaloop will call this JS function when it's done loading (optional)
			js_swf_id: this.id, // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
			autoplay: (autoplay == true) ? 1 : 0
	    };
		var params = {
			allowscriptaccess: 'always',
			allowfullscreen: 'true',
			quality: 'high'
		};
		var attributes = {
			bgcolor: '#000000'
		};

		// For more SWFObject documentation visit: http://code.google.com/p/swfobject/wiki/documentation
		swfobject.embedSWF("http://vimeo.com/moogaloop.swf", this.player.view.object.id, 609, 343, "9.0.0","expressInstall.swf", flashvars, params, attributes);

		var videoLinks = bb.evaluate('//div[@class=\'movie-library\']/div[@class=\'library\']/ul/li/ul/li/a', document);
		for (linkIndex in videoLinks) {
			var videoLink = videoLinks[linkIndex];
			videoLink.className = (videoLink.videoId == videoId) ? 'selected' : /selected/;
		}

		return false;
	};

	oMovieLibrary.hidePlayer = function() {
		this.player.view.style.marginLeft = '-50000px';
		
		// Reset the player so it won't keep playing in crappy browsers
		// Also fixes an annoying white flashy flash in IE
		swfobject.removeSWF(this.id + '_player');
		this.player.view.appendChild(this.player.view.object);
	}

	oMovieLibrary.showPlayer = function() {
		this.player.view.style.marginLeft = '0px';
	}

	oMovieLibrary.clearLibrary = function() {
		this.library.list.innerHTML = '';
	}

	oMovieLibrary.movieLoaded = function() {
		this.showPlayer();
	}

	oMovieLibrary.show = function(videoId) {

		this.loadLibrary(vimeoUserName, videoId);
		modalView().show({
			'src': '#' + this.id
			});
		
		return false;
	}

	// onClose handler called by modalView
	oMovieLibrary.onclose = function() {
		this.clearLibrary();
		this.hidePlayer();
	}

	oMovieLibrary.loadLibrary = function(vimeoUserName, videoId) {

		var url = '/editmode/movielibrary/index/fetch-mixed/username/' + vimeoUserName + '/album/' + albumId;
		var menuBase = this.library.list;

		if (typeof videoId == 'undefined') {
			videoId = -1;
		}

		bb.command.load(url, "get", null, {
			"Content-Type": "text/json"
		}, null, null,
		function(oResponseElm) {
			var mixed = JSONRequest.parse(oResponseElm.responseText);

			for (albumIndex in mixed) {
				var album = mixed[albumIndex];

				if (typeof album == 'object') {

					var albumListItem = document.createElement('li');
					var albumLink = document.createElement('a');
					var videoListBase = document.createElement('ul');

					albumLink.href = 'javascript:void(0);';
					albumLink.innerHTML = album.title;
					albumLink.title = album.description;
					albumLink.onclick = function() {
						var menuElements = bb.evaluate('../../li/ul', this);
						var parentMenuElement = bb.evaluate('../ul', this)[0];
						for (menuIndex in menuElements) {
							var menuElement = menuElements[menuIndex];
							menuElement.style.display = (menuElement == parentMenuElement) ? 'block' : 'none';
						}
						return false;
					}

					for (videoIndex in album.videos) {
						var video = album.videos[videoIndex];

						if (typeof video == 'object') {
							if (videoId == -1) {
								videoId = video.id;
							}

							if (videoId == video.id) {
								videoListBase.style.display = 'block';
							}

							var videoListItem = document.createElement('li');
							var videoLink = document.createElement('a');

							videoLink.href = video.url;
							videoLink.innerHTML = video.title;
							videoLink.title = video.description;
							videoLink.videoId = video.id;
							videoLink.onclick = function() {
								return oMovieLibrary.selectVideo(this.videoId, true);
							}

							videoListItem.appendChild(videoLink);
							videoListBase.appendChild(videoListItem);
						}
					}

					albumListItem.appendChild(albumLink);
					albumListItem.appendChild(videoListBase);

					menuBase.appendChild(albumListItem);
				}
			}

			if (videoId > -1) {
				oMovieLibrary.selectVideo(videoId, false);
			}

		});

	};

	return oMovieLibrary;
};

