﻿//CALLAWAY SLIDER

function callawaySlider(DOMObject) {
	var activeNode = jQuery(DOMObject);
	var activeSlide = null;
	var activeLoader = null;
	var slideTimer = null;
	var pause = 7000;
	var nodeCount = parseInt(activeNode.children().length);
	var containerWidth = activeNode.outerWidth();

	function getHeadline(slide) {
		return (jQuery(slide).find(":header").each(function () {
			jQuery(this).text(jQuery(this).text() + " ");
		}).text()).replace("  ", " ");
	}

	function getRealWidth(DOMObject) {
		return jQuery(DOMObject).outerWidth();
	}

	function getRealHeight(DOMObject) {
		return jQuery(DOMObject).outerHeight();
	}

	function getHorPadding(DOMObject) {
		return jQuery(DOMObject).outerWidth() - jQuery(DOMObject).width();
	}

	function formatSlideRequest(slideRequest) {
		if ((slideRequest != "") && (slideRequest != null) && (slideRequest <= nodeCount) && (slideRequest > 0)) return slideRequest - 1;
		else if ((nodeCount != 1)) return parseInt(activeNode.children(":not(.slide-controller):last").prev().attr("class").split("slide")[1]);
		else return 0;
	}

	function animateIn(moveLeft) {
		activeSlide = activeNode.css({
			visibility: "visible"
		}).children(":not(.slide-controller):last");
		if (!moveLeft && moveLeft != null) activeSlide.css({
			left: "-" + containerWidth + "px"
		});
		else activeSlide.css({
			left: containerWidth + "px"
		});

		var slideNum = parseInt(activeSlide.attr("class").split("slide")[1]);
		activeNode.animate({
			height: activeSlide.find(".slide").outerHeight() + "px"
		}, (pause * 0.1), "swing");
		activeNode.find(".slide-controller .controller-box .active").removeClass("active");
		var slideBtn = activeNode.find(".slide-controller-box-btn-" + slideNum + ":first").addClass("active").unbind("click");
		activeLoader = slideBtn.find(".loader:first").css({
			display: "block"
		});
		activeSlide.animate({
			left: "0"
		}, (pause * 0.09), function () {
			slideBtn.bind("click", function () {
				clickRequest(slideNum + 1);
			});
		});
	}

	function animateOut(slideRequest) {
		slideRequest = formatSlideRequest(slideRequest);
		var currentSlide = activeNode.children(":not(.slide-controller):last");
		var visibleSlide = currentSlide;
		var visibleSlideNum = parseInt(visibleSlide.attr("class").split("slide")[1]);
		var visibleSlideBtn = activeNode.find(".slide-controller-box-btn-" + visibleSlideNum + ":first");
		while (slideRequest != parseInt(currentSlide.attr("class").split("slide")[1])) {
			currentSlide.prependTo(activeNode);
			currentSlide = activeNode.children(":not(.slide-controller):last");
		}
		if (slideRequest != visibleSlideNum) {
			activeLoader.css({
				display: "none"
			});
			if (visibleSlide.css("visibilty") != "hidden") {
				var direction = ((slideRequest > visibleSlideNum) || (visibleSlideNum == (nodeCount - 1) && slideRequest == 0)) ? "-" : "";
				visibleSlideBtn.unbind("click");
				visibleSlide.animate({
					left: direction + activeNode.outerWidth() + "px"
				}, (pause * 0.1), function () {
					visibleSlideBtn.bind("click", function () {
						clickRequest(visibleSlideNum + 1);
					});
				});
				animateIn(direction == "-" ? true : false);
			}
		}
	}

	function animatePause() {
		clearInterval(slideTimer);
		activeLoader.stop();
	}

	function animateResume() {
		activeLoader.stop();
		//animateIn();
		clearInterval(slideTimer);
		slideTimer = setInterval(function () {
			animateOut(0);
		}, pause);
	}

	function clickRequest(slideRequest) {
		activeNode.unbind("mouseenter").unbind("mouseleave").bind("mouseleave", function () {
			activeNode.unbind("mouseleave").bind("mouseenter", function () {
				animatePause();
			}).bind("mouseleave", function () {
				animateResume();
			});
		});
		activeLoader.stop();
		animateOut(slideRequest);
		clearInterval(slideTimer);
		slideTimer = setInterval(function () {
			animateOut(0);
		}, pause);
	}
	this.startSlideFade = function (slideRequest) {
		if (slideRequest) slideRequest = formatSlideRequest(slideRequest);
		else slideRequest = 0;
		var childrenNum = activeNode.children(":not(.slide-controller)").length;
		if (childrenNum > 0) {
			animateIn();
			if (childrenNum < 2) activeNode.find(".slide-controller").css({
				display: "none"
			});
			else {
				activeNode.bind("mouseenter", function () {
					animatePause();
				}).bind("mouseleave", function () {
					animateResume();
				});
				slideTimer = setInterval(function () {
					animateOut(0);
				}, pause);
			}
		}
	}

	if (activeNode.children().length) {
		var parentNode = activeNode.wrap("<div class=\"" + jQuery(DOMObject).attr('class') + "\" style=\"display:block;\"></div>").parent().append("<div class=\"slide-controller\"> <div class=\"balloon-box\"></div> <div class=\"controller-box\"></div> </div>"); //create parent and controller
		activeNode.children().each(function (iterator) {
			jQuery(this).addClass("slide");
			jQuery(this).prependTo(parentNode).wrap("<div class=\"slide" + iterator + "\" style=\"position:absolute;left:" + parentNode.outerWidth() + "px;\"></div>"); //add child nodes with slide wrapper, no unblocked content, to New parent container
			var activeBalloon = jQuery("<div class=\"balloon-frame-" + iterator + " balloon-frame\" style=\"display:none;\"><div class=\"slide-balloon\">" + getHeadline(this) + "<div class=\"endCap\"></div></div></div>").appendTo(parentNode.find(".balloon-box:first")); //add controller balloon
			jQuery("<div class=\"slide-controller-box-btn-" + iterator + " slide-controller-box-btn\"><div class=\"loader\"></div><p>" + (iterator + 1) + "</p></div>").appendTo(parentNode.find(".controller-box:first")).hover(function () {
				jQuery(activeBalloon).css({
					display: "block"
				});
			}, function () {
				jQuery(activeBalloon).css({
					display: "none"
				});
			}).bind("click", function () {
				clickRequest(iterator + 1);
			});
		});
		var controllerBoxWidth = getRealWidth(parentNode.find(".controller-box:first").append("<div class=\"endCap\"></div>")); //close controller-box & retrieve its width
		parentNode.find(".balloon-frame").each(function () {
			if (jQuery(this).css({
				display: "block"
			}).width() < controllerBoxWidth) {
				var innerBalloon = jQuery(this).find(".slide-balloon");
				innerBalloon.css("width", (controllerBoxWidth - getHorPadding(innerBalloon)) + "px");
			}
			jQuery(this).css({
				display: "none"
			});
		});
		activeNode.remove(); //remove original container from DOM
		parentNode.find(".slide-controller").css({
			marginLeft: "-" + (Math.ceil(parentNode.find(".slide-controller").outerWidth() / 2) + "px")
		});
		activeNode = jQuery(parentNode);
	}
}

//Iterate activition through all slider class
jQuery(document).ready(function () {
	var sliderArray = jQuery(".callaway-slider").css({
		visibility: "hidden"
	}).each(function () {
		var slider = new callawaySlider(this);
		jQuery(window).load(function () {
			slider.startSlideFade();
		});
	});
	Cufon.replace(".callaway-slider h3", {
		letterSpacing: "-2px"
	})(".callaway-slider h4", {
		letterSpacing: "-1px"
	});
});

