window.job = window.job || {};

job.question = {
    id : '',
    seen : new Array(),
    allowMultiple : false,
    selectedAnswers : new Array(),
    nextQuestions : new Array(),

    init : function()
    {
        job.question.setup();
		
        $(".question ul li a").live("click", job.question.handleSelect);
        $(".question a.next").live("click", job.question.getNextQuestion);
        $(".question a.start").bind("click", job.question.beginQuestions);
        $(".buttons a.prev").live("click", job.question.getPrevQuestion);
        $("textarea").keydown(job.question.keydown);
        $("textarea").keyup(job.question.keyup);
    },

    setup : function()
    {
        job.question.allowMultiple = $("input[name='allowMultiple']").val();
        if ($(".question").attr("id")) {
            job.question.id = $(".question").attr("id").split('_').pop();
            job.question.seen.push(job.question.id);
        } else {
            job.question.id = 0;
        }
    },

    handleSelect : function(e)
    {
        e.preventDefault();
        var index = $(".question li a").index(this);
        var rel = $(this).attr("rel");

        // if we can only choose one let's choose it
        if (job.question.allowMultiple == 0) {
            $(".question a").removeClass("selected");
            $(this).addClass("selected");
            job.question.selectedAnswers = new Array();
            job.question.selectedAnswers.push(index);
            job.question.nextQuestions = new Array();
            job.question.nextQuestions.push(rel);
            job.question.explodeError();
            return;
        }

        // if we can have multiple values that means we can uncheck checked ones
        if ($(this).hasClass("selected")) {
            $(this).removeClass("selected");
            job.question.selectedAnswers.splice($.inArray(index, job.question.selectedAnswers), 1);
            job.question.nextQuestions.splice($.inArray(rel, job.question.nextQuestions), 1);
            return;
        }

        $(this).addClass("selected");
        job.question.selectedAnswers.push(index);
        job.question.nextQuestions.push(rel);
        job.question.explodeError();
    },

    keyup : function(e)
    {
        var length = this.value.length;
        var remaining = 500 - length;
        if (remaining < 0) {
            this.value = 'tricky tricky, but not going to work...';
            setTimeout("job.question.clearTextArea()", 2000);
            return;
        }
        $(".remaining > span").text(remaining);
    },
    
    clearTextArea : function()
    {
        $("textarea").val("");
        $(".remaining > span").text(500);
    },

    keydown : function(e)
    {
        var length = this.value.length;
        return !(length >= 500 && (e.keyCode > 50 || e.keyCode == 32 || e.keyCode == 0 || e.keyCode == 13) && !e.ctrlKey && !e.altKey && !(e.keyCode == 224));
    },

    beginQuestions : function(e)
    {
        e.preventDefault();
	    //var textarea = null;
        //var description = null;
        //var paymentType = null;
        //var price = null;
    
        var textarea = $("textarea");
        var description = textarea.val();
        var paymentType = $("select[name='paymentType']").val();
        var price = $("input[name='price']").val();
        //$.post("/job/next-question?fragment=1", {description : description, paymentType : paymentType, price : price}, function(data) {
        $.post("/job/set-description?fragment=1", {description : description, paymentType : paymentType, price : price}, function(data) {
            if (data.success == true) {
                window.location = data.url;
                return;
            }
			
            $("p.error").text(data.error);
            job.question.showError();
            $(":input").focus(job.question.explodeError);
        }, "json");
    },

    getNextQuestion : function(e)
    {
        e.preventDefault();
        if (job.question.selectedAnswers.length === 0) {
            job.question.showError();
            return;
        }
        $(".questionWrapper").html('<div class="loading"></div>');
        $.post("/job/next-question?fragment=1", {questionId : job.question.id, selectedAnswers : job.question.selectedAnswers.toString(), nextQuestions : job.question.nextQuestions.toString()}, job.question.handleResponse, "html");
    },

    getPrevQuestion : function(e)
    {
        e.preventDefault();
        $(".questionWrapper").html('<div class="loading"></div>');

        if (job.question.id > 0) {
            job.question.seen.pop();
        }
        $.post("/job/prev-question?fragment=1", {questionId : job.question.seen.pop()}, job.question.handleResponse, "html");
    },

    handleResponse : function(html)
    {
       $(".questionWrapper").html(html);
       job.question.setup();
       job.question.selectedAnswers = new Array();
       job.question.nextQuestions = new Array();
    },

    showError : function()
    {
        $("p.error").slideDown(500);
        var top = $("p.error").offset().top;
        $('html,body').animate({scrollTop: top}, 500);
    },

    explodeError : function()
    {
        if ($("p.error").is(":visible")) {
            $("p.error").hide("puff", 300);
        }
    }
}

job.futureMatches = {
    init : function()
    {
        $("input.notify").change(job.futureMatches.change);
    },
    
    change : function()
    {
        $(".futureStatus").html('');
        $(".futureStatus").addClass("loading");
        $(".futureStatus").css("visibility", "visible");
        var checked = $(this).attr("checked") ? 1 : 0;
        var jobId = $(this).attr("id").split("_").pop();
        $.post("/job/future-matches", {notify : checked, jobId : jobId}, function(data) {
            if (data.success === true) {
                $(".futureStatus").removeClass("loading");
                $(".futureStatus").html("saved!");
            }
        }, "json");
    }
}

$(document).ready(function() {
    job.question.init();
    job.futureMatches.init();
});

