$(function(){
	// ie fix
	if ($.browser.msie && jQuery.browser.version.substr(0,2)=='6.') {
		$('input[type=text],input[type=password]').addClass('inputIE');
	}
	// js events
	$('.jsactive').each(function(){
		$(this).addClass('active').siblings().removeClass('active');
	});
	/*
	$('.grid-table table tbody tr input[type=checkbox]').click(function(){
		if (this.checked) {
			$(this).parents('tr').removeClass('active');
		} else {
			$(this).parents('tr').addClass('active');
		}
	});
	$('.grid-table table tbody tr *').mouseup(function(){
		if (this.tagName=='A') {
			//location = this.href;
			return false;
		} else if (this.tagName=='INPUT') {
			return false;
		} else if (this.tagName=='IMG' && this.parent.tagName=='A') {
			return false;
		} else {
			var chb = $(this).parents('tr').find('input[type=checkbox]:first');
			if (chb.length>0) {
				chb.click();
			} else {
				var href = $(this).parents('tr').find('a:first').attr('href');
				location = href;
			}
			return false;
		}
	});
	*/
	$('table.checks tbody input[type=checkbox]').click(function(){
		var n = $(this).parents('tbody').find('input[type=checkbox]:checked').size();
		var n0 = $(this).parents('tbody').find('input[type=checkbox]').size();
		if (n==n0) {
			$(this).parents('table').find('tfoot input[type=checkbox]').attr('checked', true);
		} else {
			$(this).parents('table').find('tfoot input[type=checkbox]').attr('checked', false);
		}
	});
	$('table.checks tfoot input[type=checkbox]').click(function(){
		if ($(this).attr('checked')) {
			var n = $(this).parents('table').find('tbody input[type=checkbox]').attr('checked', true);
		} else {
			var n = $(this).parents('table').find('tbody input[type=checkbox]').attr('checked', false);
		}
	});
	// filter tabs
	$('input[type=radio]:checked').each(function(){
		$(this).parents('li').addClass('active');
	});
	$('input[type=radio]').click(function(){
		$(this).parents('li').addClass('active').siblings().removeClass('active');
	});
	// filter selects
	$('#filter_f4').change(function(){
		var i1 = this.selectedIndex;
		var i2 = document.getElementById('filter_f5').selectedIndex;
		if (i1>0 && i2<i1) {
			document.getElementById('filter_f5').selectedIndex = i1;
		}
	});
	// tooltips
	$('.tip').each(function(){
		$(this).simpletip({
			content: $(this).attr('title'),
			position: 'bottom',
			offset: [0, 2]
		});
		$(this).attr('title', '');
	});
	// auto populate labels
	$('.autopopulate').each(function()
	{
		var label = $(this)
		var id = label.attr('for')
		var input = $('#' + id)
		var text = label.text()

		label.css('display', 'none')
		if ((input.val()=='') || (input.val()==text)) {
			input.val(text)
			input.addClass("gray");
		}

		input.focus(function(){ if(this.value == text) { $(this).val('').removeClass('gray') }}
		).blur(function(){ if(this.value == '') { $(this).val(text).addClass('gray') }})
	});
	$('.autopopulate').parents("form").submit(function()
	{
		$(this).find('input.gray').each(function(i)
		{
			$(this).val('')
		})
	});
	// checkboxes
	$('aside input:checkbox:checked').each(function(){ $(this).parent('label').addClass('checked'); });
	$('aside input:checkbox:not(:checked)').each(function(){ $(this).parent('label').addClass('unchecked'); });
	$('aside input:checkbox').click(function(){
		if (this.checked) {
			$(this).parent('label').removeClass('unchecked').addClass('checked');
		} else {
			$(this).parent('label').removeClass('checked').addClass('unchecked');
		}
	});
});
