// <script src="http://twitter.com/statuses/user_timeline/mdulle.json?callback=twitterCallback2&count=1" type="text/javascript"></script>
// <script src="http://api.twitter.com/1/statuses/user_timeline.json?callback=twitterCallback2&screen_name=mdulle&count=1&include_entities=true" type="text/javascript"></script>

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('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id_str+'">'+twitterRelativeTime(twitters[i].created_at)+'</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function twitterRelativeTime(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 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}

function inputToggle(classname,value,backon) {
	$(classname).focus(function() {
		if (value == $(classname).val()) {
			$(this).val('');
		}
	});
	$(classname).blur(function() {
		if (backon == 'yes') {
		if ('' == $(classname).val()) {
			$(this).val(value);
		}
		}
	});
}

function whenPageLoads(template_directory) {
	//sp=100; op=.6; $("#header .headerblogname").fadeTo(sp, op); $("#header .headerpagelist").fadeTo(sp, op); $("#header input").fadeTo(sp, op);

	// if ($.browser.msie) {
	// 	var origbg = $("#headershadow").css('background');
	// 	$("#headershadow").css({
	// 		'background' : 'transparent',
	// 		'width' : '660px',
	// 		'padding' : '8px 120px 10px',
	// 		'font' : '13px Helvetica, Arial, sans-serif',
	// 		'color' : '',
	// 		'text-align' : 'center',
	// 	});
	// 	$("#headershadow").html("This site makes use of some CSS standards not supported in your browser. As such, some elements of this site may not look or work properly. I recommend upgrading to the newest version of either <a href=\"http://www.firefox.com/\">Firefox</a> or <a href=\"http://www.apple.com/safari/\">Safari</a>.");
	// 	$("#headershadow").click(function() { $(this).css({ 'background': origbg }); $(this).html(""); });
	// }

	var i=0; $(".post > .posttitle").each(function() { if (i>0) { $(this).css({ 'border-top' : '4px double #92908b' }); } i++; });
	var j=0; $(".searchresult").each(function() { if (j>0) { $(this).before('<hr>'); } j++; });
	// $(".postdate").each(function() { $(this).click(function() { $(this).children(".posttime").fadeToggle(200); }); });
	// $(".postentry p").each(function() {
	// 	if ($(this).hasClass("attachment")) {}
	// 	else if ($(this).children("img").length == 0 && $(this).children("object").length == 0) { $(this).addClass("texty"); }
	// })
	
	// $(".postentry p:first-child").each(function() {
	// 	if ($(this).children("img").length == 0) { $(this).addClass("texty-first"); }
	// })
	
	// $("#content .shadow400").after("<span class=\"picshadow pic-400\"></span>");
	$("#content .attachment-full").after("<span class=\"picshadow pic-l\"></span>");
	$("#content .attachment-full, #content .shadow400").each(function() {
		if ($(this).attr("width") < 640) { $(this).siblings("span.pic-l").removeClass("pic-l").addClass("pic-p"); }
		
		var rndmtape = Math.floor( Math.random() * 10 );
		//$(this).before("<span class=\"tape tape" + rndmtape + "\" title=\"Click for EXIF Data\"></span>");
		
		var rndmdeg = (Math.floor( Math.random() * 13 ) * .1) + .6;
		if (Math.floor(Math.random()*2) == 1) { rndmdeg = rndmdeg * -1; }
		$(this).css({ '-moz-transform' : 'rotate(' + rndmdeg + 'deg)', '-webkit-transform' : 'rotate(' + rndmdeg + 'deg)', 'transform' : 'rotate(' + rndmdeg + 'deg)' });
		$(this).siblings(".picshadow").css({ '-moz-transform' : 'rotate(' + rndmdeg + 'deg)', '-webkit-transform' : 'rotate(' + rndmdeg + 'deg)', 'transform' : 'rotate(' + rndmdeg + 'deg)' });
		
		$(this).removeClass("attachment-full").addClass("attachment-full-mod");
	});
	
	$("#content .attachment-video").after("<span class=\"picshadow pic-l\"></span>");
	$("#content .attachment-video").each(function() {
		$(this).removeClass("attachment-video").addClass("attachment-video-mod");
	});
	
	
	// $("#content .attachment-full").after("<span class=\"exifinfo\"></span>");
	// $(".exifinfo").each(function() {
	// 	var imagepath = encodeURIComponent($(this).prev("img").attr("src"));
	// 	$(this).load(template_directory + '/exifer/index.php?path=' + imagepath);
	// 	var speed = 300;
	// 	var picleft = $(this).siblings("img").position().left + (($(this).siblings("img").width() / 2) - ($(this).width() / 2) - 12);
	// 	var pictop = $(this).siblings("img").position().top + 50;
	// 	$(this).css({ left : picleft + "px", top : pictop + "px" });
	// 	$(this).siblings("img").attr('fade', 'no');
	// 	$(this).siblings("img").click(function(){
	// 		$(this).siblings(".exifinfo").fadeToggle(speed);
	// 	 	if ($(this).attr('fade') == 'no') {
	// 	 		$(this).fadeTo(speed,'.1');
	// 	 		$(this).attr('fade', 'yes');
	// 	 	} else {
	// 	 		$(this).fadeTo(speed,'1');
	// 	 		$(this).attr('fade', 'no');
	// 	 	}
	// 	});
	// });
	
	
    var fadeDuration = 100; //time in milliseconds
	$('.footerlists ul li a').hover(
		function() {
			$(this).animate({ paddingLeft: '7px' }, fadeDuration).animate({ paddingLeft: '4px' }, 50 );
		},
		function() {
			$(this).animate({ paddingLeft: '0px' }, fadeDuration);
		}
	);
	$('.postentry img.attachment-thumbnail').hover(
		function() {
			$(this).animate({ opacity: '1' }, fadeDuration).animate({ opacity: '.9' }, 50 );
		},
		function() {
			$(this).animate({ opacity: '.8' }, fadeDuration);
		}
	);

	
	inputToggle('input#comment-author','Name (required)','yes');
	inputToggle('input#comment-email','Email (required, not posted)','yes');
	inputToggle('input#comment-url','Website','no');
	inputToggle('textarea#comment-comment','Message','yes');
	inputToggle('input#quick-search-query','Search','yes');
}
