// JavaScript Document - Jquery version
		
//the timeout is to solve a Safari problem, the length of the delay is because chrome has the problem worse.
setTimeout(fw_timed_function = function() {
	jQuery(document).ready(function($){
       // Do jQuery stuff using $
      
		//get all sidebars
		$("div.widget_structure").each(function(i){
			
			$(this).attr("id" , $(this).attr("id").replace('_x',''));
			
			var sidebar_num = $(this).attr("id").replace('widget_structure_','');
			
			//check for a class that switches width with height
			var swap_dimensions = false;
			if ( $(this).hasClass('swap_dimensions') ){ swap_dimensions = true; }
			
			//check for a class to turn off the relocation of tab content (accordian)
			var separate = false;
			if ( $(this).hasClass('separate') ){ separate = true; }
			
			//check for a class to use 'next' and 'previous' instead of plus and minus
			var allow_tab_layers = false;
			if ( $(this).hasClass('allow_tab_layers') ){ allow_tab_layers = true; }
			
			//check for a class to turn off the relocation of tab content (accordian)
			var next_back_only = false;
			if ( $(this).hasClass('next_back_only') ){ next_back_only = true;}
			
			//check for transition settings
			var transition = 'fade';
			if ( $(this).hasClass('transition_slide') ){ transition = "slide"; }
						
			//check for rotation settings
			//if the class is set it will rotate until a tab is selected
			var rotate = false;
			if ( $(this).hasClass('rotate5') ){ rotate = 5; }
			if ( $(this).hasClass('rotate10') ){ rotate = 10; }
			if ( $(this).hasClass('rotate15') ){ rotate = 15; }
			if ( $(this).hasClass('rotate20') ){ rotate = 20; }
			if ( $(this).hasClass('rotate25') ){ rotate = 25; }
			if ( $(this).hasClass('rotate30') ){ rotate = 30; }
			
			//set the IDs on tabs and content and hide the content and make the tabs clicky
			$("li.widget_structure_list_item", $(this)).each(function(ti){
					
					//if we are aiming for next and back only then remove the tabs
					if ( next_back_only ) {
						
						$(this).find("span.widget_structure_title").remove();
					
					} else {
					
						//set the ID for the header
						$(this).find("span.widget_structure_title").attr('id', 'ws_st_' + $(this).attr('id'))
						
						//clickify according to transition style
						switch (transition) {
							case "fade":
								$(this).find("span.widget_structure_title").click(fwws_display_tab_fade);
								break;
							case "slide":
								$(this).find("span.widget_structure_title").click(fwws_display_tab_slide);
								break;
						}
												
					}
					
					//set the ID for the content and hide the content
					$(this).find("div.widget_structure_content").attr('id', 'ws_sc_' + $(this).attr('id')).hide();
						
			});
			
			//are we relocating
			if ( separate ){
				
				//if we are going to do calculations then do the basics here before getting into a loop
				if (allow_tab_layers){
					
					//get the width of the tab container
					var totalWidth = $('#widget_structure_'+sidebar_num+'_tab_list').width();
					var totalHeight = $('#widget_structure_'+sidebar_num+'_tab_list').height();
									
					/************************************/
					//this is the extra added to compensate for the plus or minus sign
					//this needs amending to autocalc
					//one option is to append after the current tab, check width then try before
					var control_margin = 20;		
					var control_margin_height = 30; //this might need manual amendment		
					/************************************/			
								
					//the width of tabs
					var cumulativeTabWidth = 0;
					var cumulativeTabHeight = 0;
					
					//previous value rounded up
					var previousRoundedUp = 1;
					
					//current tab level
					currentTabLevel = 1;
					
				}
				
				tab_list_counter = 0;
				//get the tabs
				$("li.widget_structure_list_item span.widget_structure_title", $(this)).each(function(ti){
					
					//if we are going for next and back only then there will be no tabs to move
						//move the tabs to the list
						//create the list item
						$list_item = $('<li id="widget_structure_'+sidebar_num+'_tab_list_item_'+tab_list_counter+'" class="widget_structure_tab_list_item"></li>');
						$('#widget_structure_'+sidebar_num+'_tab_list').append($list_item);
						$(this).appendTo($list_item);
						
						//add the click to the li element to turn off the rotation
						$list_item.click(function(){jQuery(this).parents('div.widget_structure').removeClass('dorotation')});
						
						//increment the tab counter
						tab_list_counter++;
					
					//sort out the width calc if that is allowed
					if (allow_tab_layers) {
					
						//calculate tab levels
						cumulativeTabWidth += $(this).width(); 
						cumulativeTabHeight += $(this).height();
						
						widthRatio = ( cumulativeTabWidth + control_margin ) / totalWidth;
						heightRatio = ( cumulativeTabHeight + control_margin_height ) / totalHeight;
						
						//if this is the first tab of a new level then we need to start the
						//cumulative width as the container width not carry on where we left off
						//so recalculate the width ratio using the dual control margin option
						if ( widthRatio > previousRoundedUp ) {
							cumulativeTabWidth = ( totalWidth * previousRoundedUp )  + $(this).width();
						}
						if ( heightRatio > previousRoundedUp ) {
							cumulativeTabHeight = ( totalHeight * previousRoundedUp ) + $(this).height();
						}
						
						//recalculate to account for 2nd control box if level 2 or higher
						if ( widthRatio > 1 ) { widthRatio = ( cumulativeTabWidth + (control_margin * 2) ) / totalWidth; } 
						if ( heightRatio > 1 ) { heightRatio = ( cumulativeTabHeight + (control_margin_height * 2) ) / totalHeight; }		
								
						//we are about to start showing and hiding. This line decides whether the show or
						//hide is based on width or height
						if ( swap_dimensions ) { valueRatio = heightRatio; } else { valueRatio = widthRatio; }
						
						//add tab level class
						$(this).parents('li').addClass('fwwstl'+Math.ceil( valueRatio ));
						
						if ( valueRatio > 1 ){ 
							//if non-primary layer then hide it	
							$(this).parents('li').hide(); 
						}
						
						//set the values for next and previous
						nextVal = '+';
						prevVal = '-';
												
						//do not use the calculation if it is turned off or if next and back is the order of the day
						if ( valueRatio > previousRoundedUp ){ 
							//if this is the first item in a new layer add controls to the previous layer
							//to switch to this layer
							//if not on layer 1 then make hidden
							if ( previousRoundedUp > 1 ) {
								$('<li id="fwws_layer_next_'+(previousRoundedUp+1)+'" class="fwws_layer_next"><span class="fw_tabs_next">'+nextVal+'</span></li>').addClass('tab').css("display","none").addClass('fwwstl'+previousRoundedUp).click(fwst_display_tab_level).insertBefore($(this).parents('li'));
							} else {
								$('<li id="fwws_layer_next_'+(previousRoundedUp+1)+'" class="fwws_layer_next"><span class="fw_tabs_next">'+nextVal+'</span></li>').addClass('tab').addClass('fwwstl'+previousRoundedUp).click(fwst_display_tab_level).insertBefore($(this).parents('li'));
							}
							//going to need another one for going the other way
							$('<li id="fwws_layer_prev_'+previousRoundedUp+'" class="fwws_layer_prev"><span class="fw_tabs_prev">'+prevVal+'</span></li>').addClass('tab').css("display","none").addClass('fwwstl'+Math.ceil( valueRatio )).click(fwst_display_tab_level).insertBefore($(this).parents('li'));
						
						}
						
						previousRoundedUp = Math.ceil( valueRatio );
							
					}
					
				});
				
				
				
			} else {
				//if we are not relocating then we might as well kill the spare UL
				$('#widget_structure_'+sidebar_num+'_tab_list').remove();
				if ( next_back_only == false ){
					//we also need to asign the stop rotation click to the main list
					//clicking anywhere will stop it
					$('#widget_structure_'+sidebar_num+'_list').click(function(){jQuery(this).parents('div.widget_structure').removeClass('dorotation')});
				}
			}
			
			//make the first tab selected - won't find any if next back only is selected
			$('span.widget_structure_title:first', $(this)).click();
			
			//add the next and back tabs and show first div if next back only is selected
			if ( next_back_only == true){
				
				//set the values for next and previous
				nextVal = '+';
				prevVal = '-';
				
				prevTab = $('<li id="prev_tab_cont" class="fwws_tab_prev"><span id="prev_tab_tab" class="fw_tabs_prev">'+prevVal+'</span></li>')
				prevTab.addClass('tab').hide()
				
				nextTab = $('<li id="next_tab_cont" class="fwws_tab_next"><span id="next_tab_tab" class="fw_tabs_next">'+nextVal+'</span></li>');
				nextTab.addClass('tab')
							
				//clickify according to transition style
				switch (transition) {
					case "fade":
						prevTab.children('span').click(show_prev_tab_fade)
						nextTab.children('span').click(show_next_tab_fade)
						break;
					case "slide":
						prevTab.children('span').click(show_prev_tab_slide)
						nextTab.children('span').click(show_next_tab_slide)
						break;
				}
				
				//add clicks to remove the rotation
				prevTab.click(function(){$(this).parents('div.widget_structure').removeClass('dorotation')});
				nextTab.click(function(){$(this).parents('div.widget_structure').removeClass('dorotation')});
				
				//add the next and back tabs yo
				if (next_back_only == true) {
					//if we are next back only then we neex to add them to the main list
					prevTab.prependTo($(this).find('#widget_structure_' + sidebar_num + '_list'));
					nextTab.appendTo($(this).find('#widget_structure_' + sidebar_num + '_list'));
				} else {
					prevTab.appendTo($(this).find('#widget_structure_' + sidebar_num + '_tab_list'));
					nextTab.appendTo($(this).find('#widget_structure_' + sidebar_num + '_tab_list'));
				}
				//show the first tab content
				$(this).find("ul.widget_structure_list li.widget_structure_list_item:first div").show();
			}
			
			
			
			//add the rotation stuff
			if (rotate) {
			
				//add the rotation class
				$(this).addClass('dorotation');
				
				//set rotation to acceptable and trigger rotation
				fw_controlRotation($(this).attr("id"), rotate, true);
				
			}

	
			
			function show_next_tab_fade( eventObject){
				
				//get the visible tab
				var visiTab = $(this).parents('div.widget_structure').find("div.widget_structure_content:visible");
				var nextTab = visiTab.parents('li').next('li').children('div.widget_structure_content');
				
				//if we are showing the last tab then find and hide the next tab
				if ( nextTab.parents('li').is(':last-child') ){
					$(this).parents('div.widget_structure').find('li.fwws_tab_next').hide();
				} 
				
				//if this is clicked then this, by default must not be the first tab
				//therefore we must make the previous tab tab visible
				$(this).parents('div.widget_structure').find('li.fwws_tab_prev').show();
				
				visiTab.hide();
				nextTab.fadeIn('fast');
							
			}
			
			function show_next_tab_slide( eventObject){
				
				//get the visible tab
				var visiTab = $(this).parents('div.widget_structure').find("div.widget_structure_content:visible");
				var nextTab = visiTab.parents('li').next('li').children('div.widget_structure_content');
				
				//if we are showing the last tab then find and hide the next tab
				if ( nextTab.parents('li').is(':last-child') ){
					$(this).parents('div.widget_structure').find('li.fwws_tab_next').hide();
				} 
				
				//if this is clicked then this, by default must not be the first tab
				//therefore we must make the previous tab tab visible
				$(this).parents('div.widget_structure').find('li.fwws_tab_prev').show();
				
				visiTab.slideUp('fast');
				nextTab.slideDown('fast');
							
			}
			
			
			function show_prev_tab_fade( eventObject){
				
				//get the visible tab
				var visiTab = $(this).parents('div.widget_structure').find("div.widget_structure_content:visible");
				var prevTab = visiTab.parents('li').prev('li').children('div.widget_structure_content');
				
				//if we are showing the last tab then find and hide the next tab
				if ( prevTab.parents('li').is(':first-child') ){
					$(this).parents('div.widget_structure').find('li.fwws_tab_prev').hide();
				} 
				
				//if this is clicked then this, by default must not be the first tab
				//therefore we must make the previous tab tab visible
				$(this).parents('div.widget_structure').find('li.fwws_tab_next').show();
				
				visiTab.hide();
				prevTab.fadeIn('fast');
							
			}
			
			function show_prev_tab_slide( eventObject){
				
				//get the visible tab
				var visiTab = $(this).parents('div.widget_structure').find("div.widget_structure_content:visible");
				var prevTab = visiTab.parents('li').prev('li').children('div.widget_structure_content');
				
				//if we are showing the last tab then find and hide the next tab
				if ( prevTab.parents('li').is(':first-child') ){
					$(this).parents('div.widget_structure').find('li.fwws_tab_prev').hide();
				} 
				
				//if this is clicked then this, by default must not be the first tab
				//therefore we must make the previous tab tab visible
				$(this).parents('div.widget_structure').find('li.fwws_tab_next').show();
				
				visiTab.slideUp('fast');
				prevTab.slideDown('fast');
							
			}
		
			
		});
	  
		function fwws_display_tab_fade( eventObject ){
			
			//make sure the right tab is selected
			$(this).parents('ul').children('li').find('span.widget_structure_title').removeClass('selected');
			$(this).addClass('selected');
			
			//find the one we want visible
			tabContentID = 'ws_sc_' + $(this).attr('id').replace('ws_st_','');
			
			//make sure all the tab contents are hidden
			$(this).parents('div.widget_structure').find('div.widget_structure_content:visible').hide();
			
			//make it visible
			$(this).parents('div.widget_structure').find('#'+tabContentID).fadeIn('fast');
							
		}
		
		function fwws_display_tab_slide( eventObject ){
			
			//make sure the right tab is selected
			$(this).parents('ul').children('li').find('span.widget_structure_title').removeClass('selected');
			$(this).addClass('selected');
			
			//find the one we want visible
			tabContentID = 'ws_sc_' + $(this).attr('id').replace('ws_st_','');
			
			//make sure all the tab contents are hidden
			$(this).parents('div.widget_structure').find('div.widget_structure_content:visible').slideUp('fast');
			
			//make it visible
			$(this).parents('div.widget_structure').find('#'+tabContentID).slideDown("fast");
		
		}
		
		function fwst_display_tab_level ( eventObject ){
			
			//hide the old ones
			$( 'li.fwwstl'+ currentTabLevel , $(this).parents('ul') ).hide();

			//show the new ones
			if ( $(this).hasClass('fwws_layer_next') ){ 
				$( 'li.fwwstl'+ (currentTabLevel+1) , $(this).parents("ul") ).show(); 
				currentTabLevel++;
				}
			if ( $(this).hasClass('fwws_layer_prev') ){ 
				$( 'li.fwwstl'+ (currentTabLevel-1) , $(this).parents("ul") ).show(); 
				currentTabLevel--;
				}
			
		}
		
		
	  
     });
	 


});


