$(function() {
			$('.search-button').simpleButton();
			$('.main-navigation a').simpleButton();
			$('.sub-navigation a').simpleButton();
			$('.years a').simpleButton();

			$('.main-navigation li').mouseover(function() {
				$(this).find('ul').width($(this).width())
			});

			var menuWidth = $('.main-navigation').innerWidth();
			var totalItemWidth = 0;

			var menuItems = $('.main-navigation>li');
			menuItems.each(function () {
				totalItemWidth += $(this).outerWidth();
			});
			var itemQty = menuItems.length;
			var addWidth = menuWidth - totalItemWidth - itemQty * 5 - 3 - (typeof(ie6MenuCorrection) == 'undefined' ? 0 : ie6MenuCorrection);

			if (addWidth > 0) {
				var remainWidth = addWidth;
				$('.main-navigation>li>a>i.nc>ins').each(function () {
					var addItemWidth = Math.ceil(addWidth * $(this).parents('li:first').outerWidth() / totalItemWidth / 2);
					if (addItemWidth * 2 > remainWidth) addItemWidth = Math.floor(remainWidth / 2);
					remainWidth -= addItemWidth * 2;
					$(this).css({paddingLeft: addItemWidth+'px', paddingRight: addItemWidth+'px'})
				});
			}

			show_other_insurance_types();

			// measuring left menu submenu height
			var sidebarClone = $('div.sidebar:first')
				.clone()
				.css({position: 'absolute', left: '-700px'})
				.appendTo('body');
			$('*', sidebarClone).show();

			var maxSubmenuHeight = 0;
			$('ul.sub-navigation > li.top-level-item > div', sidebarClone).each(function () {
				var currentHeight = $(this).innerHeight();
				if (currentHeight > maxSubmenuHeight) {
					maxSubmenuHeight = currentHeight;
				}
			});

			sidebarClone.remove();
			if (maxSubmenuHeight > 0) {
				$('ul.sub-navigation > li.top-level-item > div').height(maxSubmenuHeight);
				$('ul.sub-navigation > li.top-level-item > div > ul').height(maxSubmenuHeight - 23);
			}
		});

(function($) {

	$.fn.simpleButton = function() {
		this.mouseover(function() {
					$(this).addClass('mouseup');
				}).mousedown(function() {
					$(this).removeClass('mouseup').addClass('mousedown');
				}).mouseup(function() {
					$(this).removeClass('mousedown').removeClass('mouseup');
				}).mouseleave(function() {
					$(this).removeClass('mousedown').removeClass('mouseup');
				});
	};
})(jQuery);

var adjust_elements = function() {
	var w = $(this).width();
	var arrow_w = $('.arrow').outerWidth();
	var min_w = $('.page').outerWidth();
	if (w < min_w + arrow_w) {
		$('.arrow, .right-panel').hide();
	} else {
		$('.arrow').show();

		var right_panel_w = Math.min(w - min_w - arrow_w - 50, 150);
		if (right_panel_w <= 110) {
			$('.right-panel').hide();
		} else {
			$('.right-panel').show();
		}
	}
}

var open_right_panel = function(tmp) {
	$(tmp).animate({
				width : '+=846'
			}, 400, 'swing');
	$(tmp).data('opened', 1);
	right_panel_on_timer = false;
}

var close_right_panel = function(tmp) {
	$(tmp).animate({
				width : '-=846'
			}, 400, 'swing');
	$(tmp).data('opened', 0);
	right_panel_off_timer = false;
}

