/*

	TetraHentai.Com
	---------------
	
	All the javascript jQuery shizz be up in here.

*/

	var loadingCount = 0;
	var cSwitchTimer;
	var cBlockActive = 0;
	var cBlockList = ['blockMangas', 'blockMovies', 'blockGames', 'blockOtakulinks'];

	$(document).ready(function() {							  							   
		$(".recentUpdates a").click(function() {
			$(".recentUpdates a").removeClass("selectedCat");
			$(this).addClass("selectedCat");
			
			clearTimeout(cSwitchTimer);
		});
		
		cSwitchTimer = setTimeout("switchUpdateBlock();", 5000);
	});
	
	function switchUpdateBlock() {
		cBlockActive++;
		if(cBlockActive > 3) { cBlockActive = 0; }
		
		switchBlock(cBlockList[cBlockActive], 'block_recent_updates');
		$(".recentUpdates a").removeClass("selectedCat");
		$("#a_" + cBlockList[cBlockActive]).addClass("selectedCat"); 
		
		cSwitchTimer = setTimeout("switchUpdateBlock();", 5000);
	}
	
	function switchBlock(blockId, sectionId) {
		$("#" + sectionId + " div.block_content").fadeOut("slow");
		$("#" + sectionId + " #" + blockId).fadeIn("slow");
	}
	
	function expandPost(postId) {
		loadingCount++; updateLoadingDiv();
		$("#epost" + postId).slideUp("normal", function() {
			$.get("fpSource/frontPageGetPost.php?id=" + postId, function(data) {
				$("#epost" + postId).attr('innerHTML', data);
				$("#epost" + postId).slideDown("normal");
				
				//document.getElementById("br" + postId).style.display = "none";
				loadingCount--; updateLoadingDiv();
			});
		});
		
		expData = $.cookie('fp_expansion');
		
		if(expData == null) {
			expData = "|;|".split(";"); 
		} else {
			expData = expData.split(";");
		}
		var newCookie = "";

		var colData = expData[0].split("|");
		var opnData = expData[1].split("|");
		
		for(i=0;i<colData.length;i++) {
			if(colData[i] != postId && colData[i] != "") { newCookie = newCookie + "|" + colData[i] }
		}
		newCookie = newCookie + ";";
		
		for(i=0;i<opnData.length;i++) {
			if(opnData[i] != postId && opnData[i] != "") { newCookie = newCookie + "|" + opnData[i]; }
		}
		newCookie = newCookie + "|" + postId;
				
		$.cookie('fp_expansion', newCookie);
		
	}
	
	function collapseThread(threadId) {
		loadingCount++; updateLoadingDiv();
		$("#epost" + threadId).slideUp("normal", function() {
			$.get("fpSource/frontPageGetCollapse.php?id=" + threadId, function(data) {
				$("#epost" + threadId).attr('innerHTML', data);
				$("#epost" + threadId).slideDown("normal");
				
				//document.getElementById("br" + postId).style.display = "block";
				//$("#br" + threadId).slideDown("slow");	
				
				loadingCount--; updateLoadingDiv();				
			});															 
		});
		
		var postId = threadId;

		expData = $.cookie('fp_expansion');
		
		if(expData == null) {
			expData = "|;|".split(";"); 
		} else {
			expData = expData.split(";");
		}
		var newCookie = "";

		var colData = expData[0].split("|");
		var opnData = expData[1].split("|");
		
		for(i=0;i<colData.length;i++) {
			if(colData[i] != postId && colData[i] != "") { newCookie = newCookie + "|" + colData[i] }
		}
		newCookie = newCookie + "|" + postId;
		newCookie = newCookie + ";";
		
		for(i=0;i<opnData.length;i++) {
			if(opnData[i] != postId && opnData[i] != "") { newCookie = newCookie + "|" + opnData[i]; }
		}

		$.cookie('fp_expansion', newCookie);
		
	}
	
	function updateLoadingDiv() {
		if(loadingCount > 0) {
			$('#loadingDiv').fadeIn("slow");
		}
		
		if(loadingCount < 1) {
			$('#loadingDiv').fadeOut("slow");
		}
	}
	
	function closeGuide() {
		$("#top_guide").fadeOut("slow", function() { $("#top_guide_container").slideUp("slow"); });
		
		var date = new Date();
		date.setTime(date.getTime() + (1000 * 60 * 60 * 24 * 365 * 10)); // 10 years later. lolol.
		$.cookie('showguide', '0', { expires: date } );
	}
	
	function showPopUp(id) {
		document.getElementById(id).style.display = "block";
	}
	
	function destroyPopUp(id) {
		document.getElementById(id).style.display = "none";
	}	