// JavaScript Document
$(document).ready(function(){
	$('.tabbed_banner_container').each(function(){
		var banner=$(this);
		var paused=false; //variable is set to true as soon as one of the tabs is clicked
		var navnum=0;
		var tcinum=0;
		var animating=false; //set to true when the transition fade is running. This variable isn't really used for anything
		var cancel = false; //
		var next;
		var count=0;
		var autoTabChangeCount = 4; //if set to false then loop indefinitely until tab has been clicked
		
		function autoplay(){
			if(!paused && !cancel) switchtab();
		}
		
		function switchtab(){
			
			animating=true;
			
			banner.find('.tb_tt').appendTo(banner.find('.tabbed_banner_tile_container')); //move to the bottom of the stack
			banner.find('.tb_tt').css({'z-index':navnum-1}).removeClass('tb_tt'); //z index 1 less than the next panel to display
			next.addClass('tb_tt');

			next.fadeIn(500,function(){
				
				if(autoTabChangeCount!=null){
					count++;
					if(count==autoTabChangeCount-1) paused=true;
				}
				
				if(!cancel){

					if( next.hasClass('subs') ){
						next.find('.tabbed_banner_control').fadeIn(600,function(){
							$(this).removeClass('ndisplay');
						});			
					}else{
						if(!paused){
							banner.find('.tabbed_banner_control').css({'display':'none'});
						}
					}
					
					if(!paused){
						next=banner.find('.tb_tt').next('div');
						next.css({'display':'none','z-index':navnum});
						next.prependTo(banner.find('.tabbed_banner_tile_container'));
						setTimeout(autoplay,6000);		
					}
				}
				animating=false;
				
			});	
			

		}; //switchtab function

		banner.find('.tabbed_banner_nav_item').each(function(){
			var thistilebtn=$(this);
			navnum++;
			var thisnum=navnum;
			var reltile=banner.find('.tbtm_'+thisnum+'');
			var subctrl=banner.find('.subs').find('.tabbed_banner_control');
			
			$(thistilebtn).click(function(){
				
				cancel=true;		
				paused=true;
				
				if(!reltile.hasClass('tb_tt')){ //not already clicked on or in focus
										
					if(!subctrl.hasClass('ndisplay')){
						subctrl.addClass('ndisplay');
						subctrl.css({'display':'none'});
					}	
					
					reltile.css({'display':'none'}); //hide the layer to be displayed
					reltile.prependTo(banner.find('.tabbed_banner_tile_container'));	//place at the top of the stack
					next=reltile;
					next.css({'z-index':navnum});						
					//banner.find('.tb_tt').css({'z-index':navnum-1})
					cancel = false;
					switchtab();
				}else{
					cancel = false;
				}
			});
		});//for each button
		banner.find('.tabbed_banner_control_item').each(function(){
			var this_inner_tilebtn=$(this);
			tcinum++;
			var this_tcinum=tcinum;
			var relinnertile=banner.find('.tbtm_2').find('.tbti_'+this_tcinum+'');
			$(this_inner_tilebtn).click(function(){
				paused=true;
				banner.find('.tbtm_2').find('.current').removeClass('current');
				$(this).addClass('current');
				if(relinnertile.hasClass('i_tt')){
				}else{	
					banner.find('.tbtm_2').find('.i_tt').removeClass('i_tt');
					relinnertile.addClass('i_tt');
				}
			});
		});//for each button
		banner.find('.cms_components_h_popup_link').each(function(){
			$(this).click(function(){
				if(paused!=true){paused=true;}
				if(cancel!=true){cancel=true;}
				});
			});
		//initialize the autoplay		
		next = banner.find('.tbtm_2');
		next.css({'display':'none','z-index':navnum});
		next.prependTo(banner.find('.tabbed_banner_tile_container'));
		setTimeout(autoplay,6000);	
		

		
	});
});
