﻿/* previous hover for the slide3*/

var JqvSlide3AnyShown = false;
var rIx = 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();
    });

});

function fourNext() {
    var jqvWindow = $('#JqvSlide3Container');
    var jqvPane = $('#JqvSlide3Pane');
    var maxSlide = $('#JqvSlide3Container div.JqvSlide3DivShow').length
        * $('#JqvSlide3Container div.JqvSlide3DivShow:first').width()
        - jqvPane.css('marginLeft').replace('px', '').replace('-', '')
        - $('#JqvSlide3Container').width();
    var s = jqvWindow.width();
    if (s > maxSlide) s = maxSlide;
    if (s > 1) {
        s = '-=' + s;
        jqvPane.animate({ marginLeft: s }, 400);
    }
    else {
        jqvPane.animate({ marginLeft: '0' }, 400);
    }
}

function fourPrev() {
    var jqvWindow = $('#JqvSlide3Container');
    var jqvPane = $('#JqvSlide3Pane');
    var maxSlide = jqvPane.css('marginLeft').replace('px', '').replace('-', '');
    var r = $('#JqvSlide3Container div.JqvSlide3DivShow').length
        * $('#JqvSlide3Container div.JqvSlide3DivShow:first').width()
    var s = jqvWindow.width();
    if (s > maxSlide) s = maxSlide;
    if (s > 1) {
        s = '+=' + s;
        jqvPane.animate({ marginLeft: s }, 400);
    }
    else {
        jqvPane.animate({ marginLeft: (jqvWindow.width() - r) }, 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);
		});

	});
}

/**/
/* Old funtion would get one next at a time and wrap around ...
function goNext() { 
var current = $('#JqvSlide3Container div.JqvSlide3DivShow:last');

//back up 2 from curr and hide
var back2 = current.prev().prev();
if (back2.length == 1) {
back2.css('display', 'none');
back2.fadeOut(1).removeClass('JqvSlide3DivShow').addClass('JqvSlide3Div');
}

//if next is 1 (from length) use next, else get the first one
if (current.next().length == 1) {
var next = current.next();
next.fadeIn(1000).removeClass('JqvSlide3Div').addClass('JqvSlide3DivShow');

}
else {
//you have to append the first to the end of the list

var first = current.parent().children('div.JqvSlide3Div:first');
current.parent().children('div.JqvSlide3Div:first').remove();
current.parent().append(first);
var newLast = current.parent().children('div.JqvSlide3Div:last');
newLast.fadeIn(1000).removeClass('JqvSlide3Div').addClass('JqvSlide3DivShow');
setUpTrig();
}

}
*/

/* Old function would get on previous, or the last if BOF ...
function goPrev() {
var current = $('#JqvSlide3Container div.JqvSlide3DivShow:first');

//forward 2 and see if you need to hide
var for2 = current.next().next();
if (for2.length == 1) {
for2.css('display', 'none');
for2.fadeOut(1).removeClass('JqvSlide3DivShow').addClass('JqvSlide3Div');
}

if (current.prev().length == 1) {

var prev = current.prev();
prev.fadeIn(1000).removeClass('JqvSlide3Div').addClass('JqvSlide3DivShow');
}
else {
var last = current.parent().children('div.JqvSlide3Div:last');
current.parent().children('div.JqvSlide3Div:last').remove();
current.parent().prepend(last);
var newFirst = current.parent().children('div.JqvSlide3Div:first');
newFirst.fadeIn(1000).removeClass('JqvSlide3Div').addClass('JqvSlide3DivShow');
setUpTrig();
}
}
*/
