$(function() {
	InstallIntro();
});

function InstallIntro() {
	var $mainDiv = $("#main-menu .tab-content");
	if(!$mainDiv.length) return;
	
	var $mainSelect = $(".select", $mainDiv),
		$mainText	= $(".text", $mainDiv),
		$mainImage	= $(".image", $mainDiv),
		currentFrame = $(".current", $mainSelect).attr("frame");
		
	/*
	 * Объект анимации
	 */ 
	var IntroItem = function(id) {
		this.options = {
			showSpeed: 500,
			hideSpeed: 300
		}
		this.text = $(".item[frame='"+id+"']", $mainText);
		this.image = $(".item[frame='"+id+"']", $mainImage);
		this.imageWidth = this.image.width();
		this.imageHeight = this.image.height();
	}
	
	/*
	 * Показать объект
	 */
	IntroItem.prototype.pageIn = function(callback) {
		var _this = this;
		
		_this.text.css({opacity: 0})
			.addClass("current");
		
		_this.image.css({opacity: 0})
			.addClass("current")
			.animate({opacity: 1}, {
				easing: "swing",
				duration: _this.options.showSpeed,
				step: function(x) {
					_this.text.css({opacity: x});
				},
				complete: function() {
					_this.image.css({opacity: ""});
					_this.text.css({opacity: ""});
					
					if(typeof callback == "function") {
						callback.apply(this, [_this]);
					}
				}
			});
	}
	 
	/*
	 * Скрыть объект
	 */
	IntroItem.prototype.pageOut = function(callback) {
		var _this = this;
		
		_this.text.addClass("current-old")
			.removeClass("current");
		
		_this.image.addClass("current-old")
			.removeClass("current")
			.animate({opacity: 0}, {
				easing: "swing",
				duration: _this.options.hideSpeed,
				step: function(x) {
					_this.text.css({opacity: x});
				},
				complete: function() {
					_this.image.removeClass("current-old")
						.css({opacity: ""});
					_this.text.removeClass("current-old")
						.css({opacity: ""});
					
					if(typeof callback == "function") {
						callback.apply(this, [_this]);
					}
				}
			});
	}
	 
	/*
	 * Ресайз объекта
	 */
	IntroItem.prototype.pageResize = function(width) {
		if(width >= this.imageWidth) {
			this.image.children().css({width: "", height: ""});
			return;
		}
	
		var k = width / this.imageWidth,
			w = parseInt(this.imageWidth * k),
			h = parseInt(this.imageHeight * k);
			
		this.image.children().css({width: w+"px", height: h+"px"});
	}
	 
	/*
	 * Создаем объекты анимации
	 */
	var frames = [null, new IntroItem(1), new IntroItem(2), new IntroItem(3), new IntroItem(4), new IntroItem(5)];
			
	/*
	 * Запуск анимации
	 */
	var animateIntro = function(nextFrame, callback) {
		var isAnimete = false;
		animateIntro = function(nextFrame, callback) {
			if(isAnimete || nextFrame == currentFrame) return false;
			isAnimete = true;
			frames[currentFrame].pageOut();
			frames[nextFrame].pageIn(function(item){
				currentFrame = nextFrame;
				if(typeof callback == "function") {
					callback.apply(this, []);
				}
				isAnimete = false;
			});
			return true;
		}
		return animateIntro(nextFrame, callback);
	}
	  
	var _timer = false;
	$mainSelect.find("a").each(function() {
		var $a = $(this);
		$a.bind("click", function(event, timeout){
			if(animateIntro($a.attr("frame"), function(){
				var nextId = parseInt(currentFrame) + 1;
				nextId = (nextId == frames.length) ? 1 : nextId;
				clearTimeout(_timer);
				_timer = setTimeout(function(){
					$mainSelect.find("a[frame='"+nextId+"']").trigger("click", [5000]);
				}, timeout || 15000);
			})) {
				$mainSelect.find("a").removeClass("current");
				$a.addClass("current");
			}
			return false;
		});
	});
	 
	$(window).bind("resize", function(){
		var width = $mainDiv.width() - $mainImage.position().left;
		for(i = 1; i < frames.length; i++) {
			frames[i].pageResize(width);
		}
	}).trigger("resize");
	 
	_timer = setTimeout(function(){
		$mainSelect.find("a[frame='2']").trigger("click", [5000]);
	}, 7500);
	
	/*
	 * preloader
	 */
	 
	for(i = 1; i < frames.length; i++) {
		var $img = frames[i].image.children();
		if($img.attr("src_r")) {
			$img.attr("src", $img.attr("src_r"));
		}
	}
}
