jQuery(document).ready(function($) {
	
	// cache the most reused variables
	var b = $('#videoBlock'),
		c = $('#videoWrapper'),
		t = $('#videoCaption'),
		g = $('#initThumb').parent('div'), // this is either #thumbGallery or #pageThumbs
		thumbLinks = $('a.thumbSwitch');
		
	// hide the javascript warning (should only show if js not enabled)
	$('em',c).hide();
		
	// Function to initialize movie on load	
	$('#initThumb').click(function(event){
		event.preventDefault();
		
		var v = $(this).attr('href').split('?');			
		var	p = v[0],
			d = v[1].split('&');			
		var	w = d[0].substr(2),
			h = d[1].substr(2);
		
		if(b.hasClass('fullWidth')) {
			
			c.animate({
				width: w+'px',
				height: h+'px'
			}, 250, function(){
				c.flash().remove();
				c.flash({swf: p, height: h, width: w, wmode: 'transparent'});
				// add the nowPlaying icon
				playSwitch(true);
			});	
			
		} else {
				
			b.animate({
				width: w+'px'
			},200,function(){
				c.animate({
					width: w+'px',
					height: h+'px'
				}, 250, function(){
					c.flash().remove();
					c.flash({swf: p, height: h, width: w, wmode: 'transparent'});
					// add the nowPlaying icon
					playSwitch(true);
				});			
			});
			
		}
		
		
	});
	
	// Function to switch out the Now Playing icon (a 42x42 icon)
	function playSwitch(init) {
		var cur = $('a.nowPlaying',g);
		var cc = cur.offset(),
			ci = $('img',cur);
			
		if (cur.length && ci.length) {
			
			var ch = ci.innerHeight(),
				co = (ci.position().top * 2) || 0;
				
			if (init) {
				$('#nowPlaying').css({
					position: 'absolute',
					left: 0,
					top: 0,
					display: 'none',
					width: '100%',
					height: (ch + co) + 'px',
					background: 'transparent url(image/play.png) no-repeat 50% 50%',
					zIndex: 9999
				}).appendTo(cur).fadeIn(250);
			}
			else {
				$('#nowPlaying').fadeOut(250, function(){
					$(this).detach().css({
						height: (ch + co) + 'px',
						display: 'none'
					}).appendTo(cur).fadeIn(250);
				});
			}
		}
	}
	
	// Video thumbnail link behaviors	
	thumbLinks.css('opacity', '0.7');
	thumbLinks.hover(function(){
		$(this).fadeTo('fast', 1.0);
	}, function(){
		$(this).fadeTo('fast', 0.7);
	});
	
	// Function to switch featured image on thumbnail click
	thumbLinks.live('click',function(event){
		event.preventDefault();
		
		var	a = $(this);
		var o = a.offset(),
			l = a.attr('title'),
			v = a.attr('href').split('?');		
		var	p = v[0],
			d = v[1].split('&');			
		var	w = d[0].substr(2),
			h = d[1].substr(2);
			
		$('a.nowPlaying',g).removeClass('nowPlaying');
		a.addClass('nowPlaying');
		
		t.slideUp(250,function(){
			t.html(l).css({
				width: w+'px'
			});
			if(!b.hasClass('fullWidth')) {				
				b.animate({
					width: w+'px'
				});
			}
			// also change the thumb gallery width if layoutH enabled.
			if (a.hasClass('layoutH')) {
				g.animate({
					width: w + 'px'
				}, 350);
			}
			c.animate({
				width: w+'px',
				height: h+'px'
			}, 350, function(){
				c.flash().remove;
				c.flash({
					swf: p, 
					height: h, 
					width: w, 
					wmode: 'transparent',
					allowFullScreen: true,
					flashvars: {
						autoPlay: true,
						autoStart: true
					}
					});
				t.slideDown(250, function(){
					// switch out the 'Now Playing' icon
					playSwitch(false);					
				});
			});
			
		});
		// trigger a resize event on the parent
		$('#pageContent').trigger('resize');
	});	
});

// load the first movie on page load
jQuery(window).load(function(){
	jQuery('#initThumb').click();
});
