var ie6 = $.browser.msie && parseInt($.browser.version) == 6 && typeof window['XMLHttpRequest'] != "object";

$(document).ready(function() {
	
    // show and highlight the default tab
    if (tabs[defaultTab]['type'] != "popup" && tabs[defaultTab]['type'] != "top" ) {
		showTab(defaultTab);
	}
	$("div.tabs_nav li").eq(defaultTab).addClass("activeItem");
	
	// tab click handler
	$('div.tabs a').click(function () {     
	  
	    // prevent tab selection if another tab is loading
		if (isLoading) return false;
		
		// highlight the correct tab
		$("div.tabs_nav li").removeClass("activeItem");
        $(this).parent().addClass("activeItem");
		
		var tabId = 0;
		var re = new RegExp("#tab([0-9])", "");
		var match = re.exec(this);		
		if (match != null) {
			tabId = RegExp.$1;			
			
			// if the tab requested is the current tab, don't do anything
			if (tabId == currentTabId) return false;
					
			// all ok show the tab
			showTab(tabId);			
		}		 		
   }); 
});

function showTab(id){

	if (tabs[id]['type'] == "div") {			
			
		// get the tab content via ajax
		showLoading();
		$.ajax({
			type: "GET",
			url: tabs[id]['url'],
			dataType: "html",
			cache: false,
			success: function(html) {
				$("#smpResultsDiv").html(html);
				hideLoading();											
			},
            error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("#smpResultsDiv").html(textStatus);
				hideLoading();		
            }
		});
	} else if (tabs[id]['type'] == "iframe") {		
			
		// get the content via iframe
		showLoading();
		$("#smpResultsDiv").html("<iframe frameborder=\"0\" src=\"" + tabs[id]['url'] + "\" width=\"" +tabs[id]['width']+ "\" height=\"" +tabs[id]['height']+ "\" onload=\"hideLoading();\"></iframe>");      
	} else if (tabs[id]['type'] == "popup") { 		
	
                // clear the results window
                $("#smpResultsDiv").html("");

		// popup the tab in a new window		
		window.open(tabs[id]['url'], '_blank', 'status=1, toolbar=1, location=1, menubar=1, directories=1, resizable=1, scrollbars=1');
    } else if (tabs[id]['type'] == "top") { 		
	
                // clear the results window
                $("#smpResultsDiv").html("");

		// display the tab page in the same window
		window.open(tabs[id]['url'], '_top', 'status=1, toolbar=1, location=1, menubar=1, directories=1, resizable=1, scrollbars=1');
    }
	currentTabId = id;	
} 

function showLoading() {
	isLoading = true;	
	$("#smpResultsDiv").hide();
	$('#smpResultsDiv').html("");
	
	if (ie6) {
		$("#loading").show(); 
	} else {
		$("#loading").fadeIn("slow"); 
	}
}

function hideLoading() {
	isLoading = false;
	if (ie6) {
		$("#loading").hide(); 
		$("#smpResultsDiv").show();
	} else {
		$('#loading').fadeOut('slow', function() {
			$("#smpResultsDiv").show();
		}); 	
	}
}

