var popupStatus = 0;

function init()
{
	// initalize popup
	$(".formular").click(function(){  
		centerPopup();  
		showPopup();  
	});
	
	$("#formularPopupClose").click(function(){  
		hidePopup();  
	});   
	
	$("#backgroundPopup").click(function(){  
		hidePopup();  
	});  
	
	$(document).keypress(function(e){  
		if(e.keyCode == 27 && popupStatus == 1)
		{  
			hidePopup();  
		}  
	});  
	
	$("#kontaktform").submit(function() {
		return checkForm();
	});
	
	// initalize rollover
	$('#referenz .more').hide();

	$('#referenz a.ref_entry').click(
		function() {
			var elem = $(this).next();

			if(elem.is('.more'))
			{
				if(elem.is(':visible'))
				{
					elem.slideUp('fast');
				}
				else
				{
					$('#referenz .more:visible').slideUp('fast');
					elem.slideDown('normal');
				}
			}
			return false;
		}
	);
	
	// load StackOverflow data
	$.getJSON("http://stackoverflow.com/users/flair/198861.json?callback=?", flairCallback);

	// load Twitter data at end of body
	var url = 'http://twitter.com/statuses/user_timeline/dwo.json?callback=twitterCallback&amp;count=5';
	var script = document.createElement('script');
	script.setAttribute('src', url);
	document.body.appendChild(script);
}

function flairCallback(data) 
{
	$("#so_rep").append(data.reputation);
	$("#so_badges").append(data.badgeHtml);
	$("#so_user").attr("href", data.profileUrl).append(data.displayName);
}

function twitterCallback(twitters) 
{
  var statusHTML = [];
  var limit = twitters.length > 3 ? 3 : twitters.length;
  for (var i=0; i < limit; 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 target="_new" href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<li><span>'+status+'</span> <a target="_new" style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function relative_time(time_value) 
{
	var values = time_value.split(" ");
	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);

	if (delta < 60) 
	{
		return 'vor wenigen Sekunden';
	} 
	else if(delta < 120) 
	{
		return 'vor einer Minute';
	} 
	else if(delta < (60*60)) 
	{
		return (parseInt(delta / 60)).toString() + ' minutes ago';
	} 
	else if(delta < (120*60)) 
	{
		return 'vor ca. einer Stunde';
	} 
	else if(delta < (24*60*60)) 
	{
		return 'vor ca. ' + (parseInt(delta / 3600)).toString() + ' Stunden';
	} 
	else if(delta < (48*60*60)) 
	{
		return 'gestern';
	} 
	else 
	{
		return "vor " + (parseInt(delta / 86400)).toString() + ' Tagen';
	}
}

function showPopup()
{
	if(popupStatus == 0)
	{  
		$("#backgroundPopup").css({"opacity": "0.7"});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#formularPopup").fadeIn("slow");  
		popupStatus = 1;  
	}  
}

function hidePopup()
{
	if(popupStatus == 1)
	{  
		$("#backgroundPopup").fadeOut("slow");  
		$("#formularPopup").fadeOut("slow");  
		popupStatus = 0;  
	} 
}

function centerPopup()
{  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#formularPopup").height();  
	var popupWidth = $("#formularPopup").width();  
	
	$("#formularPopup").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  

	$("#backgroundPopup").css({  
		"height": windowHeight  
	});  
} 

function checkForm()
{
	var form = document.forms["kontaktform"];

	if(form.fName.value == "")
		alert("Bitte tragen Sie Ihren Namen ein!");
	else if(form.fMail.value == "")
		alert("Bitte tragen Sie Ihre email-Adresse ein!");
	else if(form.fText.value == "")
		alert("Bitte tragen Sie Ihre Nachricht ein!");
	else
	{
		alert('Vielen Dank für Ihre Anfrage!');
		return true;
	}
		
	return false;
}
