MOBX.VideoPickerUpload = function () {
    
    var publicObj = {};
    var currentUploadVideoUid = null;
    var canceledUploads = [];
    
    var uploadTemplate = new Element("div", {'id': 'uploading_video', 'class': 'video uploading'});
    //"<div id='uploading_video' class='video uploading'></div>";
    
    var uploadingText = "<h4 id='uploading_video_title'><em>Your title here</em></h4><div class='encoding_or_uploading_spinner thumbnail'><img src='/images/ajax-loader.gif' class='spinner' alt='video is uploading or encoding' width='31' height='31' /></div><p>Upload progress</p><div class='upload_progress_bar'><div class='upload_progress' style='width: 2%'></div></div>"; 
    var spinnerMessage = "<img alt='loading...' src='/images/spinner.gif' />One moment please&hellip;";
	
	var initialize = function (evt) {       
        if ($('uploading_video')) {
            return;
        }
        $("upload_error").hide();
        MOBX.VideoPicker.goToFirstPage();
        var ins = $("video_picker").insert({top: uploadTemplate });
    };

	var uploadBrowsing = function () {
        $("upload_link_messaging").update("Choose a video and click 'select' to get started: Premium Members - upload files as big as you please (we recommend you limit your videos to 300mb). Basic Members - 100mb or less");
    	displayMessage();
	};

	var uploadSelected = function (evt) {
		$("upload_link_messaging").update(spinnerMessage);
		displayMessage();
        MOBX.EventHandler.fireCustom($("video_picker"), "video_added");
		MOBX.EventHandler.fireCustom($("uploading_video"), "upload_begin");
     
	};
	
	var displayMessage = function () {
		$$(".upload_link").invoke("hide");
		$("upload_link_messaging").show();
	};
	
    var toggleUploadLink = function () {
        $$(".upload_link").invoke("toggle");
        $("upload_link_messaging").toggle();
    };
    
    var resetVideoPickerUpload = function (statusMessage) {
		MOBX.EventHandler.fireCustom($('video_picker'), "upload_reset");
		if (statusMessage) {
            $("upload_error").update(statusMessage).show();
        } else {
            $("upload_error").hide();
        }
        if ($("uploading_video")) {
            $("uploading_video").remove();
        }
        $$(".upload_link").invoke("show");
        $("upload_link_messaging").hide();
        $("upload_link_messaging").update("One moment please...");
    };
    
    
    var progressIndicator = function (evt) {
        var progress = evt.progress;
        if (progress == 100) {
            progress = 99;
        }
        if ($('upload_link_messaging').down(".video_picker_upload_percentage")) {
            $('upload_link_messaging').down(".video_picker_upload_percentage").update([progress, "%"].join(""));
            $("uploading_video").down(".upload_progress").style.width = [progress, "%"].join("");
        }
    };
    
    var uploadComplete = function (evt) {
        $("upload_link_messaging").update("Upload complete. Now we're confirming everything went ok.");
    };
    
    var uploadConfirmed = function (evt) {
        $("upload_link_messaging").update("<div>Your video is now being optimized. This may take a few minutes, especially if you <br />have a larger file. Bear with us! <a href='#' id='cancel_encoding_link'></a></div>");
		currentUploadVideoUid = evt.upload.attributes.video_uid;
        debug("fetching video after confirmation");
        debug("currentUploadVideoUid: " + currentUploadVideoUid);
        MOBX.Video.fetchVideo(currentUploadVideoUid, insertVideo);
        debug('firing video_added');
    };
    
    var insertVideo = function (video) {        
        if (canceledUploads.indexOf(video.uid) != -1) {
            return;
        }
        if ($('uploading_video')) {
            debug('removed uploading video');
            $('uploading_video').remove();
        }
        if ($("video_" + video.uid)) {
            debug('removed video_' + video.uid);
            $("video_" + video.uid).remove();
        }
        
        debug('off to insert the video');
        
        MOBX.JSONUpdater.Insert.top($("video_picker"), video, 200);
        if (video.status == "encoding") {
            MOBX.Video.insertIntoLoaded(video, 0);
            debug('that video had a status of encoding');
            var video_container = $("video_" + video.uid);
            video_container.removeClassName("responds_to_mouseover");
            MBX.EncoderController.startUpdatingFromElem(video_container);
        }
    };
    
    var uploadCanceled = function () {
        resetVideoPickerUpload();
    };
    
    var uploadUploading = function (evt) {
        $("upload_link_messaging").update("<div class='video_picker_upload_percentage'>0%</div><div class='video_picker_upload_uploading_text'>Woo-hoo! Your video is uploading as we speak. Stick around - if you leave this page it won't upload correctly! <a href='#' id='cancel_upload_link'></a></div>");
        debug('upload_link_messaging inserted');
        $("uploading_video").update(uploadingText);
        debug('everything except title');
        $("uploading_video_title").update(evt.fileObj.name.truncate(20));
    };
    
    var uploadError = function (evt) {
        if (!$("upload_error").visible()) {
            $("upload_error").update("Oops! Your upload didn't go as planned. Let's try that again.");
            resetVideoPickerUpload();
            $("upload_error").show(); 
        }
    };
    
    var uploadNotAllowed = function (evt) {
        var url = "/promotions?utm_source=motionbox&utm_medium=onsite&utm_campaign=out_of_storage_space_upgrade_link"
        $("upload_error").update("Oops! You've exceeded your limit! <a href='" + url + "'>Upgrade to Premium</a> to get <em>unlimited</em> storage or select another video.");
        resetVideoPickerUpload();
        $("upload_error").show();
    };
    
    var handleCancelUploadLink = function (evt) {
        Event.stop(evt);
        var ans = confirm("Are you sure you want to cancel your upload?");
        if (ans) {
            MOBX.Upload.cancel();
            setTimeout(function () {
                $("upload_error").update("").hide();
            }, 200);
        }
    };
    
    var handleCancelEncodingLink = function (evt) {
        Event.stop(evt);
        var ans = confirm("Are you sure you want to cancel optimization?");
        if (ans) {
			resetVideoPickerUpload();
            if (currentUploadVideoUid) {
                canceledUploads.push(currentUploadVideoUid);
            }
            $("upload_error").update("");
            var encodingElems = $$('.encoding');
            if (encodingElems) {
                if (encodingElems.first() && encodingElems.first().parentNode) {
                    MOBX.EventHandler.fireCustom(encodingElems.first(), 'stopUpdating');
                    MOBX.Video.removeVideo(MOBX.Video.fromDomElement(encodingElems.first()));
                    encodingElems.first().parentNode.remove();
                }
            }
        }
    };

	MOBX.EventHandler.subscribe("#video_picker", "upload_initialize", initialize);
    MOBX.EventHandler.subscribe("#video_picker", "upload_browsing", uploadBrowsing);
	MOBX.EventHandler.subscribe("#video_picker", "upload_selected", uploadSelected);
    MOBX.EventHandler.subscribe("#uploading_video", "upload_uploading", uploadUploading);
    MOBX.EventHandler.subscribe("#uploading_video", "upload_progress", progressIndicator);
    MOBX.EventHandler.subscribe("#uploading_video", "upload_error", uploadError);
    MOBX.EventHandler.subscribe("#uploading_video", "upload_not_allowed", uploadNotAllowed);
    MOBX.EventHandler.subscribe("#uploading_video", "upload_complete", uploadComplete);
    MOBX.EventHandler.subscribe("#uploading_video", "upload_confirmed", uploadConfirmed);
    MOBX.EventHandler.subscribe("#uploading_video", "upload_canceled", uploadCanceled);

    MOBX.EventHandler.subscribe("#cancel_upload_link", "click", handleCancelUploadLink);
    MOBX.EventHandler.subscribe("#cancel_encoding_link", "click", handleCancelEncodingLink);

    MOBX.EventHandler.subscribe(MOBX.cssNamespace, 'video_fetched', function (event) {
        if (event.video.status == 'encoding') {
            MBX.EncoderModel.startUpdating(event.video.uid);
        }
    });
    
    MOBX.EventHandler.subscribe(MBX.cssNamespace, MBX.CustomEvent.Encoder.FINISHED, function (event) {
        // go ahead and show the message, even though the video probably hasn't been redrawn yet.
        // the redraw will happen soon enough, and otherwise we're doing two identical AJAX requests in two places
        resetVideoPickerUpload();
        $("upload_error").update("Success! It's a beautiful thing. Your video has been successfully uploaded and optimized for playback. You can find it in the 'Unsorted Videos' folder!");
        $("upload_error").show();
    });
    
    MOBX.EventHandler.subscribe(MBX.cssNamespace, MBX.CustomEvent.Encoder.FAILED, function (event) {
        var videoElems = MBX.EncoderController.findVideoElems(event.uid, event.format);
        videoElems.each(function (elem) {
            elem.down(".encoding_status").update("Optimization failed.");
            elem.down(".spinner").hide();
        });
        resetVideoPickerUpload("Sorry, optimization of your video didn't go as planned. <a href='/help/faq/troubleshooting_motionbox'>What does this mean?</a>");
    });
    
    var debug = function (txt) {
        //console.log(txt);
    };
    
    return publicObj;
    
}();
