﻿// JScript 文件
jQuery(function($) {
    var Engine = {
        fixes: {
            png: function(node) {
                if (typeof DD_belatedPNG != 'undefined') {
                    DD_belatedPNG.fix(node);
                }
            }
        }
    };
    Engine.slideshows = {};

    Engine.slideshows.featured = function() {
        var projects = $('#featured .project');
        if (projects.length < 2) {
            return;
        }
        var ul_li = '<li class="slider_selected">&nbsp;</li>';
        for (var i = 1; i < projects.length; i++) {
            ul_li += "<li>&nbsp;</li>";
        }
        $('#featured .wrapper').append('<div id="featured-controls"><div class="controls"><a href="#" onfocus="this.blur();" class="prev">Prev</a> <span>|</span> <a href="#" onfocus="this.blur();" class="next">Next</a></div></div>')
                               .append('<ul id="sliderShow" class="clearfix">' + ul_li + '</ul>');

        $('#featured').mouseenter(function(e) {
            $(this).find('.controls').stop().animate({ 'top': '0' });
        }).mouseleave(function(e) {
            $(this).find('.controls').stop().animate({ 'top': '45px' });
        });

        Engine.fixes.png('#featured-controls .controls, #featured-controls a');


        var current = 0, interval;
        //show first one
        projects.removeClass('active').css({ 'display': 'none', 'opacity': 0 }).eq(current).css({ 'display' : 'block', 'opacity': '1' });

        var cycle = function() {
            var diff = arguments[0] || 1;

            projects.eq(current).stop().animate({ 'opacity': 0 }, 1000, function() { $(this).hide(); });

            if (current + diff < 0) {
                current = projects.length - 1;
            }
            else if (current + diff > projects.length - 1) {
                current = 0;
            }
            else {
                current = current + diff;
            }

            $("#sliderShow > li").removeAttr("class").eq(current).attr("class", "slider_selected");

            projects.eq(current).stop().animate({ 'opacity': 1 }, 1000).show();
        };

        var resetTimeout = function() {
            if (interval) {
                window.clearInterval(interval);
            }
            interval = window.setInterval(function() { cycle(); }, 8000);
        };

        $('#featured .controls a').click(function(e) {
            e.preventDefault();
            resetTimeout();
            var diff = $(this).hasClass('prev') ? -1 : 1;
            cycle(diff);
        });

        resetTimeout();
    };

    Engine.slideshows.solution = function() {
        var solutions = $("#solution ul li");
        var pageSize = parseInt(solutions.length / 4);
        if (solutions.length % 4 > 0) {
            pageSize += 1;
        }
        var pageWidth = pageSize * 684;
        var moveWidth = 684;
        var scrollTime = 680;
        var cur = 1;
        var showBox = $("#solution ul");

        $("#solution ul").css({ "width": pageWidth });
        $("#solution .arrowLeft").click(function() {
            if (!$(showBox).is(':animated')) {//判断展示区是否动画
                if (cur == 1) { //在第一个版面时,再向前滚动到最后一个版面
                    $(showBox).animate({
                        "left": '-=' + moveWidth * (pageSize - 1)
                    }, scrollTime); //改变left值,切换显示版面,1000(ms)为滚动时间,下同
                    cur = pageSize; //初始化版面为最后一个版面
                }
                else {
                    $(showBox).animate({
                        "left": '+=' + moveWidth
                    }, scrollTime); //改变left值,切换显示版面
                    cur--; //版面累减
                }
            }
        });
        $("#solution .arrowRight").click(function() {
            if (!$(showBox).is(':animated')) {//判断展示区是否动画
                if (cur == pageSize) { //在最后一个版面时,再向后滚动到第一个版面
                    $(showBox).animate({
                        "left": 0
                    }, scrollTime); //改变left值,切换显示版面,1000(ms)为滚动时间,下同
                    cur = 1; //初始化版面为最后一个版面
                }
                else {
                    $(showBox).animate({
                        "left": '-=' + moveWidth
                    }, scrollTime); //改变left值,切换显示版面
                    cur++; //版面累减
                }
            }
        });
    };

    Engine.slideshows.featured();
    Engine.slideshows.solution();
});
