$(document).ready(function(){

	initGallery(".gallery-area", true);

	initQuotes(".cite", true);

	initTabs(getQString("pagenum"));

});



function initGallery(holder, _autoplay)

{

	var duration = 9000;

	var gallery;

	var timer;

	var autoplay = _autoplay;



	gallery = $(holder).find("ul.gallery li");

	gallery.number = $(gallery).length;

	if(gallery.number > 1)

	{

		gallery.current = 0;

		//gallery.thumbnails = $(holder).find(".paginator li a");



		$(gallery).css({"opacity": 0, "display": "none"});

		$(gallery).eq(0).css({"opacity": 1, "display": "block"});



		$(holder).find(".play").click(function()

		{

			autoplay = true;

			timer = setTimeout(function()

			{

				rotate();

			}

			, duration);

			return false;

		});



		$(holder).find(".pause").click(function()

		{

			if(autoplay)

			{

				autoplay = false;

				clearTimeout(timer);

			}

			else

			{

				autoplay = true;

				timer = setTimeout(function()

				{

					rotate();

				}

				, duration);

			}

			return false;

		});



		$(gallery.thumbnails).each(function(i, el)

		{

			if(gallery.current == 0) return true;



			$(el).click(function()

			{

					if(gallery.current != i)

				{

					var temp = gallery.current;

					$(gallery.thumbnails).removeClass("active");

					$(el).addClass("active");



					$(gallery).eq(temp).animate({"opacity": 0}, 700, function() {  $(gallery).eq(temp).css({"display": "none"});  });

					$(gallery).eq(i).css({"display": "block"}).animate({"opacity": 1}, 700);



					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;

		$(gallery).eq(temp).animate({"opacity": 0}, 700, function() {  $(gallery).eq(temp).css({"display": "none"});  });



		if(++gallery.current >= gallery.number)

			gallery.current = 0;



		$(gallery.thumbnails).removeClass("active");

		$(gallery.thumbnails).eq(gallery.current).addClass("active");





		$(gallery).eq(gallery.current).css({"display": "block"}).animate({"opacity": 1}, 700);



		clearTimeout(timer);

	}

	function rotate()

	{

		if(autoplay)

		{

			play();

			timer = setTimeout(function()

			{

				rotate();

			}

			, duration);

		}

	}

}



function initQuotes(holder, _autoplay)

{

	var duration = 15000;

	var gallery;

	var timer;

	var autoplay = _autoplay;



	gallery = $(holder).find("ul.gallery li");

	gallery.number = $(gallery).length;

	if(gallery.number > 1)

	{

		gallery.current = 0;

		//gallery.thumbnails = $(holder).find(".paginator li a");



		$(gallery).css({"opacity": 0, "display": "none"});

		$(gallery).eq(0).css({"opacity": 1, "display": "block"});



		$(holder).find(".play").click(function()

		{

			autoplay = true;

			timer = setTimeout(function()

			{

				rotate();

			}

			, duration);

			return false;

		});



		$(holder).find(".pause").click(function()

		{

			if(autoplay)

			{

				autoplay = false;

				clearTimeout(timer);

			}

			else

			{

				autoplay = true;

				timer = setTimeout(function()

				{

					rotate();

				}

				, duration);

			}

			return false;

		});



		$(gallery.thumbnails).each(function(i, el)

		{

			if(gallery.current == 0) return true;

			$(el).click(function()

			{

					if(gallery.current != i)

				{

					var temp = gallery.current;

					$(gallery.thumbnails).removeClass("active");

					$(el).addClass("active");



					$(gallery).eq(temp).animate({"opacity": 0}, 700, function() {  $(gallery).eq(temp).css({"display": "none"});  });

					$(gallery).eq(i).css({"display": "block"}).animate({"opacity": 1}, 700);



					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;

		$(gallery).eq(temp).animate({"opacity": 0}, 700, function() {  $(gallery).eq(temp).css({"display": "none"});  });



		if(++gallery.current >= gallery.number)

			gallery.current = 0;



		$(gallery.thumbnails).removeClass("active");

		$(gallery.thumbnails).eq(gallery.current).addClass("active");





		$(gallery).eq(gallery.current).css({"display": "block"}).animate({"opacity": 1}, 700);



		clearTimeout(timer);

	}

	function rotate()

	{

		if(autoplay)

		{

			play();

			timer = setTimeout(function()

			{

				rotate();

			}

			, duration);

		}

	}

}



/*--- tabs function ---*/

function getQString(value) {
	var qstring = window.location.search.substring(1);
	var qstringArr = qstring.split("&");
	for (i=0;i<qstringArr.length;i++) {
		var currQStr = qstringArr[i].split("=");
		if (currQStr[0] == value) {
			return currQStr[1];
			break;
		}
	}
	return "";
}

function initTabs(h_list) {
	$('.tab-area').each(function() {

		var _btn = $(this).find('.tabset ul a');
		var box_h = $(this).children('.tab-holder');
		var _a = _btn.index(_btn.filter('.active').eq(0));
		if (h_list != "") _a = h_list;
		if(_a == -1) _a = 0;

		_btn.each(function(_ind, _el) {

			this._box = box_h.children('.'+_el.href.substr(_el.href.indexOf("#") + 1));

			if(_ind == _a) {
				if(_a == h_list && h_list != ' ') {
					$(this).addClass('active');
					this._box.show();
				}else{
					if(h_list != ' ') {
						_btn.get(_a)._box.hide();
						_btn.eq(_a).removeClass('active');
					}
					this._box.show();
				}
			}
			else this._box.hide();


			if(box_h.parent().hasClass('must_hover')){

				_el.onmouseover = function(){

					if(_ind != _a){
	
						_btn.get(_a)._box.hide();

						_btn.eq(_a).removeClass('active');

						this._box.show();

						$(this).addClass('active');

						_a = _ind;

						//$('select').customSelect();

					}

				}

			}

			else{
				_el.onclick = function() {

					if(_ind != _a){
			
						_btn.get(_a)._box.hide();

						_btn.eq(_a).removeClass('active');

						this._box.show();

						$(this).addClass('active');

						_a = _ind;

						//$('select').customSelect();

					}

					return false;

				}

			}

		});

	});

}