
/*
===============================================================================

 template: scripts

===============================================================================
*/

/* -------------------------------------------------------------
I-Service 2018 - jQuery
------------------------------------------------------------- */

$(document).ready(function() {

	// load default functions
	defaults();

	// load form functions
	forms();

	/* -------------------------------------------------------------
	I-Service
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	// search submit on a-tag
	$('a.submitter').on('click', function(e) {
		e.preventDefault();
		$('.int_search').submit();
	});

	function numberWithCommas(x) {
		return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ".");
	}

	// switch prices on change
	// $('.vermarktung').on('change', function() {
	// 	var thisVal = $(this).val();
	// 	if ( thisVal == 'kauf' ) {
	// 		$('.kaufpreis').show();
	// 		$('.kaltmiete').hide();
	// 		$('.disabled').hide();
	// 	}
	// 	else if ( thisVal == 'miete' ) {
	// 		$('.kaufpreis').hide();
	// 		$('.kaltmiete').show();
	// 		$('.disabled').hide();
	// 	}
	// 	else {
	// 		$('.kaufpreis').hide();
	// 		$('.kaltmiete').hide();
	// 		$('.disabled').show();
	// 	}
	// });

	// counter
	$('.counter').each(function() {
		var $this = $(this),
		countTo = $this.attr('data-count');
		$({ countNum: $this.text()}).animate({
			countNum: countTo
		}, {
			duration: 1000,
			easing:'linear',
			step: function() {
				$this.text(Math.floor(this.countNum));
			},
			complete: function() {
				v_pound = numberWithCommas(countTo);
				$this.text(v_pound)
			}
		});
	});

	// anzahl
	counted = $('h1 span.anzahl').text();
	v_pound = numberWithCommas(counted);
	$('h1 span.anzahl').text(v_pound);

	// give selected customer ID to contact form
	$('.int-obj').each(function() {
		var thisID = $(this).find('.id').text();
		$(this).find('a').on('click', function() {
			$('.int_id').val(thisID);
			$('.show_int_id').fadeIn();
			$('.show_int_id strong').text(thisID);
		});
	});

	// reset selected customer ID
	$('.reset').on('click', function() {
		$('.int_id').val('');
		$('.show_int_id').fadeOut();
	});

	// select2 search for selectfields
	$('.optionsearch').select2();

	// jumpbox a transition
	$('.jumpbox a').each(function() {
		$(this).addClass('transition');
	});

	// functions for every listobject
	$('.int-obj').each(function() {

		// show picture depending on gender/family
		if ( $(this).find('.gender').html() == 'Herr' ) {
			$(this).find('.icon .herr').show();
		}
		else if ( $(this).find('.gender').html() == 'Frau' ) {
			$(this).find('.icon .frau').show();
		}
		else {
			$(this).find('.icon .familie').show();
		}

		// handle/hide empty detail fields
		$(this).find('.single-info').each(function() {
			var $smallSpans = $(this).find('.small');
    
			if ($smallSpans.length === 2 && $smallSpans.eq(0).html().trim() === '' && $smallSpans.eq(1).html().trim() === '') {
				$(this).hide();
			}
			if ($smallSpans.length === 2 && $smallSpans.eq(0).html().trim() !== '' && $smallSpans.eq(1).html().trim() !== '') {
				$smallSpans.eq(1).hide();
			}
		});
	});

	// form status hide
	$('.form-status > div').each(function() {
		if ( $.trim( $(this).html() ) == '' ) {
			$(this).hide();
		}
	});

});

$(window).load(function() {

	$('html').addClass('loaded');

	$('img').each(function() {
		var this_src = $(this).attr('src');
		var this_data_src = $(this).attr('data-src');
		if( this_src != '' ) {
			if( this_data_src != undefined ) {
				$(this).attr( 'src', this_data_src );

				$(this).load(function() {
					$(this).fadeIn('fast');
				});
			}
		}
	});

});

// hide/show searchfields on change
$('.objektart').on('change', function() {
	var artvalue = $(this).find('option:selected').val();
	if ( artvalue == "grundstueck" ) {
		$('.grundstuecksflaeche').show();
		$('.wohnflaeche').hide();
		$('.zimmer').attr('disabled', 'disabled');
	}
	else {
		$('.wohnflaeche').show();
		$('.grundstuecksflaeche').hide();
		$('.zimmer').removeAttr('disabled');
	}
});

// reset defaults on form submit
$('form').submit(function() {
	$('input[type="text"]').each(function() {
		var this_default = $(this).attr('default');
		if( $(this).val() == this_default ) {
			$(this).val( '' );
		}
	});
	$('textarea').each(function() {
		var this_default = $(this).attr('default');
		if( $(this).text() == this_default ) {
			$(this).text( '' );
		}
	});
});

// default functions
function defaults() {

	// form defaults
	$('input[type="text"]').each(function() {
		var this_default = $(this).attr('default');
		if( $(this).val() == '' ) {
			$(this).val( this_default );
		}
		$(this).focus(function() {
			if( $(this).val() == this_default ) {
				$(this).val( '' );
			}
		});
		$(this).blur(function() {
			if( $(this).val() == '' ) {
				$(this).val( this_default );
			}
		});
	});

	$('textarea').each(function() {
		var this_default = $(this).attr('default');
		if( $(this).text() == '' ) {
			$(this).text( this_default );
		}
		$(this).focus(function() {
			if( $(this).text() == this_default ) {
				$(this).text( '' );
			}
		});
		$(this).blur(function() {
			if( $(this).text() == '' ) {
				$(this).text( this_default );
			}
		});
	});

	// empty clear
	$('div.clear').html('');

	// table spacings & paddings
	$('table').each(function() {
		var this_cellspacing = $(this).attr('cellspacing');
		var this_cellpadding = $(this).attr('cellpadding');
		var this_border = $(this).attr('border');
		if( this_cellspacing == undefined ) {
			$(this).attr('cellspacing', 0);
		}
		if( this_cellpadding == undefined ) {
			$(this).attr('cellpadding', 0);
		}
		if( this_border == undefined ) {
			$(this).attr('border', 0);
		}
	});

	// images
	$('img').each(function() {
		var this_src = $(this).attr('src');
		var this_data_src = $(this).attr('data-src');
		if( this_src != '' ) {
			if( this_data_src != undefined ) {
				$(this).hide();
			}
		}
		if( $(this).attr('style') != undefined ) {
			if ( ( this_src.indexOf('cmspics') ) >= 0 && ( ($(this).attr('style').indexOf('float: left') ) >= 0 ) ) {
				$(this).css('margin-left','0px').css('margin-top','0px');
			} else if ( ( this_src.indexOf('cmspics') ) >= 0 && ( ($(this).attr('style').indexOf('float: right') ) >= 0 ) ) {
				$(this).css('margin-right','0px').css('margin-top','0px');
			}
		}
	});
}

// default form functions
function forms() {
	var url = document.URL.split('?');
	if( url[1] != undefined ) {
		url = url[1].split('&');
		for( x = 0; x <= (url.length - 1); x++ ) {
			var this_parameter = url[x].split('=');
			if( this_parameter != '' ) {
				var this_name = this_parameter[0].replace(/\%5B/g, '[').replace(/\%5D/g, ']');
				var this_value = '';
				if( this_parameter[1] != undefined ) {
					this_value = decodeURI( this_parameter[1].replace(/\+/g, ' ').replace(/\+\%2F\+/g, ' / ').replace(/\%2C/g, ',') );
				}
				$('select[name="' + this_name + '"]').each(function() {
					$(this).find('option').each(function() {
						if( $(this).val() == this_value ) {
							$(this).attr('selected', 'selected');
						}
					});
				});
				$('input[name="' + this_name + '"]').each(function() {
					if( $(this).attr('type') == 'text' || $(this).attr('type') == 'hidden' ) {
						if( this_value != '' ) {
							$(this).val( this_value );
						}
					} else if( $(this).attr('type') == 'checkbox' ) {
						if( this_value != '' ) {
							$(this).attr('checked', 'checked');
						}
					} else if( $(this).attr('type') == 'radio' && $(this).val() == this_value ) {
						$(this).attr('checked', 'checked');
					}
				});
			}
		}
	}
}

// body class for each contentkey
function bodyClass( contentkey ) {
	var body_class = contentkey.split('.');
	$('body').addClass( 'body-' + body_class[0] );
}