

/**
 * Opodien jQuery $(document).ready configuration for the frontend and user backend
 * @author	opodo
 * @package	Opodien
 * @see jquery.opodien_functions.js
 */

$(document).ready(function() {

	resetOnLoad();
	checkErrorPods();
	bindTooltips();

	/**
	 * fixes some issues on safari
	 */
	if ($.browser.safari) {
		//fix safari baseurl issue by setting the current url as action param
		$("form.action_is_current_href").each(function(index){
			$(this).attr("action", location.href);
		});
		
		//fix safari file upload issue
		$("#upload_form_inner input.file").css("margin-left", '140px');
		
		$("#mainLogoBig").css("margin-top", '216px');
		$("#mainLogo").css("top", '122px');
	}
	
	/**
	 * css fix destofday height
	 */
	if ($("#landingpage .tx-opomaindestofday-pi1 .destofday_text").length > 0) {
		var destofday_height = $("#landingpage .tx-opomaindestofday-pi1 .destofday_text").height();
		if (destofday_height > 0) 
			var newdestofday_height = destofday_height + 102;
			var destofday_height2 = $("#landingpage .tx-opomaindestofday-pi1 .destofday_links").height();
			if (newdestofday_height < 210) newdestofday_height = 210;
			if (newdestofday_height < destofday_height2 + 30) {
				newdestofday_height = destofday_height2 + 30;
			}
			$("#landingpage .tx-opomaindestofday-pi1 .mainpod").height(newdestofday_height);
	}
	
	
	/**
	 * bind geo matching autocompleter (specific item and overall)
	 */
	$("#geo_search_edit, .geo_search_edit_item").autocomplete(
		BASEDIR + "/ajax_geosearch.php",
		{
			delay:0,
			minChars:1,
			matchSubset:1, /*1*/
			matchContains:1, /*1*/
			cacheLength:1, /*10 */
			onItemSelect:updateGeobox,
			onFindValue:findValue,
			selectFirst:1
		}
	);
	
		
	/**
	 * bind geo matching autocompleter (select continent/country only)
	 */
	$(".geo_search_edit_item_countryonly").autocomplete(
		BASEDIR + "/ajax_geosearch.php?mode=countryonly",
		{
			delay:0,
			minChars:1,
			matchSubset:1, /*1*/
			matchContains:1, /*1*/
			cacheLength:1, /*10 */
			onItemSelect:updateGeobox,
			onFindValue:findValue,
			selectFirst:1
		}
	);

	/**
	 * bind destination search autocompleter
	 */
	$("#geo_search").autocomplete(
		BASEDIR + "/ajax_geosearch.php?show_continents=1",
		{
			delay:0,
			minChars:1,
			matchSubset:1, /*1*/
			matchContains:1, /*1*/
			cacheLength:1, /*10 */
			onItemSelect:updateGeobox,
			onFindValue:findValue,
			selectFirst:1
		}
	);

	$("input.geo_autocomplete_countryonly").autocomplete(
		BASEDIR + "/ajax_geosearch.php?mode=countryonly",
		{
			delay:0,
			minChars:1,
			matchSubset:1, /*1*/
			matchContains:1, /*1*/
			cacheLength:1, /*10 */
			selectFirst:1
		}
	);
	$("input.geo_autocomplete_all").autocomplete(
		BASEDIR + "/ajax_geosearch.php?show_continents=1",
		{
			delay:0,
			minChars:1,
			matchSubset:1, /*1*/
			matchContains:1, /*1*/
			cacheLength:1, /*10 */
			selectFirst:1
		}
	);
	$("input.geo_autocomplete").result(function(event, data, formatted) {
		updateGeoboxAutocompleter(event, data, formatted, this);
	});
	

	/**
	 * bind the link to apply tags to all items	
	 */
	$('a.tag2all').bind (
		"click",
		function(){
			var tag = $('div.tags:eq(0) input').fieldValue();
			var tag = tag[0];
			//log ('tagval', tag);			
			//log ('taglen', tag.length);
			replaceTags(tag);
			return false;
		}
	);

	
	$('.setTitleAsInputHint').each(
		function (index) {
			if ($(this).attr('title') != '' ) {
				if ($(this).val() == '') {
					$(this).css('color','#CDCECD');
					$(this).val($(this).attr('title'));
				}
				$(this).bind('focus',
					function(event) {
						if ($(this).val() == '') {
							$(this).css('color','#CDCECD');
							$(this).val($(this).attr('title'));
						}
						if ($(this).val() == $(this).attr('title')) {
							$(this).css('color','#000');
							$(this).val('');
						}
					}
				);
				$(this).bind('blur',
					function(event) {
						if ($(this).val() == '') {
							$(this).css('color','#CDCECD');
							$(this).val($(this).attr('title'));
						}
					}
				);
			}
		}
	);

	/**
	 * bind "updateGeobox" to the selection on geo matching in userbackend (gallery mode = edit)
	 */
	$("input.geo_search_edit_item, input.geo_search_edit_item_countryonly, input#geo_search_edit").result(function(event, data, formatted) {
		updateGeobox(event, data, formatted, this.id, false);
	});
	

	/**
	 * bind "updateGeobox" to the selection on manual geo search (gallery mode = edit)
	 */
	$("form#myopodien_form input#geo_search").result(function(event, data, formatted) {
		updateGeobox(event, data, formatted, this.id, true);
	});

	/**
	 * bind "updateGeobox" to the selection on user registration
	 */
	$("form#tx-srfeuserregister-pi1-fe_users_form input.geo_search_edit_item, form#tx-srfeuserregister-pi1-fe_users_form input.geo_search_edit_item_countryonly").result(function(event, data, formatted) {
		updateGeobox(event, data, formatted, this.id, false, 'tx-srfeuserregister-pi1-fe_users_form');
	});

	/**
	 * bind geo search
	 */
	$("form#geosearch_form input#geo_search").result(function(event, data, formatted) {
		updateModrewriteForm(event, data, formatted, this.id, true);
	});

	/**
	 * bind geo search
	 */
	$("form#reiseziele_form input#geo_search").result(function(event, data, formatted) {
		updateGeobox(event, data, formatted, this.id, true, 'reiseziele_form');
	});


	/**
	 * bind "change this item" links in userbackend
	 */
	$('.geo_change').each(
		function(index) {
			$(this).bind (
				"click",
				function(){
					//alert(this.id);
					var field_number = getFieldNumber(this.id)
					var field_number_suffix = '';
					if (field_number) field_number_suffix = '-' + field_number;
					setGeoStatus('reset', field_number_suffix);
					return false;
				}
			);
		}
	);



	/**
	 * makes sure the geo drill form stays consistent - binds ajax updates of the geo drill search to change events of the geo drill search
	 * -> on change of the geo drill form, the above geo entities are updated
	 * example 1: click on "Germany" -> "Europe" is selected
	 * example 2: click on "Munich" -> "Bavaria", "Germany", "Europe" are selected
	 * @see updateGeoboxSelection()
	 */
	$(".geobox_edit_item select, #geobox_edit select").each(
		function(index){
			$(this).bind (
				"change",
				function() {
					var id = $(this).fieldValue();
					updateGeoboxSelection(id, this.name, false, this.form.id);
				}
			);
		}
	);

	/**
	 * frontend navi_geo
	 * @see updateGeoboxSelection()
	 */
	$(".tx-opodiennavigeo-pi1 #geobox select").each(
		function(index){
			$(this).bind (
				"change",
				function() {
					var id = $(this).fieldValue();
					updateGeoboxSelection(id, this.name, true, 'reiseziele_form');
				}
			);
		}
	);


	/**
	 * bind geo search "apply" button (overall and single item on geo matching)
	 * @see updateGeoboxLabel()
	 * @see updateGeoboxSelection()
	 */
	$("#geo_button_apply, .geo_button_apply").bind(
		"click",
		function() {
			var suffix = '';
			var field_id = this.id;
			var field_number = getFieldNumber(field_id);
			var field_number_suffix = '';
			if (field_number) {
				field_number_suffix = '-' + field_number;
				suffix = '_item' + field_number_suffix;
			}
			
			var search_id = 'geo_search_edit' + suffix;
			search_id = 'input[@name=\'' + search_id + '\']';
			var searchstr = $(search_id).fieldValue();			
	
			if (searchstr != '') {
				//show indicator
				$('#geo_response_ok' + field_number_suffix).hide();
				$('#geo_response_error' + field_number_suffix).hide();
				$('#geo_response_listselection' + field_number_suffix).hide();
				$('#response_indicator_image' + field_number_suffix).show();
				$('#geo_search_edit' + field_number_suffix).hide(); //search field
				$('#geo_button_apply' + field_number_suffix).hide(); //apply button
				$('#geo_sample_text' + field_number_suffix).hide(); //sample code
	
				//initiate name search
				var query = ($(search_id).is('.geo_search_edit_item_countryonly')?'mode=countryonly&':'') +
					'max_results=1&q=' + searchstr;
				$.ajax({
					type: "GET",
					url: BASEDIR + "/ajax_geosearch.php",
					data: query,
					success: function(msg){
						log("ajax name search response", msg);
						
						var result = msg.split("|");						
						var name = result[0];
						var id = result[1];
						var entity = result[2];
						var country_name = result[3];
						
						if (typeof(entity) != 'undefined') var entity = jQuery.trim(entity);
	
						var label_id = '#geobox_edit' + suffix;
						var label_str = '';
						if (name && country_name) label_str = name + ', ' + country_name;
						if (label_str == ''){
							var label_str_temp = $('#geo_search_edit' + field_number_suffix).fieldValue();
							label_str = label_str_temp[0];
						} 
						updateGeoboxLabel(label_id, label_str)
						
						//updateGeobox(event, result, name, id, false);
						if (suffix) entity = entity + suffix;
						updateGeoboxSelection(id, entity, false);
					}
				});
			}
		}
	);
	
	
	/**
	 * bind Where i have been and where i want to go links on content pages
	 */
	$("a#side_usertargetsbookmark_wihb_actionlink, a#side_usertargetsbookmark_wiwtg_actionlink").bind(
		"click",
		function(event) {
				// only if there  is no href action defined
			if ( $(this).is('.side_usertargetsbookmark_actionlink_ajax') ) {
				event.preventDefault();
        		event.stopPropagation();
				var type = '';
				if (this.id == "side_usertargetsbookmark_wihb_actionlink") {
					type = 'wihb';
				} else {
					type = 'wiwtg';
				}
				
					// if exists
				if ($('#side_usertargetsbookmark_'+ type +'_response_text')) {
						// hide help text if visible
					$('#side_usertargetsbookmark_'+ type +'_help_text:visible').slideUp();

						// show indicatior/response message
					$('#side_usertargetsbookmark_'+ type +'_response_text').slideDown();
					
						// send via ajax
					$.ajax({
						type: "GET",
						url: BASEDIR_ROOT + "/index.php?eID=tx_oposideusertargetsbookmark_addUsertarget",
						data: '&targetflag=' + type + '&' + $('#side_usertargetsbookmark_geoquerystring').getValue(),
						success: function(msg) {
							if (msg != '') {
									// display success message
								$('#side_usertargetsbookmark_'+ type +'_response_text')[0].innerHTML = msg;
								
									// set status to success / ok
								$('#side_usertargetsbookmark_'+ type +'_response_text').addClass('side_usertargetsbookmark_response_ok');
								
									// replace the link (<a) by non-clickable text (<span)
								$('#side_usertargetsbookmark_'+ type +'_actionlink').after(
									'<span id="side_usertargetsbookmark_'+ type +'_actionlink">' +	
										$('#side_usertargetsbookmark_'+ type +'_actionlink')[0].innerHTML + 
									'</span>'
								);
									// remove the link
								$('#side_usertargetsbookmark_'+ type +'_actionlink').remove(); 
							}
						}
					});
				}
			}
		}
	);
	
	
	
	/**
	 * bind Where i have been and where i want to go links on content pages
	 */
	$("a.contacts_actionlink_ajax").bind(
		"click",
		function(event) {
				// only if there  is no href action defined
				event.preventDefault();
        		event.stopPropagation();
				
				var link = this; 
				var text = (this.id + '').replace('actionlink', 'response_text');
				
				if ($('#' + text)) {

						// show indicatior/response message
					$('#' + text).slideDown();
					
						// send via ajax
					$.ajax({
						type: "GET",
						url: BASEDIR_ROOT + "/index.php?eID=tx_opocontacts",
						data: '&action=' + (this.rel),
						success: function(msg) {
							if (msg != '') {
									// display success message
								$('#' + text)[0].innerHTML = msg;
								
									// set status to success / ok
								$('#' + text).addClass('side_userinfo_contact_response_ok');
								
									// replace the link (<a) by non-clickable text (<span)
								$('#' + link.id).after(
									'<span id="' + (this.id) + '">' +	
										link.innerHTML + 
									'</span>'
								);
									// remove the link
								$(link).remove(); 
							}
						}
					});
				}
		}
	);

	/**
	 * bind help links for Where i have been and where i want to go links on content pages
	 */
	$("#side_usertargetsbookmark_wihb_help, #side_usertargetsbookmark_wiwtg_help").bind(
		"click",
		function(event) {
			event.preventDefault();
        	event.stopPropagation();
			if ($('#' + this.id + '_text')) {
				  $('#' + this.id + '_text').slideToggle();
			}
		}
	);
	
	

	/**
	 * geo matching: resets an item to starting values
	 * @param string suffix identified the item by "_item-123"
	 */
	function resetItem(suffix) {
		$('#myopodien_form').deserialize(eval("data={'geo_continent" + suffix + "': '0', 'geo_country" + suffix + "': '0', 'geo_region" + suffix + "': '0', 'geo_city" + suffix + "': '0'}"));
		$('#geo_search_edit' + suffix).clearFields();
		$('#geobox_edit' + suffix).show();
		$('#geo_response_ok' + suffix).hide();
	}

	/**
	 * binds the DOM flag whether the input of the first picture should be applied to all other pictures
	 */
	$('input[@name=geoall]').bind (
		"click",
		function () {
			var val = $(this).fieldValue();
			//dehighlightError(true, null);
			//dehighlightError(false, 'all');
			if (val == false) {
				//user input is different for each item
				//reset form
				resetItem('');		
				$('.geobox_edit_item').show();		
				$('.geo_editall_message').hide();
			} else {
				//user input is the same for each item
				$('.geobox_edit_item').hide();
				$('span.geolabel').show();
				$('#geo_edit_all').show();
				$('.geo_editall_message').show();
			}
		}
	);
	
	/**
	 * bind save form button
	 */
	$('.tx-myopodien-pi1 img.save_final').bind (
		"click", 
		function() {
			return $('#myopodien_form').submit();
		}
	);
	$('.tx-myopodien-pi1 form').bind (
		"submit", 
		function(event) {
			if (!checkForm('myopodien_form', true)) {
        			event.preventDefault();
        			event.stopPropagation();
				if ($('#general_error_message')) $('#general_error_message').ScrollTo(500);
				return false;
			}
		}
	);



	/**
	 * handle userreports and usertips in one check method
	 */
	$('.tx-myopodien-pi1-userreports img.save_final, .tx-myopodien-pi1-usertips img.save_final').bind (
		"click", 
		function() {
			return $('#myopodien_form').submit();
		}
	);
	$('.tx-myopodien-pi1-userreports form, .tx-myopodien-pi1-usertips form').bind (
		"submit", 
		function(event) {
			if (!checkFormReports('myopodien_form', true)) {
        			event.preventDefault();
        			event.stopPropagation();
				if ($('#general_error_message')) $('#general_error_message').ScrollTo(500);
				return false;
			}
		}
	);


	/**
	 * handle usertargets
	 */
	$('.tx-myopodien-pi1-usertargets img.save_final').bind (
		"click", 
		function() {
			return $('#myopodien_form').submit();
		}
	);
	$('.tx-myopodien-pi1-usertargets form').bind (
		"submit", 
		function(event) {
			if (!checkFormTargets('myopodien_form', true)) {
                                event.preventDefault();
                                event.stopPropagation();
				if ($('#general_error_message')) $('#general_error_message').ScrollTo(500);
				return false;
                        }
		}
	);


	/**
	 * handle userreports and usertips in one check method, redirect to upload form
	 */
	$('.tx-myopodien-pi1-userreports a#button_picassign_upload').bind (
		"click", 
		function() {
			checkFormReportsUploadRedirect('myopodien_form');
			return false;
		}
	);


	
	/**
	 * bind search button - initiate namesearch, display the results
	 */
	$("#geo_button_search").bind(
		"click",
		function(event){
			event.preventDefault();
			event.stopPropagation();

			var searchstr = $("#geosearch_form #geo_search").fieldValue();
			
			if (searchstr != '') {
				//hide search button, show indicator
				//$("#geosearch_form #geo_button_search").hide();
				//$("#geosearch_form #response_indicator_image").show();
				
				$.getJSON(BASEDIR + "/ajax_geosearch.php",{show_continents: 1, mode: 'fullnamesearch', result_is_json:'1', q: searchstr}, function(msg){
					
					log("ajax name search response", msg);

					var pagekey = $('input[@name=pagekey]').fieldValue();
					var resultset = '';
					for (var i = 0; i < msg.length; i++) {
						var name = msg[i].name;
						var id = msg[i].uid;
						var entity = msg[i].entity;
						var parent_name = msg[i].parent_name;
						var geo_url = msg[i].geo_url;
						
						if(parent_name) parent_name = ', ' + parent_name;
						
						resultset += '<li><a href="' + pagekey + '/' + geo_url + '">' + name + parent_name + '</a></li>';
					}						
					
					if (resultset) {
						$("#geosearch_results").html($.i18n._('search_your_searchresults') + ': <ul>' + resultset + '</ul>');
					} else {
						var message = '<img border="0" src="fileadmin/templates/' + LOCALE + '/img/icon_info.gif" style="float:left;"/><h5>' + searchstr + ' ' + $.i18n._('search_no_results') + '</h5> ' + $("#geosearch_error").html();
						$("#geosearch_results").html(message);
					}
					
				})
				
			}
		}
	);

	/**
	 * clears selectbox options (e.g. geo drill selects)
	 * @param string selectbox_id DOM selector of the selectbox
	 */
	function clearSelectboxes(selectbox_id) {
		$(selectbox_id).html('');
	}

	/**
	 * prepare for mod rewrite and jump to the new page by submitting the form
	 * @see setModRewriteFoundation()
	 */
	$("#geo_button_select").bind(
		"click",
		function(event){
			event.preventDefault();
			event.stopPropagation();
			$(this).hide();
			$("#geodrill_form #response_indicator_image").show();
			setModRewriteFoundation();
			$('#opodien_form').submit();
		}
	);
	
	
	/**
	 * geo matching: update countries on continent change (userbackend)
	 */
	$(".geo_response_listselection select[@name*=selectgeo_continent]").each(
		function(index) {
			$(this).bind(
				"change",
				function(){
					var field_number_suffix = '';
					var suffix = '';
					var field_id = this.id;
					var field_number = getFieldNumber(field_id);
					if (field_number) field_number_suffix = '-' + field_number;
					if (field_number_suffix) suffix = '_item' + field_number_suffix;
					clearSelectboxes('.geo_response_listselection select[@name=selectgeo_country' + suffix + ']');
					$.getJSON(BASEDIR + "/ajax_geosearch.php",{mode:'countriesbycontinent', id: $(this).val()}, function(j){
						var options = '';
						for (var i = 0; i < j.length; i++) {
							options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
						}
						if (options) {
							options = '<option value="0">' + $.i18n._('geobox_drill_choose_country') + '</option>' + options;
						} else {
							options = '<option value="">' + $.i18n._('geobox_drill_choose_no_country') + '</option>' + options;
						}
						$(".geo_response_listselection select[@name=selectgeo_country" + suffix + "]").html(options);
						if (!$.browser.msie) $('.geo_response_listselection select[@name=selectgeo_country' + suffix + '] option:eq(0)').attr('selected', 'selected');
					})
				}
			);
		}
	);		

	
	
	/**
	 * geo drill selects: update countries on continent change
	 */
	$("#geodrill_form select#geo_continent").change(function(){
		clearSelectboxes('#geodrill_form select#geo_region');
		clearSelectboxes('#geodrill_form select#geo_city');
		$.getJSON(BASEDIR + "/ajax_geosearch.php",{mode:'countriesbycontinent', id: $(this).val()}, function(j){
			var options = '';
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			if (options) {
				options = '<option value="0">' + $.i18n._('geobox_drill_choose_country') + '</option>' + options;
			} else {
				options = '<option value="">' + $.i18n._('geobox_drill_choose_no_country') + '</option>' + options;
			}
			$("#geodrill_form select#geo_country").html(options);
			if (!$.browser.msie) $('#geodrill_form select#geo_country option:eq(0)').attr('selected', 'selected');
		})
	})
	
	/**
	 * geo drill selects: update regions on country change
	 */
	$("#geodrill_form select#geo_country").change(function(){
		clearSelectboxes('#geodrill_form select#geo_city');
		$.getJSON(BASEDIR + "/ajax_geosearch.php",{mode:'regionsbycountry', id: $(this).val()}, function(j){
			var options = '';
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			if (options) {
				options = '<option value="0">' + $.i18n._('geobox_drill_choose_region') + '</option>' + options;
			} else {
				options = '<option value="">' + $.i18n._('geobox_drill_choose_no_region') + '</option>' + options;
			}
			$("#geodrill_form select#geo_region").html(options);
			if (!$.browser.msie) $('#geodrill_form select#geo_region option:eq(0)').attr('selected', 'selected');
		})
	})	
	
	/**
	 * geo drill selects: update cities on region change
	 */	
	$("#geodrill_form select#geo_region").change(function(){
		$.getJSON(BASEDIR + "/ajax_geosearch.php",{mode:'citiesbyregion', id: $(this).val()}, function(j){
			var options = '';
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			if (options) {
				options = '<option value="0">' + $.i18n._('geobox_drill_choose_city') + '</option>' + options;
			} else {
				options = '<option value="">' + $.i18n._('geobox_drill_choose_no_city') + '</option>' + options;
			}
			$("#geodrill_form select#geo_city").html(options);
			if (!$.browser.msie) $('#geodrill_form select#geo_city option:eq(0)').attr('selected', 'selected');
		})
	})		


	/**
	 * picture upload: bind upload button (show indicator, hide button)
	 */
	$('#uploadbutton').bind (
		"click", 
		function() {
			$('#uploadbutton').hide();
			$('#upload_indicator_text').show();
			$('#upload_indicator_image').show();
			$('#myopodien_form').submit();
		}
	);


	/**
	 * textlimiter for editing pictures and "about me" text
	 */
	$("textarea.comment_text, textarea.geo_edit_name, textarea#tx-srfeuserregister-pi1-tx_opouserfields_about_me").textLimiter(); /* all textarea's with an attribut maxlength will get a textlimiter */  



	/**
	 * show the flash worldmap, when #flash_worldmap exists
	 */
	$("#flash_worldmap").media({ width: 481, height: 163, autoplay: true });


	/**
	 * bind "bad content" link to show description message
	 */
	$(".badcontent a.badcontent").hover(
		function () {$(this).parent().children('span.badcontent_message').css('visibility', 'visible');},
		function () {$(this).parent().children('span.badcontent_message').css('visibility', 'hidden');}
	);

	/**
	 * validate and submit "bad content" form
	 */
	$("#submit_badcontent").bind(
		"click",
		function () {
			var fullname = $("input[@name=fullname]").fieldValue();
			if (fullname == '') {
				highlightErrorDirect("input[@name=fullname]", 'border');
			} else {
				dehighlightErrorDirect("input[@name=fullname]", 'border');
			}
			var email = $("input[@name=email]").fieldValue();
			if (email == '') {
				highlightErrorDirect("input[@name=email]", 'border');
			} else {
				dehighlightErrorDirect("input[@name=email]", 'border');
			}
			var text = $("textarea[@name=text]").fieldValue();
			if (text == '') {
				highlightErrorDirect("textarea[@name=text]", 'border');
			} else {
				dehighlightErrorDirect("textarea[@name=text]", 'border');
			}
			if ((fullname != '') && (email != '') && (text != '')) {
				$("#badcontent_form").submit();
			} else {
				$("#general_error_message").show();
			}
			return false;
		}
	);


	/**
	 * validate and submit "site feedback" form
	 */
	$("#submit_sitefeedback").bind(
		"click",
		function () {
			var fullname = $("input[@name=fullname]").fieldValue();
			if (fullname == '') {
				highlightErrorDirect("input[@name=fullname]", 'border');
			} else {
				dehighlightErrorDirect("input[@name=fullname]", 'border');
			}
			var email = $("input[@name=email]").fieldValue();
			if (email == '') {
				highlightErrorDirect("input[@name=email]", 'border');
			} else {
				dehighlightErrorDirect("input[@name=email]", 'border');
			}
			var text = $("textarea[@name=text]").fieldValue();
			if (text == '') {
				highlightErrorDirect("textarea[@name=text]", 'border');
			} else {
				dehighlightErrorDirect("textarea[@name=text]", 'border');
			}
			if ((fullname != '') && (email != '') && (text != '')) {
				$("#sitefeedback_form").submit();
			} else {
				$("#general_error_message").show();
			}
			return false;
		}
	);


	/**
	 * validate and submit "comment" form
	 */
	$("#submit_comment_button").bind(
		"click",
		function () {
			var text = $("textarea[@name=text]").fieldValue();
			if (text == '') {
				highlightErrorDirect("textarea[@name=text]", 'border');
			} else {
				dehighlightErrorDirect("textarea[@name=text]", 'border');
			}
			if (text != '') {
				$(".comment_add #submit_comment_button").hide();
				$(".comment_add #response_indicator_image").show();
				$("#comment_form").submit();
			} else {
				$(".comment_add #general_error_message").show();
			}
			return false;
		}
	);



	/**
	 * bind first step deletion buttons
	 */
	$(".tx-myopodien-pi1-userreports .button_item_delete, .tx-myopodien-pi1-usertips .button_item_delete, .tx-myopodien-pi1 .button_item_delete").each(
		function(index){
			$(this).bind(
				"click",
				function(){
					var suffix = '';
					var field_id = this.id;
					var field_number = getFieldNumber(field_id);
					$("#message_item_delete-" + field_number).hide();
					$("#message_item_delete_confirm-" + field_number).show();
				});
			}
		);


	/**
	 * bind no-deletion buttons
	 */
	$(".tx-myopodien-pi1-userreports .button_item_delete_confirm_false, .tx-myopodien-pi1-usertips .button_item_delete_confirm_false, .tx-myopodien-pi1 .button_item_delete_confirm_false").each(
		function(index){
			$(this).bind(
				"click",
				function(){
					var suffix = '';
					var field_id = this.id;
					var field_number = getFieldNumber(field_id);
					$("#message_item_delete_confirm-" + field_number).hide();
					$("#message_item_delete-" + field_number).show();
				});
			}
		);
		
	/**
	 * general delete item by an ajax request function
	 * @return void
	 * @param string pid
	 * @param string id
	 * @param {Object} callback
	 * @param bool delete_parent
	 */
	function deleteItemAjax(pid, id, callback, delete_parent) {
		var field_id = id;
		var field_number = getFieldNumber(field_id);
		var query = 'id=' + pid + '&ajax=1&mode=deletion_confirm&item_id=' + field_number;
		$.ajax({
			type: "GET",
			url: BASEDIR_ROOT + "/index.php",
			data: query,
			success: function(msg){
				log("deletion request response", msg);
				$("#useritem-" + field_number).fadeOut('fast', callback);
				if (delete_parent == true) {
					$("#useritem-" + field_number).parent().remove();
				} else {
					$("#useritem-" + field_number).remove();
				}
			}
		});
	}

	/**
	 * userreports: bind deletion confirm buttons to deletion request func
	 */
	$(".tx-myopodien-pi1-userreports .button_item_delete_confirm_true").each(
		function(index){
			$(this).bind(
				"click",
				function(){
					deleteItemAjax(PID_REPORT_CONTAINER, this.id, 
						function (id) {
							if (itemExists(".useritem") == false) {
								location.href= 'index.php?id=' + PID_REPORT_WRITE; //if no element is left, go to create page
							}
						});
				});
			}
		);

	/**
	 * usertips: bind deletion confirm buttons to deletion request func
	 */
	$(".tx-myopodien-pi1-usertips .button_item_delete_confirm_true").each(
		function(index){
			$(this).bind(
				"click",
				function(){
					deleteItemAjax(PID_TIP_CONTAINER, this.id, 
						function (id) {
							if (itemExists(".useritem") == false) {
								location.href= 'index.php?id=' + PID_TIP_WRITE; //if no element is left, go to create page
							}
						});
				});
			}
		);

	/**
	 * pictures: bind deletion confirm buttons to deletion request func
	 */
	$(".tx-myopodien-pi1 .button_item_delete_confirm_true").each(
		function(index){
			$(this).bind(
				"click",
				function(){
					deleteItemAjax(PID_PICTURE_EDIT, this.id, 
						function (id) {
							if (itemExists(".gallery_image") == false) {
								location.href= 'index.php?id=' + PID_PICTURE_UPLOAD; //if no element is left, go to upload page
							}
						}, true);
				});
			}
		);


	/**
	 * userreports: bind make public buttons to make public func
	 */
	$(".tx-myopodien-pi1-userreports a.button_item_makepublic").each(
		function(index){
			$(this).bind(
				"click",
				function(){
					var field_id = this.id;
					var field_number = getFieldNumber(field_id);
					$("#useritem-" + field_number + " .button_item_makepublic").hide();
					$("#useritem-" + field_number + " #response_indicator_image").show();
					makePublicAjax(PID_REPORT_CONTAINER, this.id, false, false, 'userreport'); 
				});
			}
		);

	/**
	 * usertips: bind make public buttons to make public func
	 */
	$(".tx-myopodien-pi1-usertips a.button_item_makepublic").each(
		function(index){
			$(this).bind(
				"click",
				function(){
					var field_id = this.id;
					var field_number = getFieldNumber(field_id);
					$("#useritem-" + field_number + " .button_item_makepublic").hide();
					$("#useritem-" + field_number + " #response_indicator_image").show();
					makePublicAjax(PID_TIP_CONTAINER, this.id, false, false, 'usertip'); 
				});
			}
		);
		
	/**
	 * usertargets: bind "show all" links in travel profile
	 */
	$(".usertargets_actionlink_extendform").each(
		function(index){
			$(this).click(
				function(event){
        				event.preventDefault();
        				event.stopPropagation();
					targetflag = (this.id == 'usertargets_actionlink_extendform_wihb'? 0 : 1);
					$.ajax({
						type: "POST",
						url: BASEDIR_ROOT + "/index.php?eID=tx_myopodien_addGeoboxes",
						data: "&targetflag="+ targetflag +"&offset="+$('#usertargets_geoboxes_wihb .geo_status').length,
						success: function(msg){ 
							$('#usertargets_geoboxes_' + (targetflag==0? 'wihb' : 'wiwtg') ).append(msg);
							$(".geo_search_edit_item_countryonly").autocomplete(
								BASEDIR + "/ajax_geosearch.php?mode=countryonly",
								{	delay:0,
									minChars:1,
									matchSubset:1, /*1*/
									matchContains:1, /*1*/
									cacheLength:1, /*10 */
									onItemSelect:updateGeobox,
									onFindValue:findValue,
									selectFirst:1
								} );
							$("input.geo_search_edit_item, input.geo_search_edit_item_countryonly, input#geo_search_edit").result(function(event, data, formatted) {
								updateGeobox(event, data, formatted, this.id, false);
							});
						}
        			});
				}
			);
		}
	);
	


	/**
	 * general delete item by an ajax request function
	 * @return void
	 * @param string pid
	 * @param string id
	 * @param {Object} callback
	 * @param bool delete_parent
	 * @param string item_type
	 */
	function makePublicAjax(pid, id, callback, delete_parent, item_type) {
		var field_id = id;
		var field_number = getFieldNumber(field_id);
		var query = 'id=' + pid + '&ajax=1&mode=makepublic&item_id=' + field_number;
		$.ajax({
			type: "GET",
			url: BASEDIR_ROOT + "/index.php",
			data: query,
			success: function(msg){
				log("makepublic request response", msg);
				$("#useritem-" + field_number + " .makepublicwrapper").hide();
				if (item_type == 'userreport') {
					$("#useritem-" + field_number + " .controls a:eq(1)").html($.i18n._('makepublic_report'));
					$("#useritem-" + field_number + " .change_message h5").html($.i18n._('makepublic_report_is_public'));
				}
				if (item_type == 'usertip') {
					$("#useritem-" + field_number + " .controls a:eq(1)").html($.i18n._('makepublic_tip'));
					$("#useritem-" + field_number + " .change_message h5").html($.i18n._('makepublic_tip_is_public'));
				}
				if (item_type == 'picture') {
					$("#useritem-" + field_number + " .controls a:eq(1)").html($.i18n._('makepublic_picture'));
					$("#useritem-" + field_number + " .change_message h5").html($.i18n._('makepublic_picture_is_public'));
				}
			}
		});
	}
	
	
	/**
	 * bind help links hotelreviews
	 */
	$(".hotelreview_denied_helper").bind(
		"click",
		function(event) {
			var item_id = getFieldNumber(this.id);
			$('#hotelreview_item-' + item_id).hide();
			$('#hotelreview_item_denied-' + item_id).show();
		}
	);

	
	$(".hotelreview_item_denied_close").bind(
		"click",
		function(event) {
			var item_id = getFieldNumber(this.id);
			$('#hotelreview_item_denied-' + item_id).hide();
			$('#hotelreview_item-' + item_id).show();
		}
	);

	$("#hotelreview_disclaimer_open").bind("click",function(event) {$('#hotelreview_disclaimer').show();});
	$("#hotelreview_disclaimer_close").bind("click",function(event) {$('#hotelreview_disclaimer').hide();});
	

	//bind the ispublic switch to change user preference
	$("input.hotelreview_ispublic, img.hotelreview_ispublic_icon, span.hotelreview_ispublic_text").bind(
		"click",
		function() {
			$('#hotelreview_ispublic_loadingindicator').show();
			var new_value = getFieldNumber(this.id);

			$('.hotelreview_public').addClass('inactive');
			$('.hotelreview_private').addClass('inactive');

			$('input.hotelreview_ispublic').setValue(new_value);

			$.ajax({
				type: "GET",
				url: BASEDIR_ROOT + "/index.php",
				data: "id=" + AJAX_HANDLER_PID + "&mode=set_userfield_hotelreview_ispublic&new_value=" + new_value,
				success: function(msg){
					if (new_value == 0) {
						$('.hotelreview_public').addClass('inactive');
						$('.hotelreview_private').removeClass('inactive');
					}	
					if (new_value == 1) {
						$('.hotelreview_public').removeClass('inactive');
						$('.hotelreview_private').addClass('inactive');
					}
					$('#hotelreview_ispublic_loadingindicator').hide();
				}
			});
		}
	);


	$('.texttruncate1500').truncate( 1500,{
        	chars: /\s/, 
		trail: ["... <a href='#' class='truncate_show'>"+ $.i18n._('more') +" &gt</a>",""]
	 });
	$('.tx_opomainguides_catlinks').click(function() {
			$('.truncate_show').trigger('click');
	});


	
	$('.thickbox').bind(
		"click",
		function() {
    			$('embed').hide();
		});
		
	if ($('.side_userinfo_writemessage a') && $('#comment_form textarea')) {
		$('.side_userinfo_writemessage a').bind(
			"click",
			function(event) {
					window.setTimeout("$('#comment_form textarea:first').focus()",500);
					event.stopPropagation();
			}
		);
	}

	$('#content_right #geoboxdrill .choicebox_inner select, '+
	  '#content_right #geoboxdrill .choicebox_inner .action-button, '+
	  '#content_right #geoboxsearch .choicebox_inner input, '+
	  '#content_right #geoboxsearch .choicebox_inner .action-button, ' +
	  '#content_right .tx-opomainnavimap-staticmap' ).css({visibility:'visible'});
		
}); //eof document ready

