$(function(){
  $('a.photoThumb').flyout();

		// Klick auf pageJump
		$('a.pageJump').css('cursor', 'pointer').click(function(){
			strValues = $(this).attr('name');
			arrValues = strValues.split('_');
			var form = $('form#formSubmenu');
			if (form.length== 0) {
			 $('body').append('<form action="/verwaltung.html" method="post" name="formSubmenu" id="formSubmenu"></form>');
      }
			for(i=0; i<arrValues.length; i=i+2) {
				if ($('form#formSubmenu input[name="'+arrValues[i]+'"]').length > 0) {
					$('form#formSubmenu input[name="'+arrValues[i]+'"]').val(arrValues[i+1]);
				} else {
					$('form#formSubmenu').append('<input name="'+arrValues[i]+'" type="hidden" value="'+arrValues[i+1]+'" />');
        }
			}
			$('form#formSubmenu').submit();
		});
		
		// markiere den Container mit 'inserate'
		$('#cbRoomContent').addClass('inserate');
		
		// aktiviere den Hover in Tabellen mit der Klasse 'activateHover'
		$("table.activateHover tbody tr").hover(function(){
			$(this).addClass('rowhover');
		}, function(){
			$(this).removeClass('rowhover');
		});
});



function helpInfo(f_sGroup, f_sField) {
  helpWindow = window.open("/fileadmin/helpinfo/de/"+f_sGroup+"/"+f_sField+".html", "helpWindow", "width=550,height=600, scrollbars=yes");
}

function layerPositioning(f_sLayerId, f_oRef, f_nTop, f_nLeft) { 
    myPos = $(f_oRef).position();
    newTop = myPos.top+f_nTop;
    newLeft = myPos.left+f_nLeft;
    $('#'+f_sLayerId).css('top', newTop+'px');
    $('#'+f_sLayerId).css('left', newLeft+'px');
}

function changeMarkt(f_sSelect) {
	$('#form_markt').hide();
	$.ajax({
		type: "POST",
		url: "/typo3conf/ext/handwer_und_industrie_eu/pi1/ajax/getMarktOptionList.php",
		data: "branche="+$(f_sSelect).val(),
		dataType: 'json',
		success: function(json){
			$('#form_markt').html('');
			var options = '';
			for(i=0; i<json.markt.length; i++) {
				options += '<option value="' + json.markt[i].key + '">' + json.markt[i].value + '</option>';
			}
			$('#form_markt').html(options);
		}
	});
	setTimeout("$('#form_markt').show();", 500);
}
function changeCountry(f_oSelector) {
	$('#form_region').hide();
	$.ajax({
		type: "POST",
		url: "/typo3conf/ext/handwer_und_industrie_eu/pi1/ajax/getRegionOptionList.php",
		data: "country="+$(f_oSelector).val(),
		dataType: 'json',
		success: function(json){
			$('#form_region').html('');
			var options = '';
			for(i=0; i<json.regions.length; i++) {
				options += '<option value="' + json.regions[i].key + '">' + json.regions[i].value + '</option>';
			}
			$('#form_region').html(options);
		}
	});
	setTimeout("$('#form_region').show();", 500);
}	

  function changeCountryWithZip(f_oSelector) {
		$('#form_region').hide();
		$('#form_zipcode').hide();
		$('#form_object').hide();
		$.ajax({
			type: "POST",
			url: "/typo3conf/ext/handwer_und_industrie_eu/pi1/ajax/getRegionOptionList.php",
			data: "country="+$(f_oSelector).val(),
			dataType: 'json',
			success: function(json){
				$('#form_region').html('');
				var options = '';
				for(i=0; i<json.regions.length; i++) {
					options += '<option value="' + json.regions[i].key + '">' + json.regions[i].value + '</option>';
				}
				$('#form_region').html(options);
			}
		});
		$.ajax({
			type: "POST",
			url: "/typo3conf/ext/handwer_und_industrie_eu/pi1/ajax/getZipcodeOptionList.php",
			data: "country="+$(f_oSelector).val(),
			dataType: 'json',
			success: function(json){
				$('#form_zipcode').html('');
				var options = '';
				for(i=0; i<json.zips.length; i++) {
					options += '<option value="' + json.zips[i].key + '">' + json.zips[i].value + '</option>';
				}
				$('#form_zipcode').html(options);
			}
		});
		var object_html = '<option value="">----</option>';
		object_html += '<option value="ware">Ware</option>';
		object_html += '<option value="immobilie">Immobilie</option>';
		object_html += '<option value="stelle">Stelle</option>';
		$('#form_object').html(object_html);
		
		setTimeout("$('#form_region').show();$('#form_zipcode').show();$('#form_object').show();", 500);
	}	
  function changeWare(f_oSelector) {
	  $('#form_warentyp').hide();
	  $('#form_verk_verm').hide();
	  $.ajax({
			  type: "POST",
			  url: "/typo3conf/ext/handwer_und_industrie_eu/pi1/ajax/checkMarktName.php",
			  data: "marktId="+$(f_oSelector).val(),
			  dataType: 'json',
			  success: function(json){
		  		if(json.markt == 'immobilie') {
		  			$('#form_stelle').hide();
		  			$('#form_warentyp').hide();
		  			$('#form_verk_verm').show();
		  			$('#waren_label').empty().append('Art des Angebots:');
		  		} else if (json.markt == 'stelle') {
		  			$('#form_verk_verm').hide();
		  			$('#form_warentyp').hide();
		  			$('#form_stelle').show();
		  			$('#waren_label').empty().append('Art des Angebots:');
		  		}
		  		else {
		  			$('#form_verk_verm').hide();
		  			$('#form_stelle').hide();
		  			$('#waren_label').empty().append('Art der Ware:');
		  			$('#form_warentyp').show();
		  		}
	  		}
	  });
  }

