var vv_embeds = new Array();

$(document).ready(function() {          
	$('.vv_div').each(function (i) {
		vv_id 	= $(this).attr('id').substring(4);
		table_id = 'mov_table_'+vv_id;
		embed_id = 'mov_embed_'+vv_id; 
		
		vv_embeds.push({
				id:vv_id, 
				table_html:$('#'+table_id).html(), 
				embed_html: $('#'+embed_id+' .embed').html(),
				preload_html: $('#'+embed_id+' .preload').html(),
				width : $('#'+embed_id).width(),
				height : $('#'+embed_id).height()
		});
        $(this).append('<div style="position:absolute;left:30px;width:450px;white-space:nowrap;overflow:hidden;top:5px;font-weight:normal;font-size:22px;color:#ffffff;z-Index:2;">'+client_company+'</div>');
		$(this).append($('#'+table_id).html());
		
		$('#mov_'+vv_id+' .vv_table .tr img').click({vv_id:vv_id}, function(e){
			var vv, vv_id, mode, mov_id, mov;
			
			vv_id = e.data.vv_id;
			mov_id = $('#mov_'+vv_id+' .vv_table .tc .dd').val();
			
			$.each(vv_embeds, function(){if (this.id==vv_id) {vv = this;return false;}});
			$.each(vv_embeds, function(){if (this.id==mov_id) {mov = this;return false;}});
			
			mode = $('#mov_'+vv_id+' .vv_table').hasClass('vv_table_o')?'o':'x';
			
			if (mode=='x') //it is closed
			{
                //$('.vvparent').not('#movparent_'+vv_id).css('z-Index','3000');
                
                $('#mov_'+vv_id+' .vv_table').removeClass('vv_table_x');
				// 1. Change style of table & show bottom parts
				$('#mov_'+vv_id+' .vv_table').removeClass('vv_table_x');
				$('#mov_'+vv_id+' .vv_table').addClass('vv_table_o');
				// 2. Show introduction
				
				$('#mov_'+vv_id+' .vv_table .cc .embed').animate(
					{height: ''+(mov.height+10)+'px', width: ''+(mov.width+10)+'px'},
					1000,
					function(){					
						$('#mov_'+vv_id+' .vv_table .cc .embed').append(mov.preload_html).queue(function() {
							$('#mov_'+vv_id+' .vv_table .cc .embed .preload_body').css('padding-top', ''+((mov.height-100)/2)+'px');
							$(this).dequeue();
						}).show().delay(3000).fadeOut(1500).queue(function() {
							$(this).empty().append(mov.embed_html);
							$(this).dequeue();
						}).queue(function() {
							$(this).show();
							$(this).dequeue();
						});	
					}
				);                                                                    
			}
			else
			{
                
				// 2. Show introduction
				$('#mov_'+vv_id+' .vv_table .cc .embed').empty().animate(
					{width: ''+($('#mov_'+vv_id+' .vv_table .tc .dd').width()+10-52)+'px', height: '0px'},
					1000,
					function(){					
						// 1. Change style of table & show bottom parts
						$('#mov_'+vv_id+' .vv_table').removeClass('vv_table_o');
						$('#mov_'+vv_id+' .vv_table').addClass('vv_table_x');
                        //$('.vvparent').not('#movparent_'+vv_id).css('z-Index','4000');
					}
				);                                                                    
			}
		});
		
		$('#mov_'+vv_id+' .vv_table .tc .dd').change({vv_id:vv_id}, function(e){
			var vv, vv_id, mode, mov_id, mov;
			
			vv_id = e.data.vv_id;
			mov_id = $(this).val();
			
			$.each(vv_embeds, function(){if (this.id==vv_id) {vv = this;return false;}});
			$.each(vv_embeds, function(){if (this.id==mov_id) {mov = this;return false;}});
			
			mode = $('#mov_'+vv_id+' .vv_table').hasClass('vv_table_o')?'o':'x';
			
			if (mode=='o')
			{
				$('#mov_'+vv_id+' .vv_table .cc .embed').empty().animate(
					{height: ''+(mov.height+10)+'px', width: ''+(mov.width+10)+'px'},
					1000,
					function(){					
//						$('#mov_'+vv_id+' .vv_table .cc .embed').empty().append(mov.embed_html);	
						
						$('#mov_'+vv_id+' .vv_table .cc .embed').append(mov.preload_html).queue(function() {
							$('#mov_'+vv_id+' .vv_table .cc .embed .preload_body').css('padding-top', ''+((mov.height-100)/2)+'px');
							$(this).dequeue();
						}).show().delay(2000).fadeOut(1500).queue(function() {
							$(this).empty().append(mov.embed_html);
							$(this).dequeue();
						}).queue(function() {
							$(this).show();
							$(this).dequeue();
						});	
					}
				);   	
			}
			
		});    
		
		$('#'+table_id).remove();
		$('#'+embed_id).remove();
	});
   
});
