"use strict";

/*globals $, jQuery, window, document */

(function () {
	var player, VideoSizes = {
		normal : {
			def : {width: 425, height: 344},
			large   : {width: 640, height: 505},
			medium  : {width: 480, height: 385},
			small   : {width: 320, height: 265}
		},		
		wide : {
			def : {width: 480, height: 295},
			large   : {width: 640, height: 385},
			medium  : {width: 560, height: 345},
			small   : {width: 425, height: 264}
		}
	};

	function YouTubeDelegate(key, options) {
		var prefs = jQuery.extend({
			useWidescreen : true
		}, options);
	
		var size = undefined !== prefs.size ? 
			VideoSizes[prefs.useWidescreen ? 'wide' : 'normal'][prefs.size] : 
			VideoSizes.wide['large'];
			
		return '<object width="' + size.width + '" height="' + size.height + '">' +
		'<param name="movie" value="http://www.youtube.com/v/' + key + '&hl=en&fs=1&rel=0"></param>' +
		'<param name="allowFullScreen" value="true"></param>' +
		'<param name="allowscriptaccess" value="always"></param>' +
		'<embed src="http://www.youtube.com/v/' + key + '&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" ' + 
		'allowscriptaccess="always" allowfullscreen="true" quality="high" width="' + size.width + '" height="' + size.height + '">' +
		'</embed>' +
		'</object>';	
	}

	$(document).ready(function () {
		player = $('#youTube');
		grabs = $('.live_screengrabs');
		videos = grabs.find('.youtube');
	
		videos.each(function () {
			var elem = $(this);
		
			elem.click(function () {
				player.html(YouTubeDelegate(elem.attr('data-videoId')));
				
				return false;
			});
		});
		
		if (player.attr('data-autoplay')) {
			grabs.find('a[data-videoId="' + player.attr('data-autoplay') + '"]').click();
		} else if (videos.length) {
			videos.eq(0).click();
		}
	});
}());