function inseratMerken(f_nInseratId, f_sDivId) {
  $.ajax({
		type: "POST",
		url: "/typo3conf/ext/handwer_und_industrie_eu/inseratsverwaltung/ajax/merklisteMerken.php",
		data: "insId="+f_nInseratId,
//		dataType: 'json',
		success: function(returnText){
//		alert(returnText);
			$('#'+f_sDivId).html('');
			$('#'+f_sDivId).html(returnText);
			
		}
	});
}
	
function showMerkliste(f_oRef) {
  $.ajax({
		type: "POST",
		url: "/typo3conf/ext/handwer_und_industrie_eu/inseratsverwaltung/ajax/merklisteGet.php",
//		dataType: 'json',
		success: function(returnText){
		  $("#merkliste").remove();
		  $("body").append('<div id="merkliste">'+returnText+'</div>')
      layerPositioning('merkliste', f_oRef, 20, -1*$('#merkliste').width());  
		}
	});
}

function showWeiterempfehlen(f_oRef) {
  $.ajax({
		type: "POST",
		url: "/typo3conf/ext/handwer_und_industrie_eu/inseratsverwaltung/ajax/weiterempfehlenFormGet.php",
		data: {url: document.URL},
//		dataType: 'json',
		success: function(returnText){
		  $("#weiterempfehlen").remove();
		  $("body").append('<div id="weiterempfehlen">'+returnText+'</div>')
      layerPositioning('weiterempfehlen', f_oRef, 20, -1*$('#weiterempfehlen').width());  
		}
	});
}
function submitWeiterempfehlen() {
  $("#weiterempfehlen").hide();
  $('form#formWeiterempfehlen input[name="url"]').val(document.URL);
  $('form#formWeiterempfehlen').ajaxSubmit({
			url: '/typo3conf/ext/handwer_und_industrie_eu/inseratsverwaltung/ajax/weiterempfehlenFormGet.php',
			success: function(returnText){
			  //alert(json);
        $("#weiterempfehlen").html(returnText);
        setTimeout('$("#weiterempfehlen").show();', 500);
			},
			type: 'post',
			dataType: 'html'
	}); 
}

function showHinweisPrivatperson(f_oRef) {
  $.ajax({
		type: "POST",
		url: "/typo3conf/ext/handwer_und_industrie_eu/inseratsverwaltung/ajax/hinweisPrivatpersonen.php",
//		dataType: 'json',
		success: function(returnText){
		  $("#hinweisPrivatpersonen").remove();
		  $("body").append('<div id="hinweisPrivatpersonen">'+returnText+'</div>')
      layerPositioning('hinweisPrivatpersonen', f_oRef, 20, -1*$('#hinweisPrivatpersonen').width());  
		}
	});
}


// jquery add on zum einfuegen von text in eingabefeldern an der cursorposition
(function() {
	var fieldSelection = {
		getSelection: function() {
			var e = this.jquery ? this[0] : this;
			return (
				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					var l = e.selectionEnd - e.selectionStart;
					return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
				}) ||
				/* exploder */
				(document.selection && function() {
					e.focus();
					var r = document.selection.createRange();
					if (r == null) {
						return { start: 0, end: e.value.length, length: 0 }
					}
					var re = e.createTextRange();
					var rc = re.duplicate();
					re.moveToBookmark(r.getBookmark());
					rc.setEndPoint('EndToStart', re);
					return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
				}) ||
				/* browser not supported */
				function() {
					return { start: 0, end: e.value.length, length: 0 };
				}
			)();
		},

		replaceSelection: function() {
			var e = this.jquery ? this[0] : this;
			var text = arguments[0] || '';
			return (
				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
					return this;
				}) ||
				/* exploder */
				(document.selection && function() {
					e.focus();
					document.selection.createRange().text = text;
					return this;
				}) ||
				/* browser not supported */
				function() {
					e.value += text;
					return this;
				}
			)();
		}
	};
	jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });

})();



// ###############################################################################

// use jQuery to inspect an object and displays the props in the html-body
function inspect(obj) {
	for (var prop in obj) {
		$('body').append("" + prop + ": " + obj[prop] + "<br />");
	}
}

// use jQuery to inspect an object recursivly and displays the props in the html-body
function inspectRec(obj, objString) {
	var objNextString = '';
	if (objString != '') objNextString = objString + '.';
	
	for (var prop in obj) {
		if (typeof obj[prop] == "object") inspectRec(obj[prop], objNextString+prop);
		else $('body').append(objString + '.' + prop + ': ' + obj[prop] + "<br />");
	}
	return '';
}
