// PLtab.js - A jQuery Plugin
// Version 1.0.1
// Written by Andy Tennison

// creates JS tabbed content

(function($) {
	$.fn.PLtab = function(options) {
		var opts = $.extend({}, $.fn.PLtab.defaults, options);
		return this.each(function() {
			var $box = $(this);
			var tab = [];
			var curIndex = 0;
			var startIndex = 0;

			$(this).find('.tab').each(function(i){
				
				function tabItem(status, index, id, a, href, content){
					this.status = status,
					this.index = index,
					this.id = id,
					this.a = a,
					this.href = href,
					this.content = content
				};
				tab[i] = new tabItem();
				tab[i].index = startIndex;
				tab[i].id = $(this);
				tab[i].a = tab[i].id.find('a');
				
				
				if($box.find(''+$(this).find('a').attr('href')+'').length){ // checks if tab has related content - href must match id of content
					tab[i].status = true;
					tab[i].href = tab[i].a.attr('href');
					tab[i].content = $box.find(''+tab[i].href+'');
					
					if (startIndex == 0){
						tab[i].id.addClass('active')
					}
					startIndex++;
					
					$(tab[i].a).click(function(){
						if (curIndex != i){
							tab[curIndex].id.removeClass('active')
							tab[i].id.addClass('active')
							tab[curIndex].content.css('display','none');
							tab[i].content.css('display','block');
							curIndex = i;
						}
						return false;
					})
					
					
				} else {
					tab[i].status = false;
					
					tab[i].id.addClass('disabled');
					$(tab[i].a).click(function(){
						return false
					})
				}; // end if 'tab'
				
				
				
			}); // end each 'tab'
			
						
		});


		
	}; // end plugin PLtab


// end of closure
})(jQuery);

$.fn.PLtab.defaults = {
	//duration: 800
};




//<div class="menu-holder"><div class="tl"><div class="tr"/></div><div class="ml"><div class="mr">
		
		// content
		
//</div></div><div class="bl"><div class="br"/></div></div>

/*
$(function() { // Create mask for navigation
	var display_timeout = 0;
	$('.nav').hover(
		function(){ // Wait half a second, then create mask
			if(display_timeout != 0) { clearTimeout(display_timeout); }
			display_timeout = setTimeout( function(){
				display_timeout = 0;
				$('body').append('<div class="mask"></div>');
				var winHeight = $('html').css('height');
				$('.mask').css({'height':winHeight,'opacity':0}).fadeTo('fast',0.25);
			}, 250);
		},
		function(){ // Fade mask out when mouse out
			if(display_timeout != 0) { clearTimeout(display_timeout); }
			$('.mask').fadeOut('medium', function() { $(this).remove(); });
		}
	);
});
		
$(function() { // Hover function for navigation dropdowns
	var display_timeout = 0;
	$('.nav > li').hover( 
		function(){ // Wait half a second, then create sub-nav
			if(display_timeout != 0) { clearTimeout(display_timeout); }
			var $this = jQuery(this);
			var theHtml = $(this).find('ul').html();
        	display_timeout = setTimeout( function(){
        		display_timeout = 0;
        		var newHtml = makeSubNav(theHtml);
        		$this.find('ul').replaceWith(newHtml);
        		$this.addClass('menu');
        	}, 350); 
		},
		function(){ // Mouse out - remove sub-nav & inject original menu
			if(display_timeout != 0) { clearTimeout(display_timeout); }
			$(this).removeClass('menu');
			var theHtml = '<ul>\n' + $('ul', this).html() + '\n</ul>';
//	window.console.log(theHtml);
			$(this).find('.menu-holder').replaceWith(theHtml);
		}
	);
});

function makeSubNav(theHtml) { // Create container for sub nav & inject content
	var htmlIntro = '<div class="menu-holder">\n<div class="tl">\n<div class="tr"></div>\n</div>\n<div class="ml">\n<div class="mr">\n<ul>';
	htmlIntro += theHtml;
	htmlIntro += '</ul>\n</div>\n</div>\n<div class="bl">\n<div class="br"></div>\n</div>\n</div>';
	return htmlIntro;
};
*/
