﻿/**
* @author Jacky Lau
*/

$(function() {

	//cufon
	Cufon.replace(".cufon");
	Cufon.replace(".cufonhover", { hover: true });

	//resize body height
	$(".body").height($(".content").height() + 169);
	$(".content").resize(function(e) {
		$(".body").height(130 + $(this).height());
		$("#footer").height($(window).height() - $(".body").height());
	});

	//resize footer height
	$("#footer").height($(window).height() - $(".body").height());
	$(window).resize(function() {
		$("#footer").height($(window).height() - $(".body").height());
	});

	//header navigation initialization
	$(".secondary-nav").each(function() {
		$(this).find("li:first a").addClass("first");
		$(this).find("li:last a").addClass("last");
	});
	$(".primary-nav li").hover(
			function() {
				$(this).find("a:first").addClass("active");
				$("ul.secondary-nav", this).fadeIn("fast");
			},
			function() {
				$(this).find("a:first").removeClass("active");
				$("ul.secondary-nav", this).fadeOut("fast");
			}
		);

	//promo tiles hover animation initial styling
	$(".promo-large-link, .video-large-link").find(".promo-large-title-description").stop(true, true).animate({
		top: '146px'
	}, 0);
	$(".promo-small-link").find(".promo-small-title-description").stop(true, true).animate({
		top: '77px'
	}, 0);
	//promo tiles hover animation binding
	$(".promo-large-link, .video-large-link").hover(
		function() {
	    var textHeight = 175 - $(this).find(".promo-large-title-description, .video-large-title-description").height();
	        $(this).find(".promo-large-title-description, .video-large-title-description").stop(true, true).animate({
				top: textHeight + 'px'
			}, 250);
		},
		function() {
			$(this).find(".promo-large-title-description, .video-large-title-description").stop(true, true).animate({
				top: '146px'
			}, 200);

		}
	);
	$(".promo-small-link").hover(
		function() {
			var textHeight = 106 - $(this).find(".promo-small-title-description").outerHeight();
			$(this).find(".promo-small-title-description").stop(true, true).animate({
				top: textHeight + 'px'
			}, 250);
		},
		function() {
			$(this).find(".promo-small-title-description").stop(true, true).animate({
				top: '77px'
			}, 200);

		}
	);

	//image gallery hover animation initial styling
	$(".image-gallery-link").find(".image-gallery-title-description").stop(true, true).animate({
		top: '77px'
	}, 0);
	//image gallery hover animation binding
	$(".image-gallery-link").hover(
		function() {
			var textHeight = 106 - $(this).find(".image-gallery-title-description").outerHeight();
			$(this).find(".image-gallery-title-description").stop(true, true).animate({
				top: textHeight + 'px'
			}, 250);
		},
		function() {
			$(this).find(".image-gallery-title-description").stop(true, true).animate({
				top: '77px'
			}, 200);

		}
	);

	//Animate opacity hovering promo tiles / image gallery
	$(".image-gallery-container, .promo-small-container, .promo-large-container, .video-large-container").hover(function() {
		$(this).find(".image-gallery-image, .promo-small-image, .promo-large-image").stop().animate({ "opacity": 0.6 });
	}, function() {
		$(this).find(".image-gallery-image, .promo-small-image, .promo-large-image").stop().animate({ "opacity": 1 });
	});

	//IE fix for anchor click promo tiles and image galleries
	$(".promo-large-link").click(function() {
		window.location.replace($(this).find("a").attr("href"));
	});
	$(".promo-small-link").click(function() {
		window.location.replace($(this).find("a").attr("href"));
	});
	$(".image-gallery-link").click(function() {
		$(this).find("a").click();
	});

	//resize fancybox image title and description width
	$(".fancyboxElement").each(function(index) {
		var imgWidth;
		$(this).find(".gallery-image").load(function() {
			// Remove attributes in case img-element has set width and height
			$(this).removeAttr("width")
			   .removeAttr("height")
			   .css({ width: "", height: "" }); // Remove css dimensions as well
			imgWidth = this.width;
		});
		$(this).find(".gallery-title").width(imgWidth);
		$(this).find(".gallery-description").width(imgWidth);
	});
});
