// The below CSS selectors trigger the JavaScript on load, if they're present.  Selectors
//  with colons (like :click or :submit) trigger on the matching DOM event attached to the
// element(s) chosen in the selectors
Event.addBehavior({
  '#tabbedWidget .tablist a' : function() {
    processTab(this);
  },
	
  '#tabbedWidget' : function(){
	setCornersAbsolute();
  	//setLastTab();
	showAndSelect("food-dining");
  }
})

// returns the first matching element, or undefined if no matches
function $$$ () {
	var el;
	var els = $$($A(arguments));
	
	if (els.size() > 0) {
		el = els.first();
	};
	
  return el;
}

function setLastTab(){
	var lastTab = $$('.notlast').last()
	lastTab.removeClassName('notlast');
	lastTab.addClassName('last');
//	var width = 0;
//	$$('.notlast').each(function(e){width = width + e.getWidth()});
//	var newWidth = $$('ul').first().getWidth() - width - (2 * $$('.last a').first().getStyle('padding-right').replace('px','')) - 1 
//	var newString = newWidth + "px";
//	$$('.last a').first().style.width = newString;
}

function processTab (link) {
  // keep only the part after the #
  var target = link.href.gsub('.*#', '');
  // add showAndSelect onclick
  link.observe('click', function() {
    showAndSelect(target);
    // then don't follow the link's href
    return false;
  });
  // hide the title within the module (it's redundant with the tabs)
  $$('#' + target + '.module h2').invoke('hide')
}
		
function showAndSelect (mod) {
  // show the given box
  showTabBox(mod);
  // make the given tab selected
  selectTab(mod);
  setCornersAbsolute();
}

function showTabBox (mod) {
  // hide each tab box
  $$('.tab-box').invoke('hide');
  // grab the tab box with the given ID, or fail to the first tab-box
  box = $$$('.tab-box#' + mod) || $$$('.tab-box')
  box.show();
  $$('#'+mod+' .most-searched').invoke('show');
}

function selectTab (mod) {
  // unselect all tabs
  $$('.tablist a').invoke('removeClassName', 'selected');
  $$('.tablist li').invoke('removeClassName', 'selected');
  // grab the tab with the given class, or fail to the first tab
  tab = $$$('.tablist a.' + mod) || $$$('.tablist a')
  tabli = $$$('.tablist li.'+mod+'li')
  tab.addClassName('selected'); 
  tabli.addClassName('selected'); 
}

function styleSwitch(id){
	//alert("id is = "+id);
	var links = $('subCatNav'+id);
	var moreNav = $('catNavMore'+id);
	
	if(links.hasClassName('hidden')){
		links.removeClassName('hidden');
		moreNav.removeClassName('rightNavIcon');
		moreNav.addClassName('downNavIcon');

	} else {
		links.addClassName('hidden');
		moreNav.addClassName('rightNavIcon');
		moreNav.removeClassName('downNavIcon');
	}
	return false;
}


function setCornersAbsolute(){
$$$('.corner-a').setStyle({'position':'absolute'})
$$$('.corner-b').setStyle({'position':'absolute'})
}
