var fixed = 1;
var size_w = 150;
var size_h = 150;
var path = '/inc/image_upload/';
var target = '/uploads';
var show_preview = true;

function initUploadParams(f, sw, sh, p, t, sp){
	fixed = f;
	size_w = sw;
	size_h = sh;
	path = p;
	target = t;
	show_preview = sp;
}

function ajaxFileUpload(){
	$("#loading").ajaxStart(function(){
		$(this).show();
	}).ajaxComplete(function(){
		$(this).hide();
	});

	$.ajaxFileUpload({
		url: path + 'functions.php?action=upload&target=' + target,
		secureuri: false,
		fileElementId: 'file',
		dataType: 'json',
		success: function(data, status){
			if(typeof(data.error) != 'undefined') {
				if(data.error != '')
					alert(data.error);
			}
			else{	
				$("#fname").val(data.msg); //get filename from the response
				$("#image").prepend($(document.createElement("img")).attr({src: target + "/" + data.msg, id:"jcrop"})).show(); // create image and append the html inside <div id=#image>
				//if crop gives fixed width then preview will be shown
				if(fixed && show_preview)
					$("#crop_preview").css({width:size_w,height:size_h,margin:"0 auto"}).append($(document.createElement("img")).attr({src: target + "/" + data.msg,id:"preview"})).show();//create image and prepend  the html inside <div id=#image>
				
				jCrop('#jcrop'); //initialize jCrop for the image
				$("#upload").hide();
			}
		},
		error: function(){
			alert("Ocorreu um erro!");
		}
	});
	
	return false;
}

/* function jCrop(f)
*  used to initialize jCrop plugin
*  f = id of image on which jCrop should work
*  boxWidth = 475
* */
function jCrop(f) {
	$(f).Jcrop({
		onChange: updateCoords,
		onSelect: updateCoords,
		aspectRatio: size_w / size_h,
		setSelect: [ 100, 100, 350, 350 ],
		boxWidth: 475 //maximum width of image
	});
}

/*
 * function updateCoords(c)
 * updates the selection in jCrop image
 * also prepares live preview
 */
function updateCoords(c){
	$('#x').val(c.x);
	$('#y').val(c.y);
	$('#w').val(c.w);
	$('#h').val(c.h);
	
	//if crop gives fixed size, preview will be shown
	if(fixed) {
		var rx = size_w / c.w;
		var ry = size_h / c.h;
		
		// Live Preview
		$('#preview').css({
			width: Math.round(rx * $("#jcrop").width()) + 'px',
			height: Math.round(ry * $("#jcrop").height()) + 'px',
			marginLeft: '-' + Math.round(rx * c.x) + 'px',
			marginTop: '-' + Math.round(ry * c.y) + 'px'
		});
	}
}

/*
 * function crop()
 * ajax function which returns the cropped image
 */
function crop(){
	$.ajax({
		type: "POST",
		url: path + "functions.php?action=crop",
		data: {x: $('#x').val(), y: $('#y').val(), w: $('#w').val(), h: $('#h').val(), fname: $('#fname').val(), fixed: fixed, size_w: size_w, size_h: size_h, target: target},
		success: function(msg){
			if(!fixed)
				$("#crop_preview").css({overflow:"auto"});
			
			filename = msg;
			cropped = $(document.createElement("img")).attr("src", target + '/' + filename).addClass('avatar');
			
			$('#image_upload_target img').remove();
			$('#image_upload_target').prepend(cropped);
			$('#image_upload_value').val(filename);
			$('#image_upload_link').html('Clique para alterar a imagem');
			
			$.facebox.close();
		}
	});
}
