function expand_twitter() {
	
	$('#twitter_feed_home').css('height', window.innerHeight - 165).css('margin-top', -100).css('z-index', 101);
	$('a[href=#expand_twitter]').hide();
	$('a[href=#collapse_twitter]').show();
	
	// Stop the video from playing
	var myPlayer = _V_("video_1");
	myPlayer.pause();
	
	$('#black_out').fadeTo(300, 0.8);
	
}

function collapse_twitter() {
	
	$('#twitter_feed_home').css('margin-top', 296).css('height', 155).css('z-index', 1);
	$('a[href=#expand_twitter]').show();
	$('a[href=#collapse_twitter]').hide();
	$('#black_out').stop().fadeTo(300, 0, function() {
		$(this).hide();
	});
	
}

function language_selection_box() {
	$('div#language_selection_box').fadeTo(400, 1);
	$('#black_out').fadeTo(400, 0.8);	
}

function close_language_selection_box() {
	$('div#language_selection_box').fadeTo(400, 0, function() { $(this).hide(); });
	$('#black_out').fadeTo(400, 0, function() { $(this).hide(); });
}

function is_touch_device() {  
  try {  
    document.createEvent("TouchEvent");  
    return true;  
  } catch (e) {  
    return false;  
  }  
}

$(document).ready(function() {
	
	$('a#language_selection_box_trigger').click(function() {
		if ($.cookie('lang') == 'none') {
			language_selection_box();
			return false;
		}
	});
	
	$('#language_selection_box a[href=#close]').click(function() {
		close_language_selection_box();
		return false;
	});
	
	$('a#language_selection_box_trigger').eq(0).click();

	$('a[href=#expand_twitter]').click(expand_twitter);
	
	$('a[href=#collapse_twitter]').click(collapse_twitter);
	
	// Load Twitter Feed
	$('#twitter_content').eq(0).load(
		'/request_handlers/twitter_feed.php',
		function() {
		}
	);
	
	
	$('div#menu ul > li > span > a').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	})
	
	if (!is_touch_device()) { // Standard browser
		
		$('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');
		});
		
	} else { // Touch browser
		
		$('div#menu>ul ul').parents('li').toggle(function() {
			$(this).find('>span>a').addClass('hover');
			$(this).find('ul').show();
			$('video').css('-webkit-transform', 'translateX(-2048px)');
			return false;
		}, function() {
			$(this).find('>span>a').removeClass('hover');
			$(this).find('ul').hide();
			$('video').css('-webkit-transform', 'translateX(0)');
			return false;
		});
		
		$('div#menu>ul ul a').click(function() {
			event.stopPropagation();
		});
		
	}
		
	// 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('li').find('a').length) {
				
				$('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').attr('alt', $(this).find('img').attr('alt'));
		
		
		$('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();
		
	});
	
	
	
	$('body.news a.load_main:first').click();
	
	
	$('div.event_cal a.event').click(function() {
		
		var event_day = $(this).parents('td').data('day');
		var event_month = $(this).parents('td').data('month');
		var event_year = $(this).parents('td').data('year');
		
		$('#event_' + event_year + '-' + event_month + '-' + event_day).fadeIn();
		
		return false;
	});
	
	$('.cal_item a.close').click(function() {
		$(this).parent().fadeOut();
		return false;
	});

		
});
