﻿
$(document).ready(function(){
	
	// REMOVE JAVASCRIPT MESSAGE
	$('#js-info').hide();
	
	// CHECK BROWSER
	if($.browser.msie) {
		if($.browser.version < 7) {
			alert("Internet Explorer 6 (und ältere Versionen) sind für die korrekte Darstellung dieser Website nicht geeignet. Wir empfehlen den Wechsel zu einem zeitgemäßen Browser. Der aktuelle Firefox zum Beispiel - Kostet nix und sorgt für ein schöneres, einfacheres und sichereres Internet.");
		}
	}
	
	// LINK NOTES TO FULLVIEW
	$('.note_body').click(function() {
		var url = $(this).find('a.title').attr('href');
		location.href = url;
    });
	
	// PROCESS TOGGLE-AFFIXES
	$('.toggle-affix').hide();
    
	$('.goto, .function').hover(
		function() {
			$(this).next('.toggle-affix').fadeIn(100);
		}, 
		function() {
			$(this).next('.toggle-affix').fadeOut(100);
		}
	);
	
	// SCROLL TO SM-ANCHORS
	$(window).load(function () {
		
		var sm = $('body').find('#sm').attr('class');
		
		if (sm == 'error') {
			window.location = "#sm";
			window.scrollBy(0, -100);
			
		} else if (sm == 'success') {
			var lastcom = $('#maincontent').find('.com_body:last').attr('id');
			if(lastcom) {
				window.location = '#' + lastcom;
				window.scrollBy(0, -100);
			}
		}
    });
	
	// AD WEBSITE-PREFIX
	$('input#com_website').click(function() {
		var value = $(this).val();
		if (value == '') {
			$(this).val('http://');
		}
	});
	
	// REMOVE WEBSITE-PREFIX
	$('input#com_website').blur(function() {
		var value = $(this).val();
		if (value == 'http://') {
			$(this).val('');
		}
	});
	
	// REMOVE SYSTEM-MESSAGE
	$('.sm_body').click(function() {
		$(this).parent('#sm').fadeOut(500);
	});
	
	
	// ENTRYLIST COMMENT-LINK
	$('.bloglist a.item .comments').click(function() {
		var url = $(this).parent('a.item').attr('href') + '#comments';
		location.href = url;
		return false;
	});
	
	// TOGGLE NOTE-VIEW IN ARCHIVE
	$('body#archiv h1 a.affix').click(function() {
		
		var status = $(this).text();
		
		if (status == 'ohne Notizen') {
			$(this).text('inkl. Notizen').blur();
		    if($.browser.msie) {
	     		$(".bloglist").find("li.note").fadeIn(300);
	     	} else {
	     		$(".bloglist").find("li.note").slideToggle(300);
	     	}
		} else {
			$(this).text('ohne Notizen').blur();
		    if($.browser.msie) {
	     		$(".bloglist").find("li.note").fadeOut(300);
	     	} else {
	     		$(".bloglist").find("li.note").slideToggle(300);
	     	}
		}
		
    });
    
    
	// PROCESS NEWSLETTER REQUESTS
	
	$('#em_email').click(function() {
		$('#em_submit').attr('class', 'submit bypass').val('...').show();
	});
	
	$('#em_email').keyup(function() {
		editMailbook( $(this).val() );
	});
	
	$('#em_email').blur(function() {
		if($('#em_submit').is('.bypass')) {
			editMailbook( $(this).val() );
		}
	});
	
	$('#em_submit').click(function() {
		if($(this).is('.bypass')) {
			$(this).blur();
			return false;
		}
	});
	
});




function validateEmail(email) {
	
	var emailStr = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	
	if (emailStr.test(email) == false) {
		return false;
	} else {
		return true;
	}
}


function editMailbook(email) {
	
	if (!(validateEmail(email))) {
		
    	return false;
    	
	} else {
		
		$('#em_submit').val('Überprüfen');
		
		var mailbook = $('#em_mailbook').val();
		var request = mailbook + email;
		
    	$.ajax({
    		type: "POST",
    		url: request,
    		global: 'false',
			success: function(xml){
				var result = $('result',xml).text();
				var id = $('id',xml).text();
				var active = $('active',xml).text();
				var root = $('#em_newsletter').val();
				
				if (result == 'error') {
					$('#em_redirect').val(root+'add/'+email);
					$('#em_submit').attr('class', 'submit').val('Eintragen').hide().fadeIn(300);
				}
				
				if (result == 'success') {
					$('#em_id').val(id);
				}
				
				if(result == 'success' && active == 'yes') {
					$('#em_active').val('no');
					$('#em_redirect').val(root+'remove/'+email);
					$('#em_submit').attr('class', 'submit').val('Abmelden').hide().fadeIn(300);
				}
				
				if(result == 'success' && active == 'no') {
					$('#em_active').val('yes');
					$('#em_redirect').val(root+'add/'+email);
					$('#em_submit').attr('class', 'submit').val('Eintragen').hide().fadeIn(300);
				}
			}
    	});
		return false;
    }
}