jQuery.noConflict();

jQuery(document).ready(function($) {

function Slide(el) {
	this.el = $(el);
	
	this.images = $(el).find('img');
	this.el.find('img').first().detach().appendTo(this.el);
}

Slide.prototype = {
	show: function(timeTillTransition) {
		this.images.css({
			display: 'block',
			opacity: 1
		});
		
		this.el.css({
			display: 'block',
			opacity: 1
		});
		
		this.el.find('*').css('opacity', 1);
		
		var that = this;
		setTimeout(function() {
			$(that.images.get(0)).animate({
				opacity: 0
			}, Slide.imageTransitionDuration, function() {
				$(this).css('display', 'none');
			});
		}, timeTillTransition);
	},
	fadeIn: function() {
		$(this.images.get(1)).css({
			display: 'block',
			opacity: 1
		});
		
		$(this.images.get(0)).css({
			display: 'none'
		});
		
		this.el.css({
			display: 'block',
			opacity: 0
		});
		
		this.el.find('*').animate({
			opacity: 1
		}, Slide.transitionDuration);
	},
	fadeOut: function() {
		var that = this;
		this.el.find('*').animate({
			opacity: 0
		}, Slide.transitionDuration, function() {
			that.el.css('display', 'none');
		});
	}
};

function slide_arrows() {
	return $('#slide_arrows').find('a');
	if ($.browser.msie && $.browser.version == 8) {
		return $('#slide_arrows').find('a');
	}
	else {
		return $('#slide_arrows');
	}
}

function Carousel() {
	var that = this;

	// Navigation arrows
	this.arrows = $('#slide_arrows a').css('z-index', 1000);
	/*$('#slide_arrows').css({
		position: 'absolute',
		zIndex: 1000,
		top: 0,
		left: 0
	});*/
	$(this.arrows.get(0)).click(function() {
		that.showPreviousSlide();
		return false;
	});
	$(this.arrows.get(1)).click(function() {
		that.showNextSlide();
		return false;
	});
	
	// Navigation items
	this.nav = $('#slide_nav li');
	/*this.nav.each(function(i, nav) {
		$(nav).click(function() {
			that.showSlide(i);
			return false;
		});
	});*/
	
	// Slides
	this.slides = [];

	var that = this;
	$('#slides .slide').each(function(i, slideEl) {
		$(slideEl).css({
			display: 'none',
			position: 'absolute'
		});
		
		that.slides.push(new Slide(slideEl));
	});
	
	// Setup up carousel for first slide
	this.currentSlideIndex = 0;
	this.slides[this.currentSlideIndex].show(Carousel.secondImageTransitionTime);
	
	this._arrow(0, false);
	slide_arrows().css({
		opacity: 0
	});
	setTimeout(function() {
		slide_arrows().animate({
			opacity: 1
		}, Slide.imageTransitionDuration);
	}, Carousel.secondImageTransitionTime);
}

Carousel.prototype = {
	showSlide: function(i) {
		if (i == this.currentSlideIndex) return;
		
		clearTimeout(this.showArrowTimerID);
		
		/*if (this.currentSlideIndex != 0 && i == 0) {
			this._arrow(0, false);
		}
		else if (this.currentSlideIndex == 0 && i > 0) {
			this._arrow(0, true);
		}
		
		if (this.currentSlideIndex != this.slides.length - 1 && i == this.slides.length - 1) {
			this._arrow(1, false); 
		}
		else if (this.currentSlideIndex == this.slides.length - 1 && i < this.slides.length - 1) {
			this._arrow(1, true);
		}*/
	
		$(this.nav.get(this.currentSlideIndex)).find('a').removeClass('activesticky');
		$(this.nav.get(i)).find('a').addClass('activesticky');
	
		this._currentSlide().el.css('z-index', 1);
		this.slides[i].el.css('z-index', 0);
		
		this._currentSlide().fadeOut();
		
		var previousSlideIndex = this.currentSlideIndex;
		this.currentSlideIndex = i;
		this._currentSlide().show(Slide.transitionDuration + Carousel.secondImageTransitionTime);
		
		slide_arrows().animate({
			opacity: 0
		}, Slide.imageTransitionDuration);

		var that = this;
		this.showArrowTimerID = setTimeout(function() {
		
			if (previousSlideIndex != 0 && i == 0) {
				that._arrow(0, false);
			}
			else if (previousSlideIndex == 0 && i > 0) {
				that._arrow(0, true);
			}
			
			if (previousSlideIndex != that.slides.length - 1 && i == that.slides.length - 1) {
				that._arrow(1, false); 
			}
			else if (previousSlideIndex == that.slides.length - 1 && i < that.slides.length - 1) {
				that._arrow(1, true);
			}
		
			slide_arrows().animate({
				opacity: 1
			}, Slide.imageTransitionDuration);
		}, Slide.transitionDuration + Carousel.secondImageTransitionTime);
	},
	
	showNextSlide: function() {
		if (this.currentSlideIndex < this.slides.length - 1) {
			this.showSlide(this.currentSlideIndex + 1);
			return true;
		}
	},
	
	showPreviousSlide: function() {
		if (this.currentSlideIndex > 0) {
			this.showSlide(this.currentSlideIndex - 1);
		}
	},
	
	_currentSlide: function()  {
		return this.slides[this.currentSlideIndex];
	},
	
	_arrow: function(arrow, shown) {
		var that = this;
		
		$(this.arrows.get(arrow)).css('display', shown ? 'block' : 'none')
		
		/*$(this.arrows.get(arrow)).css('display', 'block').animate({
			opacity: shown ? 1 : 0
		}, Carousel.arrowTransitionDuration, function() {
			if (!shown) {
				$(that.arrows.get(arrow)).css({
					display: 'none'
				});
			}
		});*/
	},
	
	startSlideshow: function() {
		var that = this;
		setTimeout(function() {
			if (that.showNextSlide()) {
				that.slideshowTimer = setInterval(function() {
					if (!that.showNextSlide()) {
						clearInterval(that.slideshowTimer);
					}
				}, Slide.transitionDuration + Carousel.secondImageTransitionTime + Slide.imageTransitionDuration + Carousel.slideDuration);
			}
		}, Carousel.secondImageTransitionTime + Slide.imageTransitionDuration + Carousel.slideDuration);
	}
};

Carousel.secondImageTransitionTime = 2000;
Slide.imageTransitionDuration = 2000;
Slide.transitionDuration = 4000;

Carousel.slideDuration = 5000;

carousel = new Carousel();
carousel.startSlideshow();

});
