/* 
	dg_tabs jQuery Plugin v1.0 Docs:
	
	options:
		tabLinks: 			[css selector]			The id/class of the list of links (can be the id / class of the parent div)
		tabPanel: 			[css selector]			The id/class of the panel (holding div) where the tab content resides
		selected: 			[css selector]			The id of the first selected tab (default: first in sequence)
		selectedClass: 		[class name (string)]	The class name that you want on the selected object (default: 'selected')
		applySelectedTo: 	[css object selector]	The object (li/div/a) that you want the selected class applied to (default: 'a');
		transition: 		[string]				Option for trasition ('slide','fade')(default: false [none])
		transitionSpeed:	[int]					The speed for the transition in msec (default: 500)
		fixedHeight: 		[boolean]				Boolean for the tab area to keep the greatest height (default: false)
*/

(function($) { 
	var self = null;
	$.fn.dg_tabs = function() {
		var args = arguments[0] || {}; 	
		return this.each(function() {
			new $.dg_tabs(this, args);
		});
	};
	
	$.dg_tabs = function (e, args) {
		this.tabObject = $(e);
		this.tabLinks = args.tabLinks || "#" + $(e.id);
		this.tabPanel = args.tabPanel || "#" + $(e.id).sibling('div');
		this.selected = args.selected || false;
		this.transition = args.transition || false;
		this.transitionSpeed = (args.transitionSpeed/2) || 250;
		this.fixedHeight = args.fixedHeight || false
		this.selectedClass = args.selectedClass || 'selected';
		this.applySelectedTo = args.applySelectedTo || 'a';
		this.fixedHieghtVal = 0;
		this.init();
	};
	
	$.dg_tabs.prototype = {
		init: function(){
			var self = this;
			this.linkCache=[];
			this.currentlyOpen = '';
			this.transitionRunning=false;
			// discover all tab links and memorise them
			$(this.tabLinks+' a[href^="#"]').each(function(){
				self.linkCache.push($(this).attr('href'));
				$(this).click(function(){
					self.invokeTab($(this).attr('href'));
					return false;
				});
			});
			// hide all tabs 
			for (i=0; i<this.linkCache.length; i++){
				if(this.fixedHeight==true){
					if($(this.tabPanel + ' ' + this.linkCache[i]).height()>this.fixedHieghtVal){
						this.fixedHieghtVal=$(this.tabPanel + ' ' + this.linkCache[i]).height();
					}
				}
				if(this.selected==false){
					if (i!=0){
						$(this.tabPanel + ' ' + this.linkCache[i]).hide();
					}else{
						this.currentlyOpen = this.linkCache[i];
						this.applySelectedToToggle(this.linkCache[i]);
					}
				}else{
					if (this.selected!=this.linkCache[i]){
						$(this.tabPanel + ' ' + this.linkCache[i]).hide();
					}else{
						this.currentlyOpen = this.linkCache[i];
						this.applySelectedToToggle(this.linkCache[i]);
					}
				}
			}
			if(this.fixedHeight==true){
				$(this.tabPanel).height(this.fixedHieghtVal);
			}
		},
		
		applySelectedToToggle: function(aHref){
			switch(this.applySelectedTo){
				case 'a':
					$(this.tabLinks+' a[href^="#"]').each(function(){
						$(this).removeClass(this.selectedClass);
					});
					$(this.tabLinks+' a[href="'+aHref+'"]').addClass(this.selectedClass);
				default:
					$(this.tabLinks+' a[href^="#"]').closest(this.applySelectedTo).each(function(){
						$(this).removeClass(this.selectedClass);
					});
					$('a[href="'+aHref+'"]').closest(this.applySelectedTo).addClass(this.selectedClass);
				break;
			}
		},
		
		invokeTab: function(target){
			var self = this;
			if (target == false){return;}
			if(this.currentlyOpen == target) {
				return;
			}
			if(this.transition==false){
				$(this.currentlyOpen).hide();
				$(target).show();
				this.applySelectedToToggle(target);
			}else{
				switch(this.transition){
					case 'slide':
						$(this.currentlyOpen).slideUp(this.transitionSpeed);
						$(target).slideDown(this.transitionSpeed,function(){self.applySelectedToToggle(target);});
					break;
					case 'fade':
						if(this.transitionRunning==true){
							return;
						}else{
							this.transitionRunning=true;
							$(this.currentlyOpen).stop(true,true);
							$(this.currentlyOpen).fadeOut(this.transitionSpeed,function(){
								$(target).fadeIn(self.transitionSpeed,function(){
									self.transitionRunning=false;
									self.applySelectedToToggle(target);
								});
							});
						}
					break;
					default:
						$(this.currentlyOpen).hide(this.transitionSpeed);
						$(target).show(this.transitionSpeed,function(){self.applySelectedToToggle(target);});
					break;
				}
			}
			this.currentlyOpen = target;
		}
	}
})(jQuery);