var elementCounter = 0;

var iv = 0;

var interval;

$(document).ready(function() {
	$("a").focus(function() {
		$(this).blur();
	});
	$("input[type='submit']").focus(function() {
		$(this).blur();
	});
	$("input[type='file']").each(function() {
		niceFile($(this));
	});
	$("input.amount").each(function() {
		addAmtStepper($(this));
	});
	$("input.submit").each(function() {
		makeButton($(this));
	});
	$("a.button").each(function() {
		makeButton($(this));
	});
	$("input._autoclear").each(function() {
		$(this).addClass('default');
		$(this).data('default', this.value);
		$(this).focus(function() {
			if (this.value == $(this).data('default')) {
				this.value = '';
				$(this).removeClass('default');
			}
		});
		$(this).blur(function() {
			if (!this.value) {
				this.value = $(this).data('default');
				$(this).addClass('default');
			}
		});
	});
	roundBoxes();
	hoverTables();
});

function niceFile(input) {
	if (input.hasClass("keep")) return;
	var name = input.attr("name");
	var value = input.attr("alt");
	input.wrap('<div class="file_wrapper" id="wrapper_' + name + '"></div>');
	var wrapper = $("#wrapper_" + name);
	wrapper.prepend('<input type="text" disabled="disabled" id="wrap_input_' + name + '" class="file_input" value="' + value + '" />');
	wrapper.prepend('<input type="button" id="wrap_button_' + name + '" class="file_button" value="Durchsuchen" />');
	input.addClass("file_browser");
	input.change(function() {
		var field = $("#wrap_input_" + $(this).attr("name"));
		field.attr("value", $(this).attr("value"));
	});
}

function roundBoxes() {
	$("div.box").each(function() {
		var box = $(this);
		box.wrapInner('<div class="box_center"></div>');
		box.prepend('<div class="box_left"></div>');
		box.append('<div class="box_right"></div>');
		box.append('<hr class="clear" />');
		box.removeClass("box");
		box.addClass("box_wrapper");
		if (box.hasClass("full")) {
			box.removeClass("full");
			box.children(".box_center").addClass("fullbox");
			box.css("margin-bottom", "20px");
		}
		if (box.hasClass("med")) {
			box.removeClass("med");
			box.children(".box_center").addClass("med");
			box.addClass("medbox");
			box.css("margin-bottom", "20px");
		}
	});
	$('a.boxlink').mouseover(function() {
		$(this).parent().parent().addClass('hover');
	});
	$('a.boxlink').mouseout(function() {
		$(this).parent().parent().removeClass('hover');
	});
}

function addAmtStepper(input) {
	elementCounter ++;
	var id = 'amount_' + elementCounter;
	input.attr('id', id);
	input.addClass('stepper');
	input.after('<ul class="stepper"><li class="up" onmousedown="initStepValue($(\'#' + id + '\'), 1)"><span>+</span>&nbsp;</li><li class="down" onmousedown="initStepValue($(\'#' + id + '\'), -1)"><span>-</span>&nbsp;</li></ul>');
}

function initStepValue(input, d) {
	clearTimeout(iv);
	iv = 1;
	interval = 400;
	stepValue(input, d);
	$(document).mouseup(function() {
		clearInterval(iv);
	})
}

function stepValue(input, d) {
	var v = parseInt(input.attr("value"), 10);
	if (!v || isNaN(v)) v = 0;
	v += d;
	v = Math.max(v, 1);
	input.attr('value', v);
	if (iv) {
		clearTimeout(iv);
		iv = setTimeout(function() {
			stepValue(input, d);
		}, interval);
		interval *= (interval == 400)? 0.4 : 0.95;
		if (interval < 10) interval = 10;
	}
}

function makeButton(input) {
	var cls = '';
	if (input.hasClass('delete')) {
		input.attr('value', '');
		cls = 'delete';
	}
	input.wrap('<div class="button_center ' + cls + '_center' + '"></div>');
	input.wrap('<div class="button_right ' + cls + '_right' + '"></div>');
	input.wrap('<div class="button_left ' + cls + '_left' + '"></div>');
	input.removeClass('submit');
	input.removeClass('button_raw');
	input.addClass('button');
}

function hoverTables() {
	$("table.categories td.img, table.categories td.title").mouseover(function() {
		var id = $(this).attr('id').split('_')[1];
		var brother = ($(this).hasClass('img'))? $('#t_' + id) : $('#i_' + id);
		if (hoverEmpty($(this), brother)) return;
		$(this).removeClass('hover');
		brother.removeClass('hover');
		$(this).addClass('hover');
		brother.addClass('hover');
	});
	$("table.categories td.img, table.categories td.title").mouseout(function() {
		var id = $(this).attr('id').split('_')[1];
		var brother = ($(this).hasClass('img'))? $('#t_' + id) : $('#i_' + id);
		if (hoverEmpty($(this), brother)) return;
		$(this).removeClass('hover');
		brother.removeClass('hover');
	});
	$("table.categories td.img, table.categories td.title").click(function() {
		var id = $(this).attr('id').split('_')[1];
		var title = ($(this).hasClass('img'))? $('#t_' + id) : $(this);
		var link = title.children('a').attr('href');
		if (link) self.location.href = link;
	});
}

function hoverEmpty(obj1, obj2) {
	return (obj1.children('a').html() && obj2.children('a').html())? false : true;
}
