$(document).ready(function(){
	
	$('#no-script').remove();	// Removing the non-JavaScript message	
	$('#navigationBar').css('visibility', 'visible');	// Making the Navigation Bar visible if JavaScript is enabled.
	//$('#loginPanel').css('visibility', 'visible');		// Making the Login Box visible if JavaScript is enabled.

	var prefix = $('#prefix').val();
	
	// The Comics dropdown menu
	$('#theComicsMenu').hide();
	$(".theComics, #theComicsMenu").hover(function(){
	  $("#theComicsMenu").show();
	},function(){
	  $("#theComicsMenu").hide();
	});
	
	// Extras dropdown menu
	$('#extrasMenu').hide();
	$(".extras, #extrasMenu").hover(function(){
	  $("#extrasMenu").show();
	},function(){
	  $("#extrasMenu").hide();
	});
	
	// Admin dropdown menu
	$('#adminMenu').hide();
	$(".admin, #adminMenu").hover(function(){
	  $("#adminMenu").show();
	},function(){
	  $("#adminMenu").hide();
	});

	// Navigation buttons
	$('.navBtnBG').hide();
	$(".navBtn").hover(function(){
		$(this).children('.navBtnBG').fadeIn("fast");
		validateDisplay();
	},function(){
		$(this).children('.navBtnBG').fadeOut("fast");
	});  
	
	function validateDisplay()
	{
		// AJAX routine to hide or deactivate the DDL options.
		$.ajax({
			type: 'GET',
			url: prefix+'Scripts/validateHeader.php',
			data: 
			{ 
			},
			
			dataType: 'json',                //data format  
	
			success:function(response)
			{
				var total = 0; var SectionNameCt = 0; var ReasonCt = 0; var SectionSecurityLevelCt = 0; var Reason = []; var SectionName = []; var SectionSecurityLevel = [];
				
				// Determining how many rows need to be actioned
				for (row in response) 
				{  
					total++;
				}
				total--;	
				
				// Iterating through the number of rows that will only contain details of the sections that are unavailable.
				for (i=1; i<=total; i+=3)
				{
					// Section Name
					SectionNameCt++;
					SectionName[SectionNameCt] = response[i];
					
					// Reason Text
					ReasonCt++;
					Reason[ReasonCt] = response[i+1];	
					
					// Section Security Level
					SectionSecurityLevelCt++;
					SectionSecurityLevel[SectionSecurityLevelCt] = response[i+2];			
				}	

				// If visitor is a Super User then do NOT make sections unavailable.
				console.log("1: MemberSecurityLevel[i]: "+$('#MemberSecurityLevel').val())
				if($('#MemberSecurityLevel').val() > 1)
				{
					for(i=1; i<=total/3; i++)
					{	
						// INSUFFICIENT ACCESS
						console.log("SectionName["+i+"]: "+SectionName[i]+ "......SectionSecurityLevel["+i+"]: "+SectionSecurityLevel[i]+ "......MemberSecurityLevel: "+$('#MemberSecurityLevel').val()+ "......Reason["+i+"]: "+Reason[i])

						if(SectionSecurityLevel[i] < $('#MemberSecurityLevel').val())
						{
							console.log('++++ STAGE 1 ++++')
							$('#'+SectionName[i]+'1').attr('href', '#')		// Deactivating the hyperlink
							$('#'+SectionName[i]+'2').css('color','#999')
							$('#'+SectionName[i]+'2').css('background-color','#333')
							$('#'+SectionName[i]+'2').css('font-style','italic')
							$('#'+SectionName[i]+'2').css('font-weight','200')
							$('#'+SectionName[i]+'2').css('cursor','default');	// Changing the appearance of the cursor to the DEFAULT cursor
							$('#'+SectionName[i]+'2').attr('title', 'Insufficient Access');	// Adding the reason for unavailability for mouseover effect.
						}
						else
						{
							if(Reason[i] != "")
							{
								// SECTION UNAVAILABLE	
								console.log('++++ STAGE 2 ('+SectionName[i]+')++++')
								$('#'+SectionName[i]+'1').attr('href', '#')		// Deactivating the hyperlink
								$('#'+SectionName[i]+'2').css('cursor','help');	// Changing the appearance of the cursor to the HELP cursor
								$('#'+SectionName[i]+'2').attr('class', 'navDropDownBtn')	// Changing the text font colour to the Dark version
								
								$('#'+SectionName[i]+'2').css('font-weight','100')
								$('#'+SectionName[i]+'2').css('font-style','italic')
								$('#'+SectionName[i]+'2').css('color','#333')
								$('#'+SectionName[i]+'2').css('background-color','#CCC')
								$('#'+SectionName[i]+'2').attr('title', Reason[i]);	// Adding the reason for unavailability for mouseover effect.
							}
							else
							{
								// NORMAL SETTING
								console.log('++++ STAGE 3 ++++')
								$('#'+SectionName[i]+'2').attr('class', 'navDropDownBtn border-Dark')	// Resetting the text font colour to normal
								$('#'+SectionName[i]+'2').css('cursor','pointer');						// Resetting the appearance of the cursor to the POINTER cursor
								$('#'+SectionName[i]+'2').attr('title', "");							// Resetting the Title details to blank.								
								$('#'+SectionName[i]+'1').attr('href', prefix+SectionName[i]+".php")	// Resetting the hyperlink

								$('#'+SectionName[i]+'2').css('color','#FFF')
								$('#'+SectionName[i]+'2').css('font-style','inherit')
								$('#'+SectionName[i]+'2').css('font-weight','bold')							
								$('#'+SectionName[i]+'2').css('background-color','')	// Setting background colour to default colour scheme colour.
							}	
						}
					}
				}			
				
			},
			error: function()
			{
				console.log("An error has occurred.")
				return false;
			}
				
		})
	}
})
