﻿var JqvSlide3AnyShown = false;
var paneWidth = 0;

$(function() {
	var timer;
	$('#JqvSlide3 div a.JqvSlide3Next').hover(function() {
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		timer = setInterval(function() {
			fourNext();
		}, 1000);
	},
    function() //mouseout
    {
    	if (timer) {
    		clearInterval(timer);
    		timer = null;
    	}
    });
	//a click event cancels the scroll
	$('#JqvSlide3 div a.JqvSlide3Next').click(function() {
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		fourNext();
	});

	/* next hover for the slide3*/
	$('#JqvSlide3 div a.JqvSlide3Prev').hover(function() {
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		timer = setInterval(function() {
			fourPrev();
		}, 1000);
	},
    function() //mouseout
    {
    	if (timer) {
    		clearInterval(timer);
    		timer = null;
    	}
    });
	//a click event cancels the scroll
	$('#JqvSlide3 div a.JqvSlide3Prev').click(function() {
		if (timer) {
			clearInterval(timer);
			timer = null;
		}
		fourPrev();
	});
	
	// The width has to be fixed to prevent wrapping ...
	paneWidth = $('#JqvSlide3Container div.JqvSlide3DivShow').length * $('#JqvSlide3Container div.JqvSlide3DivShow:first').width();
	$('#JqvSlide3Pane').css('width', paneWidth);
	
});

function fourNext() {
    var jqvWindow = $('#JqvSlide3Container');
    var jqvPane = $('#JqvSlide3Pane');
    var maxSlide = $('#JqvSlide3Container div.JqvSlide3DivShow').length
        * $('#JqvSlide3Container div.JqvSlide3DivShow:first').width()
        + getLeft(jqvPane)
        - $('#JqvSlide3Container').width();
    var s = jqvWindow.width();
    if (s > maxSlide) s = maxSlide;
    if (s > 1) {
        s = '-=' + s;
        jqvPane.animate({ 'left' : s }, 400);
    }
    else {
        jqvPane.animate({ 'left' : '0' }, 400);
	}
}

function fourPrev() {
    var jqvWindow = $('#JqvSlide3Container');
    var jqvPane = $('#JqvSlide3Pane');
    var maxSlide = -getLeft(jqvPane);
    var s = jqvWindow.width();
    if (s > maxSlide) s = maxSlide;
    if (s > 1) {
		s = '+=' + s;
		jqvPane.animate({ 'left': s }, 400);
    }
    else {
    	jqvPane.animate({ 'left': jqvWindow.width() - paneWidth }, 400);
    }
}

/*trigger funtion for the slide3
requires special positioning because the elements are relative/absolute*/
//runs on page load
$(function() { setUpTrig(); });

function setUpTrig() {
	$('#JqvSlide3Container div div.JqvSlide3PopUpTrigger').each(function() {

		// 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 = '#JqvPop_' + trigger.attr('content');
		var popUp = $(popId).first();
		popUp.css('opacity', 0);
		var popupClose = $(popId + ' div.JqvSlide3PopUpClose'); //popUp.find(' > div.JqvSlide3PopUpClose');

		popupClose.click(function() {
			shown = false;
			popUp.css('display', 'none');
		});

		$([trigger.get(0)]).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 || JqvSlide3AnyShown) {
				return;
			}
			else {
				beingShown = true;
				// reset position of popup box
				popUp.css({
					top: (trigger.parent().height() / 2), //because this is pushed to the bottom of the table...
					left: trigger.parent().position().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;
                	JqvSlide3AnyShown = true;
                });
			}
		}, function() {

			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

		});

		$([popUp.get(0)]).hover(function() {
			//goAlert(); //debug
		//alert(popUp.get(0).attr('id') + ':' + popUp.get(0).attr('class'));
			
			// 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.position().top + 20,
					left: trigger.position().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;
                	JqvSlide3AnyShown = 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');
					JqvSlide3AnyShown = false;
				});
			}, hideDelay);
		});

	});
}

function getLeft(element) {
	var cssLeft = element.css('left');
	if (cssLeft == 'auto') return 0;
	else return parseInt(cssLeft, 10);
}

