/*
Author: Marc Lloyd
*/

var delay = 5000; /* set the delay between slides */
var speed = 1000; /* set the speed of slide movement */

var numberOfLis;
var currentPosition
var offset;
var timerid;
var pagination;
var liwidth;

$(function(){
	
	
	/* COPY FIRST LI AND REPEAT AT END OF LIST */
	$('#scroller li:nth-child(1)').clone().insertAfter('#scroller li:last-child');
	
	
	/* GET NUMBER OF LIST ITEMS AND SET CURRENT POSITION TO 1 */
	numberOfLis = jQuery('#scroller li').length;
	currentPosition = 1;
	
	/*
	liwidth = $('#scroller li').width();
	*/
	
	liwidth = 976;
	
	
	/* ADD PAGINATION AND SET FIRST TO SELECTED*/
	for(i = 2;i < numberOfLis;i++)
	{
		$('ul#slider-pagination li:nth-child(1)').clone().insertAfter('ul#slider-pagination li:nth-child('+(i-1)+')');
	}
	$('ul#slider-pagination li:nth-child(1) img').attr('src', 'nav/selected.gif');
		
	
	/* AUTOMATIC LOOP */
	move = function(){
			
		if(currentPosition < numberOfLis){
			
			offset = '-' + (currentPosition * liwidth) + 'px';
			
			$('#scroller ul').animate({'left' : offset},speed, 'easeOutBack');
			currentPosition ++;
			
			if(currentPosition == numberOfLis){
				$('#slider-pagination li img').attr('src', 'nav/blank.gif');
				$('#slider-pagination li:nth-child(1) img').attr('src', 'nav/selected.gif');
			}else{
				$('#slider-pagination li img').attr('src', 'nav/blank.gif');
				$('#slider-pagination li:nth-child('+currentPosition+') img').attr('src', 'nav/selected.gif');
			};
			
		} else{
			clearInterval(timerid);
			try {
				$('#scroller ul').css('left', '0px');
			} catch(e) {
				
			}
			currentPosition = 1;
			// EY commented::
			move();
			timerid = setInterval('move()',delay);
		}	
		
	};
	
	if(numberOfLis > 0) {
		timerid = setInterval('move()',delay);
	}


	/* MOVE SLIDER USING PAGINATION */
	$('#slider-pagination li').click(function(){
		clearInterval(timerid);
		currentPosition = ($('#slider-pagination li').index(this)) + 1;
		offset = '-' + ((currentPosition - 1) * liwidth) + 'px';
		
		$('#scroller ul').animate({'left' : offset},speed, 'easeOutBack');
		
		$('#slider-pagination li img').attr('src', 'nav/blank.gif');
		$('#slider-pagination li:nth-child('+currentPosition+') img').attr('src', 'nav/selected.gif');
		
		timerid = setInterval('move()',delay);
	});

});



/* TECHNOLOGY PAGE */

$(function(){
	$('#technology-list li blockquote').addClass('hidden');
	
	$('#technology-list li').click(function(){
		$('#technology-list li blockquote').slideUp('100');
		$(this).find('blockquote').slideDown('100');
	});
});


/** FAST FACTS SLIDER **/

function switchFastFact() {
    var $active = jQuery('#fact-fader li.active');

    if ( $active.length == 0 ) $active = jQuery('#fact-fader li:last-child');

    var $next =  $active.next().length ? $active.next()
        : jQuery('#fact-fader li:nth-child(1)');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
jQuery(function() {
    setInterval( "switchFastFact()", 8000 );
});


/** TESTIMONIALS SLIDER **/

function switchTestimonial() {
    var $active = jQuery('#quote-fader li.active');

    if ( $active.length == 0 ) $active = jQuery('#quote-fader li:last-child');

    var $next =  $active.next().length ? $active.next()
        : jQuery('#quote-fader li:nth-child(1)');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
jQuery(function() {
    setInterval( "switchTestimonial()", 8000 );
});


/* CALL TO ACTION FORM */

$(function(){
	
	if(window.location.hash !='#sitewide-contact-form'){
		$('#sitewide-contact-form').addClass('hidden');
	};

	$('#cta').click(function(){
		$(this).find('span').toggleClass('clicktohide');
		$('#sitewide-contact-form').slideToggle(400);
		return false;
	});

	// EY changed to .live, and added removeClass('error') so error fields are made non-errors once re-focussed on.
	$('#sitewide-contact-form input.error, #sitewide-contact-form textarea.error').live('focus', function(){
		this.value = '';
		$(this).removeClass('error');
	});
	
});
