
/** 
** Description: Base JavaScript
** Company:     Netcel - http://www.netcel.com
** Client:      New Client
** Author:      Dominic Laurence
**
-----------------------------------------------------------------------**/


/** Functions
-----------------------------------------------------------------------**/

var $j = jQuery.noConflict();

$j(document).ready(function() {

    $j('#hero').length ? $j('#hero').homeSlideShow() : null //slideshow functionaility for homepage hero

    $j('#mainNav').mainNavHover(); //hover functionality for main navigation

});

$j(function() {
    var pane = $j('.scroll-pane');
    pane.jScrollPane(
		{
		    showArrows: true
		}
	);
    var api = pane.data('jsp');

    $j('#but-scroll-to').bind(
		'click',
		function() {
		    // Note, there is also scrollToX and scrollToY methods if you only
		    // want to scroll in one dimension
		    api.scrollTo(parseInt($j('#toX').val()), parseInt($j('#toY').val()));
		    return false;
		}
	);

    $j('#but-scroll-by').bind(
		'click',
		function() {
		    // Note, there is also scrollByX and scrollByY methods if you only
		    // want to scroll in one dimension
		    api.scrollBy(parseInt($j('#byX').val()), parseInt($j('#byY').val()));
		    return false;
		}
	);
});

$j.fn.homeSlideShow = function() {

    var $slides = $j(this).find('.slides');
    var $control = $j('.control ol');
    var noOfImg = $slides.find('li').length;
    var $nextBtn = $j('.control .next');
    var $prevBtn = $j('.control .prev');
    var timer = {};

    var slideWidth = $slides.find('.slide').outerWidth('true');
    var noOfSlides = $slides.find('.slide').length;
    var sliderWidth = noOfSlides * slideWidth;
    var easing = 'easeInOutCirc';
    var nextSpeed = 800;
    var endToEndSpeed = 1500;
    var count = 0;
    var pos;

    //set the width of the slider
    $slides.css('width', sliderWidth);

    //remove the blank li in the control div (it is in there to initially validate the page)
    $j('.control ol li').remove();

    //add the control indicators 
    for (i = 0; i < noOfSlides; i++) {
        i == 0 ? $control.append('<li class="active">.</li>') : $control.append('<li>.</li>');

    }
    //handle click events

    $nextBtn.click(function() {

        //prevent the user from clicking whilst the slider is still animating

        if (!$slides.is(':animated')) {

            pos = $slides.position();

            if (pos.left == -(sliderWidth - slideWidth)) {

                return;

            } else {

                //clear the timer to prevent overlaps/conflicts
                $j.clearTimer(timer);

                $control.find('li:eq(' + count + ')').removeClass('active');
                count++;
                $control.find('li:eq(' + count + ')').addClass('active');

                $slides.animate({ left: '-=' + slideWidth + 'px' }, nextSpeed, easing, function() {
                    //when the animation is finished start the automation
                    $j(this).autoSlide($nextBtn);
                });

            }
        }
    });

    $prevBtn.click(function() {

        //prevent the user from clicking whilst the slider is still animating

        if (!$slides.is(':animated')) {

            pos = $slides.position();

            if (pos.left == 0) {

                return;

            } else {

                //clear the timer to prevent overlaps/conflicts
                $j.clearTimer(timer);

                $control.find('li:eq(' + count + ')').removeClass('active');
                count--;
                $control.find('li:eq(' + count + ')').addClass('active');

                $slides.animate({ left: '+=' + slideWidth + 'px' }, nextSpeed, easing, function() {
                    //when the animation is finished start the automation
                    $j(this).autoSlide($prevBtn);
                });
            }

        }
    });


    //trigger the next and prev clicks after every 5 seconds

    $j.fn.autoSlide = function(x) {

        timer = $j.timer(5000, function() {

            var $btn = x;

            //if it reaches the end of the slideshow, go back
            count == noOfSlides - 1 ? $btn = $prevBtn : null;

            //if it reaches the start of the slideshow, go forward
            count == 0 ? $btn = $nextBtn : null;

            $btn.trigger('click');

        });
    }

    //start the slider
    $j(this).autoSlide($nextBtn)

}

$j.fn.mainNavHover = function() {

    $j(this).find('li ul').css('left', '0').hide();

    var speedDown = 1000;
    var speedUp = 100;

    function slideDown() {
        $j(this).addClass('selected');
        $j(this).find('ul').delay(200).slideDown(speedDown, 'easeOutExpo');
    }
    function slideUp() {
        $j(this).find('ul').slideUp('fast', function() {
            $j(this).parent().removeClass('selected');
        });
    }

    var config = {
        sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)    
        interval: 90, // number = milliseconds for onMouseOver polling interval    
        over: slideDown, // function = onMouseOver callback (REQUIRED)    
        timeout: 0, // number = milliseconds delay before onMouseOut    
        out: slideUp // function = onMouseOut callback (REQUIRED)    
    };

    $j(this).find('ul li').hoverIntent(config);
    $j(this).find('ul li li').hoverIntent(null);
}
