$(document).ready(function()
{
	var thumbnail_html_backup = new Array(); 
	
	$(".item_button").click(function()
	{
		var id = ($(this).attr('video_id'));
		var control = ($(this).attr('control'));
		var file = ($(this).attr('file'));
			
		// alert(control);
		
		if (control == 'play')
		{
			$('#'+id+'_item_thumbnail').hide();
			
			$('#'+id+'_item_preview').flowplayer(
					{
						src: 'http://cache.sermonspice.com/media/lockdown/flash/flowplayer.commercial-3.2.5.swf',
						wmode: 'opaque'
					},
					{ 
						key: $('body').data('flowplayer_license'),
						clip: {
							autoPlay: false,
							autoBuffering: true,
							baseUrl: 'http://downloads.sermonspice.com/flash_previews/',
							scaling: 'fit'
						},
						version: [9, 115],
						canvas: {
							backgroundColor: "#000000",
							backgroundGradient: 'none'
						},
						plugins: {
							controls: null
							}
					}
				);
			
			$('#'+id+'_item_preview').show();
			$('#'+id+'_item_preview').flowplayer(0).play(file+'.flv');

			$('.item_button').removeClass('item_button_pause');
			$(this).attr('control','stop');
			$(this).addClass('item_button_pause');
			
		} 
		else 
		{
			$('#'+id+'_item_preview').flowplayer(0).stop();
			$('#'+id+'_item_preview').flowplayer(0).unload();
			$('#'+id+'_item_preview').hide();
			
			$('#'+id+'_item_thumbnail').show();
			$(this).attr('control','play');
			$('.item_button').removeClass('item_button_pause');
		}
	});

	var product_title = '';
	var product_description = '';
	var product_price = '';
	var product_video = '';
	var product_file_name = '';
	
	$('#video_player').flowplayer(
		{
			src: '/media/fugu/flash/flowplayer.commercial-3.1.3.swf',
			wmode: 'opaque'
		},
		{ 
			key: '#$524273ee7f164e5e942',
			clip: {
				autoPlay: false,
				autoBuffering: true,
				baseUrl: 'http://downloads.sermonspice.com/flash_previews/',
				scaling: 'fit'
			},
			version: [9, 115],
			canvas: {
				backgroundColor: "#000000",
				backgroundGradient: 'none'
			},
			plugins: {
				controls: {
					autoHide: 'always',
					hideDelay: 1000,
					volumeSliderGradient: 'none',
					volumeSliderColor: '#000000',
					tooltipTextColor: '#ffffff',
					buttonOverColor: '#728B94',
					tooltipColor: '#5F747C',
					sliderColor: '#ffffff',
					durationColor: '#ffffff',
					progressGradient: 'medium',
					sliderGradient: 'none',
					backgroundColor: '#3d3d3d',
					borderRadius: '0px',
					backgroundGradient: 'none',
					bufferGradient: 'none',
					progressColor: '#112233',
					bufferColor: '#445566',
					timeBgColor: '#555555',
					timeColor: '#01DAFF',
					buttonColor: '#375068',
					height: 32,
					opacity: 0.70,
					fullscreen: true,
					tooltips: {                // this plugin object exposes a 'tooltips' object 
						buttons: true, 
						fullscreen: 'Enter Fullscreen mode' 
					}
				}
			}
		}
	);
		
	$(".play_video").click(function () 
	{
		var product_info_display = $('#video_buttons').css("display");
		$("#preview_box").show();
		product_id				= $(this).attr("product_id");
		product_title			= $(this).attr("product_title");
		product_description		= $(this).attr("description");
		product_price			= $(this).attr("price");
		product_video			= $(this).attr("video_file");
		product_file_name		= $(this).attr("file_name");
		product_image_name		= $(this).attr("image_name");
		
		$('#video_buttons_title').html(product_title);
		$('#video_buttons_description').html(product_description);
		$('#video_buttons_price').html(product_price);
		$('#video_buttons_image').html('<img src="/media/default/images/products/' + product_image_name + '.jpg">');
		
		$("#video_buttons_cart").css({'background-color' : '#D4DBE1', 'font-weight' : 'normal'});
		$('#video_buttons_cart').removeClass('video_buttons_cart_down');
		$("#video_buttons_cart").html('Add to Cart');
		
		if(product_info_display == 'none'){
			$("#video_buttons").show('blind', {}, 1000);
		}
		
		$('html, body').animate({
			scrollTop: $("#video_player").offset().top
		}, 1000);
		if(product_video != '.flv'){
			$('#video_player').flowplayer(0).play(product_video);
		} else {
			$('#video_player').flowplayer(0).stop();
			$('#video_player').flowplayer(0).close();
			$('#video_player').flowplayer(0).unload();
		}
		return false;
	});
		
	$('#video_buttons_cart').click(function () {
		var button_text = $('#video_buttons_cart').html();
		if(button_text == 'Add to Cart'){
			if(product_id > 0){
				$.ajax({
					type: "POST",
					url: "/cart/add_item/" + product_id,
					success: function(msg){
						window.location = '/cart';
					}
				});
			}
		} else {
			$('#video_player').flowplayer(0).pause();
			load_shopping_cart();
		}
	});
		
  
});
