$(document).ready(function(){
	WebAck.extendJQuery();

	$(".hovercolor").hover(
		function(){
			$(this).css({color:"#259ed7"});
			$(this).children("*").css({color:"#259ed7"});
		},function(){
			$(this).css({color:"#000000"});
			$(this).children("*").css({color:"#000000"});
		}
	);

	$(".hoverfade").hover(
		function(){
			$(this).css({opacity: "0.75"});
		},function(){
			$(this).css({opacity: "1.0"});
		}
	);

	// disable submit with enter in reports
	$('.report-form input').keypress(function(e){
		if (e.keyCode == 13) {
			return false;
		}
	});

	// set focus
	$('.focus').focus();

	// list hover
	$('.list-table tr:gt(0)').hover(function (){
		$(this).addClass('list-item');
	}, function() {
		$(this).removeClass('list-item');
	});

	// clickable rows in list
	$('.list-table td').click(function (e) {
		if(!$(e.target).hasClass('rowlink-exclude')) {
			var obj = $(this).parent('tr').find('td.rowLink a').eq(0);
			if(obj.length == 0){
				// if no row link td was found, use the first link found in the tr
				obj = $(this).parent('tr').find('a').eq(0);
			}
			if(obj.length > 0){
				gotoUrl(obj.attr('href'));
			}
		}
	});

	// blink news teaser on start page
	setInterval(function (){
		$(".newsBlink").fadeOut(300).fadeIn(300);
	}, 2000);

	// forms where letter should be made uppercase
	$('form.transformUpper input, form.transformUpper textarea').change(function (){
		var val= $(this).val().toUpperCase();
		$(this).val(val);
	});

	$('#quicknav select').change(function() {
		$(this).parents('form').submit();
	});


	// show welcome modal
	if($('.welcome-modal').length && $('.welcome-modal').attr('open') == 1){
		showWelcomeModal();
	}

	if (!$('#admin-permconf').hasClass('disabled')) {
		WebAck.enables('permer', ['permerp', 'permerl', 'permers', 'permera', 'permerb']);
		WebAck.enables('permib', ['permiba', 'permibd', 'permibkgs', 'permibsn', 'permibw']);
	}
});

function showWelcomeModal(){
	$('.welcome-modal').clone().removeClass('hidden').modal({containerId: 'modalContainerNormal'});

	$('input[name=welcome-ok]').click(function () {
		$.modal.close();
	});
}


// like the php function trim
function trim(str){
	return str.replace(/^\s+|\s+$/g, '')
}

// print current page
function printPage(){
	window.print();
}

// goto given url
function gotoUrl(url){
	// Arvid add 2011-03-11: This was location.replace previously,
	// which led to trouble with the browser history, so I changed to
	// assign. I am not sure if the usage of replace was for a
	// specific purpose, so the change to assign could have un
	// intended effects. Stay vigilant always.
	window.location.assign(url);
}

// goto given url
function confirmStr(str){
	return confirm(str);
}

var WebAck = {
	extendJQuery: function () {
		var funs = ['datePicker'];

		var that = this;
		$.each(funs, function(i, name) {
			var prefixedName = 'wb' + name.charAt(0).toUpperCase() + name.slice(1)
			$.fn[prefixedName] = that[name];
		});
	},

	datePicker: function (opts) {
		$(this).datePicker($.extend({
			startDate: "2000-01-01",
			clickInput: true,
			displayClose: true,
			verticalOffset: "17px"
		}, opts || {}));
	},

	enables: function(enabler, enables) {
		$('#admin-permconf input[name=' + enabler + ']').change(function () {
			var enabled = $(this).attr('checked');
			$.each(enables, function (i, perm) {
				$('#admin-permconf input[name=' + perm  + ']').attr('disabled', !enabled);
				if (!enabled) {
					$('#admin-permconf input[name=' + perm  + ']').attr('checked', false);
				}

			});
		}).change();
	}
};