var right_panel_on_timer, right_panel_off_timer;
$(function() {
			$('.right-panel').hover(function() {

						if (right_panel_off_timer) {
							window.clearTimeout(right_panel_off_timer);
							right_panel_off_timer = false;
							return;
						}
						if ($(this).data('opened')) {
							return;
						}
						var tmp = this;
						right_panel_on_timer = window.setTimeout(function() {
									$('.right-panel').css({ zIndex:"999"}); open_right_panel(tmp);
								}, 200);
					}, function() {

						if (right_panel_on_timer) {
							window.clearTimeout(right_panel_on_timer);
							right_panel_on_timer = false;
							return;
						}
						if (!$(this).data('opened')) {
							return;
						}
						var tmp = this;
						right_panel_off_timer = window.setTimeout(function() {

									window.setTimeout(function() {$('.right-panel').css({ zIndex:"10"})}, 400);
									close_right_panel(tmp);
									}, 800);

					});
			$(window).resize(adjust_elements)
			adjust_elements();

			$('.right-panel-close').click(function () {
				window.setTimeout(function() {$('.right-panel').css({ zIndex:"10"})}, 400);
				close_right_panel($('.right-panel'));
			});
		})

// ====== Mini Accordion Class =======

function MiniAccordion($tab_selector, $main_selector) {
	this.tabs = [];
	this.lastTab = false;
	this.tabSelector = $tab_selector;
	this.mainSelector = $main_selector;

	var $me = this;

	$(document).ready(
		function() {
			$me.init();
		}
	);
}

MiniAccordion.prototype.init = function () {
	var $accordion = this;
	this.tabs = $(this.tabSelector, this.mainSelector);

	this.tabs.each(
		function() {
			var $me = $(this);

			if ($accordion.isOpen($me)) {
				$accordion.lastTab = $me;
			}

			$me.hoverIntent(
				{
					interval: 200,
//					sensitivity: 7,
					over: function () {
						if ($accordion.isOpen($me)) {
							$accordion.closeTab($me);
						}
						else {
							$accordion.openTab($me);
						}

						return false;
					},
					out: function () {
						// do nothing on mouse out
					}
				}
			)
		}
	);
}

MiniAccordion.prototype.isOpen = function ($tab) {
	return $(':first', $tab).hasClass('active');
}

MiniAccordion.prototype.openTab = function ($tab, $change_last_tab) {
	if ((this.lastTab !== false) && ($tab[0] === this.lastTab[0])) {
		return ;
	}

	if ($change_last_tab === undefined || $change_last_tab === true) {
		this.lastTab = $tab;
	}

	var $me = this;

	$('a:first', $tab).addClass('active');

	var $close_tab = false;

	this.tabs.not($tab).each(
		function() {
			if ($me.isOpen( $(this) )) {
				$close_tab = $(this);
				return false;
			}
		}
	);

	if ($close_tab !== false) {
		this.closeTab($close_tab, false);

		this.syncroAnimation(
			$('div:first', $tab),
			$('div:first', $close_tab),
			{
				active: 0,
				autoHeight: true,
				complete: function () { }
			}
		);

		$('div:first', $tab).stop(true, true);
	}
	else {
		$('div:first', $tab).stop(true, true).slideDown();
	}
}

MiniAccordion.prototype.closeTab = function ($tab, $change_last_tab) {
	if ((this.lastTab !== false) && ($tab[0] === this.lastTab[0])) {
		return ;
	}

	if ($change_last_tab === undefined || $change_last_tab === true) {
		this.lastTab = $tab;
	}

	$('a:first', $tab).removeClass('active');

	if ($change_last_tab === undefined || $change_last_tab === true) {
		$('div:first', $tab).slideUp();
	}
	else {
//		$('div:first', $tab).slideUp(); // maybe on diffrent speed
	}
}

