(function(){
	
	var video = window.video = {
			
		/*
		 * options defined here can be overriden externally
		 */
		opts: {
			apiEndpoint : 'http://www.vimeo.com/api/v2/',
			embedEndpoint : 'http://www.vimeo.com/api/oembed.json',
			albumsCallback : 'video.setupAlbums',
			latestCallback : 'video.latestVideo',
			latestEmbedCallback : 'video.latestEmbed',
			videosCallback : 'video.setupVideos',
			embedCallback : 'video.openModal',
			embedModalCallback : 'video.embedVideo',
			vimeoUsername : '3136690',
			albumId : undefined,
			html : undefined
		},
		 
		latest: function(){
			/*
			 * on page load get the album get the user's last video
			 */
			$.getScript(video.opts.apiEndpoint + video.opts.vimeoUsername + '/videos.json?callback=' + video.opts.latestCallback);
		},
		
		latestVideo: function(videos) {
			
			$.getScript(video.opts.embedEndpoint + '?url=' + videos[0].url + '&width=416&wmode=opaque&portrait=false&color=ffffff&js_api=true&callback=' + video.opts.latestEmbedCallback);

		},
		
		latestEmbed: function(video) {
			$('.vimeo_video_embed').html(unescape(video.html.replace(/height="(\d+)"/, 'height="280"')));
		},
		
		init: function(options){
			/*
			 * override opts if needed
			 */
			video.opts = $.extend({}, video.opts, options);
			/*
			 * on page load get the album get the user's album list
			 */
			$.getScript(video.opts.apiEndpoint + video.opts.vimeoUsername + '/albums.json?callback=' + video.opts.albumsCallback);
		},
		
		/*
		 * display the album list
		 */
		setupAlbums: function(albums){	
			
			/*
			 * fetch the vimeo album id
			 */

			video.opts.albumId = common.param('album');
			
			video.opts.html = '<a href="?=" class="title" ><u>S</u>ELECT A VIDEO CHANNEL</a>';
			
				/*
				 * loop through the album list and build list for display
				 */
			
				for(album in albums){
					video.opts.html += '<a href="?album=' + albums[album].id + '" title="' + albums[album].id + '"';
						if(albums[album].id == parseInt(video.opts.albumId)){
							video.opts.html += ' class="current" '; }
								video.opts.html += '>' + albums[album].title + '</a>';
				}
				
					/*
					 * display album list
					 */
					$('.videos-list-albums').html(video.opts.html);	
					
				video.opts.html = null;
						
			/*
			 * get user videos. if no album is selected default to show all
			 */

			if(video.opts.albumId)
				$.getScript(video.opts.apiEndpoint + 'album/' + video.opts.albumId + '/videos.json?callback=' + video.opts.videosCallback);
			else
				$.getScript(video.opts.apiEndpoint + video.opts.vimeoUsername + '/videos.json?callback=' + video.opts.videosCallback);
		},
		
		/*
		 * display the video thumbs
		 */
		setupVideos: function(videos) {
			
			video.opts.html = '';

			/*
			 * add video thumbs to gallery
			 */
			for(var i = 0; i < videos.length; i++){
				
				video.opts.html += '<div class="videos-list-video" title="' + videos[i].id + '" >'
					+ '<a href="#" title="' + videos[i].url + '" class="play" ></a>'
						+ '<a href="#" title="' + videos[i].url + '" class="mask" ></a>'
							+'<img src="' + videos[i].thumbnail_medium + '" />'
								+ '<a href="#" >' + videos[i].title.substring(0,50) + '...</a>'
									+ '<span>Desc: ' + videos[i].description.substring(0,50) + '... </span>'
										+ '</div>';
				
				$('.videos-list-main').html(video.opts.html);
				
			}

			/*
			 * listen for thumbnail click events
			 * stop audio player if any song is currently playing : see audio.js
			 */
			$('a.mask, a.play').click(function(link){

				link.preventDefault();

				audio.stop();
				
				video.getVideo($(this).attr('title'));
				
			});
		},

		/*
		 * get the selected video 
		 * show modal popup
		 */
		getVideo: function(url) {
			
			video.openModal();
			
			$.getScript(video.opts.embedEndpoint + '?url=' + url + '&width=640&portrait=false&color=ffffff&js_api=true&callback=' + video.opts.embedModalCallback);

		},
		
		embedVideo: function(video) {			
			$('.vimeo_video_embed').html(unescape(video.html.replace(/height="(\d+)"/, 'height="360"')));
		},
		
		/*
		 * show modal popup
		 */
		openModal: function() {

			/*
			 * vars in this function have a local scope
			 */
			
			var id = '#modal-video'; // video div
			var overlayHeight = $(document).height(); // screen height
			var overlayWidth = $(window).width(); //screen width
	
				/*
				 * set overlay properties
				 */
				$('.overlay').css({
					'width': overlayWidth,
					'height': overlayHeight,
					'position': 'absolute',
					'z-index': 10000,
					'top': 0
				});
				/*
				 * transition
				 */
				$('.overlay').fadeIn("slow");

					var windowHeight = $(window).height() / 2;
					var windowWidth = $(window).width();
					var modalHeight = $(id).height() / 2;
					
					$(id).css('left', windowWidth / 2 - $(id).width() / 2 - 20);

					positionBox = function(event){
						var scrollTop = $(window).scrollTop();
						var yPosition = windowHeight - modalHeight + scrollTop + 10;
						$(id).css('top', yPosition);
					};
				
						positionBox();
						
					$(id).fadeIn("fast");

                /*
                 * if close button is clicked
                 */
                $('.window .close').click(function(link){
                    link.preventDefault();
                    $('.overlay, .window').hide();
                    $(window).unbind('scroll', positionBox);				
                });

            /*
             * if overlay is clicked
             */
            $('.overlay').click(function(){
                $(this).hide();
                $('.window').hide();
                $(window).unbind('scroll', positionBox);
            });
		}
		
	};
	
	
})();

