$(document).ready(function() {
	movieViewer.init("movie-viewer-id", ".movie-viewer", 900);
});

var movieViewer = {
	init : function (ContainerId, ClassNameToUse, ForcedWidth) {

		if(ForcedWidth) movieViewer.forcedWidth = ForcedWidth;
		
		movieViewer.addContainer(ContainerId);
		
		$(ClassNameToUse).bind('click', function() {
			movieViewer.showMovie(this);
		});
		
		$(window).resize(function () {
			movieViewer.resize();
		});
		
		$(window).scroll(function () {
			movieViewer.resize();
		});
		
		
	},
	containerId : '',
	addContainer : function (ContainerId) {
		movieViewer.containerId = ContainerId;
		var html = '<div id="'+ContainerId+'" class="image-viewer-container"></div>';
		$('body').append(html);
	},
	imagesClassName : '',
	forcedWidth : null,
	currentSize : {
		x : null,
		y : null
	},
	showMovie : function (element) {
		
		var movieIframe = $(element).find('iframe')[0];

		ForcedWidth = movieViewer.forcedWidth;
		
		if(ForcedWidth > $(window).width() - 200){
			ForcedWidth = $(window).width() - 200;
		}


		if(movieViewer.forcedWidth!=null){
			
			var HeightMultiplyer = ForcedWidth / $(element).width();
			movieViewer.currentSize.x = ForcedWidth;
			movieViewer.currentSize.y = $(element).height() * HeightMultiplyer;
			
		} else {
			
			// get original width?
			
			currentSize.x = $(movieIframe).width();
			currentSize.y = $(movieIframe).height();
		}
		
		
		
		movieViewer.addMovieViewer(movieIframe);
	}, 
	addMovieViewer : function (movieIframe) {
		
		var MovieIframeClone = $(movieIframe).clone();
		
		

		var FullScreenCss = 'width:' + $(window).width() + 'px; height:' + $(window).height() + 'px;';
		FullScreenCss += 'top:'+$(window).scrollTop()+'px; left:'+$(window).scrollLeft()+'px';
		
		$(MovieIframeClone).css({
			width: parseInt(movieViewer.currentSize.x) + 'px',
			height: parseInt(movieViewer.currentSize.y) + 'px',
			margin: parseInt(-movieViewer.currentSize.y * 0.5) + 'px ' + parseInt(-movieViewer.currentSize.x*0.5) + 'px',
			display: 'block',
			zIndex: 510, 
			position: 'absolute',
			left: '50%',
			top: $(window).height() * 0.5 + 'px',
			backgroundColor: 'white'
		});
		
		var autoPlaySource = $(MovieIframeClone).attr('src')+'&autoplay=1';
		

		$(MovieIframeClone).attr("src", autoPlaySource);
		

		
		
		EmbedHtml = $('<div>').append($(movieIframe).clone()).remove().html(); 
		
		
		
		MovieIframeCloneString = $('<div>').append($(MovieIframeClone).clone()).remove().html();
		
		var ButtonCss = 'margin: ' + parseInt(-movieViewer.currentSize.y * 0.5 - 65*0.5) + 'px ' + parseInt(-movieViewer.currentSize.x*0.5 - 65*0.5) + 'px;';
		
		var html = '';
		html += '<div class="image-viewer-instance" id="image-viewer-instance" style="'+FullScreenCss+'">';
		html += '<div class="image-viewer-background" id="image-viewer-background"></div>';
		//html += '<img src="'+MovieSrc+'" class="image-viewer-image" style="'+MovieCss+'"/>';
		html += MovieIframeCloneString;
		html += '<div class="embed-video-container"><h2>Embed on your blog:</h2>';
		html += '<textarea class="embed-text-area">' + EmbedHtml + '</textarea></div>';
		html += '<span class="image-viewer-close-button" style="'+ButtonCss+'"></span>';
		html += '<div>';
		
		$('#'+movieViewer.containerId).html(html);
		
		$('.image-viewer-close-button').bind('click', function() {
			movieViewer.removeMovieViewer();
		});
		
		
	}, 
	removeMovieViewer : function (MovieSrc) {

		$('#'+movieViewer.containerId).html('');
		
	},
	resize : function () {
		
		if($('#image-viewer-instance').length!=0){

			$('#image-viewer-instance').css({
				width: $(window).width() + 'px',
				height: $(window).height() + 'px',
				top: $(window).scrollTop() + 'px',
				left: $(window).scrollLeft() + 'px'
			});
		}
	}
}
