﻿//CALLAWAY SLIDER

function callawayCarousel(DOMObject) {
	var activeNode = jQuery(DOMObject);
	var activeCarousel = activeNode.find(".carousel-content:first");
	var leftBtn = activeNode.parent().find(".left-control").mouseover(function () {
		jQuery(this).addClass("left-control-hover");
	}).mouseout(function () {
		jQuery(this).removeClass("left-control-hover");
	});
	var rightBtn = activeNode.parent().find(".right-control").mouseover(function () {
		jQuery(this).addClass("right-control-hover");
	}).mouseout(function () {
		jQuery(this).removeClass("right-control-hover");
	});
	var pause = 300;
	var slideCount = activeCarousel.children().length;
	//var containerWidth = activeNode.outerWidth();

	function animateIn(moveLeft) {
		pauseBtns();
		var firstChild = activeCarousel.children(":first");
		var lastChild = activeCarousel.children(":last");
		if (!moveLeft && moveLeft != null) lastChild.css({
			display: "none",
			marginLeft: "-" + lastChild.outerWidth() + "px"
		}).prependTo(activeCarousel).css({
			display: "block"
		}).animate({
			marginLeft: firstChild.css("margin-left")
		}, pause, "swing", function () {
			activateBtns()
		});
		else firstChild.animate({
			marginLeft: "-" + firstChild.outerWidth() + "px"
		}, pause, "swing", function () {
			jQuery(this).appendTo(activeCarousel).css({
				marginLeft: lastChild.css("margin-left")
			});
			activateBtns();
		});
	}

	function pauseBtns() {
		leftBtn.unbind("click");
		rightBtn.unbind("click");
	}

	function activateBtns() {
		leftBtn.bind("click", function () {
			animateIn(false);
		});
		rightBtn.bind("click", function () {
			animateIn(true);
		});
	}
	this.startSlideFade = function (slideRequest) {
		if (slideCount > 4) {
			activateBtns();
			activeNode.parent().find(".control").css({
				display: "block"
			});
		} else {
			activeNode.parent().find(".control").remove();
		}
	}

	if (slideCount) {
		var maxHeight = 0;
		activeCarousel.children(".slide-container").each(function () {
			var thisHeight = jQuery(this).find(".slide-border:first").height();
			if (thisHeight > maxHeight) maxHeight = thisHeight;
			jQuery(this).bind("mouseenter", function () {
				jQuery(this).removeClass("slide-container").addClass("slide-container-active");
			}).bind("mouseleave", function () {
				jQuery(this).removeClass("slide-container-active").addClass("slide-container");
			});

			if (jQuery(this).find(".cta").size() == 1) {
				var href = jQuery(this).find(".cta:first").attr("href");
				var loc = "";
				if (href.split("://").length == 2) loc = href;
				else if (href.charAt(0) == '/') loc = window.location.protocol + window.location.hostname + href;
				else if (href.charAt(0) == '#' || href.charAt(0) == '?') loc = window.location + href;
				else loc = window.location.protocol + window.location.host + window.location.pathname.split('/', window.location.pathname.split("/").length - 1).join("/") + '/' + href;
				jQuery(this).bind("click", function () {
					window.location = loc;
				}).css({
					cursor: "pointer"
				});
			}
		});
		activeCarousel.children(".slide-container").each(function () {
			jQuery(this).find(".slide-border:first").height(maxHeight);
		});
	}
}

//Iterate activition through all slider class
jQuery(document).ready(function () {
	var carouselArray = jQuery(".callaway-carousel").each(function () {
		var carousel = new callawayCarousel(this);
		jQuery(window).load(function () {
			carousel.startSlideFade();
		});
	});
});

