
var captchaMiss = 0;

var Captcha = Class.create({
    initialize: function () {}
});

Captcha.showDialog = function () {
    var dialog = $('recaptcha_dialog').show();

    //Center the dialog
    var upper_left = document.viewport.getScrollOffsets();
    var left = parseInt((document.viewport.getWidth() / 2) - (dialog.getWidth() / 2), 10) + upper_left[0] + 'px';
    var top = parseInt((document.viewport.getHeight() / 2) - (dialog.getHeight() / 2), 10) + upper_left[1] + 'px';
    dialog.setStyle({
        left: left
    });
    dialog.setStyle({
        top: top
    });

    Captcha.showShade();
};

Captcha.showShade = function () {
    var overlay = $('lightbox_shade');
    var width = document.body.getWidth();
    var height = document.body.getHeight();

    overlay.setStyle({
        width: width + 'px'
    });
    overlay.setStyle({
        height: height + 'px'
    });
    overlay.show();
};

Captcha.hideShade = function () {
    var overlay = $('lightbox_shade');
    overlay.hide();
};

Captcha.hideDialog = function () {
    $('recaptcha_dialog').hide();
    Captcha.hideShade();
};

Captcha.prepareCaptcha = function () {
	
    Event.observe($('recaptcha_continue'), 'click', Captcha.verifyInput);
    Event.observe($('recaptcha_form'), 'submit', Captcha.verifyInput);
    Event.observe($('recaptcha_cancel'), 'click', Captcha.cancel);
    Event.observe($('recaptcha_try_another'), 'click', Captcha.reloadCaptcha);
    Event.observe($('recaptcha_audio_mode'), 'click', Captcha.audioMode);
    Event.observe($('recaptcha_image_mode'), 'click', Captcha.imageMode);
    
    dcsMultiTrack('DCS.dcsuri', 'reCaptcha_lightbox','WT.ti', 'reCaptcha_lightbox');
    Captcha.switchType('image');

    //make ajax Request to get settings
    var uri = "/checkout/ajax/createReCaptcha.php";
    new Ajax.Request(uri, {
        method: 'get',
        evalJS: false,
        evalJSON: false,
        onSuccess: Captcha.prepareCaptchaSuccess,
        onFailure: Captcha.prepareCaptchaFailure
    });
};

Captcha.prepareCaptchaSuccess = function (t) {
    var data = t.responseText.evalJSON();

    if (data.type != "reCAPTCHA") {
        
    } else {
        Captcha.createRecaptcha(data);
        Captcha.showDialog();
    }
};

Captcha.prepareCaptchaFailure = function (t) {};


Captcha.createRecaptcha = function (data) {
    Recaptcha.create(data.public_key, "", {
        theme: "custom",
        tabindex: 0,
        lang: recaptchaLang,
        callback: Recaptcha.focus_response_field,
        extra_challenge_params: data.params
    });
};

Captcha.verifyInput = function (t) {
    var input = Captcha.onInputVerified;
    Recaptcha.ajax_verify(input);
};

Captcha.cancel = function (t) {
    Recaptcha.destroy();
    Captcha.resetErrors();
    Captcha.hideDialog();
};

Captcha.onInputVerified = function (data) {
    var verify_psig;
    if (data.is_correct) {
        verify_psig = data.verify_psig;
        Captcha.ajaxVerify(verify_psig);
    } else {
    	captchaMiss++;
        Captcha.resetErrors();
        Captcha.showError('match_error');
        Recaptcha.reload();
    }
};

Captcha.resetErrors = function () {
    $('recaptcha_error_message').hide();
    var nodes = $('recaptcha_error_messages').childElements();
    for (var i = 0; i < nodes.length; i++) {
        nodes[i].hide();
    }
};

Captcha.showError = function (error) {
    $('recaptcha_error_message').show();
    var id = 'recaptcha_' + error;
    $(id).show();
};

Captcha.reloadCaptcha = function (error) {
    Captcha.resetErrors();
    Recaptcha.reload();
};

Captcha.checkout = function () {
	
	if(seatmap.leaveSingleSeat) {
		seatmap.displayLeaveSingleSeatError();
		$('lightbox_shade').hide();
		 return false;
	}
	
	
	
    Captcha.prepareCaptcha();
    return false;
};

Captcha.ajaxVerify = function (p) {
    var uri = "/checkout/ajax/verify.php?psig=" + p;
    new Ajax.Request(uri, {
        method: 'get',
        evalJS: false,
        evalJSON: false,
        onSuccess: Captcha.verifySuccess,
        onFailure: Captcha.verifyFailure
    });
};

Captcha.verifySuccess = function (t) {
    var response = t.responseText.evalJSON();
    
    if (response.verified) {
        Recaptcha.destroy();
        
        $('recaptcha_dialog').hide();
        submitEventForm();
    }
};

Captcha.verifyFailure = function (t) {
	$('recaptcha_dialog').hide();
    submitEventForm();
};

Captcha.audioMode = function () {
    Captcha.resetErrors();
    Captcha.switchType('audio');
    Recaptcha.switch_type('audio');
};

Captcha.imageMode = function () {
    Captcha.resetErrors();
    Captcha.switchType('image');
    Recaptcha.switch_type('image');
};

Captcha.switchType = function (type) {
    if (type === 'image') {
        $('recaptcha_image_prompt').show();
        $('recaptcha_audio_prompt').hide();
        $('recaptcha_audio_mode').show();
        $('recaptcha_image_mode').hide();
    }
    else {
        $('recaptcha_image_prompt').hide();
        $('recaptcha_audio_prompt').show();
        $('recaptcha_audio_mode').hide();
        $('recaptcha_image_mode').show();
    }
};
