if(navigator.appVersion.indexOf("MSIE 6")>-1) {
	self.location.href = "/ie6/";
}

$.backstretch("images/backgrounds/rotate.php");

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 

$(document).ready(function(){
	
	$("h1").wait(3500).fadeToggle("slow");
    $("#nav").wait(3500).fadeToggle("slow");
   
    function showContent(layer, animate) {
		
		$("#intro").hide();
		
		$("#content").html($("#" + layer + "_html").comments().fadeIn("slow"));
		
		initFancyBox();
		
		initTweets();
		
		formatText();
		initAnythingSlider();
				
		portfolioItems();	
		
		contactForm();
		
		labelSlider();
		
		$("#nav").children().each(function() {
		
			if ($(this).attr("id").search(layer) == -1) {
				$(this).addClass("opacity");
			} else {
				$(this).removeClass("opacity");
			}
		});
		
		setTimeout(function() {
			$("#tracker").attr("src", "/track/" + layer + "/")
		}, 500);
				
		window.location.hash = layer;
				
	}
	
	$("#nav").children().click(function() {
		showContent($(this).attr("id").split("_")[1], true);
	});
	
	$("h1").click(function() {
		showContent($(this).attr("id").split("_")[1], true);
	});
	
	if (window.location.hash && document.referrer != window.location) {
		showContent(window.location.hash.replace("#", ""), false);
	}

	preloadContentDelay = 1000;
	
	setTimeout(function() {
				
		preloadNode = $("<div></div>");
		
		preloadNode.css({
			"position": "absolute", 
			"left": "-100000px", 
			"top": "-100000px", 
			"height": "1px", 
			"width": "1px",
			"overflow": "hidden"})
	
		preloadNode.append($("#start_html").comments())
		preloadNode.append($("#portfolio_html").comments())
		preloadNode.append($("#clients_html").comments())
		preloadNode.append($("#twitter_html").comments())
		preloadNode.append($("#about_html").comments())
		preloadNode.append($("#contact_html").comments())
		preloadNode.append($("#imprint_html").comments())
		preloadNode.append($("#privacy_html").comments())
	
		$("body").append(preloadNode);
		
	}, preloadContentDelay);
	
	//////////
	
	// Fancybox
	function initFancyBox() {
		$("a.slideshow-box").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'fade',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	false,
			'hideOnOverlayClick' : true,
			'cyclic'		: false
		});
		return false;
	}	
	
	// Tweets
	function initTweets() {
		$(".tweet").tweet({
            username: "elfacht",
            join_text: "auto",
            avatar_size: null,
            count: 10,
            loading_text: "Lade Tweets..."
        });
        
	}
	
	// Portfolio Slider
	function formatText(index, panel) {
  		return index + "";
	}
		
	function initAnythingSlider() {
		$(function () {
        
    		$('.anythingSlider').anythingSlider({
    		    easing: "easeInOutExpo",        
    		    autoPlay: false,                
    		    delay: 5000,                    
    		    startStopped: false,            
    		    animationTime: 1000,             
    		    hashTags: false,                
    		    buildNavigation: true,          
				pauseOnHover: true,             
				startText: "Go",             
    		    stopText: "Stop",            
    		    navigationFormatter: formatText       
    		});
    		
    		$("#slide-jump").click(function(){
    		    $('.anythingSlider').anythingSlider(6);
    		});
    		
		});	
	}
	
	// Portfolio items
	function portfolioItems() {
		$(".mainitem").mouseover(function(){
			$('.subitem').hide();
			$('#port_' + this.id).show();
		});
	
		$(".mainitem").mouseout(function(){
			$('.subitem').hide();
		});
	}
	
	// LabelSlider
	function labelSlider() {
		$(function(){
			$('form#contact .slider label').each(function(){
			var labelColor = '#fff';
			var restingPosition = '0';
			
			$(function() {
				
				$("textarea").focus(function() {
					$(this).stop().animate({'height':'180px'}, 'fast');
				});
				
				$("textarea").blur(function() {
					var areaValue = $(this).val();
					
					if(areaValue == ''){
						$(this).stop().animate({'height':'12px'}, 'fast');
					} 
				});
			});		
			
			$(this).css({
				'color' : labelColor,
				'background': '#00c0f3',
				'position' : 'absolute',
			 	'top' : '0',
			 	'line-height' : '32px',
			 	'height' : '32px',
			 	'padding' : '0 10px',
				'left' : restingPosition,
				'display' : 'inline',
				'width' : '100px',
    			'z-index' : '99'
			});
			
			var inputval = $(this).next().val();
			
			var labelwidth = $(this).width();
			var labelmove = labelwidth + 30 +'px';
			
			if(inputval !== ''){
				$(this).stop().animate({ 'left':'-100px'}, 1);
			}    	
			
			$('input, textarea').focus(function(){
				var label = $(this).prev('label');
				var width = $(label).width();
				var adjust = '120px';
				var value = $(this).val();
				
				if(value == ''){
					label.stop().animate({ 'left':'-'+adjust, 'width':'80px' }, 'fast');
				} else {
					label.css({ 'left':'-'+adjust });
				}
			}).blur(function(){
				var label = $(this).prev('label');
				var value = $(this).val();
			
				if(value == ''){
					label.stop().animate({ 'left':restingPosition,'width':'100px' }, 'fast');
				}	
			
			});
			});
		});
		
	}
	
	// Contact
	function contactForm(){
		$("form#contact").submit(function() {
 		
 			function isValidEmailAddress(emailAddress) {
				var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
				return pattern.test(emailAddress);
			}
 		
 			var name     = $('#name').attr('value').replace(/<\/?[^>]+>/gi, '');
			var email    = $('#email').attr('value').replace(/<\/?[^>]+>/gi, '');
			var message  = $('#message').attr('value').replace(/<\/?[^>]+>/gi, '');
			
			if (name != "" && email != "" && message != "" && isValidEmailAddress(email)) {
				$.ajax({
					type: "POST",
					url: "ajax.php",
					data: "name="+ name + "&email=" + email + "&message=" + message,
					success: function(daten) {
						window.setTimeout( function() {
							$('#node').css({"display":"block"});
							//$('#node').html("<p>Your mail has been sent. Thank you.</p>").wait(4000).fadeToggle("slow");
							$('#node').html("<p>Ihre E-Mail wurde verschickt. Vielen Dank!</p>").wait(4000).fadeToggle("slow");
							$('#name, #email, textarea').val("");
					   	},500);
					}
				});
			} else {
				if (!isValidEmailAddress(email)) {
					 $("#email").fadeIn(100).fadeOut(300).fadeIn(200).fadeOut(300).fadeIn(100).fadeOut(300).fadeIn(200);
				}
				
				if (name == "") {
					$("#name").fadeIn(100).fadeOut(300).fadeIn(200).fadeOut(300).fadeIn(100).fadeOut(300).fadeIn(200);
				}
				
				if (message == "") {
					$("#message").fadeIn(100).fadeOut(300).fadeIn(200).fadeOut(300).fadeIn(100).fadeOut(300).fadeIn(200);
				}
				
				$('#node').css({"display":"block"});
				$('#node').html("<p>Bitte seien Sie so nett und f&uuml;llen alle Felder aus.</p>").stop().wait(4000).fadeToggle("slow");
			} 
			return false;
		});		
	}
		
});
