// lets not wait for document.ready

var xPositions = {
	"primary": {
		"normal": 0,
		"hover": -240,
		"open": -480,
		"openHover": -960,
		"selected": -721
	},
	"secondary": {
		"normal": 0,
		"hover": -240,
		"open": 0,
		"selected": -480
	},
	"tertiary": {
		"normal": 0,
		"hover": -240,
		"open": 0,
		"selected": -480
	}
};

// show all the parents uls
$('.left-side li.selected').parents('ul').css('display', 'block');

// add selected to the li of parents
$('.left-side li.selected').parents('li').each(function () {
	"use strict";

	$(this).addClass('open');
});

$('ul.primary > li.open > a').each(function () {
	"use strict";

	var bPos = $(this).css('backgroundPosition');
	bPos = bPos.split(" ");
	$(this).css("backgroundPosition", xPositions.primary.open + "px " + bPos[1]);
});

$(function () {
	"use strict";

	// Primary Navigation Behaviors
	$('.patient-nav .primary > li a').hover(function () {
		var bPos = $(this).css('backgroundPosition').split(" ");

		if ($(this).parent().hasClass("selected") || $(this).parent().hasClass("open")) {
			return false;
		}

		$(this).css("backgroundPosition", xPositions.primary.hover + "px " + bPos[1]);
	}, function () {
		var bPos = $(this).css('backgroundPosition').split(" ");

		if ($(this).parent().hasClass("selected") || $(this).parent().hasClass("open")) {
			return false;
		}

		$(this).css("backgroundPosition", xPositions.primary.normal + "px " + bPos[1]);
	});

	// Primary Navigation Behaviors
	$('.patient-nav .primary > li.open > a').hover(function () {
		var bPos = $(this).css('backgroundPosition').split(" ");

		if ($(this).parent().hasClass("selected")) {
			return false;
		}

		$(this).css("backgroundPosition", xPositions.primary.openHover + "px " + bPos[1]);
	}, function () {
		var bPos = $(this).css('backgroundPosition').split(" ");

		if ($(this).parent().hasClass("selected")) {
			return false;
		}

		$(this).css("backgroundPosition", xPositions.primary.open + "px " + bPos[1]);
	});

	// Secondary Navigation Behaviors
	$('.patient-nav ul.secondary > li a').hover(function () {
		var bPos = $(this).css('backgroundPosition').split(" ");

		if ($(this).parent().hasClass("selected") || $(this).parent().hasClass("open")) {
			return false;
		}

		$(this).css("backgroundPosition", xPositions.secondary.hover + "px " + bPos[1]);
	}, function () {
		var bPos = $(this).css('backgroundPosition').split(" ");

		if ($(this).parent().hasClass("selected") || $(this).parent().hasClass("open")) {
			return false;
		}

		$(this).css("backgroundPosition", xPositions.secondary.normal + "px " + bPos[1]);
	});

	// Tertiary Navigation Behaviors
	$('.patient-nav ul.tertiary > li a').hover(function () {
		var bPos = $(this).css('backgroundPosition').split(" ");

		if ($(this).parent().hasClass("selected") || $(this).parent().hasClass("open")) {
			return false;
		}

		$(this).css("backgroundPosition", xPositions.tertiary.hover + "px " + bPos[1]);
	}, function () {
		var bPos = $(this).css('backgroundPosition').split(" ");

		if ($(this).parent().hasClass("selected") || $(this).parent().hasClass("open")) {
			return false;
		}

		$(this).css("backgroundPosition", xPositions.tertiary.normal + "px " + bPos[1]);
	});

	// Slider Functionality
	(function () {
		var eq;
		var timer;
		var slides = $('#rotating-banner ul li').length;
		var index = 1;
		var autoSlide = 10000;

		$('#rotating-banner ul li').hide();
		$('#rotating-banner ul li:first-child').show();

		$('#rotating-banner .prev').click(function (e) {
			e.preventDefault();

			clearTimeout(timer);

			if (index > 1) {
				index--;
			} else {
				index = slides;
			}

			eq = index - 1;

			$('#rotating-banner ul li').stop(false, true).hide();
			$('#rotating-banner ul li:eq(' + eq + ')').stop(false, true).show();

			timer = setTimeout(function () {
				$('#rotating-banner .next').trigger('click');
			}, autoSlide);
		});

		$('#rotating-banner .next').click(function (e) {
			e.preventDefault();

			clearTimeout(timer);

			if (index < slides) {
				index++;
			} else {
				index = 1;
			}

			eq = index - 1;

			$('#rotating-banner ul li').stop(false, true).hide();
			$('#rotating-banner ul li:eq(' + eq + ')').stop(false, true).show();

			timer = setTimeout(function () {
				$('#rotating-banner .next').trigger('click');
			}, autoSlide);
		});

		timer = setTimeout(function () {
			$('#rotating-banner .next').trigger('click');
		}, 4000);
	}());

	$('#professional-tout, #for-hcps').click(function (e) {
		e.preventDefault();
		var answer = confirm("The information provided in this section is intended expressly for health care professionals in the United States. Click 'OK' to enter if you are a US health care professional");

		if (answer) {
			window.location = this.href;
		} else {
			return false;
		}
	});
});
