// target ie 6,7
var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);

/* calculate our timeZone */
var d = new Date();
var offset = (d.getTimezoneOffset()/60);             

// Used internally, takes ISO timestamp, makes it pwetty
// http://ejohn.org/projects/javascript-pretty-date/#postcomment
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," "));

	date.setHours(date.getHours()-offset);
	diff = (((new Date()).getTime() - date.getTime()) / 1000);
	day_diff = Math.floor(diff / 86400);

	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 ) {
		return;
	}

	return day_diff === 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

$(document).ready(function(){

	// Friendfeed User Settings
	var settings = {
		userName : 'gtgrf', // user name
		nam : 'gtgrf',
		itemsToShow : 10, // Number of items to retrieve
		imageSize : 70 // thumbnail size
	};

	// Call friendfeed api
	$.getJSON('http://friendfeed.com/api/feed/user/' + settings.userName + '?num=' + settings.itemsToShow + '&callback=?',
		function(data) {
			$('#friendfeed-badge').append('<ul class="friendfeed"></ul>');
			var container = $('#friendfeed-badge ul');

		// loop for each friendfeed entry retrieved
		$.each(data.entries, function(i, entry) {
			if (entry.hidden !== true) {
				var svc = entry.service;

				var t = '';

				t += '<li>';
				t += '<a href="'+svc.profileUrl+'" title="View '+settings.nam+'\'s profile on '+svc.name+'" class="friendfeed_icon">';
				t += '<img src="'+svc.iconUrl+'" alt="'+svc.name+'">';
				t += '</a>';
				t += '<span class="friendfeed_text">';
				t += '<a href="'+entry.link+'" title="'+entry.title+'">';
				if (/flickr|tumblr|youtube/i.test(entry.service.id)) {
					if (entry.media.length > 0) {
					t += '<img width="'+settings.imageSize+'" src="'+entry.media[0].thumbnails[0].url+'" alt="'+entry.title+'" />';
					}
					else {
					t += entry.title;
					}
				}
				else {
					t += entry.title;
				}
				t += '</a>';
				t += '<span class="friendfeed_date">';
				t += prettyDate(entry.published);
				t += '</span>';
				t += '</span>';
				t += '</li>';

				// append content to container
				container.append(t);
			}
		});
	});

	// pngfix
	$(document).pngFix();

	// scroll to anchor
	// http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
	function filterPath(string) {
		return string
			.replace(/^\//,'')
			.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
			.replace(/\/$/,'');
	}
	var locationPath = filterPath(location.pathname);
	$('a[href*=#]').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;
		if ( locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) {
			var $target = $(this.hash), target = this.hash;
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$(this).click(function(event) {
					event.preventDefault();
					$('html, body').animate({scrollTop: targetOffset}, 500, function() {
						location.hash = target;
					});
				});
			}
		}
	});

	// ie6 hover hack
	$("ul.postmeta, .postdate, #breadcrumb, #footer, #respond p.commentdate, .st-tag-cloud").hover(function(){
		$(this).addClass("hover");
		},
		function() {
		$(this).removeClass("hover");
	});

	// input style
	$("#contactform :input, #commentform :input, #searchform :input").addClass("text");

	// extract blockquote metadata
	// http://heliologue.com/projects/blockquote-metadata-extractor/
	$("blockquote").each(function(){
		var title = $(this).attr("title");
		var cite = $(this).attr("cite");

		$("p:last-child",this).append("<span class='quote-close'>&nbsp;</span>");

		if (cite.length < 1 && title.length > 0) {
			$(this).append("<cite>"+title+"</cite>");
		}

		else if (cite.length > 0 && title.length < 1) {
			$(this).append("<cite><a href='"+cite+"' title='"+cite+"'>"+cite+"</a></cite>");
		}

		else if (cite.length > 0 && title.length > 0) {
			$(this).append("<cite><a href='"+cite+"' title='"+title+"'>"+title+"</a></cite>");
		}
	 });

	// fancybox
	// http://fancy.klade.lv
	$("a.fancybox").fancybox({
		'hideOnContentClick': true
	});

	// fancybox addon
	$("a.fancybox:has('img')").each(function() {
		$(this).css("position","relative").prepend("<span class='zoomIcon'></span>");
		$("a.fancybox .zoomIcon").hide();
		$("a.fancybox").hover(function() {
			$(".zoomIcon",this).fadeIn("fast");
			},
			function() {
			$(".zoomIcon",this).fadeOut("fast");
		});
		$("a.fancybox").click(function() {
			$(".zoomIcon",this).hide();
		});
	});

	// hide link border
	$("a:has('img.grf')").css("border","none");

	// fix overflow in IE
	// http://remysharp.com/2008/01/21/fixing-ie-overflow-problem/
	if (isIE) {
		$("pre").each(function () {
			if (this.scrollWidth > this.offsetWidth) {
				$(this).css({ "overflow-y" : "hidden", "padding-bottom" : "26px" });
			}
		});
	}
	
	// archives
	$("#smart-archives-list ul").each(function() {
		$("li:first",this).addClass("first");
	});

	// trackback, pingback gravatar
	$('.tb_comment, .pb_comment').find('.commentby').each(function() {
		$(this).prepend('<img src="/wp/wp-content/themes/gt.candy/images/blank_gravatar.gif" class="avatar" />');
	});

	// welcome back comment author
	var $toggleA = $('a.toggle-author');
	if ($toggleA.length) {
		$('#author_info').hide();
		$toggleA.click(function() {
			$('#author_info').fadeIn(1000);
			$toggleA.parent('.info').hide();
			return false;
		});
	}

});