(function($) { $.fn.smartTab = function(options) { var options = $.extend({}, $.fn.smartTab.defaults, options); return this.each(function() { obj = $(this); var curTabIdx = options.selected; var tabs = $("ul > li > a", obj); var autoProgressId = null; options.transitionEffect = (typeof (options.transitionEffect) == 'string' && options.transitionEffect != '') ? options.transitionEffect : 'none'; $(obj).addClass(options.tabContainerClass); hideAllSteps(); showTab(); $(tabs).bind("click", function(e) { if (tabs.index(this) == curTabIdx) return false; var prevTabIdx = curTabIdx; curTabIdx = tabs.index(this); hideTab(prevTabIdx); if (options.autoProgress) { restartAutoProgress() } return false }); if (options.keyNavigation) { $(document).keyup(function(e) { if (e.which == 39) { doForwardProgress(); if (options.autoProgress) { restartAutoProgress() } } else if (e.which == 37) { doBackwardProgress(); if (options.autoProgress) { restartAutoProgress() } } }) } if (options.autoProgress) { startAutoProgress() } if (options.autoProgress && options.stopOnFocus) { $(obj).bind("mouseenter mousemove mouseover", function(e) { stopAutoProgress(); return true }); $(obj).bind("mouseleave", function(e) { startAutoProgress(); return true }) } function hideAllSteps() { $(tabs, obj).each(function() { $($(this, obj).attr("href"), obj).hide() }) } function showTab() { var selTab = tabs.eq(curTabIdx); $(tabs, obj).removeClass("sel"); $($(selTab, obj), obj).addClass("sel"); if (options.transitionEffect == 'slide') { $($(selTab, obj).attr("href"), obj).slideDown("slow") } else if (options.transitionEffect == 'fade') { $($(selTab, obj).attr("href"), obj).fadeIn("slow") } else { $($(selTab, obj).attr("href"), obj).show() } return true } function hideTab(idx) { var selTab = tabs.eq(idx); if (options.transitionEffect == 'slide') { $($(selTab, obj).attr("href"), obj).slideUp("slow", showTab) } else if (options.transitionEffect == 'fade') { $($(selTab, obj).attr("href"), obj).fadeOut("slow", showTab) } else { $($(selTab, obj).attr("href"), obj).hide(); showTab() } return true } function startAutoProgress() { if (!autoProgressId) { autoProgressId = setInterval(doForwardProgress, options.progressInterval) } } function restartAutoProgress() { stopAutoProgress(); startAutoProgress() } function stopAutoProgress() { if (autoProgressId) { clearInterval(autoProgressId); autoProgressId = null } } function doForwardProgress() { var nextTabIdx = curTabIdx + 1; var prevTabIdx = curTabIdx; if (tabs.length <= nextTabIdx) { nextTabIdx = 0 } curTabIdx = nextTabIdx; hideTab(prevTabIdx) } function doBackwardProgress() { var nextTabIdx = curTabIdx - 1; var prevTabIdx = curTabIdx; if (0 > nextTabIdx) { nextTabIdx = tabs.length - 1 } curTabIdx = nextTabIdx; hideTab(prevTabIdx) } }) }; $.fn.smartTab.defaults = { selected: 0, keyNavigation: true, autoProgress: false, progressInterval: 3500, stopOnFocus: false, transitionEffect: 'none', tabContainerClass: 'stContainer'} })(jQuery);
