/**
 * @module Ancai
 * @author jndream(http://www.jndream.net)
 * @date 2011-08-30
 */

(function ($) {
    var AC = {};

    $.extend(AC, {
        util: {

            //导航高亮
            nav: function () {
                var navIndex = $('.J_content').attr('data-nav'),
					$nav = $('#J_siteNav li');
                $nav.eq(navIndex).addClass('selected');
            },

            //侧栏显示隐藏
            sideMenu: function () {
                var $menuH = $('.J_sideMenu h3'),
					$subUL = $('.J_sideMenu ul');

                if ($('#J_crumbs').get(0)) {
                    var curPage = $('#J_crumbs').attr('data-cateid');
                    $subUL.each(function (i) {
                        if ($(this).attr('data-cateid') === curPage) {
                            $menuH.eq(i).addClass('open');
                            $(this).removeClass('hidden');
                        }
                    });
                }
                $menuH.click(function () {
                    var $curUL = $(this).next('ul');
                    if ($curUL.length === 0) return;

                    if ($curUL.hasClass('hidden')) {
                        $menuH.removeClass('open');
                        $(this).addClass('open');

                        $subUL.addClass('hidden');
                        $curUL.removeClass('hidden');
                    } else {
                        $(this).removeClass('open');
                        $curUL.addClass('hidden')
                    }
                });
            },

            //滚动新闻
            newsList: function (obj) {
                $(obj).find('ul').animate({
                    marginTop: '-34px'
                }, 500, function () {
                    //$(this).css({marginTop:"0px"}).find("li:first").appendTo(this);
                });
            },

            //分页
            pageNum: function (obj) {
                if (!$(obj).find('a:first').prev('span').length) {
                    $(obj).find('a:first').addClass('prev');
                }
                if (!$(obj).find('a:last').next('span').length) {
                    $(obj).find('a:last').addClass('next');
                }
            },

            //float box
            talkbox: function () {
                // $('#J_btnHandle').hover(function () {
                /*$(this).parent('#J_talkBox').css('left') === '-260px' ?
                $(this).parent('#J_talkBox').animate({ left: '0px' }, 500) :
                $(this).parent('#J_talkBox').animate({ left: '-260px' }, 500);*/
                /*$(this).parent('#J_talkBox').animate({ left: '0px' }, 500);
                }, function () {

                });
                $('#J_talkBox').hover(function () {
                $(this).animate({ left: '0px' }, 500);
                }, function () {
                $(this).animate({ left: '-260px' }, 500);
                }
                );

                $('#J_btnClose').click(function () {
                $(this).parent('#J_talkBox').animate({ left: '-260px' }, 500);
                });

                $('#J_talkBox li a').click(function (evt) {
                evt.preventDefault();
                window.open($(this).attr('href'), '800', '600');
                });*/

                //$(document).find('#div_company_mini, #kfivtwin').remove();
            },

            //question and answer
            qaToggle: function () {
                $('#J_FAQItem dt').toggle(function () {
                    $(this).css('margin-bottom', '0px');
                    $(this).next('dd').show();
                },
				function () {
				    $(this).css('margin-bottom', '15px');
				    $(this).next('dd').hide();
				});
            }
        },
        page: {}
    });

    $.extend(AC.page, {
        //选项卡
        tab: function (oHandle, oMain, eType) {
            if (eType === 'click') {
                oHandle.click(function () {
                    $(this).addClass('selected').siblings().removeClass('selected');
                    oMain.hide();
                    oMain.eq(oHandle.index(this)).show();

                });
            }
            if (eType === 'hover') {
                oHandle.hover(function () {
                    $(this).addClass('selected').siblings().removeClass('selected');
                    oMain.hide();
                    oMain.eq(oHandle.index(this)).show();
                }, function () { });
            }
        },

        home: function () {
            //滚动新闻
            var _newList = function (obj, i) {
                $(obj).find('ul').animate({
                    marginTop: -34 * i + 'px'
                }, 1000);
            }, i = 0, len = $('#J_newsList li').length, dir = 'r';

            var scroll = setInterval(function () {
                if (i >= 0 && dir == 'r') {
                    i++;
                    if (i === len) {
                        dir = 'l';
                    }
                }
                if (i <= len && dir == 'l') {
                    i--;
                    if (i === 0) {
                        dir = 'r';
                    }
                }
                _newList('#J_newsList', i);
            }, 2000);

            $('#J_newsList li').hover(function () {
                clearInterval(scroll);
            }, function () {
                scroll = setInterval(function () {
                    if (i >= 0 && dir == 'r') {
                        i++;
                        if (i === len) {
                            dir = 'l';
                        }
                    }
                    if (i <= len && dir == 'l') {
                        i--;
                        if (i === 0) {
                            dir = 'r';
                        }
                    }
                    if (i === len) {
                        i = 0;
                    }
                    _newList('#J_newsList', i);
                }, 2000);
            });

            AC.util.nav();
            $(function () { AC.util.talkbox(); });
        },
        init: function () {
            AC.util.nav();
            AC.util.sideMenu();
            $(function () { AC.util.talkbox(); });
            $('.J_content').parent('div').addClass('inner-parent');

            if ($('.J_tabBox').hasClass('version-tab')) {
                AC.page.tab($('.J_tabBox li'), $('.J_tabMain'), 'hover');
            }

            if ($('.J_tabBox').hasClass('tab-intro')) {
                AC.page.tab($('.J_tabBox li'), $('.J_tabMain'), 'click');
            }

            if ($('.main-title').length === 2) {
                $('.main-title').eq(1).css('margin-top', '30px');
            }
        },
        initPage: function () {
            this.init();
            AC.util.pageNum('.J_pageNum');
            /*if($('#J_FAQItem').get(0)){
            AC.util.qaToggle();
            }*/
        }
    });

    window.acSoft = AC.page;
})(jQuery);