//create the function outside of the delay to avoid scoping issues and the need for closures
function fw_controlRotation( fw_sidebar_ID , delay , first ) {

	//set the next_back_only flag
	var next_back_only = false;
	if (jQuery('#' + fw_sidebar_ID).hasClass('next_back_only')) { next_back_only = true; }
	
	//check the sidebar for the rotation command
	//if it is absent we won't do anything
	if ( jQuery('#'+fw_sidebar_ID).hasClass('dorotation') ){
		//if this is not the first time the function is triggered
		//then do the rotation
		if ( !first){
			
			//are we rotating backwards or forwards?
			if (jQuery('#' + fw_sidebar_ID).hasClass('rotrev')) {
				
				//if we are next back only then we find the tabs a different way
				if (next_back_only == true) {
				
					prevTab = jQuery('#' + fw_sidebar_ID).find('li.fwws_tab_prev span');
					prevTab.click();
					
					if (prevTab.parents('ul').find('li div.widget_structure_content:visible').parents('li').prev('li').hasClass('fwws_tab_prev')) {
						jQuery('#' + fw_sidebar_ID).removeClass('rotrev')
					}
				}
				else {
				
					//find the next tab (the span inside the tab) and click it
					prevTab = jQuery('#' + fw_sidebar_ID).find('.widget_structure_title.selected').parents('li').prev('li').children('span');
					
					//if we have a tab to click then do so
					prevTab.click();
					
					//if the prev tab is a level control tab then get the next (+1) and click that as well
					if (prevTab.hasClass('fw_tabs_prev')) {
					
						//make the next layer visible
						//hide the old ones
						jQuery('li.fwwstl' + currentTabLevel, prevTab.parents('div.widget_structure')).hide();
						
						//show the new ones
						jQuery('li.fwwstl' + (currentTabLevel - 1), prevTab.parents('div.widget_structure')).show();
						currentTabLevel--;
						
						prevTab.parents('li').prev('li').prev('li').children('span').click();
					}
					
					if (prevTab.parents('li').is(':first-child')) {
						jQuery('#' + fw_sidebar_ID).removeClass('rotrev')
					}
				}

			} else {
				
				//if we are next back only then we find the tabs a different way
				if (next_back_only == true) {
				
					nextTab = jQuery('#' + fw_sidebar_ID).find('li.fwws_tab_next span');
					nextTab.click();
					
					if (nextTab.parents('ul').find('li div.widget_structure_content:visible').parents('li').next('li').hasClass('fwws_tab_next')) {
						jQuery('#' + fw_sidebar_ID).addClass('rotrev')
					}
					
				}
				else {
				
					//find the next tab (the span inside the tab) and click it
					nextTab = jQuery('#' + fw_sidebar_ID).find('.widget_structure_title.selected').parents('li').next('li').children('span');
					
					//if we have a tab to click then do so
					nextTab.click();
					//if the next tab is a level control tab then get the next and click that as well
					if (nextTab.hasClass('fw_tabs_next')) {
					
						//make the next layer visible
						//hide the old ones
						jQuery('li.fwwstl' + currentTabLevel, nextTab.parents('div.widget_structure')).hide();
						
						//show the new ones
						jQuery('li.fwwstl' + (currentTabLevel + 1), nextTab.parents("ul")).show();
						currentTabLevel++;
						
						nextTab = nextTab.parents('li').next('li').next('li').children('span');
						nextTab.click();
					}
					
					if (nextTab.parents('li').is(':last-child')) {
						jQuery('#' + fw_sidebar_ID).addClass('rotrev')
					}
					
				}	
				
			}
			
						
		}
		
		//callmyself with the appropriate delay
		setTimeout("fw_controlRotation( '"+fw_sidebar_ID+"' , "+delay+" , false )" , delay * 1000 );
		
	}
}

