/**
 * @author Mawi
 * Background Worker Client Side Library
 */
(function($){

	//TODO: Add better handling for errors happen when making the AJAX call
    var grader = window.grader = window.grader ||
    {}, twitter = grader.twitter = grader.twitter ||
    {}, backgroundWorker = twitter.backgroundWorker = twitter.backgroundWorker ||
    {};
    
    backgroundWorker.progressContainer = "";
    backgroundWorker.finishCallback = null;
    backgroundWorker.currentWorkerId = "";
    backgroundWorker.callBackUrl = "";
    backgroundWorker.progressHandlerId = null;
    backgroundWorker.checkProgressInterval = 2000;
    backgroundWorker.triggerObject = "";
    backgroundWorker.startingMessage = "";
    backgroundWorker.inProgress = false;
    
    backgroundWorker.init = function(CallbackUrl, progressContainer, triggerObject, startingMessage){
        backgroundWorker.callBackUrl = CallbackUrl;
        backgroundWorker.progressContainer = progressContainer;
        backgroundWorker.triggerObject = triggerObject;
        backgroundWorker.startingMessage = startingMessage;
        $(document).ajaxError(function(event, request, settings){
            backgroundWorker.errored();
        });
    };
    
    backgroundWorker.startWorker = function(args, finishCallback){
        if (backgroundWorker.inProgress) {
            return;
        }
        backgroundWorker.finishCallback = finishCallback;
        url = backgroundWorker.callBackUrl + "?" + args;
        backgroundWorker.started();
        $.getJSON(url, function(data){
            if (backgroundWorker.validData(data)) {
                backgroundWorker.currentWorkerId = data.guid;
                backgroundWorker.progressHandlerId = window.setInterval(backgroundWorker.progress, backgroundWorker.checkProgressInterval);
            }
        });
    };
    
    backgroundWorker.progress = function(){
        url = backgroundWorker.callBackUrl + "?Action=Progress&WorkerId=" + backgroundWorker.currentWorkerId;
        $.getJSON(url, function(data){
            if (!backgroundWorker.validData(data) || data.status == 3) {
                backgroundWorker.errored();
            }
            else {
                $(backgroundWorker.progressContainer).text(data.progress);
                if (data.status == 2) {
                    backgroundWorker.finished();
                }
            }
        });
    };
    
    backgroundWorker.validData = function(data){
        return (data != null && data.guid != null && data.guid != "null");
    };
    
    backgroundWorker.started = function(){
    
        var starting = "Initializing...";
        if (backgroundWorker.startingMessage != null && backgroundWorker.startingMessage != "") {
            starting = backgroundWorker.startingMessage;
        }
        $(backgroundWorker.progressContainer).html(starting);
        $(backgroundWorker.progressContainer).removeClass();
        $(backgroundWorker.progressContainer).addClass("message info");
        $(backgroundWorker.progressContainer).fadeIn();
        
        $('#progressImg').remove();
        var progressImage = $(document.createElement('img')).attr('id', 'progressImg').attr('src', 'http://twitter.grader.com/assets/img/loadingSmaller.gif');
        progressImage.insertAfter($(backgroundWorker.triggerObject));
        backgroundWorker.inProgress = true;
    };
    
    backgroundWorker.errored = function(){
        window.clearInterval(backgroundWorker.progressHandlerId);
        backgroundWorker.inProgress = false;
        $(backgroundWorker.progressContainer).text("Encountered an error processing resynch");
        $(backgroundWorker.progressContainer).removeClass();
        $(backgroundWorker.progressContainer).addClass("message alert");
        $('#progressImg').remove();
    };
    
    backgroundWorker.finished = function(){
        window.clearInterval(backgroundWorker.progressHandlerId);
        backgroundWorker.inProgress = false;
        $('#progressImg').remove();
        $(backgroundWorker.progressContainer).removeClass();
        $(backgroundWorker.progressContainer).addClass("message correct");
        $(backgroundWorker.progressContainer).fadeOut(5000);
        if (backgroundWorker.finishCallback != null) {
            backgroundWorker.finishCallback();
        }
    };
    
    
})(jQuery);

