/*
 * @author Carsten Wentz <info@c6w.de>
 * Tab Navigation Modul für die Jquery Libery
 */


jQuery.fn.tab = function(settings) {
	
  return this.each(function(){
   		
		settings.obj = jQuery(this);
		
		function getTab(){
			var spl = this.id.split('_');
			settings.activate = spl[0];
			styleTabs();
		}
		
		
	
		function styleTabs(){
			var i=0;
			var az = $('#tabNavigation .active').get(0).style.zIndex;
			$("#"+settings.obj.get(0).id +" a").each(function(e){
				 $(this).attr({id:e+'_'+settings.obj.get(0).id});
				if( e==settings.activate ){					
					$(this).removeAttr("class");
							$(this).get(0).style.zIndex = az;
							$(this).addClass("active");	
							var html = $(this).html();
							
							var newHtml = html.replace('leftCornerInactive','leftCorner');
								  newHtml = newHtml.replace('rightCornerInactive','rightCorner');
							$(this).html(newHtml);
				
	
				}else{
					$(this).removeAttr("class");
						var html = $(this).html();
				    $(this).get(0).style.zIndex = 2-i;
				    if( html.match('leftCorner') && !html.match('leftCornerInactive') ){
				    		var newHtml = html.replace('leftCorner','leftCornerInactive');
								  newHtml = newHtml.replace('rightCorner','rightCornerInactive');
							$(this).html(newHtml);
				    }
					
				}
				settings.tabanz = e;
				i++;
			});
		 return true;
		}
		
		function setContent(){
			var content = $("#"+settings.contentTo);
			var clickedTab = $(this).get(0).id;
			if( clickedTab == undefined ){
				clickedTab = settings.activate+'_'+settings.obj.get(0).id;
			}
			$("#"+settings.contentTo+' '+settings.contents).each(function(e){
				if( e+'_'+settings.obj.get(0).id== clickedTab ){
					
					$(this).show();
				}else{
					$(this).hide();
				}
				 
			});
		}
		
		
		styleTabs();
		setContent();
		
		$("#"+jQuery(this).get(0).id +" a").each(function(e){
		
			$(this).bind("click", getTab);
			$(this).bind("click", setContent);
		});
	
		

  });
  
};

