$(document).ready(function()
{
	initTabs("div.tabset");
	initPopups();
	initFormOpener();
	initNews();
	initTooltips();
	initGlossaryTabs();
	if($(".table-sort").length > 0)
	{
	
	$.tablesorter.addParser({
		  id: "commaDigit",
		  is: function(s, table) {
			var c = table.config;
			return $.tablesorter.isDigit(s.replace(/[,.]/g, ""), c);
		  },
		  format: function(s) {
			return $.tablesorter.formatFloat(s.replace(/[,.]/g, ""));
		  },
		  type: "numeric"
		});

	$.tablesorter.addParser({
		id: "german_date",
		is: function(s) {
			return /\d{1,2}[\/\.]\d{1,2}[\/\.]\d{2,4}/.test(s);
		},
		format: function(s) {
			s = s.replace(/\./g,"/");
			s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1");
			return $.tablesorter.formatFloat(new Date(s).getTime());
		},
		type: "numeric"
	});
	
		$(".table-sort").tablesorter({sortList:[[1,0]], widgets: ['zebra'], headers: { 0: { sorter: false}, 
2: { sorter: "text"}}}).tablesorterPager({container: $("#pager"), size: 30});
		if ($.browser.opera)
		{
			$(".pager-info").hide().fadeIn(150);
		}
	}
});
function initFormOpener()
{
	$(".form-toggler").each(function(i, el)
	{
		$(el).click(function()
		{
			$(el).parent().toggleClass("form-active");
			if($(el).parent().hasClass("form-active"))
			{
				$(this).text("Ricerca semplice");
			}
			else
				$(this).text("Ricerca avanzata");
			getElements();
			replaceSelects();
			return false;
		});
	});
}
function initTooltips()
{
	$(".cell-wrapper").each(function(i, el)
	{
		var dur = 150;
		if ($.browser.msie)
			dur = 0;
		$(el).hover(
		  function () {
			
			$(this).find(".tooltip").fadeIn(dur);
			$(this).find(".tooltip-2").fadeIn(dur);
			$(this).css({"z-index": 10});
		  }, 
		  function () {
			$(this).find(".tooltip").fadeOut(dur);
			$(this).find(".tooltip-2").fadeOut(dur);
			$(this).css({"z-index": 0});
		  }
		);
	});
}
function initTabs(h_list)
{
	$(h_list).each(function(_ind, _el)
	{
		var btn_h = $(_el);
		var _btn = $(_el).find('a.tab');
		var _a = 0;
		_btn.each(function(_ind, _el)
		{
			this._box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).hasClass('active'))
			{
				this._box.show();
				_a = _ind;
			}
			else
			{
				this._box.hide();
			}
			_el.onclick = function()
			{
				if(!$(this).hasClass('active'))
				{
					_btn.get(_a)._box.hide();
					_btn.eq(_a).removeClass('active');
					this._box.fadeIn(500);
					$(this).addClass('active');
					_a = _ind;
					getElements();
			replaceSelects();
				}
				return false;
			}
		});
	});
}
function initPopups()
{
	var popups = $(".popup");
	$(popups).css({"display": "none"});
	$("a.popup-opener").click(function()
	{
		var top = 0;
		if(document.documentElement.scrollTop > document.body.scrollTop)
			top = document.documentElement.scrollTop;
		else
			top = document.body.scrollTop;

		var popup = $("#" + this.rel);
		$(popup).css({"display": "block", "top": 40 + top});
		return false;
	});
	$("a.btn-popup-close").click(function()
	{
		$(popups).hide();
		return false;
	});
}
function initNews()
{
	var list = $(".news-slide li");
	
	$(list).each(function(i, el)
	{
		$(el).find(".news-opener").click(function()
		{
			
			$(el).toggleClass("opened");
			if($(el).hasClass("opened"))
				$(this).parent().find(".news-content").animate({"height": $(this).parent().find(".news-slide-item").height()});
			else
				$(this).parent().find(".news-content").animate({"height": 43});
			return false;
		});
	});
}


function initGlossaryTabs()
{
	 var tabContainers = $('div#tabs > div');
		tabContainers.hide().filter(':first').show();
		
		$('div#tabs ul.alphabet a').click(function () {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('div#tabs ul.alphabet a').removeClass('active');
				$(this).addClass('active');
				return false;
		}).filter(':first').click();
}

$.getReadyList = function() {
  if(this.readyList != null) { this.myreadylist = [].concat(this.readyList); }
  return this.myreadylist;
};

$(document).ready(function() {
  readylist = $.getReadyList();
});

$.triggerReady = function() {
  $(readylist).each(function(){this();});
}
