$(document).ready(
	function() {
		// Generic rollover function
		$(".rolloverSimple").hover(
			function() {
				this.src = this.src.replace("_o", "_h");
			},
			function() {
				this.src = this.src.replace("_h", "_o");
			}
		);
		$(".thumbnails img").hover(
			function() {
				if ($(this).hasClass('fadingThumb')) {
					$(this).stop().fadeTo('fast', 1);
				} else {
					$(this).addClass('hover');
				}
			},
			function() {
				if ($(this).hasClass('fadingThumb')) {
					$(this).stop().fadeTo('slow', 0.3);
				} else {
					$(this).removeClass('hover');
				}
			}
		);
		$(".thumbnails img").click(
			// When thumb is clicked, set corresponding main image to be the one that matches the thumbnail and
			// slide the main image down if it's currently hidden. If main image is already visible and image
			// matches the thumbnail, then slide the main image back up.
			function() {
				// Behavior for anim play button
				if ($(this).hasClass('playbutton')) {
					if ($(this).parent().parent().parent().children('.mainImage').children('.animHolder').children('embed').length == 0) {
						var flv = $(this).attr('id') + '.flv';
						$(this).parent().parent().parent().children('.mainImage').children('img').hide();
						$(this).parent().parent().parent().children('.mainImage').children('.animHolder').flash({
							src: 'swf/videoplayer.swf',
							width: 801,
							height: 463,
							flashvars: { vid: flv }
						});
						$(this).parent().parent().parent().children('.mainImage').slideDown('slow');
					} else {
						$(this).parent().parent().parent().children('.mainImage').slideUp('slow');
						$(this).parent().parent().parent().children('.mainImage').children('.animHolder').children('embed').remove();
						$(this).parent().parent().parent().children('.mainImage').children('img').show();
					}
					return;
				}
				// Other flash clip (not a video)
				if ($(this).hasClass('flashClip')) {
					if ($(this).parent().parent().parent().children('.mainImage').children('.animHolder').children('embed').length == 0) {
						var swf = 'swf/' + $(this).attr('id') + '.swf';
						$(this).parent().parent().parent().children('.mainImage').children('img').hide();
						$(this).parent().parent().parent().children('.mainImage').children('.animHolder').flash({
							src: swf,
							width: 801,
							height: 600 
						});
						$(this).parent().parent().parent().children('.mainImage').slideDown('slow');
					} else {
						$(this).parent().parent().parent().children('.mainImage').slideUp('slow');
						$(this).parent().parent().parent().children('.mainImage').children('.animHolder').children('embed').remove();
						$(this).parent().parent().parent().children('.mainImage').children('img').show();
					}
					return;
				}
				// Otherwise it's an image thumbnail - first check if there's a flash file in the animHolder. If so, remove it and show
				// the image (which will be hidden before the flash file is inserted)
				if ($(this).parent().parent().parent().children('.mainImage').children('.animHolder').children('embed').length) {
					$(this).parent().parent().parent().children('.mainImage').children('.animHolder').children('embed').remove();
					$(this).parent().parent().parent().children('.mainImage').children('img').show();
				}
				// determine main image id
				var cl = $(this).attr('class').split(' ');
				var mi = 'img#' + cl[0].replace('thumb_', 'main_');
				// if main image filename matches thumbnail filename, slide up the mainImage div
				if ($(mi).attr('src') == this.src.replace('_s', '_l') && $(mi).parent().is(':visible')) {
					$(mi).parent().slideUp('slow');
					return;
				}
				$(mi).attr('src', this.src.replace('_s', '_l'));
				$(this).parent().parent().parent().children('.mainImage').slideDown('slow',
					function() {
						$(this).children('.closeButton').css('visibility', 'visible').fadeTo('slow', 0.1);
					}
				);
			}
		);
		$('.mainImage img, .mainImage span.closeButton').click(
			function() {
				$(this).parent().children('.closeButton').css('visibility', 'hidden');
				$(this).parent().slideUp('slow');
			}
		);
		$('.mainImage img, .mainImage span.closeButton').hover(
			function() {
				$(this).parent().children('.closeButton').stop().fadeTo('slow', 1);
			},
			function() {
				$(this).parent().children('.closeButton').stop().fadeTo('slow', 0.1);
			}
		);
		// Contact form validation
//		$('.contactForm .button').click(
//			function() {
//				alert('fuuuuuu');
//			}
//		);
		// Rotating banner
		$('#illuBanners').cycle();
		// Flash anim on 'unique way of working' page
		if ($('#uniqueanim').length) {
			$('#uniqueanim img').remove();
			$('#uniqueanim').flash({
				src: 'swf/anew-preloader.swf',
				width: 801,
				height: 394
			});
		}
	}
);

