/**
 * 首页图片轮换效果
 *
 * @author 李鹏飞
 * 2010-04-21
 */
//配置
var animateTime = 1000;//每次动画播放的时长，单位毫秒
var jumpTime = 3000;//每次跳转完停留的时长，单位毫秒

//程序主体
var zoneList = [], thisIndex = 0, maxIndex = 0, zoneTimer, runStatus = true;
$(document).ready(function(){
    
    $('#hotGamesNav > li').each(function(i) {
        var obj = $(this);
        if (i == 0){
            obj.addClass('selected');
        }
        
        maxIndex++;
        obj.data('zone_index',i);
        zoneList[i] = {lab:obj,displayTop:0};
    });
    
    var h = 0;
    var firstObj = $($('#hotGamesMain > div.hotGamesShow').get(0));
    $('#hotGamesMain').append("<div class=\"hotGamesShow\">"+firstObj.html()+"</div>");
    $('#hotGamesMain > div.hotGamesShow').each(function(i) {
        var obj = $(this);
        if (i == 0){
            firstObj = obj;
        }
        obj.data('zone_index',i);
        if (i == maxIndex){//超出的一个在这里初始化
            zoneList[i] = {lab:0,displayTop:0};
        }
        zoneList[i].displayTop = h;
        h += obj.height();
    });
    
    $('#hotGames').bind("mouseover",function(){
        stopRun();
    });
    
    $('#hotGames').bind("mouseout",function(){
        runStatus = true;
        startRun();
    });
    
    $('#hotGamesNav > li').bind("mouseover",function(){
        var i = parseInt($(this).data("zone_index"));
        jumpZone(i,0);
    });
    
    startRun();
});

function startRun(){
    window.clearTimeout(zoneTimer);
    zoneTimer = window.setTimeout('jumpNext()',jumpTime);
}

function jumpNext(){
    var i = thisIndex + 1;
    if (i >= maxIndex){
        i = 0;
    }
    jumpZone(i,animateTime);
}
//跳到指定区
function jumpZone(index, sec){
    if (index == thisIndex){
        return;
    }
    zoneList[thisIndex].lab.removeClass('selected');
    thisIndex = index;
    displayIndex = (index == 0) ? maxIndex : index;
    var displayTop = zoneList[displayIndex].displayTop;
    if (displayTop > 0){
        displayTop = "-"+displayTop;
    }
    if (index == 1){
        $('#hotGamesMain > div.hotGamesShow').animate({top:"0px"},0,"swing");
    }
    zoneList[index].lab.addClass("selected");
    $('#hotGamesMain > div.hotGamesShow').animate({top:displayTop+"px"},sec,"swing",function(){
        var i = parseInt($(this).data("zone_index"));
        if (i == maxIndex && runStatus && sec > 0){
            startRun();
        }
    });
}

function stopRun(){
    runStatus = false;
    window.clearTimeout(zoneTimer);
}