/** 
 * @projectDescription This jQuery Plugin is ...
 * 
 * supported browser: xxx, xxx
 * requires: jQuery >= 1.3.2 (tested with jQuery 1.3.2, xxx)
 * 	 optionally requires: if you want to enable the $.log... statements you need
 * 	 http://lab.namics.com/frontend/tools/js/debug/jquery.log/jquery.log.js
 * 
 * the tabselements has to have a backgroundcolor other than transparent (for IE)
 *
 * @example 
 * 	$.tabs({debug:true});		// xxx
 * 
 * @author namics (noel.bossart@namics.com)
 * @version 0.1
 */

// closure wrapper (begin with semicolon)
;(function($) {
	
	/**
	 * @name tabs
	 * @description this is the plugin main part and the public name of it
	 * extends default settings with input, prepares private variables and methods
	 * @param {object} options
	 * @return {$object} same jQuery collection as in input
	 * @example $('div.tabs').tabs({});
	 */
	$.fn.tabs = function(options) {
		
		// extend default settings
		var settings = $.extend({}, $.fn.tabs.defaults, options);
		settings.openTab = settings.openTab-1;

		// keep chaining
		return this.each(function(e) {                                 
			var info = function(txt){               
				if (settings.debug && typeof($.log) == "object") { 
					$.log.info('[jQuery.tabs] index('+e+') - '+txt);
				}
			}  
			var error = function(txt){               
				if (settings.debug && typeof($.log) == "object") { 
					$.log.error('[jQuery.tabs] index('+e+') - '+txt);
				}
			}    
			
			var init = function(){                                                
				info('Tabs Count: '+$(settings.content+' div', $t).length);
				
				if(settings.openTab > $(settings.content+' div', $t).length){
					settings.openTab = $(settings.content+' div', $t).length - 1;
				}
				$(settings.content+' > div', $t).hide();
				$(settings.content+' div:eq('+settings.openTab+')', $t).show(); 
				$('ul li:eq('+settings.openTab+')', $t).addClass('tabs-selected');       
				$('ul li a.tabs', $t).click(click);
			}   
			
			var click = function(){  
				var $this = $(this);
  				var currentTab = $this.attr('href');

  				$('ul li', $t).removeClass('tabs-selected'); 
				$this.parent().addClass('tabs-selected'); 
  
				$(settings.content+' > div', $t).hide(); 
				$(currentTab, $t).show();     
				
				return false;
			}                                 
			
			//
			// section: validation (plugin specific)
			//
			info('Start');   
			
		    // cache the actual jQuery objects
			var $t = $(this);       
			                       
			init();
		});
	};
	

	/**
	 * @name plugintemplate.defaults
	 * @description these are the plugin defaults with a public interface.
	 * They are added as a property on the plugin main method
	 * They need to be called only once on a page to override for all (document ready block not necessary)
	 * @example $.fn.tabs.defaults.debug = true;
	 */
	$.fn.tabs.defaults = {   
		openTab: 1,
		content: '.teasertabscontent',
		debug: NX.debug			// display some output messages, requires: http://lab.namics.com/frontend/tools/js/debug/jquery.log/jquery.log.js
	};
})(jQuery);	