var myCarousel;
var myCarouselSmall;
var counter = 1;
var playswitch = 0;
var timer;

glow.ready(function() {
myCarousel = new glow.ui.CarouselPane('#homepageleader', {spotlight: 1, loop: true, duration: 0.8, page: 1});
mySecondCarousel = new glow.ui.CarouselPane('#homepagetext', {spotlight: 1, loop: true, duration: 0.8, page: 1, vertical: true});
timer = setTimeout("shiftCarousel()",12000);
myCarousel.on('afterMove', function(e) {
	removeClass(document.getElementById(counter),"selected");
	counter++;
	if(counter > 3) { counter = 1; }
	addClass(document.getElementById(counter),"selected");
	setTimeout("shiftCarousel()",12000);
});
	glow('.jw_overlay').on('click', function(event) { var newimg;
			switch(playswitch) {
				case 0:
				newimg = 'jw_playimg.png';
				clearTimeout(timer);
				playswitch = 1;
				break;
				case 1:
				newimg = 'jw_pauseimg.png';
				timer = setTimeout("shiftCarousel()",12000);
				playswitch = 0;
				break;
				default:
				newimg = 'jw_pauseimg.png';
				timer = setTimeout("shiftCarousel()",12000);
				playswitch = 0;
				break;
			}
			glow(".jw_overlayinner").css("background","url(/images/"+newimg+") no-repeat center center scroll");
			});
});

function shiftCarousel() {
	myCarousel.next();
	mySecondCarousel.next();
}

function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if(!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
	if(hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}
