﻿var _JqvSlider2_AnyShown = false;

// JQuery Test
if (!$) alert("JQuery not loaded!");

$(function() {
	var timer;
	$(".JqvSlider2_Right").hoverIntent(
		function() { // over
			var thisSlider = $(this).closest(".JqvSlider2").first();
			if (timer) {
				clearInterval(timer);
				timer = null;
			}
			timer = setInterval(function() {
				goNextSlider2(thisSlider);
			}, 1000);
		},
		function() // out
		{
			if (timer) {
				clearInterval(timer);
				timer = null;
			}
		});
	$(".JqvSlider2_Right").click(function() {
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		goNextSlider2($(this).closest(".JqvSlider2").first());
	});
	$(".JqvSlider2_Left").hoverIntent(function() {
		var thisSlider = $(this).closest(".JqvSlider2").first();
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		timer = setInterval(function() {
			goPrevSlider2(thisSlider);
		}, 1000);
	},
    function() {
    	if (timer) {
    		clearInterval(timer);
    		timer = null;
    	}
    });
	$(".JqvSlider2_Left").click(function() {
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		goPrevSlider2($(this).closest(".JqvSlider2").first());
	});
	$(".JqvSlider2 *[display]").each(function() { $(this).css("display", $(this).attr("display")); });
	$(".JqvSlider2 *[width]").each(function() { $(this).css("width", $(this).attr("width") + "px"); });
	$(".JqvSlider2 *[align]").each(function() { $(this).css("text-align", $(this).attr("align")); });
	$(".JqvSlider2_Item img").css("max-height", "170px");
});

function goNextSlider2(caller) {
	var vpsWindow = caller.find(".JqvSlider2_Window").first();
	var vpsPane = vpsWindow.find(".JqvSlider2_Pane").first();
	var vpsItem = vpsPane.find(".JqvSlider2_Item");
	var maxSlide = vpsItem.length * vpsItem.width() + getLeftSlider2(vpsPane) - vpsWindow.width();
    var s = vpsWindow.width();
    if (s > maxSlide) s = maxSlide;
    if (s > 1) {
        s = "-=" + s;
        vpsPane.animate({ "left" : s }, 400);
    }
    else {
    	vpsPane.animate({ "left": "0" }, 400);
    }
    var buttonText = "Next Products";
    if (vpsItem.length * vpsItem.width() + getLeftSlider2(vpsPane) >= vpsWindow.width())
		buttonText = "Back to First Products";
    caller.find(".JqvSlider2_Right").attr("alt", buttonText);
}

function goPrevSlider2(sliderObject) {
    var vpsWindow = sliderObject.find(".JqvSlider2_Window");
    var vpsPane = sliderObject.find(".JqvSlider2_Pane");
    var maxSlide = -getLeftSlider2(vpsPane);
    var s = vpsWindow.width();
    if (s > maxSlide) s = maxSlide;
    if (s > 1) {
		s = "+=" + s;
		vpsPane.animate({ "left": s }, 400);
    }
    else {
    	vpsPane.animate({ "left": vpsWindow.width() - vpsPane.width() }, 400);
    }
}

/*trigger funtion for the slide3
requires special positioning because the elements are relative/absolute*/
//runs on page load
$(function() {
    showAllJqvSlider2();
});

function setUpTrigJqvSlider2() {
	$(".JqvSlider2_Trigger").each(function() {

		var thisSlider = $(this).parents(".JqvSlider2");

		// options
		var distance = 10;
		var time = 200;
		var hideDelay = 300;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = $($(this), this);
		var popId = "#JqvSlider2_Popup_" + trigger.attr("content");
		var popUp = thisSlider.find(popId);
		if (popUp == null) return false; // Prevent errors with suppressed popups
		popUp.css("opacity", 0);
		var popupClose = popUp.find(".JqvSlider2_Closer"); //popUp.find(" > div.JqvSlider2_Closer");

		popupClose.click(function() {
			shown = false;
			popUp.css("display", "none");
		});

		//$([trigger.get(0)]).hoverIntent(function() {
		trigger.hoverIntent(function() {

			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don"t trigger the animation again if we"re being shown, or already visible
			if (beingShown || shown || _JqvSlider2_AnyShown) {
				return;
			}
			else {
				beingShown = true;
				// reset position of popup box
				popUp.css({
					top: trigger.offset().top + (trigger.height() / 2),
					left: trigger.offset().left + (trigger.width() / 2),
					display: "block" // brings the popup back in to view
				})

				// (we"re using chaining on the popup) now animate it"s opacity and position
                .animate({
                	top: "-=" + distance + "px",
                	opacity: 1
                }, time, "swing", function() {
                	// once the animation is complete, set the tracker variables
                	beingShown = false;
                	shown = true;
                	_JqvSlider2_AnyShown = true;
                });
			}
		}, function() {

			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

		});

		$([popUp.get(0)]).hover(function() {

			// stops the hide event so the pop stays open while your over it
			if (hideDelayTimer) { clearTimeout(hideDelayTimer); }

			// don"t trigger the animation again if we"re being shown, or already visible
			if (beingShown || shown) {
				return;
			}
			else {
				beingShown = true;
				// reset position of popup box
				popUp.css({
					top: trigger.offset().top + 20,
					left: trigger.offset().left + trigger.width(),
					display: "block" // brings the popup back in to view
				})

				// (we"re using chaining on the popup) now animate it"s opacity and position
                .animate({
                	top: "-=" + distance + "px",
                	opacity: 1
                }, time, "swing", function() {
                	// once the animation is complete, set the tracker variables
                	beingShown = false;
                	shown = true;
                	_JqvSlider2_AnyShown = true;
                });
			}
		}, function() {

			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				popUp.animate({
					top: "-=" + distance + "px",
					opacity: 0
				}, time, "swing", function() {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn"t do the job)
					popUp.css("display", "none");
					_JqvSlider2_AnyShown = false;
				});
			}, hideDelay);
		});

	});
}

function getLeftSlider2(element) {
	var cssLeft = element.css("left");
	if (cssLeft == "auto") return 0; else return parseInt(cssLeft, 10);
}

function showAllJqvSlider2() {
    $(".JqvSlider2_Pane").each(function() { $(this).css("visibility", "visible"); });
}
