jQuery.noConflict();
(function($) {
	function TMCarousel(opts) {
		this.frames = $('#frames .frame');
		this.navigation = $('#frame_nav li');
		this.duration = opts.duration;
		this.textDuration = opts.textDuration;
		this.navItemDuration = opts.navItemDuration;
		this.fadeFirstTextOnly = opts.fadeFirstTextOnly;
		
		var that = this;
		this.frames.each(function(i, frame) {
			var img = $(frame).find('img.main');
			var text = $(frame).find('img.text');
		
			$(frame).css({
				display: '',
				'background': 'transparent url("' + img.attr('src') + '") no-repeat scroll 0 0'
			});
			
			$(frame).find('img.main').css('visibility', 'hidden');
			
			text.css('margin-top', $(frame).height() - text.height());
			text.css('position', 'static');
			
			$(frame).css('display', 'none');
		});
		
		/*this.navigation.each(function(i, li) {
			$(li).click(function() {
				if (!that.transitioning) {
					that.selectFrame(i);
				}
				return false;
			});
			
			$(li).css('position', 'relative').find('a')
			.before($('<span class="-bg"/>').css({
				position: 'absolute',
				left: 0,
				top: 0,
				width: '100%',
				height: '100%',
				display: 'block',
				'background-image': $(li).find('a').css('background-image')
			}))
			.css({
				position: 'relative',
				'background-image': 'none'
			})
		});*/
		
		
		var x = 0;
		this.navigation.each(function(i, li) {
			$(li).click(function() {
				if (!that.transitioning) {
					that.selectFrame(i);
				}
				return false;
			})
			.css({
				top: 0,
				'left': x,
				'position': 'absolute'
			})
			.find('a')
				.before(
					$('<span class="-bg"/>').css({
						position: 'absolute',
						left: 0,
						top: 0,
						width: '100%',
						height: '100%',
						display: 'block',
						'background-image': $(li).find('a').css('background-image')
					})
				)
				.css({
					position: 'relative',
					'background-image': 'none'
				});
				
			x += li.offsetWidth;
		});
		
		if (opts.textDuration) {
			this.fadeText = true && !(jQuery.browser.msie && (jQuery.browser.version != '8.0'));
		}
	}
	
	TMCarousel.prototype = {
		selectFrame: function(i) {
			if (!this.frames.get(i)) return;
			
			var that = this;
			// First fade out the current frame
			if (typeof this.currentFrame != 'undefined') {
				this._transitionElements(this.frames.get(this.currentFrame), true).animate({opacity:0}, {
					duration: this.duration,
					complete: function() {
						$(that.frames.get(previousFrame)).css('display', 'none');
					}
				});
				
				$(this.navigation.get(this.currentFrame)).find('.-bg').animate({opacity:1}, this.navItemDuration);
				$(this.navigation.get(this.currentFrame)).find('a').removeClass('activesticky');
			}
			
			// Then fade in the next frame
			this.beforeTransition(i, this.currentFrame);
			this.transitioning = true;
			
			var previousFrame = this.currentFrame;
			var frame = $(this.frames.get(i));
			
			$(frame).css('display', 'block');
			this._transitionElements(frame).css({opacity:0}).animate({opacity:1}, {
				duration: this.duration,
				complete: function() {
					if (typeof previousFrame != 'undefined') {
						$(that.frames.get(previousFrame)).css('display', 'none');
					}
					
					that.afterTransition(i, previousFrame);
					that.transitioning = false;
				}
			});
			
			$(this.navigation.get(i)).find('.-bg').animate({opacity:0}, this.navItemDuration);
			$(this.navigation.get(i)).find('a').addClass('activesticky');

			this.currentFrame = i;
		},
		
		_transitionElements: function(frame, all) {
			return $(frame);
			if (this.fadeText && !all) {
				return $(frame).find('img:first');
			}
			else {
				return $(frame).children('img');
			}
		},
		
		beforeTransition: function(nextFrame, currentFrame) {
			if (this.fadeText) {
				var frame = $(this.frames.get(nextFrame));
				frame.find('.text').css('opacity', 0);
			}
			
			this._clearAutoRotate();
		},
		
		afterTransition: function(currentFrame, previousFrame) {
			if (this.fadeText) {
				this._clearAutoRotate();
				if (this.fadeFirstTextOnly) {
					this.fadeText = false;
				}
				var frame = $(this.frames.get(currentFrame));
				var that = this;
				frame.find('.text').animate({'opacity': 1}, {
					duration: this.textDuration,
					complete: function() {
						that._resetAutoRotate();
					}
				});
			}
			else {
				this._resetAutoRotate();
			}
		},
		
		selectNextFrame: function() {
			var nextFrame = typeof this.currentFrame == 'undefined' ? 0 : (this.currentFrame + 1) % this.frames.length;
			this.selectFrame(nextFrame);
		},
		
		autoRotate: function(interval) {
			if (!this.frames.length) return;
		
			this.autoRotating = true;
			this.autoRotateInterval = interval;
			if (!this.transitioning) {
				this._autoRotate();
			}
		},
				
		_clearAutoRotate: function() {
			if (this.autoRotating) {
				clearTimeout(this.autoRotateTimer);
			}
		},
		
		_resetAutoRotate: function() {
			if (this.autoRotating) {
				this._clearAutoRotate();
				var that = this;
				this.autoRotateTimer = setTimeout(function() {
					that.selectNextFrame();
				}, this.autoRotateInterval);
			}
		}
	};
	
	$(document).ready(function() {
		var tmCarousel = new TMCarousel({
			duration: 2000, 
			navItemDuration: 1000, 
			textDuration: 1000,
			fadeFirstTextOnly: false
		});
		
		tmCarousel.selectFrame(0);
		tmCarousel.autoRotate(4000);
	});
})(jQuery);
