// JavaScript Document
$.ui.dialog.defaults.bgiframe = true;

var name = '';
var phone = '';
var organization = '';

$(function() {
	// Specify the dialog box options.
	$("#easy-uploader").dialog({
		draggable: false,
		resizable: false,
		show: "fold",
		hide: "fold",
		modal: true,
		autoOpen: false,
		stack: false,
		width: 415,
		dialogClass: 'main-dialog',
		close: function(){
			if ( name == '' || phone == '' || organization == '' ) { 
				$('#eu-1').show();
				$('#eu-2').hide();
			} else {
				$("#eu-2").show();
				$('#easy-uploader > p').remove();
				ajaxButton();
			}
		}
	});
	
	$(".easy-uploader-btn").click(function(e) {
		e.preventDefault();
		// Open the dialog box on click
		$("#easy-uploader").dialog('open');
	});
	
	$(".files-submit").hover(function () {
			$(this).addClass("hover");
		}, function () {
			$(this).removeClass("hover");
	});
	
	// Specify sub dialog aka loading dialog options
	$("#loading-dialog").dialog({
		height: 32,
		width: 32,
		draggable: false, 
		resizable: false, 
		autoOpen: false,
		modal: true,
		stack: true,
		dialogClass: 'sub-dialog'		
	});
	
	$(".easy-uploader-next").click(function(e){
		e.preventDefault();
		
		name = $('#PictureName').val();
		phone = $('#PicturePhone').val();
		email = $('#PictureEmail').val();
		organization = $('#PictureFolder').val();
		
		if ( name == '' || phone == '' || organization == '' || email == '' ) { 
			alert('Please fill out the form completely.');
		} else { 
			$("#eu-1").fadeOut(500, function(){
				$('#eu-2').fadeIn(500);
			});
		}
	});
		
	$(".easy-uploader-submit").click(function(e) {
		e.preventDefault();
		
		if ($('.files > li').length == 0) {
			alert("Error: no files have been uploaded.");
			return false;
		}
		
		var uploadedFiles = $('.files').html();
		
		$.post('/js/scripts/email.php', {name: name, phone: phone, email: email, from: organization, files: uploadedFiles});
		
		$("#eu-2").fadeOut(300, function(){
			$("#easy-uploader").append("<p class='big bold'>Your files have been received. Thank you for contacting us!<p>");
		});
		
		setTimeout(function () {
				if ($("#easy-uploader").dialog('isOpen')) {
					$("#easy-uploader").dialog('close');
				}
			},
				3000
		);
	});
	
	ajaxButton();


	function ajaxButton() {
		var uploader = new AjaxUpload( $('#eu-choose'), {
			action: '/files/eu', 
			name: 'picture',
			autoSubmit: true,
			responseType: 'text/html',
			onChange : function(file, ext){
				var folder = $("#PictureFolder").val();
				
				if (folder == '') {
					alert("Error: company field has not been specified.");
					return false;
				}
				
				if (! (ext && /^(xcf|pix|matte|mask|alpha|fli|flc|xcf|bz2|xcfbz2|c|h|dcm|dicom|eps|epsi|epsf|fit|fits|gbr|gif|gih|pat|gz|xcfgz|jpg|jpeg|jpe|cel|ico|mng|pbm|pgm|psd|png|pnm|ppm|sgi|rgb|bw|icon|iml|im8|im24|im32|tga|tif|tiff|bmp|xbm|bitmap|xpm|pcx|pcc|ai|zip|rar|pdf|indd|doc|docx|pub|dxf|xml|fs)$/.test(ext))){
					alert('Error: invalid file extension ('+ext+'). Adobe formats (PDF, AI, EPS) or archives (ZIP, RAR) are preferred.');
					return false;
				}
				
				this.setData({'folder': folder});				
				
				$("#loading-dialog").dialog('open');
			},
			onComplete: function(file, response){
				// add file to the list
				$('<li></li>').appendTo('.files').text(file);	
				
				// disable the form company input field so that all files and email is sent to the correct place
				$('#PictureFolder').attr('disabled', true);
				
				$("#loading-dialog").dialog('close');
				
				this.enable();
			}
		});
	}

});