MiniAccordion.prototype.syncroAnimation = function($to_show, $to_hide, options) {

	options = $.extend({
		easing: "swing",
		duration: 600
	}, options);

	if ( !$to_hide.size() ) {
		$to_show.animate({height: "show"}, options);
		return;
	}
	if ( !$to_show.size() ) {
		$to_hide.animate({height: "hide"}, options);
		return;
	}
	var overflow = $to_show.css('overflow'),
		percentDone = 0,
		showProps = {},
		hideProps = {},
		fxAttrs = [ "height", "paddingTop", "paddingBottom" ],
		originalWidth;
	// fix width before calculating height of hidden element
	var s = $to_show;
	originalWidth = s[0].style.width;
	s.width( parseInt(s.parent().width(),10) - parseInt(s.css("paddingLeft"),10) - parseInt(s.css("paddingRight"),10) - (parseInt(s.css("borderLeftWidth"),10) || 0) - (parseInt(s.css("borderRightWidth"),10) || 0) );

	$.each(fxAttrs, function(i, prop) {
		hideProps[prop] = 'hide';

		var parts = ('' + $.css($to_show[0], prop)).match(/^([\d+-.]+)(.*)$/);
		showProps[prop] = {
			value: parts[1],
			unit: parts[2] || 'px'
		};
	});
	$to_show.css({ height: 0, overflow: 'hidden' }).show();
	$to_hide.filter(":hidden").each(options.complete).end().filter(":visible").animate(hideProps,{
		step: function(now, settings) {
			// only calculate the percent when animating height
			// IE gets very inconsistent results when animating elements
			// with small values, which is common for padding
			if (settings.prop == 'height') {
				percentDone = ( settings.end - settings.start === 0 ) ? 0 :
					(settings.now - settings.start) / (settings.end - settings.start);
			}

			$to_show[0].style[settings.prop] =
				(percentDone * showProps[settings.prop].value) + showProps[settings.prop].unit;
		},
		duration: options.duration,
		easing: 'swing',
		complete: function() {
			if ( !options.autoHeight ) {
				$to_show.css("height", "");
			}
			$to_show.css("width", originalWidth);
			$to_show.css({overflow: overflow});
			options.complete();
		}
	});
};

function show_other_insurance_types() {
	var $other_link = $('li.other', 'ul.sub-navigation');

	$other_link.click(
		function ($e, $quick) {
			if (!$quick || $quick === undefined) {
				$(this).fadeOut(
					function () {
						$('ul.other-insurance-types').slideDown();
					}
				);

			}
			else {
				$(this).hide();
				$('ul.other-insurance-types').show();
			}
		}
	);

	if ($("li[rel='current']", 'ul.other-insurance-types').length > 0) {
		// selected insurance type is among hidden
		$other_link.trigger('click', true);
	}
}

// banners

function switchBanner() {
	var activeBanner = $('.banner-link.current');
	var nextBanner = activeBanner.nextAll('.banner-link:first');
	if (nextBanner.length == 0) nextBanner = $('.banner-link:first').not(activeBanner);

	if (nextBanner.length > 0) {
		activeBanner.fadeOut('slow').removeClass('current');
		nextBanner.fadeIn('slow').addClass('current');
		clearTimeout(nextBannerTimeout);
		nextBannerTimeout = setTimeout(switchBanner, bannerTimeoutLength);
	}
}

var nextBannerTimeout;

function initFirstPageBanners() {
	var nextBannerTimeout;
	if (!bannerTimeoutLength) bannerTimeoutLength = 5;
	bannerTimeoutLength *= 1000;

	$(document).ready(function () {
		nextBannerTimeout = setTimeout(switchBanner, bannerTimeoutLength);

		$('.useful').mouseover(function () {
			clearTimeout(nextBannerTimeout);
			var activeBanner = $('.banner-link.current').show();
			$('.banner-link').not(activeBanner).hide();
		}).mouseout(function () {
			clearTimeout(nextBannerTimeout);
			nextBannerTimeout = setTimeout(switchBanner, bannerTimeoutLength);
		});

		$('.useful-navigation a').click(function (ev) {
			ev.preventDefault();
			var activeBanner = $('.banner-link.current');
			var bannerNum = $(this).attr('rel');
			var nextBanner = $('.banner-link[rel='+bannerNum+']').not(activeBanner);

			if (nextBanner.length > 0) {
				activeBanner.fadeOut().removeClass('current');
				nextBanner.fadeIn().addClass('current');
			}
		});
	});
}

new MiniAccordion('li.top-level-item', 'ul.sub-navigation');