var ranking_tab_clicked = false;

$(function() {
    $('#lite-accordion').liteAccordion({
        autoPlay : false,
        containerWidth : 320,
        containerHeight : 120,
        headerWidth: 24,
        cycleSpeed : 6000,
        enumerateSlides : true,
        onTriggerSlide: box_review_rank_nexttab
    });
    $('#box-review-rank-tabs').tabs({ 
        fx: { opacity: 'toggle' },
        selected: Math.floor(Math.random()*$( "#box-review-rank-tabs > ul" ).first().find("li").size())
    });
    $('.review-rank-nav a').click(function() {
        ranking_tab_clicked = true;
    });
//    $('img.lazy').lazyload({
//        event: "load",
//        effect : "fadeIn",
//        skip_invisible : false
//    });
    webboard_filter_rebind();
    
    if ($('#btnVote').size() > 0) {
        if ($('#vote-dialog-confirm').size() > 0) {
            $('#btnVote').click(function() {
                if ($('input[name="vote_option"]:checked').size() > 0) {
                    $('#vote-dialog-confirm').dialog({
                        title: 'What do you think?',
                        modal: true,
                        width: 'auto',
                        resizable: false,
                        buttons: [
                            {
                                text: "Yes",
                                click: function() { 
                                    $(this).dialog("destroy"); 
                                    $.get(
                                        'ajax/index/box-vote.php',
                                        {
                                            'poll' : $('#poll_id').val(),
                                            'option' : $('input[name="vote_option"]:checked').val(),
                                            'confirm' : 1
                                        },
                                        function (html) {
                                            $('#vote-dialog').dialog({
                                                title: 'What do you think?',
                                                modal: true,
                                                width: 'auto',
                                                resizable: false,
                                                buttons: [{
                                                    text: "Ok",
                                                    click: 
                                                        function() { 
                                                            $(this).dialog("destroy"); 
                                                            $('.box-vote').replaceWith(html);
                                                        }
                                                }]
                                            });
                                        });
                                }
                            },
                            {
                                text: "No",
                                click: function() { 
                                    $(this).dialog("destroy"); 
                                    $.get(
                                        'ajax/index/box-vote.php',
                                        {
                                            'poll' : $('#poll_id').val(),
                                            'option' : $('input[name="vote_option"]:checked').val(),
                                            'confirm' : 0
                                        },
                                        function (html) {
                                            /*
                                            $('#vote-dialog').dialog({
                                                title: 'What do you think?',
                                                modal: true,
                                                width: 'auto',
                                                resizable: false,
                                                buttons: [{
                                                    text: "Ok",
                                                    click: 
                                                        function() { 
                                                            $(this).dialog("destroy"); 
                                                            $('.box-vote').replaceWith(html);
                                                        }
                                                }]
                                            });
                                            */
                                           $('.box-vote').replaceWith(html);
                                        });
                                }
                            }]
                    });
                }
            });
        } else {
            $('#btnVote').click(function() {
                if ($('input[name="vote_option"]:checked').size() > 0) {
                    $.get(
                        'ajax/index/box-vote.php',
                        {
                            'poll' : $('#poll_id').val(),
                            'option' : $('input[name="vote_option"]:checked').val()
                        },
                        function (html) {
                            $('#vote-dialog').dialog({
                                title: 'What do you think?',
                                modal: true,
                                width: 'auto',
                                resizable: false,
                                buttons: [{
                                    text: "Ok",
                                    click: 
                                        function() { 
                                            $(this).dialog("destroy"); 
                                            $('.box-vote').replaceWith(html);
                                        }
                                }]
                            });
                        });
                }
            });
        }
    }
    
    $('#lite-accordion').liteAccordion("play");
});

function box_review_rank_nexttab() {
    if (ranking_tab_clicked == false) {
        var tabcount = $( "#box-review-rank-tabs > ul" ).first().find("li").size();
        var selected = $( "#box-review-rank-tabs" ).tabs( "option", "selected" );

        selected = parseInt(selected, 10) + 1;
        if (selected >= parseInt(tabcount, 10)) {
            selected = 0;
        }

        $( "#box-review-rank-tabs" ).tabs( "option", "selected", selected );
    }
}

function webboard_filter_rebind() {
    $('.webboard-nav li').click(function() {
        var forum_id = $(this).attr('forum_id');
        webboard_filter(forum_id);
    });
}
function webboard_filter(forum_id) {
    $.get(
        'ajax/index/box-webboard.php', 
        { 'f' : forum_id }, 
        function(html) {
            $('#box-webboard-wrapper').html(html);
            webboard_filter_rebind();
        });
}
