window.register = window.register || {};
register = {
	width : 635,
	currentSection : 0,
	maxSection : 1,
	numSections : 4,
	userType : 'student',

	/**
	 * initializes registration
	 *
	 * @return void
	 */
	init : function(userType, fromJob)
	{
	    register.userType = userType;
		lightbox.open();

		// add id to first lightbox
		$("#lightboxContainer .carousel :first-child").attr("id", "step1");

		// reposition carousel
		if (register.currentSection == 0) {
			$("#lightboxContainer .carousel").css({width : register.width, left: 1000});
		}

		register.getPage(1, fromJob);
		register.moveCarousel(1);
	},

	/**
	 * gets the html for this registration page
	 *
	 * @param int step
	 * @return string
	 */
	getPage : function(step, fromJob)
	{
		$.get("/register/" + register.userType + "/part" + step, {fragment : 1, fromJob : fromJob},
			function(html) {
				$("#step" + step).html(html + '<div class="close"><!--:O--></div>');
				if (register.maxSection < step) {
					register.maxSection = step;
				}
			}
		);
	},

	/**
	 * moves carousel to the proper step
	 *
	 * @param int step
	 * @return void
	 */
    moveCarousel : function(step)
    {
		var position = -register.width / 2 - 2 * register.width * (step - 1);
		$("#lightboxContainer .carousel").animate({left: position});
		register.currentSection = step;
    },

	/**
	 * adds a node for a new registration section
	 *
	 * @param int step
	 * @return void
	 */
	addSection : function(step)
	{
		if ($("#step" + step).is(":visible")) {
			return;
		}
		$("#lightboxContainer .carousel").append('<div class="lightbox" id="step' + step + '">');
		$("#step" + step).fadeIn(350);
	},

	/**
	 * sets the width of the carousel
	 *
	 * @param int step
	 * @return void
	 */
	setCarouselWidth : function(step)
	{
		$("#lightboxContainer .carousel").css({width : 2 * register.width * step});
	},

	submitForm: function(e) {

		var form = $('#register_part_' + register.currentSection);

		$.post(form.attr('action') + '?fragment=1', form.serialize(),
		function(html) {
			if (html.indexOf('id="success"') !== -1) {
				$("p.error").html('');
				register.addSection(register.currentSection + 1);
				register.getPage(register.currentSection + 1);
				if (register.maxSection < register.currentSection + 1) {
					register.setCarouselWidth(register.currentSection + 1);
				} else {
					register.setCarouselWidth(register.maxSection);
				}
				register.moveCarousel(register.currentSection + 1);
			} else {
				$("#step" + register.currentSection).html(html + '<div class="close"><!--:O--></div>');
			}
		},
		"html");
	},

	previous : function()
	{
		register.moveCarousel(register.currentSection - 1);
	}

}

$(".registerBoxes a").click(function() {
    if ($(this).hasClass('employer')) {
        return;
    }

    if (jQuery.browser.msie && jQuery.browser.version == 6) {
        return;
    }
	register.init($(this).attr("class"));
	return false;
});

$(".registerAfterPost").click(function(e) {
    e.preventDefault();
    var times = $("form.times").serialize();
    $.post('/job/set-times', times, function () {
        if (jQuery.browser.msie && jQuery.browser.version == 6) {
            window.location = "/register/employer/part1?fromJob=1";
            return;
        }
        register.init('employer', true); 
    });
});

$("input.next").live("click", function() {
	register.submitForm();
	return false;
});

$("a.previous").live("click", function() {
	register.previous();
	return false;
})
