Epson.Category =
{
	tabContents: null,
	catnavigation: null,

	setup: function()
	{
		//reinitialize tab cookies
		jQuery('.mastheadcontent form').bind('submit', function()
		{
			jQuery.cookie('categoryTab', null );
		});
		
		// set up tab contents and hide all tabs bar the first
		Epson.Category.tabContents = jQuery( '#mainbody .tabContent' );
		Epson.Category.tabContents.hide();
		var tab = jQuery( '#mainbody ul.categoryNavigation li > strong' ).parents( 'li' )[0].className.match( /ct-([^\s]+)/ );

		var current_tab;

		if (  jQuery.cookie('categoryTab')  ) current_tab = jQuery.cookie('categoryTab');
		else current_tab = tab[1];
		
		jQuery( '#mainbody a[name="' + current_tab + '"]' ).next( '.tabContent' ).show();

		jQuery( 'li.ct-' + current_tab ).find('a').replaceWith( '<strong><span>' + jQuery( 'li.ct-' + current_tab ).text() + '</span></strong>' );
		
		var tab = jQuery( 'li.ct-' + current_tab ).siblings( 'li').find('strong');
		tab.replaceWith( jQuery( '<a href="#"><span>' + tab.text() + '</span></a>' ).bind( 'click', Epson.Category.select ) );		
		
		// setup tabs
		Epson.Category.catnavigation = jQuery( '#mainbody ul.categoryNavigation a[class!="buttonSmall"]' );
		jQuery( Epson.Category.catnavigation ).bind( 'click', Epson.Category.select );		
		
	},

	select: function(e)
	{
		// hide all tabs
		Epson.Category.tabContents.hide();

		// swap tab
		var tab = jQuery( this ).parents( 'li' )[0].className.match( /ct-([^\s]+)/ );
		if ( tab ) 
		{
			
			jQuery( '#mainbody a[name="' + tab[1] + '"]' ).next( '.tabContent' ).show();
	
			// save state in cookie
			jQuery.cookie('categoryTab', tab[1],{ path:'/' });
			
			// deselect previous tab
			var tab = jQuery( '#mainbody ul.categoryNavigation li > strong' );
			tab.replaceWith( jQuery( '<a href="#"><span>' + tab.text() + '</span></a>' ).bind( 'click', Epson.Category.select ) );

			// select the right tab
			var text = jQuery( this ).text();
			jQuery( this ).replaceWith( jQuery( '<strong><span>' + text + '</span></strong>' ) );
			
			var searchForm = jQuery('#mainbody #ProductBrowser');
			
	
			// display show all tab
//			jQuery('#Content ul.categoryNavigation a[class="buttonSmall"]').css( { visibility:'' } );			
			
		}
		appender();
	}
}

function appender(){
	var wrapper = jQuery('.tabContent:visible .SearchWrapper');
		console.log("select?", wrapper);
		var child = jQuery('#ProductBrowser');
		wrapper[0].appendChild(child[0]);
		console.log(wrapper, child);
		//e.preventDefault();
}

function showArticles(){
	//var tab = jQuery( '#mainbody ul.categoryNavigation li > strong' ).parents( 'li' )[0].className.match( /ct-([^\s]+)/ );
	//var current_tab = tab[1];
	jQuery( '#mainbody a[name="products"]' ).next( '.tabContent' ).hide();
	jQuery( '#mainbody a[name="articles"]' ).next( '.tabContent' ).show();
	var tab = jQuery( '#mainbody ul.categoryNavigation li > strong' );
			tab.replaceWith( jQuery( '<a href="#"><span>' + tab.text() + '</span></a>' ).bind( 'click', Epson.Category.select ) );
	
	//var text = jQuery( this ).text();
			jQuery('#mainbody  ul.categoryNavigation li.ct-articles' ).html( jQuery( '<strong><span>All Results</span></strong>' ) );
	//alert(current_tab);
	
	appender();
}
