$(document).ready(function(){

//console.log("hallo");
	// Indicate Javascript for CSS
	$("body").addClass('hasjavascript');
	$("body").removeClass('hasnojavascript');
	
	// Add Preloader Images
	$('.loading_white').html('<img src="http://onygo.org/wp-content/themes/onygo-template/images/ajax-loader-white.gif" />');

	// Twitter
	$.jTwitter('ma6', 1, function(posts){
		var text = posts[0].text ;
		
		// console.log (posts[0]);
		// text = text.replace(new RegExp("@"+posts[0].in_reply_to_screen_name, "gi"), '@<a href="http://twitter.com/'+posts[0].in_reply_to_screen_name+'">'+posts[0].in_reply_to_screen_name+'</a>');
		
		text = text.replace(/http:\/\/(\S*)/gi,"<a href=\"http://$1\" class=\"external\">http://$1</a>");
		text = text.replace(/https:\/\/(\S*)/gi,"<a href=\"https://$1\" class=\"external\">https://$1</a>");
		text = text.replace(/@(\w*)/gi, "@<a href=\"http://twitter.com/$1\" class=\"external\">$1</a>");
		text = text.replace(/#fb/gi, "");
		text = text.replace(/#(\w*)/gi, "#<a href=\"http://search.twitter.com/search?q=%23$1\" class=\"external\">$1</a>");
		
		//// console.log(text);
		if (posts[0].in_reply_to_screen_name) {
			text += ' <a href="http://twitter.com/'+posts[0].in_reply_to_screen_name+'/statuses/'+posts[0].in_reply_to_status_id+'" class="aktt_tweet_reply external">';
			text += 'als Antwort auf '+posts[0].in_reply_to_screen_name+'</a> ';
		}
		$('#tweet').html(text);
		

		var thedate = new Date(posts[0].created_at);
		var thetime;
		var isodate = thedate.format("c");
		thetime = '[<a href="http://twitter.com/'+ posts[0].user.screen_name +'/statuses/'+ posts[0].id +'" class="aktt_tweet_time external timestamp" timestamp="' + isodate + '" title="' + isodate + '">';
		thetime += 'am '+ thedate.format("j. M Y") + ' um ' + thedate.format("H:i");
		thetime += '</a> via ' + '<a href="http://twitter.com/'+ posts[0].user.screen_name +'/" class="external">Twitter</a>]';
		
		//// console.log(thetime);

		$('#tweettime').html(thetime);
		$(".timestamp").timeago();
		$("#twitter .preloader").addClass('hide');
		$("#twitter .status").removeClass('jshide');

	});



	// Update Gravatar on Comment Inputs
	$("#email").change(function () {
		var codedemail = hex_md5($(this).attr("value"));
		var path = "http://www.gravatar.com/avatar/"+codedemail+"?s=80&amp;d=identicon&amp;r=G";
		$("#commenter-gravatar").html('<a href="http://gravatar.com"><img alt="Gravatar" src="'+path+'" class="avatar avatar-80 photo" height="80" width="80" /></a>');
	});


	// Show Hide Default Txt
	$("input[type=text]").attr("value", function (arr) {
		return $(this).attr("value") ? $(this).attr("value") : $(this).attr("defaultText");
	});
	$("input[type=text]").focus(function(){
		$(this).addClass('active');
		if ($(this).val() == $(this).attr("defaultText")) {
			$(this).val("");
		}
	});
	$("input[type=text]").blur(function(){
		$(this).removeClass('active');
		if ($(this).val() == "") {
			$(this).val($(this).attr("defaultText"));
		}
	});
	$("textarea").attr("value", function (arr) {
		return $(this).attr("value") ? $(this).attr("value") : $(this).attr("defaultText");
	});
	$("textarea").focus(function(){
		$(this).addClass('active');
		if ($(this).val() == $(this).attr("defaultText")) {
			$(this).val("");
		}
	});
	$("textarea").blur(function(){
		$(this).removeClass('active');
		if ($(this).val() == "") {
			$(this).val($(this).attr("defaultText"));
		}
	});

	// Add Pseudo focus Classes
	$("input[type=button]").focus(function(){
		$(this).addClass('active');
	});
	$("input[type=button]").blur(function(){
		$(this).removeClass('active');
	});
	$("input[type=submit]").focus(function(){
		$(this).addClass('active');
	});
	$("input[type=submit]").mouseleave(function(){
		$(this).removeClass('active');
	});
	$("input[type=button]").mouseenter(function(){
		$(this).addClass('active');
	});
	$("input[type=button]").mouseleave(function(){
		$(this).removeClass('active');
	});
	$("input[type=submit]").mouseenter(function(){
		$(this).addClass('active');
	});
	$("input[type=submit]").blur(function(){
		$(this).removeClass('active');
	});
	
	// Add style to Textarea if it has Content
	$("textarea").change(function(){
		if ($(this).val() == "") {
			$(this).removeClass('hascontent');
		} else {
			$(this).addClass('hascontent');
		}
	});

	// Prevent Default text from being posted
	$("#commentform").submit(function(){
		$("input[type=text]").attr("value", function (arr) {
			var el = $(this);
			return (el.attr("defaultText") != el.val()) ? el.val() : "";
		});
		$("textarea").attr("value", function (arr) {
			var el = $(this);
			return (el.attr("defaultText") != el.val()) ? el.val() : "";
		});
	});


	// Add reply Functionality to Comments
	$(".replyto").click(function(){
		var el = $("#comment");
		var reply = $(this).attr("replyto");
		reply = reply + " " + ((el.val() != el.attr("defaultText")) ? el.val() : "");
		el.val(reply);
		el.addClass('hascontent');
		el.focus();
	});

	// Add Lightbox effect
	$("a.lightbox").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayOpacity': 0.25,
		'overlayColor': '#000'
	});

	// Add infinite Scroll
	$('#content').infinitescroll({
		navSelector  : "#paging",
		nextSelector : "#paging .older a",
		itemSelector : "#content div.post",
		loadingImg   : "http://martin-gude.de/wp-content/themes/onygo-template/images/ajax-loader-black.gif",
		bufferPx     : 800,
		loadingText  : ""
	});

	// Google Analytics
	var trackSite = ($.cookie("ga_optout") != "dontrack");

	if (trackSite) {
		$.GATrackPage({account_id: 'UA-290795-1', anonymize: true});
		$(".external").each(function() {
			$(this).bind('click keypress', function(event) {
				var code=event.charCode || event.keyCode;
				if(!code || (code && code == 13)) {
					var fixedLink = $(this).attr('href');
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/external/' + fixedLink;
					$.GATrackPageview(fixedLink);
				}
			});
		});
		
		$('#optout').attr('checked','checked');
		$.cookie("ga_optout", "track", { path: '/', expires: 365 });
	} else {
		$('#optout').attr('checked','');
		$.cookie("ga_optout", "dontrack", { path: '/', expires: 365 });
	}

	$('#optout').change(function() {
		$("#optoutloader").removeClass ("hide");
		if ($(this).attr('checked')) {
			$.cookie("ga_optout", "track", { path: '/', expires: 365 });
			// console.log ("track");
		} else {
			$.cookie("ga_optout", "dontrack", { path: '/', expires: 365 });
			// console.log ("optout");
		}
		setInterval(function() { $("#optoutloader").addClass ("hide"); }, 2000);
		// $("#optoutloader").addClass ("hide");
	});

});