(function($){

	$.medialibrary = function(link,flash){
		if(!link.href) {throw new Error('Media Library file path not defined.');return;}
		if(!flash) {throw new Error('Media Player file name not defined.');return;}
		hash = link.hash.replace('#','');
		medialibrary.open(link.href,flash,hash);	
	}
	
	$.medialibrary.recommend = function(id){
		if(medialibrary.isOpen) {
			var rec = $('#'+id+' p.recommendation a').attr('href');
			medialibrary.recommend.apply($(rec));
		}
	}
	
	$.medialibrary.play = function(id){
		if(medialibrary.isOpen) {
			medialibrary.play.apply($('#'+id));
		}
	}
	
	var medialibrary = {
		
		open:function(source,flash,hash) {
		
			this.overlay = $.overlay().append('<div class="intro"><p class="transcripts"><a href="#">Transcripts available</a></p><h2></h2></div><p class="subtitle"></p><div id="flash_movie"></div><div id="thumbnails"></div>').addClass('gallery_window_loading');
			this.overlay.bind('overlay.close',function(){medialibrary.isOpen=false;});
			this.isOpen = true;
			
			source = source.split('#')[0];
			
			var f = new HW.Flash();
			f.width = 750;
			f.height = 462;
			f.src = [8,flash];
			f.name = 'video_player';
			f.id = 'video_player';
			f.allowScriptAccess = 'always';
			f.load('flash_movie');
			
			this.swf = window['video_player']||document['video_player'];
			
			$('#thumbnails').load(source+' ul.mediaThumbnails',medialibrary.loaded);
			
		},
		
		loaded:function() {
				
			if(hash && $('#thumbnails ul.mediaThumbnails li#'+hash).length) {
				medialibrary.play.apply($('#thumbnails ul.mediaThumbnails li#'+hash));
			}
			else {
				medialibrary.play.apply($('#thumbnails ul.mediaThumbnails li:first'));
			}
			
			$('#thumbnails ul.mediaThumbnails').carousel({offset:20,easing:'easeOutQuart'});
			
			$('#thumbnails ul.mediaThumbnails li .info').css({bottom:'-47px'});
			$('#thumbnails ul.mediaThumbnails li').hover(function(){
					$(this).find('.info').stop().animate({bottom:'0px'},'fast');
				},function(){
					$(this).find('.info').stop().animate({bottom:'-47px'},'fast');
				});
			
			$('#thumbnails ul.mediaThumbnails li p.recommendation').hide();
			$('#thumbnails ul.mediaThumbnails li p.related').hide();
			
			$('#thumbnails ul.mediaThumbnails li').click(medialibrary.play);
			
			
			$('.gallery_window_loading').removeClass('gallery_window_loading');
			
		},
			
		play:function() {
			
			var $this = $(this);
			try {
				$this.parent().find('li').removeClass('selected');
				$this.addClass('selected');
				medialibrary.overlay.find('h2').html($this.find('h3 a').html());
				medialibrary.overlay.find('p.subtitle').html($this.find('p.description').html());
				var str = [];
				$this.find('p.related a').each(function() {
					str.push($(this).attr('href') + '++' + $(this).text());
				});
				medialibrary.swf.loadMedia($this.find('h3 a').attr('href'),$this.attr('id'),str.join('--'));
				
			}
			catch(e) {
				setTimeout(function(){medialibrary.play.apply($this);},100);
			}
			return false;
			
		},
		
		recommend:function() {
			
			var $this = $(this);
			medialibrary.swf.setRecommendationData($this.attr('id'),$this.find('img').attr('src'),$this.find('h3 a').html(),$this.find('p.description').html());
			
		}
		
	}
	
	$.overlay = function() {
		var $content;
		
		function build() {
			
			if($.browser.msie && $.browser.version < 7) {
				$(document.body).append('<iframe class="gallery_mask"></iframe>');
			}
			$(document.body).append('<div class="gallery_mask"></div><div class="gallery_window"><div class="gallery_window_close"><a title="Close" href="#"></a></div><div class="gallery_window_top"><div class="gallery_window_content"></div></div><div class="gallery_window_bottom"></div>');
			
			$('.gallery_mask').css({height:$(document.body).height()});
			
			$content = $('.gallery_window_content').bind('overlay.close',close);
			$('.gallery_window_close a').click(function(){close();return false;});
			
			var top = $(window).scrollTop() + 100;
			$('.gallery_window').css({top:top+'px'});
			
			return $content;
		}
		
		function close() {
			$content.find('object,embed').remove();
			$('.gallery_mask,.gallery_window').empty().remove();
			$content.trigger('overlay.close');
		}
		
		return build();
	}
	
	$.fn.carousel = function(opts) {
		
		var defaults = {
			width:	750,
			offset:	0,
			easing:	'swing',
			speed:	'slow'
		}
		
		opts = $.extend(defaults,opts);
		
		return $(this).each(function(){
		
			var $this = $(this);
			
			$this.wrap('<div class="carousel"><div class="carousel_window"><div class="carousel_inner"></div></div></div>');
			
			$this.closest('.carousel').append('<a href="#" class="carousel_left"></a><a href="#" class="carousel_right"></a>');
			$this.closest('.carousel_window').css({width:opts.width+'px'});
			
			$this.closest('.carousel').find('a.carousel_left,a.carousel_right').click(function(){move($(this).hasClass('carousel_left')?-1:1);return false;});
			
			var w = 0;
			$this.find('li').each(function(){w+=$(this).outerWidth();});
			
			var steps = $this.find('li').size(),
				visible = Math.ceil(opts.width/$this.find('li').outerWidth());
			$this.closest('.carousel_inner').css({width:w+'px'});
			
			function move(d) {
				
				var	cur = $this.data('cur') || 0,
					to = Math.max(Math.min(steps-visible,cur+d),0),
					lft = Math.max(-1*to*$this.find('li').outerWidth(),opts.width-w+opts.offset);
				if(to != cur && (lft+'px') != $this.closest('.carousel_inner').css('left')) {
					$this.data('cur',to);
					$this.closest('.carousel_inner').stop().animate({left:lft+'px'},opts.speed,opts.easing,$this.trigger('carousel.move',to));
				}
			}
		
		});
	}

})(jQuery)