$(function(){
	setupInputs();
	//setupTooltip();
	setupAutocompleteMusiker();
	setupForms();
	setupHover();
	$('#foot3').click(function() {
		message('message', 'ok');
	});
	$("img").lazyload({
	     placeholder : "Bilder/T.gif",       
		 threshold : 20,
	     effect      : "fadeIn"
	 });
	$("#youTubeList img").lazyload({
	     placeholder : "Bilder/T.gif",       
	     effect      : "fadeIn",
		 threshold : 20,
		 container: $('#youTubeList')
	 });
	 $('ul.bandmusiker').css({'width':400, 'padding': '10px 0', 'margin': '-10px 0'}).sortable({
		 connectWith: 'ul.bandmusiker',
		 forcePlaceholderSize: true,
		 placeholder: 'ui-state-highlight',
		 tolerance: 'intersect',
		 delay: 300,
		 revert: true,
		 cursor: 'crosshair',
		 revertDuration: 200

	 }).disableSelection();
	 $('ul.bggandjjjmusiker > li').draggable({
			connectToSortable: 'ul.bandmusiker',
			helper: 'clone',
			revert: 'invalid'
		});

	 //$('ul.bandmusiker > li').draggable({axis:'y'});
	 $('ul.bandmusiker > ui-state-highlight').css({'padding': 5, 'width': 200});
});
function setupForms() {
	fs = $('form[ajax=1]');
	
	fs.each(function() {
		f = $(this);
		f.attr('action', 'A/' + f.attr('action'));
		var options = { 
	        //target:        '#output1',   // target element(s) to be updated with server response 
	        //beforeSubmit:  showRequest,  // pre-submit callback 
	        success: function(responseText, statusText, xhr, f) {
				message(responseText.message, responseText.error);
			},
			dataType:  'json'
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
		}; 
		f.ajaxForm(options);		
	});
	fs = $('form[message]');
	fs.each(function() {
		f = $(this);
		//f.fadeTo(0.5);
		bn = f.attr('button');
		m = f.attr('message');
		b = f.find('input[name="' + bn + '"]');
		if(b) {
//			b.css('border', '20px solid red');
			//alert(b.attr('value'));
			b.css('width', function(index){
				if(index==0) index=b.innerWidth();
				index = index + 50;
				return(index);
			});
			f.submit(function() {
				if(f.hasClass('submitted')) {
					return(false);
				} else {
					f.addClass('submitted');
					b.attr({
						'disabled': 'disabled',
						'value': m
						});
					setInterval(function() {
						var t = b.val();
						//alert(t);
						if (substr_count(t, '.') < 6){
							if (t.substr(0, 1) != ' ') {
								t = ' ' + t;
							}
							b.val(t + ' .');					
						} else {
							t = t.substr(1, t.length-1) + t.substr(0, 1);
							b.val(t);				
						}
					}, 150);	
					return(true);
				}
			});
		}
	});
}
function setupInputs() {
	i=$("input.datum");
	if(i) {
		$.datepicker.setDefaults($.datepicker.regional['']);
		$.datepicker.setDefaults({
			//showOn: 'both',
			//changeMonth: true,
			//changeYear: true,
			showAnim: 'fadeIn',
			numberOfMonths: [1, 4]
		});
		i.attr('autocomplete', 'off');
		i.datepicker($.datepicker.regional['de']);
	}
	
	$("input.uhrzeit").each(function() {
		i = $(this);
		w = parseFloat(i.css('width')) - 8;
		i.timePicker({step:30, width:w});		
	});
}
function setupTooltip() {
	i = $('a[title]');
	if(i) {
		i.tooltip({
			tip: '#tooltip',
			effect: 'fade',
			fadeOutSpeed: 100,
			predelay: 100,
			position: 'center right',
			opacity: 0.90
		});
	}
}
function setupAutocompleteMusiker() {
	inputs=$("input[rel='ajax_search']");
	inputs.attr('autocomplete', 'off');
	inputs.each(function() {
//		alert('hh');			
		var ms = $(this);
		var was = ms.attr('was');
		var res = ms.attr('res');
		var ul = ms.attr('ul');
//		var w = parseFloat(ms.css('width')) - 8;
		// default
		var mc = 3;
		var h = 250;
		var w = 500;
		var uurl = was;
		switch(was) {
			case 'alles':
				h=450;
				break;
			case 'konzert': case 'ort':
				uurl='ort';
				break;
			case 'musiker': case 'band': 
				break;
			case 'musiker2':
				uurl = 'musiker';
				break;
		}
		ms.autocomplete(null, {
			url: 'A/' + uurl + '_Search.asp',
			width: w,
			minChars: mc,
			highlight:false,
			multiple: false,
			matchContains: true,
			max:300,
			scrollHeight:h,
			formatItem: function(data, i, n, value) {
				id = data[1];
				feld = data[0];
				switch(was) {
					case 'musiker':
						return(feld);
						break;
					case 'musiker2':
						bild = data[2];
						bildwo = data[3];
						bildurl = '';
						switch(bildwo) {
							case '1':
								//http://www.jazz-kalender.de/~upload/MusikerBilder/DRPPUSOPSKLQPBA.jpg
								bildurl = '/~upload/MusikerBilder/' + bild;
								break;
							case '2':
								bildurl = bild;
								break;
						}
						if(bildurl=='') bildurl = '/Bilder/KeinThumb150.gif';

						//item = feld + ' ' + bild;
						item = "<img height=\'40\' src='" + bildurl + "' />" + feld;

//						item = '<img src="' + bild + '" height="30" />' + feld;
						return(item);
						break;
					case 'band':
						return(feld);
						break;
					case 'ort':
						return(feld);
						break;
					default:
						return(feld);
						break;
				}
				//return value.split(".")[0];

			},
			formatResult: function(data, value) {
				id = data[1];
				feld = data[0];
				switch(was) {
					case 'musiker':
						return(feld);
						break;
					case 'musiker2':
						return(feld);
						break;
					case 'band':
						return(feld);
						break;
					case 'ort':
						return(feld);
						break;
				}
			}
		});
		ms.result(function(event, data, formatted) {
			//var ms = $(this);
			/*
			res = ms.attr('res');
			was = ms.attr('was');
			*/
			id = data[1];
			feld = data[0];
			switch(was) {
				case 'musiker':
					break;
				case 'musiker2':
					bild = data[2];
					break;
				case 'band':
					break;
				case 'ort':
					break;
			}
			inputname = ms.attr('inputname');
			append = ms.attr('append');

			//bild = data[0];
			switch(res) {
				case 'li1':
					liId = inputname + '-' + id;
					ist = $('#' + liId).length;
					if(ist) {
						li = $('#' + liId).closest('li');
					} else {
						li = $('<li><input id="' + liId + '" name="' + inputname + '" checked="checked" value="' + id + '" type="checkbox"><label for="' + liId + '">' + feld + '</label></li>');
					}
					if(ul) {
						start= ms.offset();
						if(ist) {
							$('#' + liId).attr('checked', true);
						} else {
							if(append==1) {
								$('#' + ul).append(li);
							} else {
								$('#' + ul).prepend(li);
							}
						}
						ziel = li.offset();
						ms.val('').focus();
						li.css({'opacity':0});
						if(ist) {
							li2 = li.clone().css({'z-index':10, 'position':'absolute', 'top':ziel.top-5, 'left':ziel.left, 'opacity':1});
							$('#' + ul).append(li2);
							li2.animate({'left':ziel.left-20}, 200, function() {
								li2.animate({'left':ziel.left}, 200, function() {
									$(this).remove();
									li.css('opacity', 1);
								});
							});
						} else {
							li2 = li.clone().css({'z-index':10, 'position':'absolute', 'top':start.top, 'left':start.left, 'opacity':1});
							$('#' + ul).append(li2);
							var ab = (ziel.top - start.top) / 100;
							ab = 1 + ab / 10;
							var t = 150 * ab;
							li2.animate({'left': start.left-5}, 150).animate({'top':ziel.top-5, 'left':ziel.left-5}, t, function() {
								li2.animate({'left':ziel.left}, 150, function() {
									$(this).remove();
									li.css('opacity', 1);
								});
							});

						}
						// move
						/*
						li2.animate({'top':start.top + (ziel.top - start.top) / 2, 'left':start.left - (ziel.left - start.left + t / 25) / 2, 'scale': ab}, t, function() {
							li2.animate({'top':ziel.top-5, 'left':ziel.left, 'scale': 1}, t, function() {
								li2.css('opacity', 0).remove();
								li.css('opacity', 1);
							});
						});
						*/
/*

 */
					}
					break;
				case 'goDetail':
					$(this).val(feld);
					url=null;
					//if(!id) alert(was);
					switch(was) {
						case 'band':
							url = 'Jazz_Band.asp?ID=' + id;
							break;
						case 'musiker': case 'musiker2':
							url = 'Jazz_Musiker.asp?ID=' + id;
							break;
						case 'konzert':
							if(!id) {
								url = 'Suche_Jazz_Konzert.asp';
							} else {
								url = 'Jazz_Konzerte.asp?' + feld + '=&OrtID=' + id;
							}
							break;
					}
					if(url) {
						window.location.href=url;
					}
					break;	
			}
			//this.parent().next('ul').append(li);
			return(false);
		});
	});
}

