$(document).ready(function(){
	var hasHash = false; var _hash, _orig_hash, a_el, _link, _height, _name, _td_class,_t_class, _expander, _content, _clean, _h_a;
	if(window.location.hash.length >= 1){
		hasHash = true;
		_orig_hash = window.location.hash
	}
		
	$(".tab-content table").each(function(){
		_t_class = ($(this).index()%2 == 0) ? 'even' : 'odd';
		$(this).find('tr:first > td').each(function(){
			_td_class = ($(this).index() <= 0) ? 'prodId' : 'prodTitle';
			$(this).addClass(_td_class);
		});
		$(this).addClass(_t_class);
	});
	
	$(".tab-content .expander").each(function(){
		_expander = $(this).attr('id');
		_clean = _expander.toLowerCase().replace(/ /g, '-');
		_clean = _clean.replace(/[^a-zA-Z 0-9]+/g,'-');
		$(this).wrapInner('<div id="'+_clean+'" class="hidden"></div>');
		$(this).find('.hidden').before('<ul><li><a href="#'+_clean+'">'+_expander+'</a></li></ul>');
	});
	
	$(".tab-content .expander li > a").live('click', function(){
		// _h_a --> _hidden_anchor
		_h_a = $(this).attr('href').split('#');
		$(this).toggleClass('active');
		$("div.hidden#"+_h_a[1]).slideToggle('fast').toggleClass('active');
		return false;
	});
	
	if(hasHash){
		_hash = _orig_hash.toString().split('#');
		_name = _orig_hash.toString().split('#tab-')
		a_el = "div[name='"+_hash[1]+"'].tab-content, li[name='"+_name[1]+"']";
		if($("div[name='"+_hash[1]+"']").length > 0){
			$(a_el+", "+a_el+" > a").addClass('active');
		}else{
			$('ul.tabset > li:first, .tabset > li:first > a:first, .tab-content:first').addClass('active');
		}
    	
    }else{
    	if(!$('ul.tabset > li:first, .tabset > li:first > a:first, .tab-content:first').hasClass('active')){
    		$('ul.tabset > li:first, .tabset > li:first > a:first, .tab-content:first').addClass('active');
    	}
    }
    $('ul.tabset > li > a, ul.navigation > li > a').click(function(){
    	window.location = $(this).attr('href');
    	_link = $(this).attr('href').split('#');
    	$('ul.tabset > li.active, ul.tabset > li.active > a.active, ul.navigation > li.active, ul.navigation > li.active > a.active').removeClass('active');
    	$('.tab-content.active').removeClass('active');
    	$(this).addClass('active').parent().addClass('active');
    	$("div[name='"+_link[1]+"']").addClass('active');
    	return false;
    });
});
