$(document).ready(function() {
    $('div#voting-dialog').dialog({
        autoOpen: false,
        position: [708, 458],
        width: 277});

    $('div#container').append('<div id="flashbg"></div>');

    $('div#flashbg').flash({
        swf: '../../files/profile.swf',
        height: 800,
        width: 1200,
        wmode: 'transparent',
        flashvars: {
            Syear: 2010,
            Smonth: 01,
            Sdate: 10,
            Shour: 19
        }
    });

    $('div#container').append('<div id="imgbg"></div>');

    $('a.video-link').each(function() {
        var video = $(this).attr('rel');
        var player = '<div class="video-player"><div>';
        $(this).parent().append(player);

        $(this).parent().find('div.video-player').flash({
            swf: '../../files/basic_youtube_embed.2.swf',
            wmode: 'transparent',
            height: 223,
            params: {
                flashvars: {
                  flashVarsVideoID: video
                }
            }
        });
        $(this).remove();
    });

    $('div.post-gallery').mygallery();

    $('a#vote-btn.active').livequery('click', function() {
        $('div#voting-dialog').dialog('open');
        return false;
    });

    $('a#vote-btn.disabled').click(function() {
        alert('Prima etapa a concursului House of Fun s-a terminat. Echipele nu mai pot fi votate. Revino pe site pentru a vedea cele 10 echipe finaliste');
        return false;
    });

    $('#get-code-btn').click(function() {
        var data = 'data[Vote][phone]='+$('#VotePhone').val();
        data += '&data[Vote][team_id]='+$('#VoteTeamId').val();
        var action = $('form#vote-form').attr('action');
        $.post(action, data, function(result) {
            if(result.code == 'voted') {
                alert("De pe acest numar de telefon s-a votat deja. De pe un numar de telefon se poate vota decat o singura data, pentru o singura echipa.");
                $('#voting-dialog').dialog('close');
            } else {
                $(this).click(function() {
                    alert('Numarul tau se trimite. Asteapta codul.');
                    return false;
                })
                $('#VoteCodeHash').val(result.code);
            }
        }, 'json');
        return false;
    });

    $('#vote-form').find('.submit input').click(function() {
        var action = $(this).parents('form').attr('action');
        $.post(action, $(this).parents('form').serialize(), function(result) {
            //$('div#team-points').find('span.large').text(result.points);
            //$('div#rating').find('span.data').text(result.rating);
            //$('div#votes_last_week').find('span.points').text(result.points_last_week);
            $('#VotePhone').val('');
            $('#VoteCode').val('');
            $('#VoteCodeHash').val('');
            if(result.message) alert(result.message);
        }, 'json');
        $('#voting-dialog').dialog('close');
        return false;
    });

    $('div#blog').corner('round bottom');
    $('div#bottom').corner('round bottom');

    $('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth:19, dragMaxHeight:69});
});

