//var fileSizeLimit = '2097152';//2mb
//var fileSizeLimit = '209715200';//200mb
var fileSizeLimit = '524288000';//500mb

$(document).ready(function() {
	$('#step-2').hide();
	$('#step-3').hide();
	$("#upload-form").validate();
	$('#step-1 button.next').click(function(event){
		event.preventDefault();
		if ($("#upload-form").valid()) {
			$('#step-1').fadeOut('normal',function(){
				$('#step-2').fadeIn();
			});
		}
		$('button.upload').hide();
		$('#upload-errors').hide();
		$('#upload-success').hide();
		var data = $('#upload-form').serializeObject();
		$("#fileUpload").uploadify({
			uploader: "/flash/uploadify.swf",
			expressInstall: "/flash/expressInstall.swf",
			buttonImg: '/images/browse.png',
			cancelImg: '/images/cancel.png',
			folder: 'clientFiles',
			script: '/upload.php',
			multi: true,
			scriptData: data,
			sizeLimit: fileSizeLimit,
			wmode: 'transparent',
			onSelectOnce: selectOnce,
			onError: uploadError,
			onCancel: fileCancel,
			queueSizeLimit: 10,
			onComplete: fileComplete,
			onAllComplete: uploadComplete,
			width: 110,
			height: 30
			
		});
		$('button.upload').click(function(event){
			event.preventDefault();
			$(this).fadeOut();
			window.onbeforeunload = function(){
				return "Clicking OK will cancel the upload.\n"
				+"Clicking Cancel will return you to the upload page.\n"
				+"\nYou are still uploading files.\nClosing this page will cancel all uploads.";
			};
			$("#fileUpload").uploadifyUpload();
		});
	});
	$('#step-2 button.next').hide().click(function(event){
		event.preventDefault();
		var formData = $('#upload-form').serialize();
		formData += files;
		$.post('/upload-complete.php', formData, function(){
			$('#step-2').fadeOut('normal',function(){
				$('#step-3').fadeIn();
			});
		});
		window.onbeforeunload = null;
	});
	function selectOnce(event, data){
		if (data.fileCount > 0) {
			$('button.upload').fadeIn();
		} else {
			$('button.upload').fadeOut();
		}
	}
	function uploadError(event, queueID, fileObj, errorObj){
		$('#upload-errors').show();
		switch(errorObj.type){
			case "File Size": error = "File size is larger then allowed upload size.";
		}
		$('#upload-errors ul').append('<li id="error-'+queueID+'">'+fileObj.name+': ' + error + '</li>');
	}
	var files = '';
	function fileComplete(event, queueID, fileObj){
		files +="&File[]="+fileObj.name;
	}
	function fileCancel(event, queueID, fileObj, data){
		if (data.fileCount <= 0) {
			$('button.upload').fadeOut();
		}
		$('#error-'+queueID).remove();
		if (!$('#upload-errors li').length){
			$('#upload-errors').hide();
		}
	}
	function uploadComplete(event, data){
		if (data.errors == 0) {
			$('#upload-success').show();
		}
		$('#step-2 button.next').show();
		window.onbeforeunload = function(){
			return "You have not finished submiting your request. Leaving this page now will cancel your uploads.";
		}
	}
	
});

$(function() {
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};	
});

jQuery.validator.addMethod("phone", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");