// WebWiz Wida ImageTool - Version 1.0.000

var imagetool_id=''
var imagetool_angle=0;
var imagetool_flip_h=false;
var imagetool_flip_v=false;
var imagetool_imagename='';
var imagetool_mode='';

var imagetool_x1=150;
var imagetool_y1=100;
var imagetool_x2=350;
var imagetool_y2=400;


function loadimagetool(tag_id,start_mode,start_crop,minimum_crop,keep_aspect_ratio){
	imagetool_id='#'+tag_id;
	imagetool_mode=start_mode;
	
	imagetool_imagename=$(imagetool_id+'_image').attr('src').replace('_'+imagetool_mode+'.','_[mode].')
	
	if(start_crop!=''){
		
		var coords=start_crop.split(',');
		imagetool_x1=coords[0];
		imagetool_y1=coords[1];
		imagetool_x2=coords[2];
		imagetool_y2=coords[3];
		
		if(minimum_crop!=''){
			
			var coords=minimum_crop.split(',');
			var minwidth=coords[0];
			var minheight=coords[1];
			
			if(keep_aspect_ratio=='1'){
			
				$(imagetool_id+'_image').imgAreaSelect({
					handles:'corners',
					persistent:true,
					x1:imagetool_x1,
					y1:imagetool_y1,
					x2:imagetool_x2,
					y2:imagetool_y2,
					minWidth:minwidth,
					minHeight:minheight,
					aspectRatio:minwidth+':'+minheight,
					onSelectEnd:function(img,selection){
						imagetool_x1=selection.x1;
						imagetool_y1=selection.y1;
						imagetool_x2=selection.x2;
						imagetool_y2=selection.y2;

						update_data_fields();
					}
				});
				
			}else{
				
				$(imagetool_id+'_image').imgAreaSelect({
					handles:'corners',
					persistent:true,
					x1:imagetool_x1,
					y1:imagetool_y1,
					x2:imagetool_x2,
					y2:imagetool_y2,
					minWidth:minwidth,
					minHeight:minheight,
					onSelectEnd:function(img,selection){
						imagetool_x1=selection.x1;
						imagetool_y1=selection.y1;
						imagetool_x2=selection.x2;
						imagetool_y2=selection.y2;

						update_data_fields();
					}
				});
				
			}
		}else{
		
			$(imagetool_id+'_image').imgAreaSelect({
				handles:'corners',
				persistent:true,
				x1:imagetool_x1,
				y1:imagetool_y1,
				x2:imagetool_x2,
				y2:imagetool_y2,
				onSelectEnd:function(img,selection){
					imagetool_x1=selection.x1;
					imagetool_y1=selection.y1;
					imagetool_x2=selection.x2;
					imagetool_y2=selection.y2;

					update_data_fields();
				}
			});

		}

	}
	
	$(imagetool_id+'_tools').css('display','block');
	$(imagetool_id+'_modes').css('display','block');
	
	applytransform();
}

function wida_imagetool(type,value){
	switch(type){
		case 'rotate':
			if(value=='ccw'){
				if(imagetool_flip_h||imagetool_flip_v){
					imagetool_angle+=90
				}else{
					imagetool_angle-=90
				}
			}
			
			if(value=='cw'){
				if(imagetool_flip_h||imagetool_flip_v){
					imagetool_angle-=90
				}else{
					imagetool_angle+=90
				}
			}
			
			break;
	
		case 'flip':
			if(value=='h'){
				if(imagetool_flip_v){
					imagetool_flip_v=false;
					imagetool_angle+=180;
				}else{
					imagetool_flip_h=!imagetool_flip_h;
				}

				//var x=imagetool_x1;
				//imagetool_x1=500-imagetool_x2;
				//imagetool_x2=500-x;
			}
			
			if(value=='v'){
				if(imagetool_flip_h){
					imagetool_flip_h=false;
					imagetool_angle+=180;
				}else{
					imagetool_flip_v=!imagetool_flip_v;
				}
				
				//var y=imagetool_y1;
				//imagetool_y1=500-imagetool_y2;
				//imagetool_y2=500-y;
			}
			
			//var ias=$(imagetool_id).imgAreaSelect({instance:true});
			//ias.setSelection(imagetool_x1,imagetool_y1,imagetool_x2,imagetool_y2,true);
			//ias.update();
			
			break;
		
		case 'mode':
			imagetool_mode=value;
			break;
	}

	if(imagetool_angle<0){imagetool_angle+=360;}
	if(imagetool_angle>270){imagetool_angle-=360;}

	applytransform();
}

function applytransform(){
	$(imagetool_id+'_image').attr('src',imagetool_imagename.replace('[mode]',imagetool_mode));
	
	if(imagetool_flip_h){
		$(imagetool_id+'_image').transform({
			reflectY:true,
			rotate:imagetool_angle+'deg'
		});
	}
	
	if(imagetool_flip_v){
		$(imagetool_id+'_image').transform({
			reflectX:true,
			rotate:imagetool_angle+'deg'
		});
	}
	
	if(!imagetool_flip_h&&!imagetool_flip_v){
		$(imagetool_id+'_image').transform({
			rotate:imagetool_angle+'deg'
		});
	}
	
	update_data_fields();
}

function update_data_fields(){
	$('#img_mode').val(imagetool_mode);
	$('#img_angle').val(imagetool_angle);
	
	$('#img_fliph').val(imagetool_flip_h);
	$('#img_flipv').val(imagetool_flip_v);
	
	$('#img_x1').val(imagetool_x1);
	$('#img_y1').val(imagetool_y1);
	$('#img_x2').val(imagetool_x2);
	$('#img_y2').val(imagetool_y2);
}

