// fallback für console.log
if (typeof console == 'undefined') console={log:function(){}};

$(document).ready(function(){
	
	// ********************* REGISTER *********************
	
	// insert link into label
	var $container = $('div#property_container_12 .wv-form-notice'); 
	if ( $container.length ) {
		var html = $container.html();
		var url = 'http://'+document.location.hostname + document.location.pathname +'?article_id=41&'+getParamFromUri('clang'); 
		html = html.replace('AGB', '<a href="'+url+'">AGB</a>');
		$container.html(html);
		}
	
	// show hidden values on click
	$('input[name=exclude]').change(function(){
		if ($(this).val() == 1 ){
			$(this).siblings('#exclude').show();
		} else {
			$(this).siblings('#exclude').hide();
		}
	});
	// init
	$('input[name=exclude]:checked').change();
	
	// verbeleibende Zeichen
	$('.remainingChars').remainingChars();
	
	// Max length für Textareas
	$('textarea').imposeMaxLength(200);
	
	// Sternchen * für die fest im System registrierten Systeme
	$elem = $('#art9 label[for=login]')
	if ($elem.length) $elem.html($elem.html().slice(0,-1)+'*:');
			
	$elem = $('#art9 label[for=password]')
	if ($elem.length) $elem.html($elem.html().slice(0,-1)+'*:');
	
	$elem = $('#art9 label[for=password2]');
	if ($elem.length) $elem.html($elem.html().slice(0,-1)+'*:');
	
	
	
	
	// // Übersicht tabs
	// $('#tabs').tabs({
		// event: 'click'
	// });

	// // Button
	// $("input:submit", ".button").button(); // Button
	
	
	// ********************* URL EINSTELLEN *********************
	
	$('.price_column input').keyup(function(){
		var val = $(this).val(),
			cleanVal = val.replace(',','.'),
			price = parseFloat(cleanVal*(1+tlm.fees/100)).toFixed(2);

		// only if input has a value
		if ( ! val )	return;
		
		// // set real price
		$(this).parent().next().children('input').val(price);
		
		// replace commas with points
		$(this).val(cleanVal);
	})
	$('.price_column input').keyup();
	
	// ********************* CART *********************
	
	// tabelle win wenig verschönern
	$('#cart-price-row').prev().children().css('padding-bottom','5px');
	
	
	$("#art12" ).accordion({ header: 'h3', collapsible: true }).accordion( "activate" , false );	
	
	// get the developer utils to show images
	$('img').each(function(){
			var src = $(this).attr('src');
			if (src.search('include/addons' != -1)) {
				src = src.replace('include/addons','redaxo/include/addons');
				$(this).attr('src',src);
			}	
	})
	
	// hide second filter-form from webvariants addons
	var filter = $('p.wv-filter');
	if (filter.length>1)	filter.eq(1).remove();
	
	// fancybox
	$('a.fancybox').fancybox();
	
	// externe Links in neuem Fenster öffnen
	var uri = 'http://' + window.location.host;
	$('a[href^=http://]:not([href~="'+uri+'"])').attr({target: "_blank"});
	$('area[href^=http://]:not([href~="'+uri+'"])').attr({target: "_blank"});
		
	/* tables */
	$('.table_total_row').prev().children().css('padding-bottom', '5px');
	
	// position the language select box depending on window size
	
	$(window).resize(function(){
		$(window).width() < 1155 ? putLangSelectDown() : putLangSelectUp();
	})
		
	if ($(window).width() < 1155){
		putLangSelectDown();
	}
	
	function putLangSelectDown(){
		$('#langSwitch').css('top','67px');
	}
	function putLangSelectUp(){
		$('#langSwitch').css('top','2px');
	}
	
	// CONTACT
	if ($('#co').length && $('#form_formular').length){
		$('#el_1').val($('#co_name').text());
		$('#el_3').val($('#co_mail').text());
	}
	
	// Bestätigungen

	$('form.form_close').submit(function(e){
		
		Check = confirm("Sind sie sicher?");
		if (!Check) {
			e.preventDefault();
		}	
		
	});
})

function getParamFromUri(param){
	var regex = new RegExp(param + '=[^?&]');
	var param = window.location.href.match(regex);
	return param;
}


var jmDevHelp = {
	
	/* ***************************** extend the jQuery object ***************************** */
	
	container : function($){ 
		$.fn.extend({
			/* 
			* autoclear
			* deletes input fileds on click and restores default value on blur
			*/
			autofill : function (){
				//this.inputValue__tudr57xhcxu = false;
				$(this)
					.click(function(e){
						// get value
						var val = $(this).val();
						
						// if not saved before, save
						if ( ! $(this).attr('old_val') ) $(this).attr('old_val', val);
						
						if ( val && (val == $(this).attr('old_val')) ) {
							$(this).val('');
						}
					})
				.blur(function(){
						if ( ! $(this).val() ) {
							$(this).val($(this).attr('old_val'));
						}
					})
			},
			/* 
			* autoclear
			* only deletes input fileds on click
			*/
			autoclear : function (){
				$(this)
					.focus(function(e){						
						$(this).val('');
					})
			},
			
			/* 
			** remainingChars
			** Shows in span, how many characters are still allowed to be used in input
			*/
			remainingChars : function (){
				$(this).each(function(){
					// initial value 
					var max = $(this).attr('class').match(/rcv_(\d+)/)[1];
					$(this).parent().find('.remaiingCharsCount').first().html( max-$(this).val().length );

					// keypress event
					$(this).keypress(function(){
						$(this).parent().find('.remaiingCharsCount').first().html( max-$(this).val().length );
					})
				})
			}
			
			,/*
			** fakes max-length property for textareas
			*/
			imposeMaxLength : function (maxLength){
				$(this).each(function(){
					$(this).keypress(function(e){
						
						if ( $(this).val().length >= maxLength){
							$(this).val($(this).val().substr(0, maxLength -1));
						}
						//return $(this).val().length < maxLength
					});
				})
					
			}
			
		})
	}(jQuery)
	
	
}
