$(document).ready(function() {
	i = 2;
	graphblocks = $(".side-fade").size();
	while (i <= graphblocks) {
		$("#graphic-block"+i).hide();
		i++;
	}
	setInterval("changeGraphicBlock()",9000);
	
	$("#checkbox-control").click(function() {
		checkboxController()
	});
	
	$(".post-time").each(function (i) {
		$(this).text(prettyDate($(this).text()));	
	});
	$("#card-filter").click(function() {
		cardFilterForm();
	});
	
	$(".s_twitter").click(function() {
		window.location="http://twitter.com/home?status=Check out this awesome article on ManaNation! - "+encodeURIComponent(window.location);
	});
	$(".s_reddit").click(function() {
		window.location="http://www.reddit.com/submit?url=" + encodeURIComponent(window.location);
	});
	$(".s_mixx").click(function() {
		window.location='http://www.mixx.com/submit?page_url='+window.location;
	});
	$(".s_ff").click(function() {
			var e=document.createElement('script');
			e.setAttribute('type','text/javascript');
			e.setAttribute('src','http://friendfeed.com/share/bookmarklet/javascript');
			document.body.appendChild(e)
	});
	$(".s_fb").click(function() {
		window.location="http://www.facebook.com/sharer.php?u="+encodeURIComponent(window.location)+"&t="+encodeURIComponent(document.title);
	});
	$(".s_email").click(function() {
		//Do nothing
	});
	$(".s_digg").click(function() {
		window.location="http://digg.com/submit?url="+encodeURIComponent(location.href)+"&title="+encodeURIComponent(document.title)+"&bodytext=&media=&topic=";
	});
	$(".s_delicious").click(function() {
		window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550');
		return false;
	});
	
	$("#tab_twitter").getTwitter({
		userName: "mananation",
		numTweets: 1,
		loaderText: "Loading tweets...",
		slideIn: false,
		slideDuration: 0,
		showHeading: false,
		headingText: "Latest Tweet",
		showProfileLink: false,
		showTimestamp: true
	});

	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	$('#slideshowHolder').jqFancyTransitions({ width: 468, height: 200, effect: 'curtain' });

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});

	$("table.striped tr:even").addClass("alt");
});

function changeGraphicBlock() {
	graphblocks = $(".side-fade").size();
	for (i = 1; i <= graphblocks; i++) {
		j = i+1;
		if (j > graphblocks) { j = 1; }
		if (sidebaranimate(i,j)) {
			return;	
		}
	}
}
function sidebaranimate(i,j) {
	if ($("#graphic-block"+i).is(":visible")) {
		$("#graphic-block"+i).fadeOut("slow",function() {
			$("#graphic-block"+j).fadeIn("slow");
		});
		return true;
	}
	return false;	
}

function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		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 < 120 && "just now" ||
			diff < 3600 && Math.floor( diff / 60 ) + " min. 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";
}

function filterTo(tag,all) {
    if (all) {
        $('.event-box').hide();
    }
    $('.'+tag).show();
}
function filterHide(tag) {
    $('.'+tag).hide();
}
function cardFilterForm() {
	$(".spoiler-card").slideUp();
	var isExact = $("#search-exact").is(":checked");
	var searchString = "";
	$("#spoiled-cards input:checked:enabled").each(function(index, val) {
		var theval = $(this).val();

		if (isExact) {
			if (theval != "search-exact")
				searchString += "."+theval;
		} else {
			$("."+theval).slideDown();
		}
	});
	if (searchString != "") {
		$(searchString).slideDown();	
	}
}

function checkboxController() {
	var cbcontrol = $("#checkbox-control").html();
	var chkd = $("#search-exact").is(":checked");
	if (cbcontrol == "Uncheck All Checkboxes") {
		$("#spoiled-cards input:enabled").attr("checked","");
		$("#checkbox-control").html("Check All Checkboxes");
	} else {
		$("#spoiled-cards input:enabled").attr("checked","true");
		$("#checkbox-control").html("Uncheck All Checkboxes");
	}
	$("#search-exact").attr("checked",chkd);
}