// newtabs.js
// generic tab script
// Written by Jeff Parker - jeffreyp(at)codero.com
//
// Props to the jQuery community for guidance and support
//

$(document).ready(function() {
	/*
	 * 20110829 by Marcelino new applet for supporting nested tabs
	 * A new set of css styles named tab-nest/tab-nested are needed
	 * The main idea is, inside of a tab-main div just place a new ul/li ahref structure,
	 * but set the css like this  <ul class=tab-nest... instead of <ul class=tab-tabs...
	 * Also the div with the real content must but set
	 * like <div class=tab-nested... instead of <div class=tab-main
	 * You are done, now every tab-main can hold a new set of tab-nested.
	 */
	$(".tab-tabnest li a").click(function() {
		var tab = this.href.split('#');
		tab = tab[tab.length-1];
		$(".tab-tabnest .tab-active").removeClass("tab-active");
		$(".tab-nested").hide();
		$(this).parent().addClass("tab-active");
		$('.'+tab).show();
	});
	function nestedShow(clicked) {
		var tabn = $('.'+clicked + ' .tab-tabnest li a');
		var clickedtabn = 0;
		for(var i =0;i < tabn.length;i++){
			if( $(tabn[i]).parent().hasClass('tab-active') ){
				clickedtabn= i;
				break;
			}
		}
		if(clickedtabn > 0) {
			$(tabn[clickedtabn]).click();
		} else {
			$(tabn[0]).click();
		}
	}

	/*
	 * Here start the code for controling tab-tabs/tab-main
	 * These was originally written by Jeff Parker (spider man little brother???? maybe!!!)
	 * It was slightly tweaked for also supporting nested tabs
	 */
  var tabs = $('.tab-tabs li a');
  var clickedtab = 0;

	/* 20110829 by Marcelino due to being not is use for long long time.
   function switchTab(clicked) {
     var tab = $(".tab-active");
     $(".tab-active").removeClass("tab-active");
     $(".tab-main").hide();
     $("li.tab0" + clicked).addClass("tab-active");
     $(".tab-main.tab0" + clicked).show();
     return false;
   }
	 */

	$(".tab-tabs li a").click(function() {
		var tab = this.href.split('#');
    tab = tab[tab.length-1];
			 
    $(".tab-tabs .tab-active").removeClass("tab-active");
    $(".tab-main").hide();
		/* 20110829 by Marcelino to support nested tabs */
    $(".tab-nested").hide();
		nestedShow(tab);
		/* --- */
		
    $(this).parent().addClass("tab-active");
    $('.'+tab).show();
		/* 20110829 by Marcelino - just a better comment, this code is unactive for a long time */
    /*temp = (tabs.index(this)+1);
             if(temp != clickedtab)
                 {
                     clickedtab = parseInt(temp);
                     switchTab(clickedtab);
                 }*/
    //return false;
    });
		
    if(document.location.href.indexOf("#")!==-1){
      var url = document.location.href.split("#");
      url = url[url.length -1];
      for(var i =0;i < tabs.length;i++){
        var tab = tabs[i].href.split('#');
        tab = tab[tab.length-1];
        if(tab === url){
          clickedtab= i;
          break;
	      }
      }
    }
    if(clickedtab > 0) $(tabs[clickedtab]).click();

    return false;
});

