function labelInInputClick(el, defaultString) {
	if ($(el).val() == defaultString) {
		$(el).val('')
	}
}

function labelInInputBlur(el, defaultString) {
	if ($(el).val() == '') {
		$(el).val(defaultString);
	}
}

function sameHeight(selector, iterator) {
	var heights = [];
	var maxHeight = 0;
	var rowNo = 0;
	if (iterator == undefined) { // backward compatability
		iterator = 3;
	}

	$(selector).each(function(i, el) {
		if (maxHeight < $(this).height()) {
			maxHeight = $(this).height();
		}

		if (i && ((i + 1) % iterator) == 0) {
			heights[rowNo++] = maxHeight;
			maxHeight = 0;
		}
	});
	heights[rowNo] = maxHeight;
	
	rowNo = 0;
	$(selector).each(function(i, row) {
		$(this).height(heights[rowNo]);
		
		if (i && ((i + 1) % iterator) == 0) {
			rowNo++;
		}
	});
}

function number_format(number, decimals, dec_point, thousands_sep) {
	var n = number, prec = decimals;
	var toFixedFix = function (n,prec) {
		var k = Math.pow(10,prec);
		return (Math.round(n*k)/k).toString();
	};

	n = !isFinite(+n) ? 0 : +n;
	prec = !isFinite(+prec) ? 0 : Math.abs(prec);
	var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
	var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

	var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
	var abs = toFixedFix(Math.abs(n), prec);
	var _, i;
	if (abs >= 1000) {
		_ = abs.split(/\D/);
		i = _[0].length % 3 || 3;

		_[0] = s.slice(0,i + (n < 0)) +
		_[0].slice(i).replace(/(\d{3})/g, sep+'$1');
		s = _.join(dec);
	} else {
		s = s.replace('.', dec);
	}
	var decPos = s.indexOf(dec);
	if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
		s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
	}
	else if (prec >= 1 && decPos === -1) {
		s += dec+new Array(prec).join(0)+'0';
	}
	return s; 
}

(function($) {
	$.fn.stabs = function(options) {
		var options = $.extend({
			selectedClass: "active",
			selected: 0,
			onShow: null
		}, options);

		return this.each(function(i, tabs) {
			$(tabs).find("a").click(function(event) {
				$(tabs).find("li").removeClass(options.selectedClass);
				
				$(tabs).find("a").each(function(i, el) {
					$($(el).attr("href")).hide();
				});
				
				$(event.target).parents("li:first").addClass(options.selectedClass);
				$($(event.target).attr("href")).show();
				if (typeof options.onShow == 'function') {
					options.onShow(event.target);
				}
				return false;
			});

			$(tabs).find("a:eq("+options.selected+")").click();
		});
	};
})(jQuery);

$(document).ready(function() {
	$("a.lightbox").fancybox({
		overlayOpacity: 0.6,
		speedIn: 100,
		speedOut: 100,
		type: "image",
		titlePosition: "inside",
		// hack for flash in the background... Take a look at wmode transparent?
		onStart: function() {
			$("object").hide();
		},
		onClosed: function() {
			$("object").show();
		}
	});

	$("table.hoverable tr").hover(function() {
		$(this).toggleClass("hover");
	}, function() {
		$(this).toggleClass("hover");
	});

	$(".switch input:checkbox, input.switch:checkbox").click(function() {
		var $target = $($(this).attr("rel"));
		if (!$target.size()) {
			alert("SWITCH: Unknown target");
			return false;
		}

		if ($(this).attr("checked")) {
			$target.slideDown("fast");
		} else {
			$target.slideUp("fast");
		}
	});

	$(".switch input:checkbox, input.switch:checkbox").each(function(i, checkbox) {
		if (!$(checkbox).attr("checked") && $(this).attr("rel") != undefined) {
			$($(this).attr("rel")).hide();
		}
	});
	
	$(".ajax-window").fancybox({
		hideOnContentClick: false,
		padding: 0,
		autoDimensions: true,
		transitionIn: 'none',
		transitionOut: 'none',
		type: 'ajax'
	});
	
	$(".close").live('click', function() {
		$.fancybox.close();
		return false;
	})
});
