$(function() {
	$('.cityfield').autocomplete({
         //define callback to format results
		source: "util/cities.php",
		minLength: 2,
		delay: 100
	});


	// activate tabs
	$( ".tabs" ).tabs({
	});
});

$(document).ready(function() {

	// add a date picker to date fileds
	$( ".dateField" ).datepicker({
		firstDay: 1,
		buttonImage: "_javascript/jscalendar/img.gif",
		dateFormat: "yy-mm-dd",
		buttonImageOnly: true,
		//gotoCurrent: true,
		monthNames: ['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
		monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'],
		showOn: 'both',
		showButtonPanel: true,
		// numberOfMonths: 2,
		closeText: 'schließen',
		currentText: 'Heute'
		//beforeShow: function(input, inst) {
		//	$(this).datepicker( "setDate" , '+0');
		//}
	});

	$('#profileGalleryPreview').cycle({
	    fx:    'fade',
	    speed:  1000,
	    pause:  0
	});

});


/**
 * jQuery equals heigh code from http://www.cssnewbie.com/equalheights-jquery-plugin/
 * usage:
 * $(document).ready(function() {
 * 	$(".equalColumns").equalHeights();
 * });
 */
(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","auto");
		});
	}
})(jQuery);
$(document).ready(function() {
	$(".equalColumns").equalHeights();
});

	/**
	 * Format the date values of text fields identified by their ids
	 * @param dateFieldIds array with the field ids
	 * @return
	 */
	function formatDateFields(dateFieldIds){
		for(i = 0; i < dateFieldIds.length; i++){
			formatDateValue(dateFieldIds[i])
		}
	}

	/**
	 * Format the date value of a text field identified by it's
	 * element id
	 *
	 * @param dateFieldId The id of the element
	 * @return
	 */
	function formatDateValue(dateFieldId){
		fieldElement = document.getElementById(dateFieldId);
		value = fieldElement.value;
		if(value.indexOf('.') > -1){
			dateChunks = value.split('.');
			newValue='';
			for(j = 0; j < dateChunks.length; j++){
				if(j > 0){
					newValue = '-' + newValue;
				}
				newValue = dateChunks[j] + newValue;
			}
			fieldElement.value = newValue;
		}
	}

	/**
	 * Open the form to request the contact data for an provider
	 */
	function openContactForFree(entryid,preDir){
		url = preDir+"suche/popup_contactforfree.php?entryid="+entryid;
		window.open(url,'kontakt_formular','width=400,height=650,location=no,menubar=no,status=no,resizable=no');
	}

	/**
	 * Open the contact form window to send an email to an provder
	 */
	function opencontactprovider(entryid,preDir){
		url = preDir+"suche/popup_contact_provider.php?entryid="+entryid;
		window.open(url,'kontakt_formular','width=380,height=600,scrollbars=auto,location=no,menubar=no,status=no,resizable=no');
	}

	/**
	 * Calculate the left characters for an input field that could be typed in
	 * @param	textfeld 	Eingabefeld, das begrenzt sein soll
	 * @param	textfeld 	Ausgabefeld, in dem die anzahl an noch freien zeichen
	 *						dargestellt werden soll
	 * @param	int			Maximale anzahl an einzugebenden zeichen
	 */
	function charsleft(inputfield,displayfield,maxcount) {
		var anz = inputfield.value.length;
		var frei = 0;
		if (anz > maxcount ) {
			inputfield.value = inputfield.value.substring(0,maxcount);
			frei = 0;
		}else{
			frei = maxcount -anz;
		}
		displayfield.value = frei;
	}

	/**
	 * Anzeigen eines Anbieterprofils
	 */
	function showProfile(url){
		window.open(url,'_blank','width=500,height=560,scrollbars=yes,location=no,menubar=no,status=no,resizable=no');
		return false;
	}

	/**
	 * ?ffnen eines neuen Browserfenster
	 */
	function openNewWindow(url){
		window.open(url,'_blank','');
	}

