// Custom scripts added by Tuljo in May 2011.

// Image rotator on the front page http://globaldatapoint.com/
function rotateImages(){
	// time between image rotate
	var delay = 3000;
	// for each list item in
	// the rotator ul, generate
	// show a random list item
	$('#rotator > li').each(function(){
		// save images in an array
		var $imgArr = $(this).children();
		// show a random image
		$imgArr.eq(Math.floor(Math.random()*$imgArr.length)).show();
	});
	// run the changeImage function after every (delay) miliseconds
	setInterval(function(){
		changeImage();
	},delay);
	function changeImage(){
		// save list items in an array
		var $liArr = $('#rotator > li');
		// select a random list item
		var $currLi = $liArr.eq(Math.floor(Math.random()*$liArr.length));
		// get the currently visible image
		var $currImg = $('.rotatorImage:visible', $currLi);
		if ($currImg.next().length == 1) {
			var $next = $currImg.next();
		} else {
			var $next = $('.rotatorImage:first', $currLi);
		}
		$currImg.fadeOut(2500);
		$next.fadeIn(2500);
	}
};

// Slider on http://globaldatapoint.com/index.php/what-we-do/event-coverage/
function toggleSlides(){
	$('.switchTrigger').click(function(){
		$(this).next().slideToggle();
		var sign=$(this).children(':first');
		sign.text(sign.text()=='\uff0b' ? '\uff0d' : '\uff0b');
	});
};

// Centering elements horisontally and vertically. See usage http://www.tuljo.com/articles/centering-horizontally-vertically-jquery
function centerHorizontally(){
	$('.centerHorizontally').each(function(){
		$(this).css('margin-left',($(this).parent().innerWidth()-$(this).outerWidth())/ 2 + 'px');
	});
};
function centerVertically(){
	$('.centerVertically').each(function(){
		$(this).css('margin-top',($(this).parent().innerHeight()-$(this).outerHeight())/ 2 + 'px');
	});
};
function centerBoth(){
	$('.centerBoth').each(function(){
		$(this).css('margin-left',($(this).parent().innerWidth()-$(this).outerWidth())/ 2 + 'px');
		$(this).css('margin-top',($(this).parent().innerHeight()-$(this).outerHeight())/ 2 + 'px');
	});
};

// Call functions when page is ready
$(function(){
	rotateImages();
	toggleSlides();
	centerHorizontally();
	centerVertically();
	centerBoth();
});

// Carousel
$(function($){
	$.fn.imageCarousel = function(options) {
		var busy = false, i=0,endReached = false;
		var opts = $.extend({}, $.fn.imageCarousel.defaults, options);
		this.each(function(){
			$this = $(this);
			$children = $(this).children();
			$firstchild = $children.eq(0);
			var itemWidth = $firstchild.outerWidth(true);//width of one image space
			var totWidth = parseInt($children.length) * itemWidth;
			$this.data("imageCarouselData", {'itemWidth':itemWidth});
			$('.imgContainer').css({width:totWidth+'px'});//total width of all items
		});
		$('.carouselControls').children().click(function(){
			if (busy) return false;
			busy = true;
			var carouselWidget = $(this).parent().prev();
			var featureContainer = carouselWidget.children().eq(0);
			var features = featureContainer.children();
			var curPos = featureContainer.offset().left - carouselWidget.offset().left //- carouselWidget.offsetParent().offset().left;
			var direction = this.className.indexOf('arouselControlLeft');
			var dat = featureContainer.data('imageCarouselData');
			var xMove = dat.itemWidth * opts.moveElements;
			var newX = parseInt(curPos) + (xMove * direction)
			if (direction===1 && curPos >=0){
				newX = -parseInt((featureContainer.outerWidth(true))- carouselWidget.outerWidth(true));
				endReached = true;
			}else{
				//check for end of the list/*
				if (direction == -1){
						if((parseInt(featureContainer.outerWidth(true)+newX )- carouselWidget.outerWidth(true))< 0){
						if (!endReached){
							endReached = true;
							newX = -parseInt((featureContainer.outerWidth(true))- carouselWidget.outerWidth(true));
						}else{
							endReached = false;
							newX=0;
						}
					}
				}else if (newX >=0){newX = 0}
			}
			featureContainer.animate({left: newX+'px'}, opts.speed,function(){busy=false;});
			return false;
		});
		if (opts.autoScroll){
			var lastArrow = $('.carouselControlRight').eq($('.carouselControlRight').length-1);
			setInterval(function(){lastArrow.click()},opts.speed+1000);
		};
		return this;
	};
	$.fn.imageCarousel.defaults = {
		visibleItems:4,
		moveElements:3,
		speed:2000,
		autoScroll:false
	};
});

$(function(){
	$('.imgContainer').imageCarousel({
		visibleItems:4,
		moveElements:1,
		speed:2000,
		autoScroll:true
		});
});

