var Checkmate = { next_url: null };
Checkmate.entry = function(thickbox_trigger_id, next_url) {
    return true;
//     if ( $.cookie('entried') == 'yes' || (typeof(checkmate_is_not_accepting)!="undefined" && checkmate_is_not_accepting) ) {
//         return true;
//     }
//     else {
//         $('#' + thickbox_trigger_id).click();
//         Checkmate.next_url = next_url;
//         return false;
//     }
}

Checkmate.challenge = function() {
    var data = {
        name:    $('#userName').val(),
        email:   $('#userEmail').val(),
        receive: $('#receiveEmail1')[0].checked ? 1 : 0
    };
    $.post( '/cgi-bin/entry.cgi', data, function(json) {
        if ( json.result == 'ok' ) {
	    $.cookie('entried','yes', { expires : 30 * 12 }); //days
            location.href = Checkmate.next_url;
        }
        else {
            // show validation error
            $('#userEmail').parent().children('.error').show();
        }
    }, 'json' );
}

Checkmate.close = function() {
    tb_remove();
}

Checkmate.Renderer = new function() {
    // swf: http://swf.wonderfl.net/swf/usercode/2/2f/2fe5/2fe5bff6db33223955b6f482caeca0100475c265.swf
    // box: box_2fe5bff6db33223955b6f482caeca0100475c265
    return {
        make_swf_path : function( uid ) {
            var matches = uid.match(/((((.).)..).*)/);
            matches.shift();
            return "http://swf.wonderfl.net/swf/usercode/" + matches.reverse().join('/') + ".swf";
        },
        render : function( uid, box_id ) {
            var swf = this.make_swf_path( uid );

            var flashvars  = {};
            var params     = {};
            params.quality = "high";
            params.allowfullscreen = "true";
            var attributes = {};
            attributes.align = "middle";
            swfobject.embedSWF( swf, box_id, "465", "465", "10.0.0", "/swf/expressInstall.swf", flashvars, params, attributes );
        }
    }
}

Checkmate.Prizepage = new function() {
    return {
        load : function() {
            $('.play_button').click( function() {
                var fork_link = $(this).parents('.parentsCode').find(".column a[title='fork']").attr('href');
                var code_uid  = fork_link.match(/[0-9a-f]{40}/)[0];
                var box_id    = "box_" + code_uid;
                var box       = $(this).parents('.swf').attr('id', box_id);

                Checkmate.Renderer.render( code_uid, box_id );
                return false;
            });
        }
    };
}

$( function() {
    Checkmate.Prizepage.load();
});

