/**
 * @author dave
 */
(function($) {
    $.fn.BluntTab = function(settings){
		//alert('infunction');
		var config = {'TC' : $('#Tabs > .Tab')};
		if (settings) $.extend(config, settings);
		
		var $Tabs = $('ul.Tabbed >li a');
		var Url= $('ul.Tabbed li#Url').text();
		var $TC = config.TC;
		//alert($TC.html());
		
		$Tabs.filter(':first').addClass('ActiveTab');
		$TC.hide().filter(':first').show();
		$Tabs.each( function(e) { 
			$(this).mouseover( handleMouseOver ); 
			$(this).click( handleClick ); 
		});
    
    	function handleMouseOver() {
			//console.log(this);
			$TC.hide().filter(this.hash).show();
			$Tabs.removeClass('ActiveTab');
			$(this).addClass('ActiveTab');
			
			return false;
		}
    	
    	function handleClick(e) { 
    		//alert (this.hash);
    		var h=this.hash;
    		//alert (Url+'/'+h.replace('#', ''));
    		window.location = Url+'/'+h.replace('#', '');
    		return false; 
    	}

		return this;
	}
})(jQuery);

(function($) {
  $.fn.switchr = function(settings) { 
    var tabs = $(this)
      , tabIds = []
      , iid //Interval id
      , indx = 0
      , tLen 

    
    tabs.each(function(i, el) { 
      tabIds.push($(el).attr('id'));
    });     
    
    tLen = tabIds.length;  
    
    var nxtTab =  function(nxtTabId) {       
      tabs.removeClass('ActiveTab').hide();
      $('#'+nxtTabId).addClass('ActiveTab').show(); 
      
      //swallow tail
      iid = setTimeout(function() { 
        (indx < tLen -1) ? indx++ : indx = 0;
        nxtTab(tabIds[indx]); 
      },
      4000);
    }
    
    //Kick it all off
    nxtTab(tabIds[0]);
    
    
    //Click Handlers
    $('a.Prev').click(function(e){ 
      clearInterval(iid);      
      (indx > 0 ) ? indx -= 1 : indx = tLen -1;
      nxtTab(tabIds[indx]);     

      e.preventDefault();
      return false;
    });
    
    $('a.Next').click(function(e){ 
      clearInterval(iid);   
      (indx < tLen - 1) ? indx += 1 : indx = 0;
      nxtTab(tabIds[indx]);       

      e.preventDefault();
      return false;
    });
  }
})(jQuery);

(function($) {
	$.fn.TabSwitcher = function (settings) { 
		var config = { };
		if(settings) $.extend(config, settings);
		
		var $self = $(this); 
		var items = new Array();
		var intervalID = 0;
		var index = 1; 
		var max = 0;
		
		
		
		
		$self.each(function(i, el) { 
			var aTag = $(el);
			var hash = $(el).attr('href');
			
			if(i > 0) { 
				$(hash).css('display', 'none');
			}
			
			items.push( {'tag' : aTag, 'hash' : hash } );
			
			//console.log('tag : '+items[i].tag+' href: '+items[i].hash);
		});
		
		max = items.length -1;
		
		
		function switchTab() { 
			var aTag, hash, prevHash, prevIndex;
			var $nextSection, $prevSection;
			
			// get the link tag and hash value;
			aTag=items[index].tag; 
			hash=items[index].hash;
			
			//set up the last index that was shown;
			(index > 0) ? prevIndex=index -1 : prevIndex = max;
			
			prevHash = items[prevIndex].hash;
			
			//Get the section objects for manipulation;
			$nextSection =  $(hash);
			$prevSection = $(prevHash);
			
			
			$self.removeClass('ActiveTab');
			
			$(items[index].tag	).addClass('ActiveTab');

			
			$prevSection.fadeOut('fast', function() { $nextSection.fadeIn('fast'); });
			
			(index < max ) ? index++ : index = 0;
			

			
			//console.log(' switch Called index: '+index+' max: '+max);
			
		}
		
		intervalID = setInterval(function() { switchTab(); }, 4000);
		
	  $('a.Prev').click(function(e){ 
	    (index > 1 ) ? index -= 1 : index = max;
	    clearInterval(intervalID);
      switchTab();
      intervalID = setInterval(function() { switchTab(); }, 4000); 
      e.preventDefault();
      return false;
    });

	
	};
})(jQuery);
