/**
 * @author aidentailor
 * @copyright 2011 Aiden Tailor <http://aidentailor.net>
 */

$(document).ready(function() {
	
	// check browser
	// check for ipad users first
	if (navigator.platform.indexOf("iPad") != -1 &&
		navigator.platform.indexOf("iPod") != -1 &&
		navigator.platform.indexOf("iPhone") != -1)
	{
	    $.reject({  
	        reject: { unknown: true, msie5: true, msie6: true, msie7: true, msie8: true, msie9: false, konqueror: true, safari: false},  
	        header: 'Dein Browser macht mir Sorgen', 
	        paragraph1: 'Ich habe festgestellt, dass du mit einem veralteten Browser im Internet unterwegs bist. Bitte zieh doch in Erw&auml;gung einen der folgenden Browser zu installieren. Es ist zu Deinem Besten!',
	        paragraph2: 'Klicke auf einen der Browser um zum kostenlosen Download zu gelangen',
	        display: ['chrome', 'firefox', 'safari', 'msie'],
	        browserInfo: {
	        	msie: {
	        		text: 'Internet Explorer 9',
	        		url: 'http://www.internet-explorer9.de/'
		        }
	        },
	        closeLink: 'Fenster Schlie&szlig;en',
	        closeMessage: 'Wenn du das Fenster schlie&szlig;t msst du mit eventuellen Fehldarstellungen und Sicherheitsrisiken rechnen!',
	        imagePath: 'images/browsers/',
	        closeCookie: true
	    });
	}
	
    // store wether contactform is being displayed
	var contactShown = false;
	
	// store formfields for loop
	var formfields = [$('#name'), $('#email'), $('#subject'), $('#message')];
	
	// link logo
	$('#logo').click(function() {
		window.location.href = "http://aidentailor.net";
	});
	
	// display contact form or hide it
	$('#li-contact').click(function() {
		if (contactShown) {
			$('#contact').animate({
				opacity: 0
			}, 100, function() {
				$('#content').animate({
					opacity: 1
				}, 100, function() {
					contactShown = false;
					$('#contactForm').validate().resetForm();
				});
			});
		}
		else {
			contactShown = true;
			$('#content').animate({
				opacity: 0
			}, 100, function() {
				$('#contact').animate({
					opacity: 1
				}, 100);
			});
		}
		
		// cancel contact form and reset it
		$('#cancel').click(function() {
			$('#contact').animate({
				opacity: 0
			}, 100, function() {
				$('#content').animate({
					opacity: 1
				}, 100, function() {
					for (var i=0; i < formfields.length; i++) {
						formfields[i].val("");
						$('#contactForm').validate().resetForm();
					}
					contactShown = false;
				});
			});
			return false;
		});
		
		// submit form
		$('#submit').preventDefault;
		$('#submit').click(function() {
			$('#contactForm').validate({
				rules: {
					name: 'required',
					email: {
						required: true,
						email: true
					},
					subject: {
						required: false
					},
					message: 'required'
				},
				submitHandler: function(form) {
					var strData = 'name=' + $('#name').val() + '&email=' + $('#email').val() + '&subject=' + $('#subject').val() + '&message=' + $('#message').val();
					$.ajax({
						type: 'POST',
						url: 'php/submit-form.php',
						data: strData,
						success: function() {
							$('#contact').animate({
								opacity: 0
							}, 100, function() {
								$('#content').animate({
									opacity: 1
								}, 100, function() {
									for (var i=0; i < formfields.length; i++) {
										formfields[i].val("");
									}
									contactShown = false;
									$('#content').html("Wir haben jetzt Post von dir :)");
								});
							});
							return false;
						}
					});
				}
			});
		});
		return false;
	});
	
	// handle navigation morph with color plugin
	$('#navigation a').mouseover(function(event) {
		var strTitleTag = event.target.title;
		if (!contactShown) {
			$('#content').css({'opacity':'1'});
			$('#content').html(strTitleTag);
		}
		$(this).animate({
			color: '#95C940'
		}, 85);
	});
	
	// handle navi morph out
	$('#navigation a').mouseout(function(event) {
		$('#content').html("");
		
		$(this).animate({
			color: '#999999'
		}, 100);
	});
	
	// link impressum
	$('#impressum').click(function() {
		$.modal("<h1>Impressum</h1><p>Ich &uuml;bernehme keinerlei Haftung f&uuml;r verlinkte Ressourcen au&szlig;erhalb meines Einflu&szlig;bereichs.</p><p>Dies ist eine private Website.</p><p>Ansprechpartner:<br />Aiden Tailor<br />Bredenhop 8<br />32609 H&uuml;llhorst</p><p>E-Mail: info [at] aidentailor [punkt] net</p><p>Diese Website verwendet <a href='https://www.google.com/analytics/' title='Google Analytics'>Google Analytics</a></p>");
		$('#simplemodal-overlay, #simplemodal-container').click(function() {
			$.modal.close();
		});
	});
});

