var effect_id = 0;
var effect_folder = 0;
var cropEw = 0;
var cropEh = 0;
var thumb_w = 0;
var thumb_h = 0;

function start(id,width,height,folder) {
   	effect_id = id;
   	effect_folder = folder;
	cropEw = width;
	cropEh = height;
	modal('choosePhoto');
}

function choosePhotoClick(id,thumb_path,thumb_size) {
	photo_id = id;
	modalClose();
	ttemp = thumb_size.split('x');
	thumb_w = ttemp[0];
	thumb_h = ttemp[1];
	$('#choosePhotoCropCont').append('<img id="cropPhoto" src="'+sys_url+thumb_path+'" border="0" hspace="0" vspace="0">');
	modal('choosePhotoCrop');
}

function choosePhotoCrop_start() {
	k = cropEw/cropEh;
	img_width = thumb_w;
	img_height = Math.round(thumb_w/k);
	if(img_height>thumb_h) {
		img_height = thumb_h;
		img_width = Math.round(thumb_h*k);
	}
	$('#cropPhoto').imgAreaSelect({
		minWidth: 20,
		minHeight: 20,
		x1: 0, y1: 0, x2: img_width, y2: img_height,
		persistent: false,
		handles: true,
		aspectRatio: cropEw+":"+cropEh,
		onInit: function (img, selection) {
			cropW = selection.width;
			cropH = selection.height;
			cropX = selection.x1;
			cropY = selection.y1;
		},
		onSelectEnd: function (img, selection) {
			cropW = selection.width;
			cropH = selection.height;
			cropX = selection.x1;
			cropY = selection.y1;
		}
	});
}

function choosePhotoMakeCrop() {
    modalClose();
    modal('loading');
	$.get(sys_url+"index.php", {c:"main",a:"process",photo_id:photo_id,effect_id:effect_id,w:cropW,h:cropH,x:cropX,y:cropY,e_w:cropEw,e_h:cropEh,folder:effect_folder},
  		function(data){
			msg = data.split("|");
			$('div#resultCont').append('<img id="result" src="'+sys_url+msg[0]+'" border="0" hspace="0" vspace="0" />');
			$('#link').val(sys_url+'result/'+msg[1]);
  			addthis.toolbox("#result_share_toolbox", {},{url: sys_url+'result/'+msg[1], title: 'Funny photo effects', desription: 'by photofacefun.com'});
  			modalClose();
  			modal('result');
		}
  	);
}

function result_start() {}
function result_end() {
	$('div#resultCont').empty();
}
