$(document).ready(function() {
	AutoRotation();
});
var timer, timer2, timer3;
var _play = true;
var _play2 = true;
var _play3 = true;
var _play4 = true;
function AutoRotation() {
	_duration = 5000; //ms
	$('.gallery-holder').each(function() {
		_gallery = $(this).find(".gallery li");
		_gallery.css({"opacity": 0});
		_gallery.eq(0).css({"opacity": 1});
		_sgallery = $(this).find(".small-gallery li a");
		_num = _gallery.length;
		_button = $(this).find(".about-us")
		var el_cur = 1;
		var el_scur = 0;
		var temp = 0;
		timer = setTimeout(function() {play();}, _duration);
		_button.mouseover(function(){
			$(".about-block").addClass(" hover");
		})
		_button.mouseout(function(){
			$(".about-block").removeClass(" hover");
		})
		_button.click(function () {
			if (_play3)	{
			clearTimeout (timer);
			clearTimeout (timer2);
			clearTimeout (timer3);
			_play = false;
			_play2 = false;
			_play3 = false;
				timer4 = setTimeout(function() {hover ();}, 700);
				el_cur = 1;
				el_scur = 0;
			}
			return false;
		});
		_gallery.mouseenter (function () {
			clearTimeout (timer);
			clearTimeout (timer2);
			_play = false;
		});
		_gallery.mouseleave (function () {
			_play = true;
			clearTimeout (timer2);
			timer = setTimeout(function() {play();}, _duration);
		});
		 _sgallery.mouseenter (function () {
		 	clearTimeout (timer);
			clearTimeout (timer2);
			_play = false;
		 });
		 _button.mouseenter (function () {
		 	clearTimeout (timer);
			clearTimeout (timer2);
			_play = false;
		 });
		  _sgallery.eq(0).click(function() {
			if (_play4) {
				clearTimeout (timer);
				clearTimeout (timer2);
				clearTimeout (timer3);
				_play = false;
				_play2 = false;
				_play3 = true;
				_play4 = false;
				timer3 = setTimeout(function() {hover1(1,2,3,0);}, 700);
				_sgallery.removeClass("active");
				$(this).addClass("active");
			}
				return false;
		});
		 _sgallery.eq(1).click(function() {
			if (_play4) {
				clearTimeout (timer);
				clearTimeout (timer2);
				clearTimeout (timer3);
				_play = false;
				_play2 = false;
				_play3 = true;
				_play4 = false;
				timer3 = setTimeout(function() {hover1(2,1,0,3);}, 700);
				_sgallery.removeClass("active");
				$(this).addClass("active");
			}
				return false;
		});
		 _sgallery.eq(2).click(function() {
			if (_play4) {
				clearTimeout (timer);
				clearTimeout (timer2);
				clearTimeout (timer3);
				_play = false;
				_play2 = false;
				_play3 = true;
				_play4 = false;
				timer3 = setTimeout(function() {hover1(3,1,2,0);}, 700);
				_sgallery.removeClass("active");
				$(this).addClass("active");
			}
				return false;
		});
function rotate() {
	timer = setTimeout(function(){
			play();
		}, _duration);
}
function play() {
	if (_play & _play2) {
		temp = el_cur - 1;
		if (temp == -1) {
			temp = _num - 1;
			el_cur = 1;
			temp2 = _num - 2;
		}
		_gallery.eq(temp).animate({
			"opacity": 0
		}, 700, function(){
			_gallery.eq(temp).removeClass("active");
		});
		_gallery.eq(el_cur).addClass("active").animate({
			"opacity": 1
		}, 700);
		_sgallery.removeClass("active");
		_sgallery.eq(el_scur).addClass("active");
		
		if (el_cur == _num - 1) {
			el_cur = 0;
			el_scur = 0;
		}
		else {
			el_cur++;
			el_scur++;
		}
	}
	timer = setTimeout(function(){
			rotate();
		}, _duration);
}
function hover () {
	_gallery.eq(1).animate({"opacity": 0}, 700, function(){_gallery.eq(1).removeClass("active");});
	_gallery.eq(2).animate({"opacity": 0}, 700, function(){_gallery.eq(2).removeClass("active");});
	_gallery.eq(3).animate({"opacity": 0}, 700, function(){_gallery.eq(3).removeClass("active");});
	_gallery.eq(0).addClass("active").animate({"opacity": 1}, 700);
	_sgallery.removeClass("active");
	_play4 = true;
}
function hover1 (elem, hide1, hide2 ,hide3) {
	_gallery.eq(elem).addClass("active").animate({"opacity": 1}, 700);
	_gallery.eq(hide1).animate({"opacity": 0}, 700, function(){_gallery.eq(hide1).removeClass("active");});
	_gallery.eq(hide2).animate({"opacity": 0}, 700, function(){_gallery.eq(hide2).removeClass("active");});
	_gallery.eq(hide3).animate({"opacity": 0}, 700, function(){_gallery.eq(hide3).removeClass("active");});
	_play4 = true;
}
});
}	
function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs before change
							for (var i = 0; i < this.tabs.length; i++)
							{
								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}
if (window.addEventListener) window.addEventListener("load", initTabs, false);
else if (window.attachEvent) window.attachEvent("onload", initTabs);
