jQuery(document).ready(function()
{
	initTabs(".tabset");
	initInputs();
	initMenu();
	initGallery(".gallery", false);
	if(jQuery(".slider-holder").length > 0)
	{
		jQuery("div.slider-holder").galleryScroll({
			duration: 500,
			circleSlide: true,
			scrollElParent: ".images-list",
			scrollEl: ".images-list li",
			slideNum: ".pager",
			autoSlide: 8000
		});
	}
});
function initMenu()
{
	jQuery("#main-nav > ul > li").each(function(id, el)
	{
		el.height = jQuery(el).find("ul").height();
		jQuery(el).find("ul").css({"height": 0});
		jQuery(el).hover(function()
		{
			jQuery(this).find("ul").animate({"height": el.height, paddingBottom: 10}, {duration: 400,queue:false});
			jQuery(this).addClass("hover");
		}, function()
		{	
			jQuery(this).find("ul").animate({"height": 0, paddingBottom: 0}, {duration: 400,queue:false});
			jQuery(this).removeClass("hover");
		});
	});
}
function initInputs()
{
	jQuery("input").each(function(_ind, _el)
	{
		if(_el.type == "text")
		{
			_el.valueHtml = _el.value;
			jQuery(_el).focus(function ()
			{
				if(this.valueHtml == jQuery(this).val())
				jQuery(this).val("");
				jQuery(this).parent().addClass("focus");
			});
			jQuery(_el).blur(function ()
			{
				this.value != "" ? this.value = this.value: this.value = this.valueHtml;
				jQuery(this).parent().removeClass("focus");
			});
		}
	});
}
function initTabs(h_list)
{
	jQuery(h_list).each(function(_ind, _el)
	{
		var btn_h = jQuery(_el);
		var _btn = jQuery(_el).find('a.tab');
		var _a = 0;
		_btn.each(function(_ind, _el)
		{
			this._box = jQuery('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			if(jQuery(_el).hasClass('active'))
			{
				this._box.show();
				_a = _ind;
			}
			else
			{
				this._box.hide();
			}
			_el.onclick = function()
			{
				if(!jQuery(this).hasClass('active'))
				{
					_btn.get(_a)._box.hide();
					_btn.eq(_a).removeClass('active');
					this._box.show();
					jQuery(this).addClass('active');
					_a = _ind;
				}
				return false;
			}
		});
	});
}
function initGallery(holder, _autoplay)
{
	var duration = 5000;
	var gallery;
	var timer;
	var autoplay = _autoplay;
	
	gallery = jQuery(holder).find("ul.image li");
	gallery.number = jQuery(gallery).length;
	if(gallery.number > 1)
	{
		gallery.current = 0;
		gallery.thumbnails = jQuery(holder).find(".thumbs li a");
		
		jQuery(gallery).css({"opacity": 0, "z-index": "0"});
		jQuery(gallery).eq(0).css({"opacity": 1, "z-index": "1"});

		jQuery(holder).find(".play").click(function()
		{
			autoplay = true;
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
			return false;
		});
		
		jQuery(holder).find(".pause").click(function()
		{
			if(autoplay)
			{
				autoplay = false;
				clearTimeout(timer);
			}
			else
			{
				autoplay = true;
				timer = setTimeout(function()
				{
					rotate();
				}
				, duration);
			}
			return false;
		});
		
		jQuery(gallery.thumbnails).each(function(i, el)
		{
			jQuery(el).click(function()
			{
    				if(gallery.current != i)
				{
					var temp = gallery.current;
					jQuery(gallery.thumbnails).removeClass("active");
					jQuery(el).addClass("active");

					jQuery(gallery).eq(temp).animate({"opacity": 0}, 600, function() {  jQuery(gallery).eq(temp).css({"z-index": "0"}); });
					setTimeout(function(){jQuery(gallery).eq(i).css({"z-index": "1"});
					jQuery(gallery).eq(i).animate({"opacity": 1}, 700);}, 10);
					
					
					clearTimeout(timer);
					
					gallery.current = i;
					
					timer = setTimeout(function()
					{
						rotate();
					}
					, duration);
				}
				return false;
			});
		});
		
		if(autoplay)
		{
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
		}
	}
	function play()
	{
		var temp = gallery.current;
		jQuery(gallery).eq(temp).animate({"opacity": 0}, 700, function() {  jQuery(gallery).eq(temp).css({"display": "none"});  });
		
		if(++gallery.current >= gallery.number)
			gallery.current = 0;

		jQuery(gallery.thumbnails).removeClass("active");
		jQuery(gallery.thumbnails).eq(gallery.current).addClass("active");
		
		
		jQuery(gallery).eq(gallery.current).css({"display": "block"}).animate({"opacity": 1}, 700);
		
		clearTimeout(timer); 
	}
	function rotate()
	{
		if(autoplay)
		{
			play();
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
		}
	}
}
