var cropType;

function switchProfileImage (num,src) {
	$('displayImage').src = src;
	$('image_id').options[num].selected = true;
}

function startUpload(){
	var agb = $('accept_agb').checked;
	if (agb == false) {
		window.alert('Sie müssen die ABG akzeptieren.');
		return false;
	} else {
		$('image_loading').show();
		$('image_submit').value = 'Bild wird gespeichert...';
		$('image_submit').disabled = true;
		return true;
	};
}

function stopUpload (succes,filename) {
	if (succes) {
		Element.hide('settingsImageFrame');
		Element.hide('settingsImageContentFrame');	
		Element.hide('settingsFrameRight');	
		$('settingsFrameLeft').style.width = '100%';
		new Ajax.Updater('temp_image', "/?module=users&submodule=settings&section=image_edit&image=" + filename +"&ajax=1",{method:'get'});
		// Effect.BlindDown('temp_image');
		Effect.Appear('temp_image',{from:0.8});
		startCrop();
	} else {
		Element.hide('temp_image');
		Element.show('settingsFrameRight');
		settingsChange('image',1);
		window.alert('Upload fehlgeschlagen.');
	};
}

function imageComplete (path) {
	var path = path;
	$('settingsFrameLeft').style.width = '600px';
	Element.show('settingsFrameRight');
	settingsChange('image',1);
	window.setTimeout("switchProfileImage('0','" + path + "');",100);
}

function onEndCrop( coords, dimensions ) {
	$( 'x1' ).value = coords.x1;
	$( 'y1' ).value = coords.y1;
	$( 'x2' ).value = coords.x2;
	$( 'y2' ).value = coords.y2;
	$( 'width' ).value = dimensions.width;
	$( 'height' ).value = dimensions.height;
}

function startCrop (type, width, height) {
	cropType = type;
	window.setTimeout('loadCrop('+width+', '+height+');',200);
}

function loadCrop(width, height) {
		new Cropper.ImgWithPreview(
			'bigImage',
			{ 
				minWidth: width, 
				minHeight: height,
				ratioDim: { x: width, y: height },
				displayOnInit: true, 
				onEndCrop: onEndCrop,
				previewWrap: 'previewArea'
			}
		);
}

function sendResize () {
	new Ajax.Updater(
		'temp_image','/?module=users&submodule=settings&section=image_edit&ajax=1',
		{method:'post', evalScripts:true, parameters:Form.serialize('sendResize')}
	);
}
