/*
-------------------------------------------------------- 
The Fabler - Homepage specific js
Author: Sam Lu
Author URI: http://samlu.ca

-------------------------------------------------------- 
*/

// Functions

var News = {
	init: function(){
		News.hijackLinks();
		News.blogActive();
	},
	hijackLinks: function(){
		$('#nl_blog_btn').attr('href', 'javascript:;');
		$('#nl_recent_btn').attr('href', 'javascript:;');
		$('#nl_blog_btn').click(News.blogActive);
		$('#nl_recent_btn').click(News.recentActive);
	},
	blogActive: function(){
		if($('#nl_recent_btn').hasClass('nl_recent_active')) {
			$('#nl_recent_btn').removeClass('nl_recent_active');
			$('#nl_recent_btn').addClass('nl_recent');
		}
		if($('#nl_blog_btn').hasClass('nl_blog')){
			$('#nl_blog_btn').removeClass('nl_blog');
			$('#nl_blog_btn').addClass('nl_blog_active');
		}
		$('#recent_activity').hide();
		$('#blog_news').show();
	},
	recentActive: function(){
		if($('#nl_blog_btn').hasClass('nl_blog_active')) {
			$('#nl_blog_btn').removeClass('nl_blog_active');
			$('#nl_blog_btn').addClass('nl_blog');
		}
		if($('#nl_recent_btn').hasClass('nl_recent')){
			$('#nl_recent_btn').removeClass('nl_recent');
			$('#nl_recent_btn').addClass('nl_recent_active');
		}
		$('#blog_news').hide();
		$('#recent_activity').show();
	}
}

var Sorter = {
	init: function(){
	
		$('.sl_overall').click(function(e){clearSortItems(); $(this).toggleClass("sl_overall_active"); loadComics("_topOverall"); e.preventDefault();});
		$('.sl_artwork').click(function(e){clearSortItems(); $(this).toggleClass("sl_artwork_active"); loadComics("_topArt"); e.preventDefault();});
		$('.sl_story').click(function(e){clearSortItems(); $(this).toggleClass("sl_story_active"); loadComics("_topStory"); e.preventDefault();});
		$('.sl_newest').click(function(e){clearSortItems(); $(this).toggleClass("sl_newest_active"); loadComics("_newest"); e.preventDefault();});
		
		$(".sl_newest").toggleClass("sl_newest_active");
		$(".blue_block710 .sortbar").after($("#_newest").html());
		$(".blue_block710 .comicpreview").fadeIn("slow");
		$(".web-comic-container .star").click(function(){openComic(this);});
	}
};


function loadComics(comicDiv){
	$(".blue_block710 .comicpreview").fadeOut("slow", function(){
		$(".blue_block710 .comicpreview").remove();
		$(".blue_block710 .sortbar").after($("#" + comicDiv).html());
		$(".blue_block710 .comicpreview").fadeIn("slow");
		$(".web-comic-container .star").click(function(){openComic(this);});
	});
}

function clearSortItems(){
	$(".sorting_list li a").each(function(){
		this.className = this.id;
	});
}

function getBlogEntries(){
	$("#blog_news").load("/rss/thefablerblog");
}

function openComic(ele){
	var comicId = ele.id.substring(0, ele.id.indexOf("_"));
	window.location.href = $(".web-comic-container .comic_link_" + comicId).get(0).href;
}

function openPrintComic(ele){	
	var comicId = ele.id.substring(0, ele.id.indexOf("_"));
	window.location.href = $(".print-comic-container .comic_link_" + comicId).get(0).href;
}

$(document).ready(function(){
	News.init();
	Sorter.init();
	getBlogEntries();
	
	$(".web-comic-container .star, .web-comic-container .star").click(function(){openComic(this);});
	$(".print-comic-container .star, .print-comic-container .star").click(function(){openPrintComic(this);});
});