$(document).ready(function(){
    //var codigoGaleria = document.getElementById('idProduct').value;
    
	$('#image').uploadify({
		'uploader': '../jquery.upload/uploadify.swf', // 
		'script': '../includes/upload.php', // Script que será utilizado no upload
		'cancelImg': '../jquery.upload/cancel.png', // Imagem do botão de cancelar
		'auto': true, // True = fazer o upload assim que o arquivo for selecionado
		'multi': false, // True = permitir que que seja feito upload de mais de um arquivo ao mesmo tempo

		'scriptData': { },

		'fileDesc': 'Image Files', // Descrição do tipo de arquivo que vai ser feito o upload
		'fileExt': '*.jpg;*.png;*.gif', // Extensões permitidas no upload

		'queueID' : 'imagemQueue', // Id da "div" onde vai aparecer o loading da imagem 
		'sizeLimit': 4194304, // ( 4 Mb ) Tamanho máximo permitido para o arquivo
		'buttonText': 'Selecione ...', // Texto que será exibido dentro do botão
		'folder': 'uploads/profile', // Diretório que será salvo os arquivos
		'onComplete': function(event, queueID, fileObj, response, data) {
			alert(response);
			/*var newImg = document.createElement("img");
			newImg.style.margin = "5px";

			document.getElementById("thumbnails").innerHTML = "";
			document.getElementById("thumbnails").appendChild(newImg);
			
			if (newImg.filters) {
				try {
					newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
				} catch (e) {
					// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
					newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')';
				}
			} else {
				newImg.style.opacity = 0;
			}

			newImg.onload = function () {
				fadeIn(newImg, 0);
			};
			newImg.src = response;
			*/
			
		},
		'onError': function(event, queueID, fileObj, errorObj) {
			alert(errorObj.type +" = "+errorObj.info);
		}
    });
});


function fadeIn(element, opacity) {
	var reduceOpacityBy = 5;
	var rate = 30;	// 15 fps


	if (opacity < 100) {
		opacity += reduceOpacityBy;
		if (opacity > 100) {
			opacity = 100;
		}

		if (element.filters) {
			try {
				element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
			}
		} else {
			element.style.opacity = opacity / 100;
		}
	}

	if (opacity < 100) {
		setTimeout(function () {
			fadeIn(element, opacity);
		}, rate);
	}
}
