$(document).ready(function() {
	$("#content form input, #content form textarea").each(function() {
		$(this).bind("focus", function(e) {
			$(this).addClass("active");
		}).bind("blur", function(e) {
			$(this).removeClass("active");
		});
	});
	
	$(".contact-email").each(function() {
		var tld = "co" + "m";
		var preAt = "ma" + "il";
		var domainFirst = "x" + "a" + "bit";
		$(this).html(preAt + "@mi" + domainFirst + "." + tld);
	});
	
	$('div.boxes').each(function() {
		$(this).append('<span class="clear" />');
	});
	
	if ($("#socialstatus").length > 0) {
		$("#socialstatus").append('<div class="menu"><img src="/int/img/socials.png" usemap="#socialstatusmenu" /><map name="socialstatusmenu"><area shape="poly" coords="32,110,160,49,160,103,32,164" href="javascript:clickSocialStatusInfo(\'facebook\');" alt="Facebook" /><area shape="poly" coords="32,172,160,111,160,165,32,226" href="javascript:clickSocialStatusInfo(\'xing\');" alt="Xing" /><area shape="poly" coords="32,234,160,173,160,227,32,288" href="javascript:clickSocialStatusInfo(\'lastfm\');" alt="Last.FM" /><area shape="poly" coords="32,296,160,235,160,289,32,350" href="javascript:clickSocialStatusInfo(\'twitter\');" alt="Twitter" /></map></div>');
		loadSocialStatusInfoNext = "facebook";
		socialStatusAutoMode = true;
		socialStatusTimer();
		
		$("#socialstatus").bind("mouseenter", function(e) {
			socialStatusAutoMode = false;
		});
		$("#socialstatus").bind("mouseleave", function(e) {
			socialStatusAutoMode = true;
		});
	}
});

function socialStatusTimer() {
	if (socialStatusAutoMode) {
		loadSocialStatusInfo();
	}
	window.setTimeout("socialStatusTimer()", 5000);
}

function clickSocialStatusInfo(mode) {
	socialStatusAutoMode = false;
	loadSocialStatusInfoNext = mode;
	loadSocialStatusInfo();
}

function loadSocialStatusInfo() {
	
	var mode = loadSocialStatusInfoNext;
	var valid = false;
	var text = null;
	
	if (mode == "facebook") {
		valid = true;
		$("#socialstatus .marker").animate({ top: "54px" }, 1000 );
		loadSocialStatusInfoNext = "xing";
		text = '<strong>Networking bei Facebook</strong><br /><a href="http://www.facebook.com/mixabit/" target="_blank">Profil aufrufen</a>';
		$("#socialstatus .info").html('<a href="http://www.facebook.com/mixabit" target="_blank"><img src="http://badge.facebook.com/badge/1245753048.1221.367013791.png" width="120" height="280" style="border: 0px;" /></a>');
	}
	
	if (mode == "xing") {
		valid = true;
		$("#socialstatus .marker").animate({ top: "116px" }, 1000 );
		loadSocialStatusInfoNext = "lastfm";
		text = '<strong>Networking bei Xing</strong><br /><a href="https://www.xing.com/profile/Maximilian_Franzke2" target="_blank">Profil aufrufen</a>';
		$("#socialstatus .info").html('<img src="/int/img/xing.jpg" alt="Xing" />');
	}

	if (mode == "lastfm") {
		valid = true;
		$("#socialstatus .marker").animate({ top: "178px" }, 1000 );
		loadSocialStatusInfoNext = "twitter";
		text = '<strong>Gerade geh&ouml;rte Musik via Last.FM</strong><br /><a href="http://www.lastfm.de/user/mixabit/" target="_blank">Profil aufrufen</a>';
		
		$.ajax({
  			url: "/int/external/?load=lastfm",
  			cache: true,
  			success: function(xml) {
  				var html = "";
  				  $('track', xml).each(function(i) {
  				  	var date = new Date(parseInt($(this).find("date").attr('uts')) * 1000);
    				html = html + '<p>' + $(this).find("artist").text() + ':<br />' + $(this).find("name").text() + '<br /><a href="http://www.lastfm.de/user/mixabit/" target="_blank" class="more">';
    				if ($(this).find("date").length == 0) {
    					html = html + "l&auml;uft gerade";
    				} else {
    					html = html + ((date.getDate() < 10) ? "0" + date.getDate() : date.getDate()) + '.' + ((date.getMonth()+1 < 10) ? "0" + (date.getMonth()+1) : (date.getMonth()+1)) + ' ' + ((date.getHours() < 10) ? "0" + date.getHours() : date.getHours()) + ':' + ((date.getMinutes() < 10) ? "0" + date.getMinutes() : date.getMinutes());
    				}
    				html = html + '</a></p>';
				});
			
    			$("#socialstatus .info").html(html);
  			}
		});
	}
	
	if (mode == "twitter") {
		valid = true;
		$("#socialstatus .marker").animate({ top: "240px" }, 1000 );
		loadSocialStatusInfoNext = "facebook";
		text = '<strong>Aktuelle Statusupdates bei Twitter</strong><br /><a href="http://twitter.com/mixabit" target="_blank">Profil aufrufen</a>';
		
		$.ajax({
  			url: "/int/external/?load=twitter",
  			cache: true,
  			success: function(xml) {
  				var html = "";
  				  $('status', xml).each(function(i) {
  				  	var twittertime = $(this).find("created_at:first").text();
  				  	var timevalues = twittertime.split(" ");
            		var parsed_date = Date.parse(timevalues[1] + " " + timevalues[2] + ", " + timevalues[5] + " " + timevalues[3]);
            		var date = new Date(parsed_date);
            		
            		modifyStatusLinks = function(status) {
            			return status.replace(/[a-z]+:\/\/[a-z0-9-_]+\.[a-z0-9-_:~%&\?\/.=]+[^:\.,\)\s*$]/ig, function(m) {
            				return '<a href="' + m + '" target="_blank">' + ((m.length > 25) ? m.substr(0, 24) + '...' : m) + '</a>';
          					});
          			}
          			modifyStatusUsers = function(status) {
        				return status.replace(/(^|[^\w]+)\@([a-zA-Z0-9_]{1,15})/g, function(m, m1, m2) {
            				return m1 + '@<a href="http://twitter.com/' + m2 + '" target="_blank">' + m2 + '</a>';
          				});
          			}
			        modifyStatusHashs = function(status) {
			        	return status.replace(/(^|[^\w'"]+)\#([a-zA-Z0-9_]+)/g, function(m, m1, m2) {
            				return m1 + '#<a href="http://search.twitter.com/search?q=%23' + m2 + '" target="_blank">' + m2 + '</a>'
            			});
            		}

    				html = html + '<p>' + modifyStatusHashs(modifyStatusUsers(modifyStatusLinks($(this).find("text").text()))) + '<br /><a href="http://www.twitter.com/mixabit/status/' + $(this).find("id:first").text() + '" target="_blank" class="more">' + ((date.getDate() < 10) ? "0" + date.getDate() : date.getDate()) + '.' + ((date.getMonth()+1 < 10) ? "0" + (date.getMonth()+1) : (date.getMonth()+1)) + ' ' + ((date.getHours() < 10) ? "0" + date.getHours() : date.getHours()) + ':' + ((date.getMinutes() < 10) ? "0" + date.getMinutes() : date.getMinutes()) + '</a></p>';
				});
			
    			$("#socialstatus .info").html(html);
  			}
		});
	}
	
	if (valid) {
		$("#socialstatus span").html(text);
	}
}