function HoverTerminEditShow(id) {
	var TerminIDdiv = '';
	
}
function HoverTerminEdit() {
	var id = $('a.hte').attr('href');
	var i = id.indexOf('ID=');
	if (i != -1) {
		id = id.substr(i+3);
	}
	TerminID = id;
	$('a.hte').attr('href', '#');
	$('a.hte').hover(function(){
		$('div.TE').slideUp();
		$('#TE' + id).slideDown();
	});
	$('#TE' + id).mouseout(function(){
		$('#TE' + id).slideUp();
	});	
	$('a.hte').mouseout(function(){
		$('#TE' + id).slideUp();
	});	
}
function message(mess, stil) {
	var tempo=150;
	var warten =800;
	
	m = $("#message");
	m.removeClass().addClass(stil).text(mess);

	y=$(window).height()/3;
	x=$(window).width()/2;
	m.width(x);
	m.height(y);

	y=$(window).height()/6;
	x=$(window).width()/4;
	
	m.css('top', y).css('left', x);
	
	m.fadeIn(tempo).delay(warten).fadeOut(tempo);
}
function debug(data) {
	$('#foot3').text(data);
}
function jsaktiv(m) {
	alert(m);
}
function getMusiker(nachname, vorname) {
	
}
function nixx() {

}
function setupHover() {
	var t = 400;
	img = $('ul.detail > li.bild img');
	img.mouseenter(function() {
		img = $(this);
		position = img.position();

		div = img.closest('li');
		img2 = div.find('img.hover');
		if(!img2.length) {
			img2=img.clone();
			img2.addClass('hover');
			img2.css({'border':'6px solid #FFECCF', 'display': 'none', 'position':'absolute', 'top':position.top-6, 'left':position.left-6, 'z-index':'100'});
			img2.appendTo(div);
			img2.mouseleave(function() {
				$(this).fadeOut(t);

			});
		}
		img2.fadeTo(t, 1);
	});

li = $('li[rechts]');
	if(li) {
		li.children('a').attr('href', '#');
		li.click(function() {
			li=$(this);
			div = li.attr('rechts');
			div = li.children(div);
			div.find('input').focus();
		});
		li.hover(function() {
			li=$(this);
			div = li.attr('rechts');
			div = li.children(div);
			div.stop().fadeTo(t, 1);
			div.find('input').focus();
		}, function() {
			li=$(this);
			div = li.attr('rechts');
			div = li.children(div);
			div.stop().fadeOut(t);
			div.find('input').val('');
			$('div.ac_results').fadeOut(t);
		});

	}
	li = $('ul.tabs > li').hover(function() {
		var li =$(this);
		ul = li.find('ul.tabs');
		ul.stop().fadeTo(t, 1);
	}, function() {
		var li =$(this);
		ul = li.find('ul.tabs');
		ul.stop().fadeOut(t);

	});
	i = $('img.icon');
	i.hover(function() {
		$(this).stop().animate({scale:'1.1 1.1', skewX:'-5'}, t);
	}, function() {
		$(this).stop().animate({scale:'1 1', skewX:0}, t);
	});
	i = $('ul.ulKlein li');
	i.hover(function() {
		d = $(this).find('div.infos');
		c = d.find('div.copyright');
		c.fadeIn(t);

//		c=d.find('.jk_fotoThumb');
	//	c.removeClass('jk_fotoThumb').addClass('jk_fotoThumb2');
		d.stop().css('z-index', 10).animate({'left':'-50px', 'top':'-50px', 'opacity':1, 'width':380, 'height':160}, t);
		d = d.find('.jk_fotoThumb2');
		if(d) d.stop().switchClass('jk_fotoThumb2', 'jk_fotoThumb', t);
	}, function() {
		d = $(this).find('div.infos');
		c = d.find('div.copyright');
		c.css('display', 'none');

		//c=d.find('.jk_fotoThumb2');
		//c.removeClass('jk_fotoThumb2').addClass('jk_fotoThumb');
		d.stop().css('z-index', 5).animate({'left':'0', 'top':'0', 'opacity':0.1, 'width':300, 'height':60}, t, function() {
			$(this).css('display','none');
		});
		d = d.find('.jk_fotoThumb');
		d.stop().switchClass('jk_fotoThumb', 'jk_fotoThumb2', t);
	});
	i = $('#zzzz ul.ulKlein li');
	i.hover(function() {
		alert('hh');
		d = $(this).find('div.infos');
		d.stop().css('z-index', 10).animate({'left':'-50px', 'top':'-50px', 'opacity':1, 'width':380, 'height':160}, 400);
		d = d.find('img');
		d.animate({'height':'120'});
	}, function() {
		d = $(this).find('div.infos');
		d.stop().css('z-index', 0).animate({'left':'0', 'top':'0', 'opacity':0.7, 'width':280, 'height':60}, 400, function() {
			$(this).css('display','none');
		});
		d = d.find('img');
		d.animate({'height':'60'});
	});
}