var cache_innerHeight = 0;
var cache_offsetY = 0;
var cache_headerState = 0;
var counter = 0;

function init() {	
	$("#content").fadeIn(1000);
	
	// BigPictures [Begin]
	$(".bigPicture").each(function() {	
		$(this).html($(this).html() + '<div class="white"></div><div class="white2"></div>');
	});	
	
	updateView(true);
	
	$("#menu li a").fadeTo(1, 0).mouseenter(function() {
		if ($(this).hasClass("active")) return;
		
		$(this).stop(true, false).fadeTo(500, 1);
		
		var sel = "#indicator li." + $(this).parent().attr("class") + " a";
		$(sel).stop(true, false).fadeTo(500, 1);
	}).mouseleave(function() {
		if ($(this).hasClass("active")) return;
		
		$(this).stop(true, false).fadeTo(500, 0);
		
		var sel = "#indicator li." + $(this).parent().attr("class") + " a";
		$(sel).stop(true, false).fadeTo(500, 0);
	});
	
	$("#indicator li a").fadeTo(1, 0).mouseenter(function() {
		if ($(this).hasClass("active")) return;
		
		$(this).stop(true, false).fadeTo(500, 1);
		
		var sel = "#menu li." + $(this).parent().attr("class") + " a";
		$(sel).stop(true, false).fadeTo(500, 1);
	}).mouseleave(function() {
		if ($(this).hasClass("active")) return;
		
		$(this).stop(true, false).fadeTo(500, 0);
		
		var sel = "#menu li." + $(this).parent().attr("class") + " a";
		$(sel).stop(true, false).fadeTo(500, 0);
	});
	
	$("#menu li a.active").fadeTo(1, 1);
	$("#indicator li a.active").fadeTo(1, 1);
	
	if ($(".skills").parent().hasClass("invisible")) {
		$(".skills ul li").hide();
		$(".skills ul").css("width", 1);
		$(".skills").addClass("init");
	}
	
	$("a").each(function() {
		if (
			$(this).attr("target") != "_blank" && 
			!$(this).hasClass("ipWidgetFileLink") && 
			!$(this).hasClass("ajax") && 
			!$(this).hasClass("ipWidgetPhotoGalleryImage")) {
			
			$(this).click(function() {
				var href = $(this).attr("href");
				$("#content").fadeOut(500, function() {
					window.location.href = href;
				});
				return false;
			});
		}
	});
	
	$("a.contact").click(function() {
		$("#contact").stop(true, false).fadeIn();
		$("#contact .background").click(function() {
			$("#contact").stop(true, false).fadeOut(500, function() {
				$("#contact .postcard").hide();
			});
		})
		
		$.ajax({
			url: $(this).attr("href") + "?ajax=1",
			success: function(c){
				$("#contact .postcard .content").html(c);				
				$("#contact .postcard").fadeIn();
			}
		});
		
		return false;
	});
	
	$("a.ipWidgetPhotoGalleryImage").lightBox();
	
	$("ul#menu a.active").parent().css("backgroundImage", "none");
}

