$(window).load(function () {
	var currentSlide = 1;
	var totalSlides = $("ul.imageList").children("li").length;
	fixOffset();
	//fixListOffset();
	$("div.pageButtons div a").click(function () {
		if ($(this).attr("class") == "prev") {
			if (currentSlide == 1) { currentSlide = totalSlides } else { currentSlide-- }
		}
		if ($(this).attr("class") == "next") {
			if (currentSlide == totalSlides) { currentSlide = 1 } else { currentSlide++ }
		}
		$("div.imageContainer img").fadeOut(300);
		fixOffset();
		$("div.imageContainer img.slide" + currentSlide).fadeIn(700);
	});

	$("ul.imageList li a").each(function () {
		if ($(this).children("img").width() < 116) {
			var offset = (116 - $(this).children("img").width()) / 2;
			$(this).children("img").css("margin-left", offset + "px");
		}
		$(this).parent("li").height(83).css("padding", "6px 0px 0px 6px");
	});

	function fixOffset() {
		if ($("div.imageContainer img.slide" + currentSlide).width() < 605) {
			var offset = (611 - $("div.imageContainer img.slide" + currentSlide).width()) / 2;
			$("div.imageContainer img.slide" + currentSlide).css("left", offset + "px");
		}
	}

	$("ul.siteNav>li").hover(function () {
		//alert($(this).children("div").children("ul").height())
		$(this).addClass("hover");
		$(this).children("div.subContainer").width($(this).outerWidth() + 1);
		$(this).children("div.subContainer").children("ul").width($(this).outerWidth());
		$("div.subContainer div.shadowBgLeft, div.subContainer div.shadowBgRight")
			.height($(this).children("div.subContainer").children("ul").height() + 7);
		$(this).children("div.subContainer").animate({
			height: $(this).children("div.subContainer").children("ul").height() + 11
		}, 200);
	},
	function () {
		$(this).removeClass("hover");
		$(this).children("div.subContainer").animate({
			height: 0
		}, 200);
	});

	//IMAGE SCROLLER
	$("div.imageContainer img:first").show();
	var position = 0;
	var currentImage = 0;
	var totalImages = $("ul.imageList").children("li").length - 4;
	bindClick();
	function bindClick() {
		$("div.imageListContainer a").unbind("click");
		$("div.imageListContainer a").click(function () {
			if ($(this).children("img").length > 0) {
				$("div.imageContainer img").fadeOut(300);
				fixOffset();
				$("div.imageContainer img.slide" + $(this).attr("rel")).fadeIn(700);
				currentSlide = $(this).attr("rel");
			}
			p = $("ul.imageList").position();
			if ($(this).attr("class") == 'down') {
				if (currentImage < totalImages) {
					$("ul.imageList").animate({
						top: p.top - 89
					}, 100, function () { bindClick() });
					currentImage++;
				}
			} else if ($(this).attr("class") == 'up') {
				if (currentImage > 0) {
					$("ul.imageList").animate({
						top: p.top + 89
					}, 100, function () { bindClick() });
					currentImage--;
				}
			}
		});
	}
	//ROTATER
	$('div.rotater').jshowoff({
		links: false,
		controls: false,
		effect: 'none',
		cssClass: 'basicFeatures',
		hoverPause: false,
		speed: 5000
	});

	//FANCYBOX BINDINGS
	$("ul.pressReleases li a").fancybox({ autoDimensions: false, width: 500, height: 475 });
	$("div.videoGrid div a, a.fbVideo").fancybox({ type: 'iframe', height: 260, width: 340, scrollbars: false });
	$("a.iconPreview").fancybox();
});
