﻿var _VerideanProductSlider_AnyShown = false;

// JQuery Test
if (!$) alert("JQuery not loaded!");

// Hover-Intent
(function($) { $.fn.hoverIntent = function(f, g) { var cfg = { sensitivity: 7, interval: 300, timeout: 0 }; cfg = $.extend(cfg, g ? { over: f, out: g} : f); var cX, cY, pX, pY; var track = function(ev) { cX = ev.pageX; cY = ev.pageY; }; var compare = function(ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) { $(ob).unbind("mousemove", track); ob.hoverIntent_s = 1; return cfg.over.apply(ob, [ev]); } else { pX = cX; pY = cY; ob.hoverIntent_t = setTimeout(function() { compare(ev, ob); }, cfg.interval); } }; var delay = function(ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); ob.hoverIntent_s = 0; return cfg.out.apply(ob, [ev]); }; var handleHover = function(e) { var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; while (p && p != this) { try { p = p.parentNode; } catch (e) { p = this; } } if (p == this) { return false; } var ev = jQuery.extend({}, e); var ob = this; if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } if (e.type == "mouseover") { pX = ev.pageX; pY = ev.pageY; $(ob).bind("mousemove", track); if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout(function() { compare(ev, ob); }, cfg.interval); } } else { $(ob).unbind("mousemove", track); if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout(function() { delay(ev, ob); }, cfg.timeout); } } }; return this.mouseover(handleHover).mouseout(handleHover); }; })(jQuery);

$(function() {
	var timer;
	$(".VerideanProductSlider_Right").hoverIntent(
		function() { // over
			var thisSlider = $(this).closest(".VerideanProductSlider").first();
			if (timer) {
				clearInterval(timer);
				timer = null;
			}
			timer = setInterval(function() {
				goNext(thisSlider);
			}, 1000);
		},
		function() // out
		{
			if (timer) {
				clearInterval(timer);
				timer = null;
			}
		});
	$(".VerideanProductSlider_Right").click(function() {
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		goNext($(this).closest(".VerideanProductSlider").first());
	});
	$(".VerideanProductSlider_Left").hoverIntent(function() {
		var thisSlider = $(this).closest(".VerideanProductSlider").first();
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		timer = setInterval(function() {
			goPrev(thisSlider);
		}, 1000);
	},
    function() {
    	if (timer) {
    		clearInterval(timer);
    		timer = null;
    	}
    });
	$(".VerideanProductSlider_Left").click(function() {
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		goPrev($(this).closest(".VerideanProductSlider").first());
	});
	$(".VerideanProductSlider *[display]").each(function() { $(this).css("display", $(this).attr("display")); });
	$(".VerideanProductSlider *[width]").each(function() { $(this).css("width", $(this).attr("width") + "px"); });
	$(".VerideanProductSlider *[align]").each(function() { $(this).css("text-align", $(this).attr("align")); });
	$(".VerideanProductSlider_Item img").css("max-height", "170px");
});

function goNext(caller) {
	var vpsWindow = caller.find(".VerideanProductSlider_Window").first();
	var vpsPane = vpsWindow.find(".VerideanProductSlider_Pane").first();
	var vpsItem = vpsPane.find(".VerideanProductSlider_Item");
	var maxSlide = vpsItem.length * vpsItem.width() + getLeft(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() + getLeft(vpsPane) >= vpsWindow.width())
		buttonText = "Back to First Products";
    caller.find(".VerideanProductSlider_Right").attr("alt", buttonText);
}

function goPrev(sliderObject) {
    var vpsWindow = sliderObject.find(".VerideanProductSlider_Window");
    var vpsPane = sliderObject.find(".VerideanProductSlider_Pane");
    var maxSlide = -getLeft(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() { setUpTrig(); });

function setUpTrig() {
	$(".VerideanProductSlider_Trigger").each(function() {

		var thisSlider = $(this).parents(".VerideanProductSlider");

		// 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 = "#VerideanProductSlider_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(".VerideanProductSlider_Closer"); //popUp.find(" > div.VerideanProductSlider_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 || _VerideanProductSlider_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) - (popUp.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;
                	_VerideanProductSlider_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;
                	_VerideanProductSlider_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");
					_VerideanProductSlider_AnyShown = false;
				});
			}, hideDelay);
		});

	});
}

function assignClass(selector, className) {
	$(selector).addClass(className);
}

function getLeft(element) {
	var cssLeft = element.css("left");
	if (cssLeft == "auto") return 0; else return parseInt(cssLeft, 10);
}

function Announce(mySelf) {
	//return mySelf.context.tagName + ": " + mySelf.context.className + "." + mySelf.context.id;
	var s = "Empty";
	try{ 
		s = mySelf.get(0).tagName; 
	}
	catch (ex){
		// do nada
	}
	return s + ": " + mySelf.attr("class") + "." + mySelf.attr("id");
}
