// var step1_upload_ready = false;
var ajax_rotate_process = false;

jQuery(document).ready(
function() {

    jQuery.fn.exists = function() {
        return ($(this).length > 0);
    }

    if (jQuery('form#step1').exists()) {



		if(jQuery('input#imagefilename').attr('value')!='') {
			set_image( jQuery('input#imagefilename').attr('value'));
		}


        new AjaxUpload('#uploadbutton', {
            action: 'step1-uploadimage.php',
            name: 'imagefile',
            onSubmit: function(file, ext) {
                //if (ext && new RegExp('^(' + allowed.join('|') + ')$').test(ext)){
				if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)) {
					jQuery('div#cropimage_placeholder').html('<img style="padding: 100px 220px 0px 0px" src="images/ajax-loader.gif" />');
					jQuery('div#content_right .status').text('Uploading...' + file);
				} else {
					// extension is not allowed
                    jQuery('div#content_right .status').text('Error: only images are allowed');
                    // cancel upload
                    return false;
                }

            },
            onComplete: function(file, response) {
				jQuery('div#cropimage_placeholder').html('&nbsp;');
	
                if (response.charAt(0) == '0') {
					jQuery('div#content_right .status').html('&nbsp;');
	            	jQuery('input#crop_x').val('');
	            	jQuery('input#crop_y').val('');
	            	jQuery('input#crop_x2').val('');
	            	jQuery('input#crop_y2').val('');

                    set_image(response.substring(2));
                } else {
					var error_msg = '';
					if(lang=='da') {
						error_msg = 'Billedet du har valgt fylder mere end de tilladte 4 MB. Forsøg venligst igen med et mindre billede';
					}
					else if(lang=='de') {
						error_msg = 'Das gewählte Bild ist größer als 4 MB, bitte wählen Sie eine kleinere Variante';
					}
					else {
						error_msg = 'The chosen image is larger than 4 MB, please try again with a smaller image';
					}
                    jQuery('div#content_right .status').html('<p style="color: red"><b>'+error_msg+'</p>');
                }

            }
       });
		

			

    }


    if (jQuery('form#step3-mail').exists()) {
		if(jQuery('input#display_mailform').val()=='1') {
			toogle_sendmail();
		}
	
	}

});


function update_jcrop_coords(c) {
    jQuery('input#crop_x').val(c.x);
    jQuery('input#crop_y').val(c.y);
    jQuery('input#crop_x2').val(c.x2);
    jQuery('input#crop_y2').val(c.y2);
};

function set_image(filename) {
    jQuery('input#imagefilename').attr('value', filename);
    jQuery('div#inner_right .status').text('');

    // set the image load event
     	img = new Image();
    jQuery(img).load(function() {
		var orig_w = jQuery(img).attr('width');
		var orig_h = jQuery(img).attr('height');
        // show image
        jQuery(img).width(434);
		jQuery(img).height(orig_h/(orig_w/434));
        jQuery('div#cropimage_placeholder').html('');
        jQuery('div#cropimage_placeholder').append(this);

        // save the size
        // jQuery('input#w').attr('value', jQuery(this).width());
        // jQuery('input#h').attr('value', jQuery(this).height());

        // calc setselect
		if(jQuery('input#crop_x').attr('value')!='') {
			var x = jQuery('input#crop_x').attr('value');
			var x2 = jQuery('input#crop_x2').attr('value');
			var y = jQuery('input#crop_y').attr('value');
			var x2 = jQuery('input#crop_x2').attr('value');
		}
		else {
			var ratio = (jQuery(this).width() / jQuery(this).height());
			var x = 0;
			var y = 0;
			var x2 = 0;
			var y2 = 0;
			if(ratio > 1.5) {
				y2 = jQuery(this).height();
				x = (jQuery(this).width() - (jQuery(this).height() * 1.5)) / 2;
				x2 = jQuery(this).width() - x;
			}
			else if(ratio < 1.5) {
				x2 = jQuery(this).width();
				y = (jQuery(this).height() - (jQuery(this).width() / 1.5)) / 2;
				y2 = jQuery(this).height() - y;
			}
			else {
				x2 = jQuery(this).width();
				y2 = jQuery(this).height();
			}
		}

        // start the crop engine
        jQuery('div#cropimage_placeholder img').Jcrop({
            onSelect: update_jcrop_coords,
            onChange: update_jcrop_coords,
            setSelect: [x, y, x2, y2],
            aspectRatio: 3 / 2
        });

	jQuery('div#rotate_nav').show();

    }).attr('src', 'uploads/'+jQuery('input#imagefilename').attr('value'));

	jQuery('input#rotate_right').click( function() {
		rotateimage( jQuery('input#imagefilename').val(), 270);
	});
	jQuery('input#rotate_left').click( function() {
		rotateimage( jQuery('input#imagefilename').val(), 90);
	});
}

function rotateimage(imagefilename, degrees) {
	if(ajax_rotate_process!=true) {
		ajax_rotate_process = true;
		jQuery.get('step1-rotate.php?imagefilename='+imagefilename+'&degrees='+degrees, function(data) {
			ajax_rotate_process = false;
			if (data.charAt(0) == '0') {
				jQuery('input#crop_x').val('');
				jQuery('input#crop_y').val('');
				jQuery('input#crop_x2').val('');
				jQuery('input#crop_y2').val('');
				set_image(data.substring(2));
			} else {
				jQuery('div#inner_right .status').text('Error: Fejl rotering af billede');
			}
		});
	}
}

function go_back(form_element, action) {
	jQuery(form_element).attr('action', action);
	jQuery(form_element).submit();
}



function toogle_sendmail() {
	if(jQuery('div#mail').css('display')=='none') {
		jQuery('div#mail').show();
		jQuery('input#mailform_tooglebutton').attr('class', 'graybutton');
		jQuery('input#modtager_navn').focus();
		jQuery('input#display_mailform').val('1');
		
	}
	else {
		jQuery('div#mail').hide();
		jQuery('input#mailform_tooglebutton').attr('class', 'redbutton');
		jQuery('input#display_mailform').val('0');
	}
}
