(function($) {
    /**
 *  项目列表导航
 */
    $(document).ready(function() {
        var $pagination = $('#content .pagination a');
        $pagination.click(function() {
            var $this = $(this);
            var $listBox = $('#content .list .box');
            var $list = $listBox.find('ul');
            var id = this.className.match(/page-(\d)+/)[0];
            var $toShow = $list.filter('.'+ id);
            if (!$this.hasClass('current')) {
                $listBox.stop();
                $pagination.removeClass('current');
                $this.addClass('current');
                $list.hide();
                $listBox.css('top','-250px');
                $toShow.show();
                $listBox.animate({
                    'top':'0'
                }, 700, 'easeOutBounce');
            }
            return false;
        });
    });

    $(document).ready(function() {
        var $left = $('#content .subnav .left');
        var $right = $('#content .subnav .right');

        var $container = $('#content .subnav .pagination');
        var width = $container.width();
        var $ul = $container.find('ul');
        var $li = $container.find('li');
        var liWidth = $li.eq(0).width() + parseInt($li.eq(0).css('padding-left'))*2;
        $ul.width(liWidth * $li.length);

        //绑定事件
        $left.bind('click', {
            dt:'left'
        }, paginationMove).click(function() {
            return false;
        });
        $right.bind('click', {
            dt:'right'
        }, paginationMove).click(function() {
            return false;
        });

        //移动导航
        function paginationMove(e) {
            //移除事件
            $left.unbind('click', paginationMove);
            $right.unbind('click', paginationMove);

            var dt = e.data.dt;
            if (dt == 'left') {
                dt = 1;
            } else if (dt == 'right') {
                dt = -1;
            }
            var offset = parseInt($ul.width()) - width;
            if (offset <= 0) {
                return;
            } else {
                var newLeft = (parseInt($ul.css('left')) + (dt * liWidth)) * 1;
                if (newLeft > 0) {
                    newLeft = 0;
                } else if (newLeft < -1*Math.abs(offset)) {
                    newLeft = -1*Math.abs(offset);
                }
                $ul.animate({
                    'left':newLeft
                }, 'fast', function() {
                    //重新绑定事件
                    $left.bind('click', {
                        dt:'left'
                    }, paginationMove);
                    $right.bind('click', {
                        dt:'right'
                    }, paginationMove);
                });
            }
        }
    });


    /**
     * 项目展示
     */
    //加载fancybox所需的CSS
    loadCSS(wpTplUrl+ '/css/fancybox/jquery.fancybox-1.2.6.css');
    $(document).ready(function() {
        var isLoading = false;

        $('#content .subnav .list a').click(function() {
            if (!isLoading) {
                isLoading = true; //锁定ajax调用
                var $this = $(this);
                var $target = $('#content .project-panel');
                $('#content .subnav .list li').removeClass('current');
                $this.parent('li').addClass('current');
                loading('show'); //显示loading...
                $.getJSON($this.attr('href'), '', function(d) {
                    var result = d.result[0];
                    if (d.success == 1) {
                        $target.find('.gallery .box ul').empty().prepend(result.content);
                        var url;
                        if (result.project_url == '') {
                            url = '点击查看大图';
                        } else {
                            url = '演示地址: <a href="http://' +result.project_url+ '" target="_blank">' +result.project_url+ '</a>';
                        }
                        $target.find('.url').empty().append(url);

                        var loadNum = 0;
                        var $imgs = $target.find('.gallery .box ul img');
                        var imgNum = $imgs.length;
                        $imgs.load(function() {
                            //判断图片时候全部加载 全部加载完成后绑定事件
                            loadNum++;
                            if (loadNum >= imgNum) {
                                //图片全部加在成功
                                $('#gallery-left, #gallery-right').remove();
                                $target.find('.gallery .box').unbind().removeAttr('style')
                                .find('ul').css('margin-left', '0px').end()
                                .easySlider({
                                    prevId:'gallery-left',
                                    nextId:'gallery-right'
                                });
                                $imgs.each(function() {
                                    $(this)
                                    .wrap('<a href="' +this.src.replace(/\-[\dx]*?\.(jpg|gif|png)$/, '.$1')+ '"></a>')
                                    .removeAttr('title').removeAttr('alt')
                                    .parent().fancybox({
                                        'hideOnOverlayClick':true,
                                        'hideOnContentClick':false
                                    });
                                });
                            }
                        });
                        $target.find('.excerpt p').text(result.excerpt);
                    } else {
                        alert(d.error);
                    }
                    loading('hide');
                    isLoading = false;
                });
            }
            return false;
        });

        function loading(action) {
            var $loading = $('#loading');
            $loading.css('opacity') == 1 && $loading.css('opacity', 1.0);
            if (action == 'show') {
                $loading.css('opacity', 0).show().fadeTo('slow', 0.7);
            } else if (action == 'hide') {
                $loading.fadeOut('normal');
            }
        }
    });

    /*
     * 默认触发
     */
    $(document).ready(function() {
        $items = $('#content .subnav .list li');
        var $current = $items.filter('.current');
        if ($current.length == 0) {
            $current = $items.eq(0);
        }
        var id = $current.parent('ul').attr('class');
        $('#content .subnav .pagination li a.'+ id).click();
        $current.find('a').click();
    });

})(jQuery);
