// Core funky friends object
var fff = {
	data: {},
	state: {},
	nodes: {},
	archive: {
		init: function(){
			var nodes = fff.nodes;
			nodes.archive = $(".archive");
			nodes.archiveInner = $(".archive .inner");
			nodes.archiveMonths = $(".archive dl");
			nodes.archiveNext = $("#nextMonth");
			nodes.archivePrev = $("#prevMonth").addClass("disabled");
			
			nodes.archiveInner.css("width",(nodes.archiveMonths.length*111)+"px");
			
			var currentMonth = 0;
			
			nodes.archiveMonths.each(function(index, month){
				$(month).css("left", (index*111)+"px");
			});
			
			nodes.archiveNext.click(function(){
				if(currentMonth < nodes.archiveMonths.length-2) {
					currentMonth += 1;				
					nodes.archiveInner.animate({
					    left: '-=110'
					}, 200);
				}
				if(currentMonth+1 > nodes.archiveMonths.length-2){					
					$(this).addClass("disabled");					
				} else {
					$(this).removeClass("disabled");
				}
				if(currentMonth-1 < 0){
					nodes.archivePrev.addClass("disabled");
				} else {
					nodes.archivePrev.removeClass("disabled");
				}
			});
			nodes.archivePrev.click(function(){
				if(currentMonth > 0) {
					currentMonth -= 1;
					nodes.archiveInner.animate({
					    left: '+=110'
					}, 200);
				}
				if(currentMonth-1 < 0){
					$(this).addClass("disabled");
				} else {
					$(this).removeClass("disabled");
				}
				if(currentMonth+1 > nodes.archiveMonths.length-2){					
					nodes.archiveNext.addClass("disabled");					
				} else {
					nodes.archiveNext.removeClass("disabled");
				}				
			});			
		}
	},
	decorateImages: function(container){
		fff.nodes.contentImages = $(container+" img");
		
		fff.nodes.contentImages.each(function(index, image){
			var $image = $(image);
			var newWidth = $image.attr("width");
			var newClass = $image.attr("class");
		
			var $wrapper = $('<div class="thumbnail '+newClass+'" style="width: '+newWidth+'px;"></div>');

			// Check for an anchor surround image and make it the $image target instead
			var $parent = $image.parent();
			if($parent[0].tagName.toLowerCase()=="a"){
				$image = $parent;
				$image.append('<span class="icon"></span>');
			}
			
			// Wrap the image with the thumbnail class etc
			$image.wrap($wrapper);
			
			// Add corners and icon
			$wrapper = $image.parent();
			//$wrapper.append('<span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span>');
		});		

		fff.nodes.linkImages = $(container+" a.link-image");
		fff.nodes.linkImages.fancybox({
			'titleShow' 	: true,
			'titlePosition' : 'inside',
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack'
		});				

		// IE6 png fix for the new dynamically created png corners
		if($.browser.msie && $.browser.version=="6.0") {
			DD_belatedPNG.fix('#header img, div.member-tools, div.member-tools div.inner, #promotions li, #promotions a, #page, #page-middle, #page-bottom, #post-it, .post-featured span, #footer div.container, .text-and-images .thumbnail .tl, .text-and-images .thumbnail .tr, .text-and-images .thumbnail .bl, .text-and-images .thumbnail .br');
			$("#promotions li:first").addClass("first-child");
		};
		
	},	
	init: function(){
		$(document).ready(function() {
		
			jQuery("abbr.timeago").timeago();
			fff.nodes.pageContent = $("#page-content");
					   
			$(".text-and-images p:first").addClass("intro");
			fff.decorateImages(".text-and-images");
 			fff.archive.init();
 			$("#page-middle").height(fff.nodes.pageContent.height()-480);
		});
	}
};

// Begin
fff.init();
Cufon.replace('.intro h1');
Cufon.replace('#footer h2');
Cufon.replace('#post-it h2');
Cufon.replace('legend');

