//  TO-DO:
// Eigentlich muss dieses gesamte Geraffel zu einer Frontend-Bibliothek zusammengefasst werden

var bantDialogURL 		= 'https://www.ashampoo.com/dialog-bant/v3/';
//var bantDialogURL 		= 'https://img.ashampoo.com/ashampoo.com_images/img/1/bant/v3/';
var bantDebugOldURL 	= 'https://www.ashampoo.com/dialog-bant/v3/license.debug.html';
var bantDebugURL 		= 'https://img.ashampoo.com/ashampoo.com_images/img/1/bant/v3/license.debug.html';
var varSupportMail 		= '%SUPPORTMAIL%';
var resolveSupportMail 	= '<a data-linktarget="support">support@ashampoo.com</a>'


function setDebugMessages(){
	
	alert('Debug-Messages for this Dialog activated.');
	
	//if (!console || !console.log) {
	console = {
		log: function (stuff) {
			var str;
			try {
				str = JSON.stringify(stuff, null, 4);
			} catch (e) {
				str = stuff;
			}
			alert(str);
		}
	};
	//}
}




// Boilerplate
document.createElement("ash-dialog");
document.createElement("ash-content");
document.createElement("ash-sidebar");
document.createElement("ash-headbar");	
document.createElement("ash-close-button");		
document.createElement("ash-button-module");
document.createElement("ash-input-module");
document.createElement("ash-input");
document.createElement("ash-info");
document.createElement("ash-textlink-module");	
document.createElement("section");	


function onCloseDialog(event){
	if(event.keyCode == 27){
		bant.dialog.close(this, 'cancel');
	}
}


$(document).ready(function(){
	// Hier werden LinkTargets und Doppelklickverbote gesteuert

/*
	// Doppelklick blockieren
	$('a').dblclick(function(evt){
		evt.preventDefault();
		return false;
	});	
*/	
	
	// Linktargets aufrufen
	$('a[data-linktarget]').click(function(evt){
		evt.preventDefault();
		
		var lt = $(this).attr('data-linktarget');
		bant.execute.linktarget(lt, 'xpos=html');

	});
	

	// Loads aufrufen aufrufen
	$('a[data-dialogtarget]').click(function(evt){
		
		evt.preventDefault();	
		
		var dt = $(this).attr('data-dialogtarget');
			dt = bantDialogURL + dt;
		

//		console.log(dt);
		
//		$('ash-content').slideUp( 500, function(){
			bant.dialog.load(dt, 'error.html');
//			return;
//		});
	
		
	});
	
	// Debug
	$('#boxshot').dblclick(function(evt){
		
		evt.preventDefault();
		
		bant.dialog.load(bantDebugOldURL);
		
/*		
		var r = confirm("IT’S DANGEROUS TO GO ALONE. TAKE THIS.\n\nClick OK for img.ashampoo.com\nClick CANCEL for www.ashampoo.com");
		if (r == true) {
			bant.dialog.load(bantDebugURL);
		} else {
			bant.dialog.load(bantDebugOldURL);
		} 
		*/
	});

});


	
	/*
	// Entertaste ruft hero-link auf
	$('body').keyup(function(k){
		if(k.which == 13){
			var cn = $('body').attr('class');
			var sel = '#' + cn + ' a.hero';
			$(sel).trigger('click');
		}
	});
	
*/


// Enter-Taste für Input-Felder
function setKeypressButton(inputQuery,btnQuery){
	$('input').off('keyup');
	$(inputQuery).on( "keyup", function(k){
		if(k.which == 13){
			// Enter
			$(btnQuery).trigger('click');
		}
	}).focus();
}

function clearKeypressButton(){
	$('input').off('keyup');
	$('body').focus();
}


// Button auf Wartestellung setzen
function disableButton(btnQuery){
	$(btnQuery).addClass('wait');
}

// Wartestellung entfernen
function enableButton(btnQuery){
	btnQuery = btnQuery || null;
	if(!btnQuery){
		$('a.ashbutton').removeClass('wait');
		return;
	}
	$(btnQuery).removeClass('wait');
}



// Das wird mal die Navigations-Funktion ...
function navigateTo(className, resetPw){
	resetPw = resetPw || null;
	
	//Passwortboxen resetten
	if (resetPw === true) {
		$('input[type="password"]').val('');
	}
	
	// Fehlermeldungen ausblenden
	$('ash-info[type="error"]').hide();
	
	// Button wiederherstellen
	enableButton();
	
	// Listener für Eingabefelder / Entertaste entfernen;
	clearKeypressButton();
	
	document.body.className = className;
	
}

// Platzhaltertexte in Attribute schreiben
function savePlaceholders(elementSelector){
	$(elementSelector).each(function(index){
		preEmailString = $( this ).html();

		if ( preEmailString.indexOf( '%USERMAIL%' ) != -1 ){
//			alert(preEmailString);
			$( this ).attr('data-dynamic-placeholder', preEmailString)
		}
	});
}	
	
// Emailplatzhalter tauschen
function replaceEmailString( elementSelector, email ){
	savePlaceholders(elementSelector);
	
	quer = elementSelector + '[data-dynamic-placeholder]';
	$(quer).each(function(index){
		var preEmailString = $( this ).attr('data-dynamic-placeholder');// placeHolderElements[ index ];
		var replacedEmailString = preEmailString.replace('%USERMAIL%','<em>' + email +'</em>');
		$( this ).html( replacedEmailString );
	});
	
}	


/*
function replaceEmailString( elementSelector, email ){
	
	// Wir müssen die Originalstrings wiederherstellen, sonst
	// haben wir keine Variable zum tauschen
//		
	
	/*

	$(elementSelector).each(function(){
		var preEmailString = $( this ).html();
		if ( preEmailString.indexOf('%USERMAIL%') != -1 ){
			var replacedEmailString = preEmailString.replace('%USERMAIL%','<em>' + email +'</em>');
			$( this ).html( replacedEmailString );
		}
	});
	*/
/*	
	elementSelector = elementSelector || null;
	
	var emailStr = email;
	var myBantData = bant.resources.get();
	myBantData.placeholder['%USERMAIL%'] = emailStr;
	bant.resources.set(myBantData);

	
	
}
*/
function replaceSupportString( ElementSelector ){
//	console.log('replacing Support-string.');
	$(ElementSelector).each(function(){
		var preSupportString = $( this ).html();
		if ( preSupportString.indexOf( '%SUPPORTMAIL%' ) != -1 ){
//			console.log(preSupportString);
			var replacedSupportString = preSupportString.replace('%SUPPORTMAIL%', resolveSupportMail);
			$( this ).html( replacedSupportString );
//			console.log(replacedSupportString);
		}
	});
}

// Abfrage ob die E-Mail-Adresse gut geformt ist
function isCorrectMailSytax(email){
	return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}

// Abfrage ob das Passwort lang genug ist
function isLongEnough(s) {
	return s.length >= 5;
}

// Abfrage ob das Passwort leerzeichen enthält
function isWhiteSpaceFree(s) {
// console.log("hasWhiteSpace " + s.indexOf(' ') >= 0 );
	return s.indexOf(' ') == -1;
//	/[\s]/
}