/*
Sample HTML structure
---------------------
<div class="tabs-wrapper">
	<div id="tab1" class="unselected" style="display: none">
		Tab 1
	</div>
	<div id="tab2" class="unselected" style="display: none">
		Tab 2
	</div>
	<div>
		<div id="tabContainer1" style="display: none">
			tab 1 content
		</div>
		<div id="tabContainer2" style="display:none;">
			tab 2 content
		</div>
	</div>
</div>

jQuery code sample
------------------
$(".tabs-wrapper").pmTabs();
*/

(function($) {
    $.fn.extend({
        pmTabs: function() {
			return this.each(function() {
				//	Tab container 
				var oTabContainer = $(this);
				
				var tabIsActive = false;
				
				oTabContainer.find("div[id^='tabContainer']").each(function(){
				oTabContentContainer = $(this);
				
					if ($.trim(oTabContentContainer.html()).length > 0) {
						var strTabId = oTabContentContainer.attr("id");
						
						strTabId = strTabId.substring(12, strTabId.length);
						
						oTab = $("#tab" + strTabId);
						
						//	Show tab
						oTab.show();
						
						//	Select first tab with content
						if (!tabIsActive) {
							oTab.removeClass().addClass("selected");
							oTabContentContainer.show();	
							
							tabIsActive = true;
						}
						
						//	Select selected tab
						if (getTabId(document.location).length > 0 && getTabId(document.location) == oTab.attr("id").substring(4, 3)) {
							oTabContainer = oTab.parents("div[id*='wrapper']");
							oTabContentContainer = oTabContainer.find("div[id^='tabContainer" + strTabId + "']");
							
							oTab.siblings().removeClass().addClass("unselected");
							oTabContainer.find("div[id^='tabContainer']").hide();
							
							oTab.removeClass().addClass("selected");
							oTabContentContainer.show();	
							
							tabIsActive = true;
						}
						
						oTab.find("a").click(function(event){
							event.preventDefault();
							window.location = "Default.aspx?sec=" + currentSectionId + "&tab=" + strTabId;
						});
						
						//	Handle article links in each tab
						oTabContentContainer.find(".list-lead a").each(function(){
							oArticleLink = $(this);
							oArticleLink.attr("href", oArticleLink.attr("href") + "&tab=" + strTabId);													
						});
					}															   
				});
			});
			
			function getTabId(str) {
				var myregexp = /tab=(\d{1,4})/;
				var match = myregexp.exec(str);
				
				if (match != null) {
					result = match[1];
				} else {
					result = "";
				}
			
				return result;
			}
		}
    });
})(jQuery);