function updateView(force) {
	if ($("div:first").attr("id") != "content") return; // for impressPagesBackend
	if ($("#footer").css("position") == "relative") return // for iOS Devices
	
	if (scrollingChanged() || force) {
		var v1 = 250;
		var v2 = 200;
		
		if (force) {
			v1 = 1;
			v2 = 1;
		}
		
		/*
		if (cache_offsetY > 60 && cache_headerState == 0) {
			$("#header").stop(true, false).animate({
				height: "50px",
				marginTop: "-10px"
			}, v1);
			cache_headerState = 1;
		} else if (cache_offsetY <= 60 && cache_headerState == 1) {
			$("#header").stop(true, false).animate({
				height: "60px",
				marginTop: "0px"
			}, v1);
			cache_headerState = 0;
		}
		*/
		
		var firstWhite = true;
		
		$("#content .content").children().each(function() {
			var borderBottom = 150;
			
			if (!$(this).hasClass("bigPicture")) {
				var headerPos = $(this).offset().top - cache_offsetY - 40 + ($(this).height() / 3);
				var menuePos = $(this).offset().top + $(this).height() - cache_offsetY - cache_innerHeight + borderBottom; // + ($(this).height() / 3);
				
				headerPos = 999999;
				
				
				if ((headerPos < 0 || menuePos > 0) && !$(this).hasClass("invisible")) {
					$(this).not(".bigPicture, .whiteContainer").children().stop(true, false).fadeTo(v2, 0, function() {
						$(this).parent().addClass("invisible");
					});
	
					if (force) $(this).addClass("invisible");
				} else if ((headerPos >= 0 && menuePos <= 0) && $(this).hasClass("invisible")) {
					$(this).not(".bigPicture, .whiteContainer").children().stop(true, false).fadeTo(v2, 1, function() {
						$(this).parent().removeClass("invisible");					
						if ($(this).hasClass("skills") && $(this).hasClass("init")) showSkills();
					});
				
					if (force) $(this).removeClass("invisible");
				}
			} else {
				var grenze_unten = cache_innerHeight + cache_offsetY - 250;
				var whiteTop = grenze_unten - $(this).offset().top;
				
				if (whiteTop < -60) whiteTop = -60;
				if (whiteTop > $(this).height()) whiteTop = $(this).height();
				
				var whiteHeight = 1000; //cache_innerHeight - $(this).offset().top;
				
				var dif = Math.abs(whiteTop - $(this).offset().top);
				
				var v3 = v2;
				
				if (dif != 0) {
					$("div.white", this).stop(true, false).animate({
						top: whiteTop + "px"
					}, v3);
					
					$("div.white2", this).stop(true, false).animate({
						top: whiteTop + 230 + "px",
						height: whiteHeight + "px"
					}, v3);	
				}
			}
		});
	}
}

function isFadable(obj) {
	if ($(obj).context.tagName == "img" && $(obj).parent().hasClass("bigPicture"))
		return false;
		
	return true;
}

function showSkills() {	
	$("div.skills ul").each(function() {
		width = $(this).attr("id");
		
		$(this).animate({
			width: width + "%"
		}, 2000 * (100 / width), function() {
			$("li", this).fadeIn();
		});
	});
	
	$("div.skills").removeClass("init");
}

function scrollingChanged() {
	if ((Math.abs(window.innerHeight - cache_innerHeight) > 20 || Math.abs(window.pageYOffset - cache_offsetY) > 20) || 
		(counter > 5 && (window.innerHeight != cache_innerHeight || window.pageYOffset != cache_offsetY))) {
		cache_innerHeight = window.innerHeight;
		cache_offsetY = window.pageYOffset;
		counter = -1;
		return true;
	}
	
	counter++;	
	return false;
}

function replaceURLByLink(text) {
	var start = 0;
	var gefunden = -1;
	
	do {
		gefunden = strpos(text, "http://", start);
		if (gefunden > -1) {
			ende = strpos(text, " ", gefunden);
			if (ende == -1) ende = text.length - 1;
			
			url = text.substring(gefunden, ende);
			url = '<a href="' + url + '" target="_blank">' + url + '</a>';
			tmp_text = text.substring(0, gefunden) + url;
			
			if (ende < text.length - 1) tmp_text += text.substring(ende + 1, text.length - 1);
			
			text = tmp_text;
			start = gefunden + url.length - 1;
		}
	} while (gefunden > -1);
	
	return text;
}

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/strpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);    // *     returns 1: 14
    var i = (haystack + '').indexOf(needle, (offset || 0));
    // return i === -1 ? false : i;
    return i;
}

function twitterCallback2(twitters) {
	var statusHTML = [];
	for (var i=0; i<twitters.length; i++) {
		var username = twitters[i].user.screen_name;
		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
			return '<a href="'+url+'">'+url+'</a>';
		}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		});
		
		statusHTML.push('<span class="date">' + relative_time(twitters[i].created_at) + '</span>' + status);
	}
	
	document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function relative_time(time) {
	var month = new Array()
	month["Jan"] = "Januar";
	month["Feb"] = "Februar";
	month["Mar"] = "März";
	month["Apr"] = "April";
	month["May"] = "Mai";
	month["Jun"] = "Juni";
	month["Jul"] = "Juli";
	month["Aug"] = "August";
	month["Sep"] = "September";
	month["Oct"] = "Oktober";
	month["Nov"] = "November";
	month["Dec"] = "December";
	
	time = time.split(" ");
	
	return time[2] + ". " + month[time[1]] + " " + time[5];
}

$(document).ready(function() {
	jQuery.easing.def = "easeOutCubic";
	init();
	window.setInterval("updateView(false)", 50);
});
