$.fn.extend({
	dataset: function(key, value) {
		// Allows read and write access to HTML5 dataset attributes
		if (arguments.length === 1) {
			return($(this).attr('data-'+key));
		} else {
			$(this).attr('data-'+key, value);
			return;
		};
	}
});

var settings = {
	ajax: {
		data_prefix: 'in_ajax_mode=1&act=',
		server_type: 'POST',
		timeout: 15000
	},
	lang: {
		required_txt: 'Required!'
	}
};

var func = {
	ajax: {
		verify_sync: function(server_reply) {
			if(server_reply === 'false') {
				window.location.reload();
				return false;
			}
			return true;
		}
	}
};

$(document).ready(function(){
	$.ajaxSetup({
		error: function() {
			window.location.reload();
		},
		success: function(server_reply) {
			func.ajax.verify_sync(server_reply);
		},
		timeout: settings.ajax.timeout,
		type: settings.ajax.server_type
	});

	$("textarea.expandable").resizable({
		handles: "se",
		minHeight: 64,
		minWidth: 300
	});

	$('input:text[data-prefilled_txt]').each(function() {
		if ($(this).val() == '') {
			$(this).addClass('prefilled_txt').val($(this).dataset('prefilled_txt'));
		}
	}).focus(function() {
		if ($(this).val() == $(this).dataset('prefilled_txt')) {
			$(this).removeClass('prefilled_txt').val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).addClass('prefilled_txt').val($(this).dataset('prefilled_txt'));
		}
	});

	$('form').live('submit', function() {	// remove all "prefilled_txt" values from form submits
		$('input[data-prefilled_txt]', this).each(function() {
			if ($(this).val() == $(this).dataset('prefilled_txt')) {
				$(this).val('');
			}
		});
	});

	selectCheckboxes = function(parentEl, select_switch) {
		$parentEl = $(parentEl);

		if (select_switch == 'all') {
			$(':checkbox', $parentEl).attr('checked', 'checked');
		} else if (select_switch == 'none') {
			$(':checkbox', $parentEl).attr('checked', '');
		} else {
			$(':checkbox:checked', $parentEl).attr('disabled', 'disabled');
			$(':checkbox', $parentEl).attr('checked', 'checked');
			$(':checkbox:disabled', $parentEl).attr('checked', '');
		}
		return false;
	}
});
