var crop = new Array();

function showTip(tip) {
	$('#comment').html(tip);
}

function rotate(dir) {
	params = { i: i, action: 'rotate'+dir }; //, w: $('editImage').width, h: $('editImage').height }
	toolAction(params);
}

function mirror(dir) {
	params = { i: i, action: 'mirror'+dir }
	toolAction(params);
}

function toggleTool(actionOn, actionOff, actionCancel, comment) {
	$('#comment').html('');
	$('#controls').hide();
	$('#subcontrols').html($('#'+comment+'Comment').html() +
		'<div id="apply"><a href="javascript:'+actionOff+'">'+lang.apply+'</a>' +
		' | <a href="javascript:'+actionCancel+'">'+lang.cancel+'</a></div>');
	$('#subcontrols').show();
	eval(actionOn);
}

function toggleOffTool(action) {
	if (action) eval(action);
	$('#subcontrols').hide();
	$('#controls').show();
}

function textStart() {
}

function textCancel() {
	$('#textHandler').attr({src: '/img/px.gif'});
	toggleOffTool();
}

function textApply() {
	params = { action: 'textSave', posX: $('#textPosX').val(), posY: $('#textPosY').val(), color: $('#colorField').val(), font: $('#font').val(), text: $('#textt').val(), size: $('#fontSize').val(), i: i };
	$('#textHandler').attr({src: '/img/px.gif'});
	toolAction(params);
}

function textAdd() {
	params = { action: 'textAdd', color: $('#colorField').val(), font: $('#font').val(), text: $('#textt').val(), size: $('#fontSize').val(), i: i };
	toolAction(params, 1);
}

function effectsPreview() {
	effect = $('#effectSel').val();
	if (effect != 'none') {
		params = { action: 'effect', effect: effect, i: i, preview: 1 };
		toolAction(params, 1, 1);
	}
}

function effectsStart() {
}

function effectsApply() {
	params = { action: 'effectApply', i: i };
	toolAction(params);
}

function effectsCancel() {
	swapImg(img);
	toggleOffTool();
}

function undo() {
	params = { action: 'undo', i: i }
	toolAction(params);
}

function toolAction(params, toggle, preview) {
	if (preview) img2 = temp; else img2 = img;
	loadingToggle();
	$.ajax({
		url: '/create/editor_action', 
		type: 'POST',
		dataType: 'html',
		data: params,
		success: function(data, status) {
		    $('#console').html(data);
            loadingToggle();
            swapImg(img2, 1);
			if (!toggle) toggleOffTool();
			if (!preview) $('#undo').show();
		}
	});
}

function swapImg(img, swapThumb) {
    r = Math.random()*100;
    $('#editImage').attr({src: img + '?' + r});
    if (thumb) {
    	$('#frame_img_'+i).attr({src: thumb + '?' + r});
	}
}

function colorPicker() {
	var t = $('#colorPickerButton').offset();
	$('#colorPicker').css({top:t.top+14+'px',left:t.left-88+'px'});
	$('#colorPicker').toggle();
}

function fontsList() {
	$('#fontsList').fadeIn();
}

function setFont(font) {
	$('#font').val(font);
	$('#fontPreview').attr({src: 'data/fonts/'+font+'.gif'});
	$('#fontsList').hide();
}

function saveChanges() {
    hideModal();
}

function cropStart() {
	CropImageManager.init();
}

function cropApply(params) {
	CropImageManager.removeCropper();
	params = { action: 'crop', x: crop[0], y: crop[1], w: crop[2], h: crop[3], i: i }
	toolAction(params);
}

function cropCancel() {
    CropImageManager.removeCropper();
	toggleOffTool();
}

var CropImageManager = {
		curCrop: null,
		
		init: function() {
			this.attachCropper();
		},
		
		onChange: function( e ) {
			var vals = $F( Event.element( e ) ).split('|');
			this.setImage( vals[0], vals[1], vals[2] ); 
		},
		
		setImage: function( imgSrc, w, h ) {
			$( 'testImage' ).src = imgSrc;
			$( 'testImage' ).width = w;
			$( 'testImage' ).height = h;
			this.attachCropper();
		},
		
		attachCropper: function() {
			options = {
					//captureKeys: false,
					//displayOnInit: true,
					//onloadCoords: { x1: 0, y1: 0, x2: 100, y2: 100 },
					onEndCrop: function (coords, dimensions) {
						crop[0] = coords.x1;
						crop[1] = coords.y1;
						crop[2] = dimensions.width;
						crop[3] = dimensions.height;
					}
			};
			if (cw) options.ratioDim = { x: cw, y: ch };
			if( this.curCrop == null ) this.curCrop = new Cropper.Img( 'editImage', options );
			else this.curCrop.reset();
		},
		
		removeCropper: function() {
			if( this.curCrop != null ) {
				this.curCrop.remove();
			}
		},
		
		resetCropper: function() {
			this.attachCropper();
		}
}