// Javascript functions extracted from the index page

				var x, y = 0;
				var status = 0;
				var actLayer = "";

				function closeAll() {
					var arLayers = new Array('navevents','navkuenstler','navmesse','navmedien','navbesonderes','navwerbung');

					for(i=0;i<arLayers.length;i++)
					{
						if(document.all)
						{
							document.all[arLayers[i]].style.left = 0;
							document.all[arLayers[i]].style.top = -200;
						}
						else
						{
							document.layers[arLayers[i]].left = 0;
							document.layers[arLayers[i]].top = -200;
						}
					}
				}

				function showLayer(layerName, stat){
					status = 0;

					if(layerName == "tipps")
					{
						var nX = 20;
						var nY = 70;
					}
					else
					{
						var nX = 300;
						var nY = 260;
					}

					//closeAll();

					if (document.all)
					{
						document.all[layerName].style.left = nX;
						document.all[layerName].style.top = nY;
					}
					else
					{
						if (document.layers)
						{
							document.layers[layerName].left = nX;
							document.layers[layerName].top = nY;
						}
					}
					actLayer = layerName;

					window.setTimeout("status=1;",500);
				}
				var count1 = 0;
				var count2 = 0;
				var count3 = 0;
				var count4 = 0;

				function moveMyLayer(num,dir){
					counter = eval("count"+num);
					if(((dir == -1 && counter > 0) || dir == 1))
					{
						counter += dir;
						var step = 38 * dir;
						if(strBrowserName == "ie")
						{
							actTop = document.all['content'+num].style.getAttribute("top",false);
							actTop = Number(actTop.substr(0,actTop.length-2)) - step;
							document.all['content'+num].style.setAttribute("top",actTop,false);
						}
						else
						{
							actTop = document.layers['scroll'+num].document.layers['content'+num].top;
							actTop = actTop - step;
							document.layers['scroll'+num].document.layers['content'+num].top = actTop;
						}
					}
					if(num==1) count1 = counter;
					if(num==2) count2 = counter;
					if(num==3) count3 = counter;
					if(num==4) count4 = counter;
				}

				// hide the tipps layer
				function closeTipps(){
					if(document.all)
					{
						document.all['tipps'].style.left = -2000;
						document.all['tipps'].style.top = 0;
					}
					else
					{
						document.layers['tipps'].left = -2000;
						document.layers['tipps'].top = 0;
					}
				}

				// unhide the tipps layer
				function showTipps(){
					var nX = 20;
					var nY = 61;

					if (document.all)
					{
						document.all['tipps'].style.left = nX;
						document.all['tipps'].style.top = nY;
					}
					else
					{
						if (document.layers)
						{
							document.layers['tipps'].left = nX;
							document.layers['tipps'].top = nY;
						}
					}
				}



				// manages the area selection so only one area of one land could be selected
				function setArea(areaLand) {
					var areas = new Array('de','ch');
					if(strBrowserName == "ie") {
						areaVal = document.forms['suche'].elements['area_'+areaLand].value;
					}
					else {
						areaVal = document.forms['suche'].elements['area_'+areaLand].selectedIndex;
						if(areaVal > 0) {
							areaVal = document.forms['suche'].elements['area_'+areaLand].options[areaVal].value;
						}
					}
					document.forms['suche'].area.value = areaVal;
					for(i=0;i<areas.length;i++) {
						if(areaLand != areas[i]) {
							document.forms['suche'].elements['area_'+areas[i]].selectedIndex = 0;
						}
					}
				}




// Functions extracted from main.js.php
// function calls extended to get the directory prefix as additional parameter


function bigmap(prefix){
	map = window.open(prefix+"bigmap.php","Karte","width=400,height=500,scrollbars=0,locationbar=0,resizable=0");
}
function fenster(ort,wname,w,h) {
     	win = window.open(ort, wname, 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=no,copyhistory=no,width='+w+',height='+h);
}

function fenster2(ort,wname,w,h) {
     	win = window.open(ort, wname, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+w+',height='+h);
}

function fensterFree(ort,wname,w,h,props) {
		if (props!="") props = props+',';
     	win = window.open(ort, wname, props+'width='+w+',height='+h);
}



// rollover ...
// 2004-09-16 bk
// function adopted to get the imagedirectory prefix as fourth parameter and the optional status setting as fifth
function hilo(bild, ext, io, dir, statext) {
	if (document.images) {
		eval("document.images['"+io+bild+"'].src='"+dir+bild+"_"+ext+".gif'");
	}
	if(statext != "" && String(statext) != "undefined")
	{
		status = statext;
	}
}

// preload ...
function preloader(a,prefix) {
	var pl = new Array();
	for (i=0; i<a.length; i++) {
		pl[i] = new Image;
		pl[i].src = prefix+"_images/nav/"+a[i] + "_h.gif";
	}
}


function getBrowser() {
	nameTemp = navigator.appName;
	if(navigator.appName.indexOf("Netscape") >= 0 || navigator.appName == "Netscape"){
		strBrowserName = "ns";
	} else {
		strBrowserName = "ie";
	}

	strVersion = String(navigator.appVersion)
	if(strVersion.charAt(0)=="4") {
		strBrowserVersion = 4;
	}

	if(strVersion.charAt(0)=="5") {
		strBrowserVersion = 5;
	}

	if(strVersion.charAt(0)=="6") {
		strBrowserVersion = 6;
	}

	if(strBrowserName=="ns" && (strBrowserVersion==6 || strBrowserVersion==5)) {
		strBrowserName = "ie";
		strBrowserVersion = 5;
	}
}


function swapImg(sName,status) {
	if(status==1) {
		sSrc = document.images[sName].src;
		sEnd = sSrc.slice(sSrc.length-3,sSrc.length);
		sSrc = sSrc.slice(0,sSrc.length-6);
		document.images[sName].src = sSrc + "_h." + sEnd;
	}

	if(status==0) {
		sSrc = document.images[sName].src;
		sEnd = sSrc.slice(sSrc.length-3,sSrc.length);
		sSrc = sSrc.slice(0,sSrc.length-6);
		document.images[sName].src = sSrc + "_l." + sEnd;
	}
}

function musterOeffnen(image){
	popup = window.open(image,'musterseite','width=415,height=580,scrollbars=0,locationbar=0,menubar=0,status=0,resizable=1');
}

function openExample(selection,root){
	popup = window.open(root+'produkte/popup_handbuchmuster.php?'+selection,'Beispielseite','width=430,height=680,scrollbars=0,locationbar=0,menubar=0,status=0,resizable=1');
}

function openBeispiel(src){
	popup = window.open(src,'presse','width=880,height=620,scrollbars=1,locationbar=0,menubar=0,status=0,resizable=1');
}

function MM_preloadImages() {
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} }


function trackconversion() {
        var google_conversion_id                = 1036094168;
        var google_conversion_label             = "Es_gCIq70gEQ2JWG7gM";
        image = new Image(1,1);
        image.src = "http://www.googleadservices.com/pagead/conversion/1036094168/?label=Es_gCIq70gEQ2JWG7gM&amp;script=0";
}
