// JavaScript Document

/***********************************************************************************************
function popup_subir_imagen(imagen_actual)
***********************************************************************************************/
function popup_subir_imagen(imagen_actual, id){
	var popup = "";
	
	popup += "<div class='popup' id='popup_subir_imagen'>";
	popup += "<div class='header'></div>";
	popup += "<div class='cuerpo'>";
	popup += "<div class='title'>SUBIR IMAGEN</div>";
	popup += "<a href='javascript:void(0);' onclick='jQuery(\"#popup_behind\").fadeOut(\"slow\");jQuery(\"#popup\").fadeOut(\"slow\");";
	popup += "jQuery(\"#popup\").html(\"\");'>[x]cerrar</a>";
	
	popup += "<div id='img_producto_container'>";
	popup += "<img src='"+imagen_actual+"' width='200px'/>";
	popup += "</div>";
	
	popup += "<div id='subir_img_container'>";
	popup += "<form id='subir_img_form' method='post' enctype='multipart/form-data' action='gestor/uploadImage.php' target='iframeUpload'>";
	popup += "<input type='file' id='subir_img_file' name='subir_img_file' size='20'/>";
	popup += "<input type='hidden' id='image_to_save' name='image_to_save' value='"+imagen_actual+"'/>";
	popup += "<iframe name='iframeUpload' style='display:none'>";
	popup += "</iframe>";
	popup += " </form>";
	popup += "</div>";
	
	popup += "<div class='btn_close'><a href='javascript:void(0);' onclick='";
        popup += "conservarImagen("+id+");";
	popup += "jQuery(\"#popup_behind\").fadeOut(\"slow\");jQuery(\"#popup\").fadeOut(\"slow\");jQuery(\"#popup\").html(\"\");";
	popup += "'>OK</a></div>";
	popup += "</div>";
	popup += "<div class='pie'></div>";
	popup += "</div>";
	
	jQuery('#popup').html(popup);
	centrarYMostrarPopUp("popup");
	jQuery('#subir_img_file').change(function() {
		document.getElementById('subir_img_file').form.submit();
	});
}
/**********************************************************************************
***********************************************************************************/
function conservarImagen(id) {
        var prod_img_to_save = 'product_image_to_save';
        var product_img = '#product_img';
        var img_width = '200px';
        if(id != '-1'){
            prod_img_to_save = prod_img_to_save+'_'+id;
            product_img += '_'+id;
            img_width = '70px';
        }
	var src = document.getElementById('image_to_save').value;
	var rand_number = Math.floor(Math.random()*10001);
	document.getElementById(prod_img_to_save).value = src;
	jQuery(product_img).html("");
	jQuery(product_img).html("<img src='"+src+"?"+rand_number+"' width='"+img_width+"' />");
}

/**********************************************************************************
***********************************************************************************/
function resultadoUploadImage(data) {
	var popup_content;
	var popup_actions;
	var aux = data.split(';');
	switch(aux[0]){
		case "NO_FILE":
			jAlert('El archivo no existe. Sube otra imagen.', 'ERROR DE SUBIDA DE IMAGEN');
		break;
		case "FILE_ALREADY_EXISTS":
			jAlert('El archivo ya existe.', 'ERROR DE SUBIDA DE IMAGEN');
		break;
		case "EMPTY_FILE":
			jAlert('El archivo está vacío. Sube otra imagen.', 'ERROR DE SUBIDA DE IMAGEN');
		break;
		case "MAX_SIZE":
			jAlert('El tamaño máximo permitido para las imágenes es de 1MB', 'ERROR DE SUBIDA DE IMAGEN');
		break;
		case "EXT_NOT_ALLOWED":
			jAlert('Sólo puedes subir imágenes en formato JPG o PNG.', 'ERROR DE SUBIDA DE IMAGEN');
		break;
		case "UPLOAD_FAILED":
			jAlert('El archivo no se ha subido correctamente. Inténtalo de nuevo.', 'ERROR DE SUBIDA DE IMAGEN');
		break;
		case "UPLOAD_OK":
			var rand_number = Math.floor(Math.random()*10001);
			var nueva_imagen = "<img src='uploads/product_images/"+aux[1]+"?"+rand_number+"' width='200px'/>";
			jQuery('#img_producto_container').html(nueva_imagen);
			document.getElementById("image_to_save").value = "uploads/product_images/"+aux[1];
		break;
	}
}
/*************************************************************/
/***********************************************************************************************
function lightbox_imagen(id_producto, img_src)
***********************************************************************************************/
function lightbox_imagen(id_producto, img_src){
	var lightbox = "";
	
	lightbox += "<div class='lightbox' id='lightbox_img_"+id_producto+"'>";
	lightbox += "<div class='header'></div>";
	lightbox += "<div class='cuerpo'>";
	lightbox += "<div class='btn_close'><a href='javascript:void(0);' onclick='jQuery(\"lightbox\").fadeOut(\"slow\");";
	lightbox += "jQuery(\"#popup_behind\").fadeOut(\"slow\");jQuery(\"#lightbox\").html(\"\");'>[x]cerrar</a></div>";
	
	lightbox += "<img src='"+img_src+"' />";
	lightbox += "</div>";
	lightbox += "<div class='pie'></div>";
	lightbox += "</div>";
	
	jQuery('#lightbox').html(lightbox);
	
	centrarYMostrarPopUp("lightbox");
}





