/*
	Generic tabbing plugin
*/

var tabs_set = false;
var tab_div_id = '';
var $tabs = false;

/**
 * On page load.
 */
$(document).ready(function(){
  tabs_init();
	bindTabs(); // SM: Use my hacky tab_div_id since I dont' yet understand how to get that kind of data otherwise
	//$(".ui-tabs-nav-container").show(); // hides rendering until it's done?
	$('.ui-tabs').show();
});

/**
 * SM 11Nov10: Abstracted for use in SE2.
 */
function tabs_init() {
  // SM 12Nov09: Allow for cookies to be used to remember position.
  $('.ui-tabs').each(function() {
    if($(this).hasClass('ui-tabs-cookie')) {
      $tabs = $(this).tabs({ cookie: { expires: 30 } });
    } else {
      $tabs = $(this).tabs();
    }
  });
  //$('.selector').tabs({ cookie: { expires: 30 } });
  
  //$(".ui-tabs-nav-container > ul").tabs();
	//$(".ui-tabs-nav").tabs(); // SM: Note as sets up tabs on its own too :(
}

/**
 * SM 11Nov10: Atm the code just sets tab_div_id. A kludge basically.
 */
function bindTabs() {
	// Help the UI by setting the id of the tabbed div being shown:
	$('.ui-tabs-nav').bind('tabsshow', function(event, ui) {
		tab_div_id = ui.panel.id;
	});									
	
	// SM 29Jul10: When a user clicks on a tab
	$('.ui-tabs-nav').bind('tabsselect', function(event, ui) {
		var test = 'testing yes';
	});
}

/**
 * Work out which reload url to return.
 */
function getStaticTabReloadUrl() {
	
	var oldURL = location.href;

	// If tab_div_id is not set, return current url
	if(0 == tab_div_id.length) {
		return oldURL;
	}
	
	var anchorStartPos = oldURL.indexOf('#');
	if(-1 != anchorStartPos) {
		// An #anchor tag already exists, just replace that
		var oldURLWithoutAnchor = oldURL.substring(0, anchorStartPos);
		return oldURLWithoutAnchor + '#' + tab_div_id;
	}
	
	// Otherwise append the anchor for the refresh
	newUrl = oldURL + '#' + tab_div_id;
	//alert('newUrl:'+newUrl);
	return newUrl;	
}
