$j(function(){
	$j('#Accordion').accordion();
	//hero.init({auto: true, timer: 3000});
	$j('.full-hero').cycle({
		fx: 'scrollHorz',	
		pause:  1,
	   // fx:      'fade',
	    speed:   300, 
		  timeout: 3000,
	    easing:  'easeInOutQuad',
			next:   '.full-hero-next', 
			prev:   '.full-hero-prev'
	});
});
hero = function(){
	var o = {};
	var animateRunning = 0;
	function init(options){
		o.widget = $j('#Hero');
		o.imagesList = $j('.slideshow-images img', o.widget);
		o.imagesTotal = (o.imagesList.size() - 1); //Array adjust
		o.current = 0;
		o.nextButton = $j('.next', o.widget);
		o.prevButton = $j('.prev', o.widget);
		//Run to options
		if(options.auto === true) {
			var timer = function(){
				if(options.timer) { return options.timer; } else { return false; }
			}();
			startScroll(timer);
		}
		events();
	}
	function startScroll(length){
		if (o.interval) { window.clearInterval(o.interval); }
		else {
			o.interval = window.setInterval(function(){ if (animateCheck()) { next(); } },length);
		}
	}
	function stopScroll(){
		//Stop auto scroll once  UI clicked
		if (o.interval) { window.clearInterval(o.interval); }
	}
	function events(){
		o.nextButton.click(function(){ stopScroll(); if (animateCheck()) { next(); } });
		o.prevButton.click(function(){ stopScroll(); if (animateCheck()) { prev(); } });
	}
	function animateCheck(f){
		//Do not run if any animations are currently running
		if (animateRunning > 0) {
			return false;
		} else {
			return true;
		}
	};
	function runAnimation(settings){
		effect();
		function effect(){
			animateRunning = 2;		
			o.imagesList.eq(o.current).animate({opacity: "hide"},500,function(){
				animateRunning--;
			});
			if (settings.next === true){
				//Going forwards/next
					if (o.current >= o.imagesTotal){
						//Loop back to beginning if at last image
						o.targetItem = 0;
						o.current = 0;
					} else {
						o.targetItem = o.current + 1;
						o.current++;
					}
			} else {
				//Going backwards/previous
				if (o.current == 0){
					//Loop from the end if at the start and going backwards
					o.current = o.imagesTotal;
					o.targetItem = o.current;
				} else {
					o.current--;
					o.targetItem = o.current;
				}
			}
			o.imagesList.eq(o.targetItem).animate({opacity: "show"},800,function(){
				animateRunning--;
			});
		}
	}
	function next(){
		if(o.current < o.imagesTotal) {
			runAnimation({loop: false, next: true});
		} else {
			runAnimation({loop: true, next: true});
		}
	}
	function prev(){
		if(o.current > 0) {
				runAnimation({loop: false, next: false});
			} else {
				runAnimation({loop: true, next: false});
		}
	}
	return { init:init, animateRunning: animateRunning }
}();
