(function($) {
	/* offset fix for mobile safari */
	if ( /webkit.*mobile/i.test(navigator.userAgent)) {
		(function($) {
			$.fn.offsetOld = $.fn.offset;
			$.fn.offset = function() {
				var result = this.offsetOld();
				result.top -= window.scrollY;
				result.left -= window.scrollX;
				return result;
			};
		})(jQuery);
	}

	/* German initialisation for the jQuery UI date picker plugin. */
	/* Written by Milian Wolff (mail@milianw.de). */
	$.datepicker.regional['de'] = {
		closeText: 'schließen',
		prevText: '&#x3c;zurück',
		nextText: 'Vor&#x3e;',
		currentText: 'heute',
		monthNames: ['Januar','Februar','März','April','Mai','Juni',
		'Juli','August','September','Oktober','November','Dezember'],
		monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
		'Jul','Aug','Sep','Okt','Nov','Dez'],
		dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
		dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		weekHeader: 'Wo',
		dateFormat: 'dd.mm.yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		beforeShow: function() {$(this).dialog("widget").css("z-index",20);},
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['de']);

	/* styleswitch js/nojs */
	document.write(
		"<style>"+
		".dateinputwrapepr .nojs {display: none;}"+
		".dateinputwrapepr .js {display: inline;}"+
		"<\/style>"
	);

	/* list hover */
	$('#main_content .listview .itemd, #main_content .listview tr[class!=label]').live(
		'mouseover',function(e){
			$(this).addClass('itemhover');
		}
	).live(
		'mouseout',function(e){
			$(this).removeClass('itemhover');
		}
	).live(
		'click',function(e){
			if (e.target.nodeName.toLowerCase() == "a") {
				return true;
			}
			e.preventDefault();
			$(this).find('.previewmore a, .last a').each(function(){
				location.href = $(this).attr('href');
			});
		}
	);

})(jQuery);

jQuery(function($) {
	/* von/bis datepickers */
	var datepicker = function(wrapper,name,cb) {
		var dp = wrapper.find('.js input');
		var valid = true;
		var start = new Date();
		wrapper.find("select").each(function() {
			if (this.name == name+'[d]') {
				if ($(this).val() == "") valid=false;
				start.setDate(parseInt($(this).val()));
			}
			if (this.name == name+'[m]') {
				if ($(this).val() == "") valid=false;
				start.setMonth(parseInt($(this).val())-1);
			}
			if (this.name == name+'[Y]') {
				if ($(this).val() == "") valid=false;
				start.setFullYear(parseInt($(this).val()));
			}
		});
		var onSelectDate = function(text) {
			var date=null;
			try{
				var date=$.datepicker.parseDate("yy-mm-dd",text);
			}catch(e){
			}
			if (date) {
				wrapper.find("select").each(function() {
					if (this.name == name+'[d]') {
						$(this).val(date.getDate());
					}
					if (this.name == name+'[m]') {
						$(this).val(date.getMonth()+1);
					}
					if (this.name == name+'[Y]') {
						$(this).val(date.getFullYear());
					}
				});
			} else {
				wrapper.find("select").val("");
			}
			var o = "";
			if (null == date)  {
				o = "beliebig";
			} else {
				o = ($.datepicker.formatDate('yy-mm-dd',new Date())==text)
					? 'heute' : $.datepicker.formatDate('dd.mm.yy',date);
			}
			wrapper.find('.js span').text(o);
			cb(date);
		};
		var lastclose = new Date();
		dp.datepicker({
			minDate: 0,
			dateFormat: "yy-mm-dd",
			onSelect: function(text) {
				onSelectDate(text);
				$('#datereset .js').show();
			},
			onClose: function() {
				lastclose = new Date();
			}
		});
		if (valid)
			dp.datepicker('setDate',start);
		onSelectDate(dp.val());
		wrapper.find('.js').click(function() {
			if ((new Date()-lastclose) > 250) {
				dp.focus();
			} else {
			//	dp.blur();
			}
		})
		return dp;
	}

	var vondp = null;
	var bisdp = null;
	bisdp = datepicker($("#bisdate"),"bis",function(date){
	});
	vondp = datepicker($("#vondate"),"von",function(date){
		bisdp.datepicker('option','minDate',date);
	});

	// reset datepicker
	$('#datereset .js').click(function() {
		$('#vondate .js span').text('heute');
		$('#bisdate .js span').text('beliebig');
		var date = new Date();
		$('#vondate').find("select").each(function() {
			if (this.name == 'von[d]') {
				$(this).val(date.getDate());
			}
			if (this.name == 'von[m]') {
				$(this).val(date.getMonth()+1);
			}
			if (this.name == 'von[Y]') {
				$(this).val(date.getFullYear());
			}
		});
		$('#bisdate').find('select').each(function() {
			if (this.name == 'bis[d]') {
				$(this).val("");
			}
			if (this.name == 'bis[m]') {
				$(this).val("");
			}
			if (this.name == 'bis[Y]') {
				$(this).val("");
			}
		});
		$('#datereset .js').hide();
	});

	/* autosuggest queryfield */
	function split(val) {
		return val.split(/,\s*/);
	}
	function extractLast(term) {
		return split(term).pop();
	}
	$("input[name=query]").autocomplete({
		minLength: 0,
		source: function(request, response) {
			$.getJSON(XWK.host+'XooWebKit/bin/suggest.php?m='
					+ $('#suggest_class').val() + '&p='
					+ $('#suggest_modules').val(), {
				q: extractLast(request.term),
				limit: 15
			}, response);
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function(event, ui) {
			var terms = split( this.value );
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push( ui.item.value );
			// add placeholder to get the comma-and-space at the end
			terms.push("");
			this.value = terms.join(", ");
			return false;
		}
	});

	/* inputtooltip */
	$('input.inputtooltip[alt]').each(function() {
		var i = $(this);
		var text = i.attr('alt');
		var dummy = $('<input type="text">');
		dummy.attr('class', i.attr('class'));
		dummy.attr('style', i.attr('style'));
		dummy.attr('name', i.attr('name') + '_tooltipdummy');
		dummy.val(text);
		i.after(dummy);
		dummy.hide();

		dummy.focus(function() {
			i.show();
			dummy.hide();
			i.focus();
		});

		i.blur(function() {
			if (i.val() == "") {
				i.hide();
				dummy.show();
			}
		});

		i.blur();

	});

	/* slidegallery */
	$('.images').css( {
		position : 'relative'
	});

	var min = 0;
	var max = 0;
	var center = 0;
	var current = 0;
	var div = $('.images').width() - $('.images div').width();

	$('.slider').css('marginLeft', 0)
	var pos = function(x) {
		current = Math.max(0, Math.min(max, x));
		$('.slider').css('marginLeft', current);
		$('.images div').css('marginLeft', div * current / max);
	};
	var posAbs = function(x) {
		pos(x - min - center);
	};

	var holdtimer;
	var want = 0;
	var force = 0;
	var ac = 10;
	var lastX;

	$('form input[name=__offsetx]').each(function() {
		var f = $(this.form);
		var o = $(this);
		if (o.val() != "") {
			$(window).scrollTop(o.val());
		}
		f.submit(function(e) {
			o.val($(window).scrollTop());
		});
	});


});
