$(document).ready(function () {

	// Initialize Shadowbox
	Shadowbox.init();

	var handle_height = 0;

	// Page block images
	$('.jcarousel li').mouseenter(function () {

		var id = $(this).attr('id');

		$('.jcarousel li').removeClass('active');
		$(this).addClass('active');

		$(this).parents('.page-block-images').find('.pbi-large').css('display', 'none');

		id = id.replace ('thumb', 'large');

		$('#' + id).css('display', 'block');

	});

	/**
	 * Home header
	 */

	var items = $('#header-con ul#header-images li');

	// Custom animation
	$("ul#blocks li").mouseenter(function(){

		if (!$(this).hasClass('active')) {

			var title = $(this).attr('id').replace('block_', '');

			active_img = $('#header-con ul#header-images li.active');
			active_block = $('#header-con ul#blocks li.active');

			slide_img = $('#header-con ul#header-images li#' + title);

			// Remove active
			active_img.removeClass('active');
			active_block.removeClass('active');

			slide_img.addClass('active');
			$(this).addClass('active');

			var next_item = title.replace ('nr', '');

			$(active_img).fadeOut(1000);
			$(slide_img).fadeIn (1000);

			clearInterval(loader);

			loader = setInterval (function () {

				// Remove active
				active_img = $('#header-con ul#header-images li.active');
				active_block = $('#header-con ul#blocks li#block_' + $(active_img).attr('id') + '.active');

				// Calculate next
				next = items.index(active_img) + 1;

				// Check for offset
				if (next == items.length) {
					next = 0;
				}

				// Fetch next
				slide_img = items.get(next);
				slide_block = $('#header-con ul#blocks li#block_' + $(slide_img).attr('id'));

				// Remove active
				$(active_img).removeClass('active');
				$(active_block).removeClass('active');

				$(slide_img).addClass('active');
				$(slide_block).addClass('active');

				$(active_img).fadeOut(1000);
				$(slide_img).fadeIn (1000);

			}, 5000);
		}

	});

	if (items.length > 1) {

		$('#header-con ul#header-images li').css ("display", "none");
		$('#header-con ul#header-images li.active').css ("display", "block");

		loader = setInterval (function () {

			// Remove active
			active_img = $('#header-con ul#header-images li.active');
			active_block = $('#header-con ul#blocks li#block_' + $(active_img).attr('id') + '.active');

			// Calculate next
			next = items.index(active_img) + 1;

			// Check for offset
			if (next == items.length) {
				next = 0;
			}

			// Fetch next
			slide_img = items.get(next);
			slide_block = $('#header-con ul#blocks li#block_' + $(slide_img).attr('id'));

			// Remove active
			$(active_img).removeClass('active');
			$(active_block).removeClass('active');

			$(slide_img).addClass('active');
			$(slide_block).addClass('active');

			$(active_img).fadeOut(1000);
			$(slide_img).fadeIn (1000);

		}, 5000);

	}

	$('#flyout-handle').mouseenter (function() {

		$('.service-block').css ('height', '150');
		$('.service-block').css ('display', 'block');
		$('#flyoutmenu').css ('height', '160');

		handle_height = 150;

	});

	$('#flyoutmenu').mouseleave (function () {

		$('.service-block').css ('height', '0');
		$('.service-block').css ('display', 'none');
		$('#flyoutmenu').css ('height', '0');

		handle_height = 0;
	});

	$('#flyout-handle').click (function() {

		if (handle_height == 0) {

			$('.service-block').css ('height', '150');
			$('.service-block').css ('display', 'block');
			$('#flyoutmenu').css ('height', '160');

			handle_height = 150;

		} else {

			$('.service-block').css ('height', '0');
			$('.service-block').css ('display', 'none');
			$('#flyoutmenu').css ('height', '0');

			handle_height = 0;
		}

	});

});
