$(document).ready(function(){
	initTabs();
	initFadeGallery("div.gallery li", ".dots");

});

var slides;
var buttons;
var current = 0;
var duration = 10000;/*4 sec*/

function initFadeGallery(images, buttons)
{
	slides = $(images);
	buttons = $(buttons);
	$(this).text($(".dots").html("<ul>"));
	$(slides).each(function(i, el)
	{
		$(".dots ul").append("<li><a href='javascript:;'><span>" + parseInt(i+1) + "</span></a></li>");
	});
	$(buttons).find("a").eq(0).addClass("active");
	$(slides).css({"opacity": 0}).eq(0).css({"opacity": 1, "z-index": 1});

	$(buttons).find("a").each(function(i, el)
	{
		$(el).click(function()
		{
			if(current != i)
			{
				$(buttons).find("a").removeClass("active");
				$(slides).eq(current).animate({"opacity": 0 });
				$(slides).css({"z-index": 0}).eq(i).css({"z-index": 1});
				$(slides).eq(i).animate({"opacity": 1});
				current = i;
				$(this).addClass("active");
				clearTimeout(timer);
				timer = setTimeout("rotate(" + current + ")", duration);
			}
		});
	});
	timer = setTimeout("rotate(" + 0 +")", duration);
}

function rotate(_index)
{
	_index = current;
	$(slides).eq(_index).fadeTo("slow", 0);
	if(_index == $(slides).length - 1)
		_index = -1;
	$(slides).eq(++_index).fadeTo("slow", 1);
	$(slides).css({"z-index": 0}).eq(_index).css({"z-index": 1});
	$("div.dots").find("a").removeClass("active").eq(_index).addClass("active");
	current = _index;
	timer = setTimeout("rotate(" + current + ")", duration);
}


function initTabs() {
	$('.tabset').each(function() {
		var _a = 0;
		var _btn = $(this).find('a.tab');
		_btn.each(function(_ind, _el) {
			_el.box = $('#' + _el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).hasClass('active')) {
				if(_el.box.length) _el.box.show();
				_a = _ind;
			}
			else {
				if(_el.box.length) _el.box.hide();
			}
			_el.onclick = function() {
				tabChange(_ind);
				return false;
			}
		});
		function tabChange(_i) {
			if(_i != _a) {
				_btn.get(_a).box.hide();
				_btn.eq(_a).removeClass('active');
				_btn.get(_i).box.show();
				_btn.eq(_i).addClass('active');
				_a = _i;
			}
		}
	});
}
