// Creating captcha modal class
function FrostedFlakesCaptcha()
{
	// Event listeners
	$('#modal-close').click(function(event)
	{
		event.preventDefault();
		
		closeModal();
		trackEvent('CLOSE.CAPTCHA');
	});
	
	$('#view-results').live('click', function(event)
	{
		event.preventDefault();
		
		viewResults();
	});
	
	$('#build-a-mural').live('click', function(event)
	{
		event.preventDefault();
		
		buildMural();
	});
	
	// Modal methods
	function openModal(userId)
	{
		if(userId)
		{
			$("#UserId").val(userId);
		}
		
		var overlayValues = {
			height: $(window).height(),
			width: $(window).width()
		};
		
		$('#overlay').css('height', overlayValues.height).css('width', overlayValues.width);
		$('#overlay').show();
		$('#voting-modal').show();
		
		showRecaptcha();
	}
	
	function closeModal()
	{
		$('#overlay').hide();
		$('#voting-modal').hide();
		$("#captchaInvalidMessage").hide();
		$("#votingModaler").html("");
	}
	
	// Captcha methods
	function showRecaptcha()
	{
		var publicKey = $("#recaptchaPublicKey").val();
		
    	Recaptcha.create(publicKey, 'recaptchaDiv', {
            theme: "white",
            tabindex: 0,
            callback: Recaptcha.focus_response_field
        });
    }
	
    function reCaptchaInvalid()
    {
        $("#captchaInvalidMessage").show();
        
        showRecaptcha();
    }
	
    function successCaptcha()
    {
    	$("#captchaInvalidMessage").hide();
    	$("#voteContainer").hide();
    	
        var flash = document.getElementById('flashWrapper');
        
        try
        {
            flash.voteRecorded();
        }
        catch(e) {}
		
        Recaptcha.destroy();
        trackEvent('ENTER.CAPTCHA');
    }
    
    // Privileged methods
    this.callOpenModal = function(userId)
    {
        if (userId)
        {
            openModal(userId);
        }
        else
        {
            openModal();
        }
    }
	
	this.callCloseModal = function()
	{
		closeModal();
	}
	
	this.callReCaptchaInvalid = function()
	{
		reCaptchaInvalid();
	}
	
	this.callSuccessCaptcha = function()
	{
		successCaptcha();
	}
}

// Flash methods
function openhtmlvotingform(userId) {
	FrostedFlakesCaptcha.callOpenModal(userId);
}

function htmlformclose() {
	FrostedFlakesCaptcha.callCloseModal();
}

function openCaptchaFailure() {
    FrostedFlakesCaptcha.callReCaptchaInvalid();
}

function openCaptchaSuccess() {
    FrostedFlakesCaptcha.callSuccessCaptcha();
}

function buildMural() {
	FrostedFlakesCaptcha.callCloseModal();
	
	var flash = document.getElementById('flashWrapper');
	flash.buildMural();

	$("#votingModaler").html("");
	$("#UserId").val("")
}

function viewResults() {
	FrostedFlakesCaptcha.callCloseModal();
	
	var flash = document.getElementById('flashWrapper');
	flash.viewResults();

	$("#votingModaler").html("");
	$("#UserId").val("");
	
	trackEvent('SEE.RESULTS');
}

// Tracking method
function trackEvent(evt) {
	pageTracker._trackPageview(evt);
}

// Init captcha class
FrostedFlakesCaptcha = new FrostedFlakesCaptcha;