Current File : /data/web/virtuals/215191/virtual/www/domains/gryna2.com.pl/js/admin/gallery_uploader.js
/**
 * @author Jan Řasa, honza.rasa@seznam.cz
 * @copyright 2012
 * @system SpeePlum by Jan Řasa
 * @description Gallery uploading functions
**/

$(document).ready(function(){
		$('.photo_src').change(function(){
			var file = this.files[0];
			name = file.name;
			size = file.size;
			type = file.type;
			type_data = file.type.split("/");
			image_types=new Array("jpeg", "jpg", "png", "gif", "zip");
			if(!inArray(type_data[1], image_types))
			{
				alert("Povolené koncovky jsou: jpg, jpeg, png, gif a zip");
				$('.photo_src').val("");
			}
			if(max_upload_filesize < size){
				alert("Nelze nahrát soubor větší, než je povolená velikost.");
				$('.photo_src').val("");
			}			
		});
});

function galleryAddPhotos(){
			var formData = new FormData($('.form_photos')[0]);
			$.ajax({
				url: '../ajax/admin/gallery/gallery_upload_photos.php',  
				type: 'POST',
				xhr: function() {  
					myXhr = $.ajaxSettings.xhr();
					if(myXhr.upload){ 
						myXhr.upload.addEventListener('progress',uploadProgressFile, false); 
					}
					return myXhr;
				},
				
				success: function(data){
					//alert(data);
					output = $.parseJSON(data);
					
					if(output.result == "fail"){
						alert(output.reason);
						return false;
					}
					if(output.result == "ok"){
						fileUploadComplete(output.ids, output.names);
					}
					
				
				},
				error: function(){alert("Nepodařilo se nahrát daný soubor na server.")},
				data: formData,				
				cache: false,
				contentType: false,
				processData: false
			});
}

function fileUploadComplete(id_array, name_array){
	/*set rows into #file_handler table*/
	for(var i = 0; i < id_array.length; i++){
		img_id = id_array[i];
		if(!inArray(img_id, uploaded_ids)){
			$("#table_files").append("<tr class='upload_file upload_file_"+img_id+"'><td><img src='../img/no_image.png' class='upload_image file_img_"+img_id+"' /></td><td class='upload_file_name upload_file_name_"+img_id+"'>"+name_array[i]+"</td><td class='upload_file_progress upload_file_progress_"+img_id+"'><img src='../img/ajax-loader.gif' /></td><td><input type='button' value='Upravit' class='files_edit_button' /> <input type='button' class='files_delete_button' value='Smazat' /></td></tr>");
			uploaded_ids[uploaded_ids.length+1] = img_id;
		}		
	}
	
	installImage(id_array[0], id_array, name_array, 1);
}

function uploadProgressFile(e){
    if(e.lengthComputable){
    	p_value = e.loaded / 2;
        $('.gallery_progress').attr({value:p_value,max:e.total});
    }
}

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}


function installImage(id, id_array, name_array, image_number){	
	$.ajax({	url: '../ajax/admin/gallery/gallery_install_photo.php',  
				type: 'POST',				
				success: function(data){
					//alert(data);
					output = data.split("|");
					if( typeof output[0] == "undefined" ){
						alert("Chyba serveru");
						return;
					}
					
					if(output[0] == "1"){
						step_ = 50 / id_array.length;
						step = (Math.round(step_).toFixed(0) * image_number) + 50;
						$('.gallery_progress').attr({value:step,max:100});
						$('.upload_file_progress_'+id).html("");
						$('.upload_file_progress_'+id).html("<img src='../img/ok.png' legend='Ok' width='30'/>");
						$('.file_img_'+id).attr("src", output[1]);
						if(id_array[image_number]){
							installImage(id_array[image_number], id_array, name_array, image_number+1);
						}
						else{
							$('.gallery_progress').attr({value:0,max:100});
							$('.progress_comment').html("");
						}						
					}
					else{
						$('.upload_file_progress_'+id).html("");
						$('.upload_file_progress_'+id).html("<img src='../img/ko.png' title='"+output[1]+"' width='30'/>");
						if(id_array[image_number]){
							installImage(id_array[image_number], id_array, name_array, image_number+1);
						}
						else{
							$('.gallery_progress').attr({value:0,max:100});
							$('.progress_comment').html("");
						}
					}			
				},
				beforeSend: function(){
					$('.progress_comment').html("");
					$('.progress_comment').html("Zpracovávám soubor: " + name_array[image_number-1]);
				},
				error: function(){alert("Chyba při instalaci obrázků")},
				data: "gallery_id="+gallery_id+"&image_id="+id,				
				dataType: "html"
			});
}