jQuery(document).ready(function($) {
	
	$("img.star").click(function () {
		var helpId=this.id;
		var a=window.location.pathname;
		var imgPath= a.substring(0,a.indexOf("index.php")) + "typo3conf/ext/faplfang/res/img/";
		var imgName= $("#"+this.id).attr("src").indexOf("_ak") > 0 ? "star_ik.png" : "star_ak.png";
		var gew= this.id.substring(this.id.indexOf("-")+1);
		var z= imgName.indexOf("_ak")>0 ? 1 :0;
		//alert(gew);
		$("#"+this.id).attr("src",imgPath + imgName);
		//alert(this.id);
		$('#test-'+gew).load("index.php?eID=tx_faplfang_pi1&id="+gew+"&z="+z);
		return false;
			//alert("Bin da!");
		//$.post("index.php?id=369&tx_faplfang_pi1[action]=myFavSpot", {gew:this.id}, function(data){
		//$("#imgStar-"+this.id).after('Erfolg???');
		//});
		//alert("id ist " + this.id );
	});
	

	var ShowAndHideParts = function() {
		$(':input[name='+$(this).attr('name')+']:not(:checked)').each(function() {
			$('div.tx_faplfang_pi1_group_'+$(this).val())
				.hide()
				.find(':input')
				.addClass('ignore');
		});
		$(':input[name='+$(this).attr('name')+']:checked').each(function() {
			$('div.tx_faplfang_pi1_group_'+$(this).val())
				.show()
				.find(':input')
				.removeClass('ignore');
		});
	};
	
	var updateCountryList = function() {
		var code = $(this).attr('id').replace(/tx_faplfang_pi1_toggler_land_/g,'');
		if (code == 'de' || code == 'at' || code == 'ch' || code == 'nl' ) {
			$.ajax({
				url: 'index.php',
				type: 'POST',     
				data: {          
					eID: 'faplfang',
					action: 'getRegions',  
					code: code
				},
				dataType: 'json',
				success: function(data) {
					var oldVal = $(':hidden#region_id_selected').val();
					$(':input#tx_faplfang_pi1_region_id').empty();
					$(':input#tx_faplfang_pi1_region_id').append('<option selected="selected" disabled="disabled" value="">Bitte wählen Sie...</option>');
					$.each(data, function(entryIndex, entry) {
						$(':input#tx_faplfang_pi1_region_id').append('<option value="'+entry['uid']+'">'+entry['zn_name_local']+'</option>');
					});
					$(':input#tx_faplfang_pi1_region_id [value='+oldVal+']').attr("selected",true);

				}                                            
			});                                                   
		}
	}
	
	$("div.tab-content").hide();
	$("ul.navi_reiter li a.default").addClass("current");
	$("div.default").show();
	
	$("ul.navi_reiter li a").click(function() {
		
		$(this).parent()
				.siblings()
				.find("a")
				.removeClass("current");
		
		$(this).addClass("current");
		
		var currentTab = $(this).attr("href");
		currentTab = currentTab.replace(/index.php\?id=fangbuch/g, "");
		
		$(currentTab).siblings().hide();
		
		$(currentTab).show(); 

		
	});
	
	
	$(':input[name=tx_faplfang_pi1_toggler_land]')
		.change(updateCountryList)
		.filter(':checked')
		.each(updateCountryList);
	
	$(':input[name^=tx_faplfang_pi1_toggler_]')
		.each(ShowAndHideParts)		// set initial status
		.change(ShowAndHideParts);	// set status on every change
	
	
//	$(':input#tx_faplfang_pi1_q').autocomplete(
//		'index.php?eID=faplfang&action=getWaterNames',
//		{
//			cacheLength: 1
//		}
//	);


	$('#tx_faplfang_pi1_waterEditForm').validate({
			rules: {
				'tx_faplfang_pi1[bild_ok]': {
					required: '#tx_faplfang_pi1_karte:filled'
					},
				'tx_faplfang_pi1[longitude]': {
					required: true,
					number: true,
					minlength: 3
					},
				'tx_faplfang_pi1[latitude]': {
					required: true,
					number: true,
					minlength: 3
					},
				},	
			errorPlacement: function(error, element) {
					element.parent().addClass("error");
					error.prependTo( element.parent());
				},
				errorClass: 'message',
				errorElement: 'i',
				onkeyup: false, 
				onclick: false,
				ignore: '.ignore'
	});
		
	
	
	$('#tx_faplfang_pi1_catchEditForm').validate({
		rules: {
			'tx_faplfang_pi1[datum]'	: 'required',
			'tx_faplfang_pi1[fischart_id]'	: 'required',
			'tx_faplfang_pi1[groesse]': {
				required: true,
				digits: true
			},
			'tx_faplfang_pi1[gewicht]': {
				required: true,
				digits: true
			},
			'tx_faplfang_pi1[bild_ok]': {
				required: '#tx_faplfang_pi1_bild:filled'
			},
			'tx_faplfang_pi1[gewaesser_id]'	: 'required',
		},
		errorPlacement: function(error, element) {
			element.parent().addClass("error");
			error.prependTo( element.parent());
		},
		errorClass: 'message',
		errorElement: 'i',
		onkeyup: false, 
		onclick: false,
		ignore: '.ignore'
	});
	

	/********************************************************/	
	$.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,
		yearSuffix: ''
	};

	$.datepicker.setDefaults($.datepicker.regional['de']);
	
	$('.datepicker').datepicker({
		minDate: new Date(1970, 0, 0),
		maxDate: 0
	});


	/********************************************************/	
	
//	$.tablesorter.addParser({
//	    id: 'germandate',
//		is: function(s) {
//		        return false;
//		},
//		format: function(s) {
//			var a = s.split('.');
//			a[1] = a[1].replace(/^[0]+/g,"");
////			return new Date(a.reverse().join("/")).getTime();
//			return new Date(a[1]+'/'+a[0]+'/'+a[2]).getTime();
//		},
//		type: 'numeric'
//	});


//	$('#waterList').tablesorter();
//	
//	$('#waterList thead a')
//		.each(function() {
//			$(this).replaceWith($(this).text());
//		});

//	var url_str = $(':input#gewaesserUrl').val();
//	if (url_str.indexOf('?')!=-1) {
//		url_str += '&amp;';
//	}
//	else {
//		url_str += '?';
//	}
//
//	$('#tx_faplfang_pi1_waterFilterForm')
//		.prepend('<input type="hidden" name="eID" value="faplfang" />')
//		.prepend('<input type="hidden" name="action" value="getWaters" />');
//	
//	var page = 0;
//	
//	var ajaxLoadWaters = function() {
//		var str = $('#tx_faplfang_pi1_waterFilterForm').serialize() + '&page='+page;
//		$.post(
//			'index.php',
//			str,
//			function(data) {
//				count = data['count'];
//				$rows = data['rows'];
//				var row_str = '';
//				$.each($rows, function(entryIndex, entry) {
//					row_str += '<tr><td>'+entry['plz']+'</td>';
//					if (entry['name']) {
//						row_str += '<td><a class="gewaesserLink" href="'+url_str+'tx_faplfang_pi1[action]=show&amp;tx_faplfang_pi1[uid]='+entry['uid']+'">'+entry['name']+'</a></td>'; 
//					}
//					else {
//						row_str += '<td></td>'; 
//					}
//					if (entry['typ']) {
//						row_str += '<td>'+entry['typ']+'</td>'; 
//					}
//					else {
//						row_str += '<td></td>'; 
//					}
//					row_str += '</tr>';
//				});
//				$('#waterList tbody')
//					.empty()
//					.append(row_str);
//				$("#waterList").trigger("update");
//			},
//			"json"
//		);
//	};
	
//	$('a.gewaesserLink').live('mouseover',function(event) {
//		$thisLink = $(this);
//		var linktarget = $(this).attr('href').replace(/.*tx_faplfang_pi1\[uid\]=(\d+).*/,"$1");
//		$.ajax({
//			url: 'index.php',
//			type: 'POST',     
//			data: {          
//				eID: 'faplfang',
//				action: 'getWater',  
//				uid: linktarget
//			},
//			dataType: 'json',
//			success: function(data) {
//				$('div.gewaesserTooltip').remove();
//				$('<div></div>')
//					.addClass('gewaesserTooltip')
//					.css({
//						left: event.pageX+20+'px',
//						top: event.pageY+10+'px'
//					})
//					.html(data['strasse']+'<br />'+data['plz']+' '+data['stadt'])
//					.appendTo('body')
//					.fadeIn('slow');
//			}                                            
//		});                                                   
//	});
//		
//	$('a.gewaesserLink').live('mousemove',function(e) {
//		$('div.gewaesserTooltip')
//		.css("left",(e.pageX)+20 + "px")
//		.css("top",(e.pageY)+10 + "px");
//	})
//	.live('mouseout',function() {
//		$('div.gewaesserTooltip').remove();
//	});

//	$('#tx_faplfang_pi1_q')
//		.attr('autocomplete','off')
//		.attr('name','q')
//		.change(ajaxLoadWaters)
//		.keyup(ajaxLoadWaters);
//
//
//	$('#tx_faplfang_pi1_plz')
//		.attr('autocomplete','off')
//		.attr('name','plz')
//		.change(ajaxLoadWaters)
//		.keyup(ajaxLoadWaters);
//	
//	$('#tx_faplfang_pi1_w_fish')
//		.attr('name','fish[]')
//		.change(ajaxLoadWaters);
//
//	$('#tx_faplfang_pi1_w_type')
//		.attr('name','type[]')
//		.change(ajaxLoadWaters);
//	
//	$('#tx_faplfang_pi1_nodetails')
//		.attr('name','nodetails')
//		.change(function() {
//			page = 0;
//			ajaxLoadWaters();
//		});
//
//	$('#tx_faplfang_pi1_waterFilterForm :submit').remove();

	/********************************************************/	

	$('<input type="reset" /><span>&nbsp;</span>')
		.val('Liste zurücksetzen')
		.insertBefore('#tx_faplfang_pi1_waterFilterForm :submit')
		.click(function() {
			var $form = $('#tx_faplfang_pi1_waterFilterForm');
			$form.clearForm();
			$form.trigger('submit');
			return false;
		});
	
	/********************************************************/	
	
	$('#tx_faplfang_pi1_waterFilterForm')
		.before('<button type="button" id="tx_faplfang_pi1_waterFilterForm_toggler"></button>');
	$.cookie('tx_faplfang_pi1_waterFilterForm_toggler','on');
	
	if ($.cookie('tx_faplfang_pi1_waterFilterForm_toggler') != 'on') {
		$.cookie('tx_faplfang_pi1_waterFilterForm_toggler','off');
		$('#tx_faplfang_pi1_waterFilterForm_toggler').text('Filter einblenden');
		$('#tx_faplfang_pi1_waterFilterForm').hide();
	}
	else {
		$('#tx_faplfang_pi1_waterFilterForm_toggler').text('Filter ausblenden');
	}
	
	$('#tx_faplfang_pi1_waterFilterForm_toggler').click(function() {
		if ($.cookie('tx_faplfang_pi1_waterFilterForm_toggler') == 'off') {
			$(this).text('Filter ausblenden');
			$('#tx_faplfang_pi1_waterFilterForm').slideDown();
			$.cookie('tx_faplfang_pi1_waterFilterForm_toggler','on');
		}
		else {
			$(this).text('Filter einblenden');
			$('#tx_faplfang_pi1_waterFilterForm').slideUp();
			$.cookie('tx_faplfang_pi1_waterFilterForm_toggler','off');
		}
	});

	/********************************************************/
	
	$('.tx-comments-pi1').appendTo($('.tx-faplfang-pi2 .tabs #tabs-3'));
	$('.tabs .nav').show();
	$('.tabs').tabs();
	
});