/*
 * This is a function to help diagnose CSS specificity settings
 */
function fwws_check_parents( wnum ){
	
	//if the version with an x still exists then there is a javascript problem
	if ( jQuery('#widget_structure_' + wnum + '_x').hasClass('widget_structure')){ alert('You probably have javascript errors that are preventing the structured code from running. Please check this before continuing.');}
	
	if ( wnum < 1 ){ wnum = 1; }
			
	var p1 = jQuery('#widget_structure_' + wnum).parents('[id]:first');		
	var p2 = p1.parents('[id]:first');
	var p3 = p2.parents('[id]:first');
			
	var p1id = false;		
	var p2id = false;
	var p3id = false;	
			
	if (p1.attr('id') != '' && typeof(p1.attr('id')) != 'undefined') {p1id = p1.attr('id')};
	if (p2.attr('id') != '' && typeof(p2.attr('id')) != 'undefined') {p2id = p2.attr('id')};
	if (p3.attr('id') != '' && typeof(p3.attr('id')) != 'undefined') {p3id = p3.attr('id')};
	
	reportParents = 'There does not seem to be any valid IDs in the document structure above this. Curious.';
	
	if ( p1id ){ reportParents = p1id; }
	if ( p2id ){ reportParents += ' - ' + p2id; }
	if ( p3id ){ reportParents += ' - ' + p3id; }

	alert(reportParents);
	
}
