function infotab_selector (context, el, opts) {        
    var selected_tab = $('li a.selected', context).eq(0);    
    $(selected_tab).removeClass('selected');            
    $( $(selected_tab).attr('href') ).hide();
    selected_tab = $(el);
    $(selected_tab).addClass('selected');       
    $( selected_tab.attr('href') ).show();
    
	if (opts["prev_button"] && opts["next_button"]) {
		if (selected_tab.parent().prev('li').length == 0) {
			$(opts["prev_button"]).css({ visibility : 'hidden' });
		}
		else {
			$(opts["prev_button"]).css({ visibility : 'visible' });
		}

		if (selected_tab.parent().next('li').length == 0) {
			$(opts["next_button"]).css({ visibility : 'hidden' });
		}
		else {
			$(opts["next_button"]).css({ visibility : 'visible' });
		}
	}

}

(function () {

    var my_infotab_options; 
    
    $.fn.infotabs = function (opts) {
		my_infotab_options = opts || {};
        $(this).each(function () { 
            var home = $(this).children('ul');                     
            $('li a', home).bind('click', function (e) {
				infotab_selector(home, this, my_infotab_options);
				e.preventDefault();
            });            
        });
    };
    
    $.fn.select_tab = function (to_select) {
        var home = $(this).children('ul');                     
        $('li a', home).each(function () {
            if ($(this).attr('href') == to_select) {
                infotab_selector(home, this, my_infotab_options);
            }
        });
    };   
    
    $.fn.select_prev_tab = function () {
        var home = $(this).children('ul');  
        infotab_selector(home, $('li a.selected', home).eq(0).parent().prev('li').children('a'), my_infotab_options); 
    };     
    
    $.fn.select_next_tab = function () {
        var home = $(this).children('ul');                     
        infotab_selector(home, $('li a.selected', home).eq(0).parent().next('li').children('a'), my_infotab_options);
    };
    
})(jQuery);

$(document).ready(function () {
    $('.infotabs').infotabs();
});
