var jcrop_api;

$(document).ready(function() { 
	
	$('a.upload-preview').each(function(){
		
		$(this).attr('href', $(this).attr('href') + $('#file-name').html());
		$(this).attr('target', '_blank');
	});

	if ($('#file-versions .upload-version-size span.can-crop').length){
		
		$('#file-versions .upload-version-size span.can-crop').click(function(){
			
			$('#file-versions .upload-version-size span.can-crop').removeClass('active');
			$(this).addClass('active');
			
			$('#crop-cont').css('background', 'none repeat scroll 0 0 #697165');
			$('#crop-cont').html('<p>Loading the original image...</p><p><img src="/images/loader-green.gif" width="220" height="19" alt="" /></p>');
			$('#crop-cont').show();
			$('#iframe-' + field_name, window.parent.document).data('orig_height', $('#iframe-' + field_name, window.parent.document).height());
			$('#iframe-' + field_name, window.parent.document).height(200);
			
			$.post(crop_url, { size : $(this).attr('rel'), request : 'init', file_name : $('#file-name').html() }, function(data){
				
				if (data.status == 1){
					
					$('#iframe-' + field_name, window.parent.document).height(data.height + 200);

					var html	= '<p>Drag and resize the box to where you want to crop. '
								+ 'When you are finished, click &nbsp;&nbsp; <input type="submit" id="crop-now" value="crop now" />&nbsp;&nbsp;&nbsp;'
								+ '<a class="crop-closer">cancel</a></p>'
								+ '<p>'
								+ '<img id="to-be-cropped" src="' + data.web_path + '" width="' + data.width + 'px" height="' + data.height + 'px" />'
								+ '</p>';

					$('#crop-cont').html(html);

					jcrop_api = $.Jcrop('#to-be-cropped', {
						
						aspectRatio: data.crop_width / data.crop_height,							
						setSelect:   [ 40, 40, (40 + (data.crop_width / 2)), (40 + (data.crop_height / 2)) ]
					});

				} else {
					
					alert('We are having trouble finding the original image to crop. Please contact us or try again later.');
					close_crop_tool();
				}

			}, 'json');
		});
	}

	$('.overlay-closer', window.parent.document).live('click', function(){
		
		$('#overlay', window.parent.document).hide();
	});

	$('#crop-now').live('click', function(){
		
		$('#crop-cont').html('<p>Cropping, please wait...</p><p><img src="/images/loader-green.gif" width="220" height="19" alt="" /></p>');

		pos = jcrop_api.tellSelect();
		
		var vars = { 
			
			size		: $('span.can-crop.active').attr('rel'), 
			request		: 'make_crop', 
			file_name	: $('#file-name').html(),
			x			: pos.x,
			y			: pos.y,
			x2			: pos.x2,
			y2			: pos.y2
		};

		$.post(crop_url, vars, function(data){
			
			close_crop_tool();

			var height = $('#iframe-' + field_name, window.parent.document).data('orig_height') + 60;
			$('#iframe-' + field_name, window.parent.document).height(height);
			$('#crop-cont').html('<div class="success" style="display: none; padding: 10px;">The image was cropped and saved.</div>');
			$('#crop-cont').css('background', 'none');
			$('#crop-cont div.success').fadeIn();

		}, 'json');
	});

	$('a.crop-closer').live('click', function(){
		
		close_crop_tool();
		$('#crop-cont').fadeOut();
		return false;
	});
});

function close_crop_tool(){
	
	var height = $('#iframe-' + field_name, window.parent.document).data('orig_height');
	$('#crop-cont').html('');
	$('#iframe-' + field_name, window.parent.document).height(height);
	$('#file-versions .upload-version-size span.can-crop').removeClass('active');
}
