﻿(function ($) {
    //{'id':'value'}
    $.fn.tabMenu = function (options) {
        var defaults = {};
        var options = $.extend(defaults, options)

        this.each(function () {
            var $obj = $(this), $lis = $('li', $obj);
            var li = defaults.id ? $("li a[href='" + defaults.id + "']").parent('li') : $('li:first', $obj);
            var href = li.addClass('current').find('a').attr('href');
            defaults.id ? $obj.nextAll(defaults.id).show() : $obj.next('div' + href).show();

            $lis.click(function () {
                var $li = $(this), id = $('a', $li).attr('href');
                if (!$li.hasClass("current")) {
                    $('li.current', $obj).removeClass('current');
                    $li.addClass('current');
                    $obj.nextAll().hide();
                    $obj.nextAll(id).show();
                    /*开始逻辑*/
                    var tagType = 1; //默认新车
                    var topTagType = 0; //顶部标签
                    var leftTagType = 0; //左边标签
                    var topTagReg = /^#(newcar|tab)[\-_](\d+)[\-_]?(\d*)$/i;
                    if (topTagReg.test(id)) {//新车                        
                        topTagType = id.match(topTagReg)[2];
                        leftTagType = id.match(topTagReg)[3];
                        if (!leftTagType) leftTagType = 0;
                        var obj = $("#tab_" + topTagType + "_" + leftTagType);
                        qiehuan(obj, tagType, topTagType, leftTagType);
                    } else {//二手车                        
                        topTagReg = /^#(oldcar|otab)[\-_](\d+)[\-_]?(\d*)$/i;
                        tagType = 0;
                        topTagType = id.match(topTagReg)[2];
                        leftTagType = id.match(topTagReg)[3];
                        if (!leftTagType) leftTagType = 0;
                        var obj = $("#otab_" + topTagType + "_" + leftTagType);
                        qiehuan(obj, tagType, topTagType, leftTagType);
                    }
                }
                return false;
            });
        });
    };
})(jQuery);