$(document).ready(function(){

	previewpath = 'resources/shared/img/dogtagpreview/';	
	
	function trim(s) {
		while (s.substring(0,1) == ' ') {
			s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == ' ') {
			s = s.substring(0,s.length-1);
		}
		return s;
	}
	
	function center(rowoffset) {
		for(i=1; i<=5; i++) {
			row = "#row_" + (i + rowoffset);
			p = '';
			if ($(row) != '') {
				c = $(row).val();
				c = trim(c);
				l = c.length;
				n = Math.floor((16-l)/2);
				for (s=1; s<=n; s++) {
					p += ' ';
				}
				$(row).val(p + c);
			}
		}
	}
	
	function changeTag() {
		dogtagtype = $("input[name='dogtagtype']").val();
		attributes = $('.attribute').find('option').filter(':selected').text();
		finish = 'matt'; // preset if undefined
		style = 'newstyle'; // preset if undefined
		if (attributes.search(/glänzend/i) != -1) finish = 'glossy';
		if (attributes.search(/matt/i) != -1) finish = 'matt';
		if (attributes.search(/new-style/i) != -1) style = 'newstyle';
		if (attributes.search(/newstyle/i) != -1) style = 'newstyle';
		if (attributes.search(/hochprägung/i) != -1) style = 'newstyle';
		if (attributes.search(/old-style/i) != -1) style = 'oldstyle';
		if (attributes.search(/oldstyle/i) != -1) style = 'oldstyle';
		if (attributes.search(/tiefprägung/i) != -1) style = 'oldstyle';
		
		if ($("input[name='zeichensatz']").val()) style = $("input[name='zeichensatz']").val();
		
		$(".previewtag").css("background-image", "url('"+previewpath+"tags/"+dogtagtype+"_"+finish+"_"+style+".gif')");
		tagfont = dogtagtype + '_' + style;	
		if ('newstyle' == style) stylechars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,#+*[]()"/-%';
		if ('oldstyle' == style) stylechars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ @&.,'-";
		if ('newstyle_em' == style) stylechars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,#+*[]()"@/-%';
		if (dogtagtype == 'steel_em') {
			// overwrite allowed chars for www.erkennungsmaren.de
			if ('newstyle' == style) stylechars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,/-:*&+@()';
		}
		changeText();
	}
	
	function changeText() {
		if ($("#row_10").length > 0	) mx = 10; else mx = 5;
		for(r=1; r <= mx; r++) {			
			row = "#row_" + r;
			prow = "#prow_" + r;
			ps = '';
			w = '';
			for (i=0; i < $(row).val().length; i++) {
				x = $(row).val().charAt(i);
				if (stylechars.indexOf(x,0) != -1) {
					w += x.toUpperCase();
					c = w.charCodeAt(i);
					ps += '<img src="' + previewpath + 'fonts/' + tagfont + '/' + c + '.gif" />';
				} else {
					$(row).addClass("error");
					setTimeout("$('" + row + "').removeClass('error')",1000);
				}
			}
			$(row).val(w);
			$(prow).html(ps);
		}
	}
	
	// Setup
	changeTag();
	changeText();
	
	// Follow changes
	$('.attribute').change(function(){ changeTag(); });
	$('.tagtext').keyup(function() { changeText(); })
	
	$('#copytext').click(function() {
		for($i=1; $i<=5; $i++) {
			row = '#row_' + $i;
			crow = '#row_' + ($i+5);
			$(crow).val($(row).val());
			changeText();
		}
	});
	
	$('#center1').click(function(){ center(0); });
	$('#center2').click(function(){ center(5); });
	
});
