/*
 *	copyright david coombes
 *	http://www.david-coombes.com
 *
 *	plugin page: http://david-coombes.com/projects/jquery-options-box
 */

// JavaScript Document
 (function($){  
     $.fn.extend({   	
		optionsBox: function(options){
			
			//vars
			var d = {
				c : '#363636',
				bg : '#C0C0C0',
				w : '400',	//container width
				h : '300',	//container height
				fS : '16',	//fontsize
				nH : '30'		//nav-bar height
			}
			var o = $.extend(d, options);

			return this.each(function(){
				
				//vars
				t = $(this);
				
				//set container
				id = optBxUID('opt-box');
				$(this).width(o.w+'px')
					.height(o.h+'px')
					.css('color',o.c)
					.css('padding','0')
					.attr("id",id);
				
				//create nav-bar
				_n = optBxUID('opt-nav');
				t.prepend('<div id="'+_n+'"></div>');
				$('#'+_n).css('position','relative');
				n = $('#'+_n);
				
				//add links, hide divs
				count=0;
				t.find('div').each(function(){
					tl = $(this).attr("title")
					if(tl){
						ti = optBxUID('opt-box');
						$(this).attr("id", ti);
						i = ti+'-link-'+count;
						n.append('<a name="'+ti+'" id="'+i+'" alt="'+tl+'">'+tl+'</a>');
						$(this).css('border','1px solid '+o.c)
							.css('background',o.bg)
							.css('color',o.c)
							.css('clear','both')
							.hide();
						count++;
					}
				});
				l = $('a',n);
				l.css('font-size',o.fS+'px')
					.css('line-height','normal')
					.css('border','1px solid '+o.c)
					.css('background',o.bg)
					.css('float','left');
				
				//css links and set nav-bar height based on links css
				cur = l.innerHeight();
				if(cur<o.nH) padding = Math.floor((o.nH-cur)/2)+'px 3px';
				else padding = '2px 3px';
				l.css('padding',padding)
					.css('margin','0 0 -1px 5px')
					.css('cursor','pointer')
					.css('text-decoration','none');
				o.nH = l.innerHeight();
				n.height((o.nH+1)+'px');
				
				//add default div
				if(!o.d){
					_d = optBxUID('opt-default');
					t.append('<div id="'+_d+'"></div>');
					d = $('#'+_d);
					d.css('border','1px solid')
						.css('clear','both')
						.width((o.w)-2+'px')
						.height((o.h-o.nH)-3+'px');
				}

				//link onclicks
				l.click(function(){
								 
					//vars
					bx = $('#'+$(this).attr("name"));					
					pid = $(this).parent().parent().attr("id");
					_n = $(this).parent().attr("id");
					l = $('#'+_n+' > a');
					
					//hide all divs except nav-bar
					$('#'+pid+' > div').each(function(){
						if($(this).attr("id")!=_n) $(this).hide();
					});
					
					//reset tabs and change this
					l.css('border','1px solid '+o.c)
						.css('background',o.bg)
						.css('margin-top','0');
					$(this).css('margin-top','1px');
					
					//show this box
					bx.css('width',o.w+'px')
						.css('height',(o.h-o.nH)+'px')
						.show();
				});

				//if no default click first tag
				if(o.d){
					l.each(function(){
						if($(this).text()==o.d) $(this).click();
					})
				}
				
				//create unique id's
				function optBxUID(pre){
					id = pre+'-1';
					count = 1;
					while($('#'+id).attr("id")){
						count++;
						id = pre+'-'+count;
					}
					return id;
				}
			});
		}
	
	});
})(jQuery);
