//quickFlip Javascript

$(function(){

    var num = 3;
    var flag = false;
    var ajax_count = 1;
    var targetNum = [];

    var $windowElement = $(window);
    var $flipElement = $("#content > div.pola_wrap");

    $flipElement.quickFlip();

    $("#content").hover(

        function() {

            if(flag) timeFlipper();
            clearInterval(timerID);

        },

        function() {

            timerID = setInterval(timeFlipper, 3000);

        })

    $flipElement.live("mouseenter",

        function() {
            var $target = $(this);
            if ( !$target.hasClass("pola_wrap") ) $target = $target.parents("div.pola_wrap");
            if ( $target.find("div:eq(2) > div:eq(0)").queue() ) {
                if ( $target.find("div:eq(2) > div:eq(0)").queue().length > 1 ) {
                    $target.find("div:eq(2) > div").clearQueue();
                    return;
                }
            }
            $target.quickFlipper({}, 1);
        });

    $flipElement.live("mouseleave",

        function() {
            var $target = $(this);
            if ( !$target.hasClass("pola_wrap") ) $target = $target.parents("div.pola_wrap");
            if ( $target.find("div:eq(3) > div:eq(0)").queue() ) {
                if ( $target.find("div:eq(3) > div:eq(0)").queue().length > 1 ) {
                    $target.find("div:eq(3) > div").clearQueue();
                    return;
                }
            }
            $target.quickFlipper({}, 0);
        });

    function timeFlipper() {

        if(flag) {
            for(var i=0; i<num; i++){
                $flipElement.eq(targetNum[i]).quickFlipper({}, 0)
            }
            flag = false;

        }else{

            randomNumMake();

            for(var i=0; i<num; i++){
                $flipElement.eq(targetNum[i]).quickFlipper({}, 1)
            }
            flag = true;

        }

    }

    function randomNumMake() {

        targetNum = [];
        var flipperArray = [];

        for(var i=0, l=$flipElement.size(); i<l; i++) {
            flipperArray.push(i);
        }

        for(var i=0; i<num; i++) {
            targetNum[i] = flipperArray.splice(Math.random() * flipperArray.length, 1);
        }

    }

    function ajaxEvent() {

        var scrollBottom = $windowElement.scrollTop() + $windowElement.height();

        if( scrollBottom / $("body").height() > 0.95 ) {

            $windowElement.unbind("scroll");

            $.ajax({
                type:"get",
                url:"index-" + ajax_count + ".html",

                dataFilter: function(data) {
                    return $(data).find("#content > div");
                },

                success: function(data) {
                    if( $(data).html() ) {
                        data.appendTo("#content");
                        $windowElement.scroll(function () { ajaxEvent(); })
                    }
                    ajax_count++;
                }
            });

        }

    }

    $windowElement.scroll(function () {

        ajaxEvent();
    
    });

    var timerID = setInterval(timeFlipper, 3000);

});
