$(document).ready(function() {

	$('div#menu ul > li > span > a').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	})
	
	$('div#menu>ul li').hover(function() {
		$(this).find('>a').addClass('hover');
		$(this).find('ul').show();
	}, function() {
		$(this).find('>a').removeClass('hover');
		$(this).find('ul').hide();
	});
	
	$('div#menu>ul li ul').hover(function() {
		$(this).parent().find('a').addClass('hover');
	}, function() {
		$(this).parent().find('a').removeClass('hover');
	});
		
	// CSS Helpers
	$('ul li:first-child').addClass('first-child');
	
	// Attempt to load the background first
	/*$('div#wrapper').hide();
		setTimeout(function() {
			$('div#wrapper').fadeIn();	
		},1000);*/
	
	
	$('form.korda_newsletter').submit(function() {
		
		var form_ref = this;
		var request_url = '/newsletter_signup/request_handlers/newsletter_signup.php';
		var name = $(this).find('input[name="nl_name"]').val();
		var email = $(this).find('input[name="nl_email"]').val();
		
		$.post(
			request_url,
			{nl_name: name,
			nl_email: email},
			function (data, textStatus) {
				
				switch(data) {
					case 'ok' :
						alert('Thank you. Your details have been recorded.');
						$(form_ref).find('input[name="nl_name"]').val('');
						$(form_ref).find('input[name="nl_email"]').val('');
					break;
					case 'missing' :
						alert('Some required details are missing, please try again.');
					break;
					case 'invalid_email' :
						alert('The e-mail address you entered appears to be invalid.');
					break;
					case 'error' :
						alert('Sorry, an error occurred, please try again later.');
					break;
				}
				
			},
			"text"
		);

		
		return false;
		
	});
	

	$('a.article_next').click(function() {
		
		// Determine the current
		var current = $('div.article').find('div:visible');
		
		// Default to the first image in the set
		var next = $('div.article').find('div:first');
		
		// Next image should be the next sibling, should it exist...
		if ($(current).next('div').length != 0) {
			next = $(current).next('div');
		}
		
		// Perform the fade in/out		
		next.show();
		current.hide();
		
		return false;
	});
	
	$('a.article_prev').click(function() {
		
		
		
			var current = $('div.article').find('div:visible');

			// Default to the last image in the set
			var next = $('div.article').find('div:last');

			// Next image should be the previous sibling, should it exist...
			if ($(current).prev('div').length != 0) {
				next = $(current).prev('div');
			}

			// Perform the fade in/out
			next.show();
			current.hide();
		
		return false;
	});
	
	
	function updateNavigation() {
		
		$('div.main_image_caption a.previous').hide();
		$('div.main_image_caption a.next').hide();
		
		if ($('ul.small_thumbnails a').length > 1) {
			
			

			if ($('ul.small_thumbnails a.current_image').parents('li').prevAll().length > 0) {
				$('div.main_image_caption a.previous').show();
			}
		
			
			if ($('ul.small_thumbnails a.current_image').parents('li').nextAll().length > 1) {
				$('div.main_image_caption a.next').show();
			}
			
		}
		
		
		
	}
	
	
	$('div.main_image_caption a.previous').click(function() {
		$('ul.small_thumbnails li a.current_image').parents('li').prev().find('a').click();
		return false;
	});
	
	
	$('div.main_image_caption a.next').click(function() {
		$('ul.small_thumbnails li a.current_image').parents('li').next().find('a').click();
		return false;
	});
	
	
	$('a.load_main').click(function() {
		
		
		$(this).parents('ul').find('a').removeClass('current_image');
		$(this).addClass('current_image');
		
		$('div.main_image_caption:not(:visible)').show().fadeTo(0, 0).fadeTo(300, 0.8);
		
		var image_url = $(this).attr('href');
		var image_caption = $(this).attr('title');
		
		/*$('div.news_image_full img').fadeOut(200, function() {
			$('div.news_image_full img').attr('src', image_url);
			$('div.news_image_full img').fadeIn(200);
		});*/
		
		$('div.news_image_full img').hide();
			$('div.news_image_full img').attr('src', image_url);
			$('div.news_image_full img').show();
		
		$('div.news_image_full div p.caption').html(image_caption);
		
		updateNavigation()

		return false;
		
	});
	
	
	$('a.show_tm_gallery').click(function() {
		
		$('div.general_content div.main').hide();
		$('div.general_content div.gallery_index').show();
		$('div.tm_gallery_back').show();
		
	});
	
	$('div.tm_gallery_back').click(function() {
		
		$('div.general_content div.main').show();
		$('div.general_content div.gallery_index').hide();
		$('div.tm_gallery_back').hide();
		
	});

		
});