//Media part


function addImage(){
	var unique = Math.floor(Math.random()*11);
	txt =  '<table style="margin:5px" cellpadding="3" cellspacing="0" id="generated_image_'+unique+'_container">';
	txt +='	<tr>';
	txt +='		<th>'+lang_small_description+'</th>';
	txt +='		<td><input size="45" name="short_description" id="generated_image_'+unique+'_short_description"></td>';
	txt +='	</tr>';
	txt +='	<tr>';
	txt +='		<th>'+lang_description+'</th>';
	txt +='		<td><textarea cols="34" name="description" id="generated_image_'+unique+'_description"></textarea></td>';
	txt +='	</tr>';
	txt +='	<tr>';
	txt +='		<th>'+lang_image+'</th>';
	txt +='		<td><input type="file" name="images" value="" id="generated_image_'+unique+'"></td>';
	txt +='	</tr>';
	txt +='</table>';
	$('#generated_image_container').append(txt);
	activateUpload();
}

function activateUpload(){	
	$(":file[name='images']").each(function(){
		var input = this;
		$.ajax_upload(this, {
			action : 'upload-manager.php',
			data:{
				type:'media_image'
			},
			onSubmit: function(){
				$("#images_container").html(small_load);
			},
			onComplete : function(file,response){
				var desc = $('#'+input.id+'_description').val();
				var title = $('#'+input.id+'_short_description').val();
				updateItem(response,desc,title);
				$("#"+input.id+"_container").hide();
				getImages();
			}	
		});
	});
	$.ajax_upload('#v_other_id', {
		action : 'upload-manager.php',
		data:{
			type:'video',
			category:'v_other'
		},
		onSubmit: function(){
			$('#v_other_container').html(small_load);
		},
		onComplete : function(file,response){
			$('#v_other_container').html(file);
			addDeleteForm('#v_other_container','v_other_id',response);
		}	
	});
	$.ajax_upload('#presentation_id', {
		action : 'upload-manager.php',
		data:{
			type:'video',
			category:'presentation'
		},
		onSubmit: function(){
			$('#presentation_container').html(small_load);
		},
		onComplete : function(file,response){
			$('#presentation_container').html(file);
			addDeleteForm('#presentation_container','presentation_id',response);
		}	
	});
	$.ajax_upload('#pitch_id', {
		action : 'upload-manager.php',
		data:{
			type:'video',
			category:'pitch'
		},
		onSubmit: function(){
			$('#pitch_container').html(small_load);
		},
		onComplete : function(file,response){
			$('#pitch_container').html(file);
			addDeleteForm('#pitch_container','pitch_id',response);
		}	
	});
}

function updateItem(id,desc,title){
	$.ajax({
		type:'POST',
		url:'company-media.php',
		data:{
			action:'updateItem',
			id:id,
			desc:desc,
			title:title
		}
	});
}



function getImages(){
	$.ajax({
		type:'POST',
		url:'company-media.php',
		data:'action=getImages',
		success:function(data){
			$('#images_container').hide();
			$('#images_container').html(data);
			$('#images_container').fadeIn();
		}
	});
}


