$(document).ready(function() {

	// initialize lightboxes
	bindAnsweredEvents();

    // question submission
    answerQuestion();

    // submit quiz
    submitQuiz();

    // round some corners
    $('.round').corner('3px');
    $('.storeindev img').corner('top 3px');
    $('.storeindev div').corner('bottom 3px');

	$('A[rel="external"]').popupWindow(
	{
		height:602,
		width:800,
		top:50,
		left:50,
		centerBrowser:1,
		menubar:0,
		location:0
	});

	/* google analytics tracking */

	$("a[rel*='facebox']").click(function()
	{
		pageTracker._trackPageview('/mellowmatch'+ $(this).attr('href'));
	});

    /*
	$("#facebox a.submit-button").click(function()
	{
		pageTracker._trackPageview('/mellowmatch/questionscomplete');
	});*/

});

var quizSwf;
$(window).load(function() {
    quizSwf = swfobject.getObjectById("takethequiz");
    // see if we need to set any questions as answered
    if (questionsAnswered.length > 0) {
        updateQuizQuestions();
    }
});

// cufon initializers
Cufon.replace('.RockwellBold9', { fontFamily: 'RockwellBold' });
Cufon.replace('.RockwellBold11', { fontFamily: 'RockwellBold' });
Cufon.replace('.RockwellBold12', { fontFamily: 'RockwellBold' });
Cufon.replace('.RockwellBold18', { fontFamily: 'RockwellBold' });
Cufon.replace('.RockwellRegular16', { fontFamily: 'RockwellRegular' });
Cufon.replace('.MontereyBTRegular32', { fontFamily: 'Monterey BT'});
Cufon.replace('.MontereyBTRegular52', { fontFamily: 'Monterey BT'});
Cufon.replace('.RockwellBold12yellow', { fontFamily: 'RockwellBold' });

$(".RockwellBold9").css({"visibility" : "visible"});
$(".RockwellBold11").css({"visibility" : "visible"});
$(".RockwellBold12").css({"visibility" : "visible"});
$(".RockwellBold18").css({"visibility" : "visible"});
$(".RockwellRegular16").css({"visibility" : "visible"});
$(".MontereyBTRegular32").css({"visibility" : "visible"});
$(".MontereyBTRegular52").css({"visibility" : "visible"});
$(".RockwellBold12yellow").css({"visibility" : "visible"});

function onQuizStartClick()
{
	$("a.takethequiz").click();
}

function answerQuestion() {
    $('#facebox .continue-button').live('click', function() {
        // get the question number
        var question = $('#questionNum').val();

        // ensure question was answered
        if ($(".quizbox:not(:has(input:radio:checked))").length > 0) {
            alert('You must answer the question before continuing.');
            return false;
        }

        // get the serialized version of the form
        var data = $(this).parent('form').serialize();

        // post question answer
        $.post('/quiz/' + question, { data : data }, function(data) {
            // display the response HTML

			$('#facebox .content').html(data);


            // remove event
            $('a:not(.answered)[rel*=facebox]').facebox('destroy');
            // make sure to disable the link
            $('.mellowmatch').addClass('answered');


            // rebind events
            // add questions to array
            answeredQuestion(question);
            // update the swf
            updateQuizQuestions();
			updatePopupSwf();
            // close the window after 1.5s if not on final
            if (questionsAnswered.length < 10) {
                setTimeout('$.facebox.close(); bindAnsweredEvents();', 3500);
            }

        }, 'html');

        return false;
    });
}

function submitQuiz() {
    $('#facebox .submit-button').live('click', function() {
        // get the serialized version of the form
        var data = $(this).parent().parent('form').serialize();

        $.post('/quiz/submit', { data : data }, function(data) {
            // display the response HTML
            $('#facebox .content').html(data);
            setTimeout('$.facebox.close(); bindAnsweredEvents();', 1500);
            pageTracker._trackPageview('/mellowmatch/thankyou');
        });
    });
    return false;
}

/**
 * Rebind .
 */
function bindAnsweredEvents() {
    // rebind
    $('a:not(.answered)[rel*=facebox]').facebox();
    $('a.answered').click(function(e) { return false; });
}

/**
 * Add answered questions to array.
 */
function answeredQuestion(question) {
    if (question.indexOf('-') > -1) {
        question = question.split('-');
        var count = question.length;
        for (i = 0; i < count; i++) {
            questionsAnswered.push(question[i]);
        }
    } else {
        questionsAnswered.push(question);
    }
}

/**
 * Update the number of questions answered in the SWF.
 */
function updateQuizQuestions() {
   quizSwf.updateQuizStatus(questionsAnswered);
   quizSwf.hasStartedQuiz(true);
}

function updatePopupSwf() {
    var popupSwf = swfobject.getObjectById("takethequizpopup");
    popupSwf.updateQuizStatus(questionsAnswered);
    if (questionsAnswered.length > 0)
        popupSwf.hasStartedQuiz(true);
}

