$(document).ready(function() {
	
	$.ajaxSetup({async: false});
	$.getScript("/inc/jjfunctions.js");
	$.ajaxSetup({async: true});
	
	$.ajaxSetup({async: false});
	$.getScript("/inc/jquery.bxSlider.min.js");
	$.ajaxSetup({async: true});
	
	$.ajaxSetup({async: false});
	$.getScript("/inc/jquery.colorbox.js");
	$.ajaxSetup({async: true});
	
	$.ajaxSetup({async: false});
	$.getScript("/inc/jquery.jtweetsanywhere.js");
	$.ajaxSetup({async: true});
	
	$("#truth_comic_link").live("click",function(){
		if($("#truth_cover_large").length>0) { $("#truth_cover_large").remove(); }
		pageShadow.css("display","block").animate({ opacity: .9 },500).click(function(){ $("#truth_cover_large").fadeOut(500,function(){ pageShadow.fadeOut(500); }); });
		$("body").prepend("<div id=\"truth_cover_large\"><img src=\"/images/coming_soon.png\" id=\"coming_soon\"><img src=\"/images/truth_cover_large.jpg\"></div>");
		
		if($(this).parent().attr("id")=="post_truth") { popup_top=center("y")+250; }
			else { popup_top=center("y")-250; }
		
		$("#truth_cover_large").css({
			top: popup_top,
			left: center("x")-182
		}).click(function(){ $("#truth_cover_large").fadeOut(500,function(){ pageShadow.fadeOut(500); }); });
		return false;
	});
	
	$("#truth-guy").click(function(){ document.location.href="http://yourmomcomics.com/2012/02/08/the-truth-is-coming/"; });
	
	$('.tweets').each(function(){
		$(this).jTweetsAnywhere({
    	username: $(this).attr("rel"),
	    count: 2,
			tweetTimestampDecorator: function(tweet, options){
       var screenName =
           tweet.user ? tweet.user.screen_name : false || tweet.from_user;
       var date = new Date(formatDate(tweet.created_at));

       var dateString =
           date.getHours() + ':' +
           (date.getMinutes() < 10 ? '0' : '') +
           date.getMinutes() + ' ' +
           (date.getMonth() + 1) + '/' +
           date.getDate() + '/' +
           date.getFullYear();

        var html =
            '';

       return html;
    }
		});	
	});
	
	$(".colorbox").colorbox();
	// Drop the shadow onto the board.
	
	$("window").resize(function(){
		
		if($("#linksBox").length>0) {
			$("#linksBox").css({
				left: center("x")-256,
				top: center("y")-200
			});
		}
		
		if($("#title_animation").length>0) {			
			$("#title_animation").css({			
				left: (center("x")-150)			
			});				
		}
		
		if($("#animationLinks").length>0) {			
			$("#animationLinks").css({ left: center("x")-296 });			
		}

		if($("#theaterBox").length>0) {			
			$("#theaterBox").css({									
				top: center("y")-215,
				left: center("x")-390			
			});			
		}
		
		if($("#linksBox").length>0) {
			$("#linksBox").css({
				left: center("x")-256,
				top: center("y")-200
			});
		}
		
	});
	
	$("body").prepend("<img src=\"/images/shadow_spotlight.png\" id=\"shadow-spotlight\"><div id=\"page-shadow\"></div>");
	
	$("#page-shadow").css({
		
		width: $(document).width(),
		height: $(document).height()*2,
		opacity: 0
		
	});
	
	// $("*").hover(function(){ $("#posBox").attr("value","#"+$(this).attr("id")+": "+pos($(this)).top+","+pos($(this)).left); });
	
	$("#shadow-spotlight").css({
		
		width: $(document).width(),
		height: $(document).height(),
		opacity: 0
		
	});

	var pageShadow=$("#page-shadow");
	var pageSpotlight=$("#shadow-spotlight");

	$("#topImage ul").bxSlider({
  	  displaySlideQty: 1,
	    moveSlideQty: 1
 	});
	
	$("#ymcGallery").bxSlider({
  	  displaySlideQty: 1,
	    moveSlideQty: 1
 	});
	
	$("#topImage .bx-prev").html("");
	$("#topImage .bx-next").html("<a href=\"#\" id=\"button_nextFrame\"><div id=\"tooltip_nextFrame\"><span class=\"alt\">Next Frame</span></div></a>");
		
	$("#welcomeMessage").html($("#homeSection").html());

	$("#button_nextFrame").hover(function(){
			
		setTimeout(function(){
		
			$("#tooltip_nextFrame").css({ opacity: 0, display: "block" }).animate({
				marginTop: "+=16px",
				opacity: 1
			},1500);
				
		},1500);
			
	},function(){
			
		setTimeout(function(){
			
			$("#tooltip_nextFrame").animate({
				marginTop: "-=16px",
				opacity: 0
			},500,function(){ $(this).css({ opacity: 0, display: "block" }); });
				
		},1500);
			
	});
	
	/* $("#ymcLogo").hover(function(){
		$(this).animate({
			marginTop: "-16px"
  	}, 250);
	},function(){
		$(this).animate({
			marginTop: 0
		}, 250);	
	}); */
	
	$(".blackBG").each(function(){
		
		$(this).css({
			width: $(this).parent().width()+"px",
			height: $(this).parent().height()+"px"			
		}).animate({ opacity: .5 },250);
		
	});
	
	$("#header").append("<div id=\"ymcFace\"><div id=\"adminLogin\">[+]</div><div id=\"ymcThoughtCarrot\"></div></div>");
	
	$("#ymcFace").css({
		width: 296,
		height: 301,
		background: "url(\"/images/ymc_face.png\") top left no-repeat",
		position: "absolute",
		top: ($("#topImage").height()),
		left: ($("#topImage").width()-200),
		zIndex: 50
	}).children("#ymcThoughtCarrot").css({
		position: "absolute",
		width: 194,
		height: 171,
		background: "url('/images/bubble_thought_carrot.png')",
		marginLeft: "-128px",
		marginTop: "-32px",
	});
	
	function pos(elementObj) {
		
		return elementObj.position();
		
	}
	
	var ymcFace=[];
	
	ymcFace["left"]=pos($("#ymcFace")).left;
	ymcFace["top"]=pos($("#ymcFace")).top;
	
	function resetEverything() {
		
		$("#ymcFace").animate({
			top: ymcFace["top"],
			left: ymcFace["left"],
			opacity: 1
		},500);
		
		$("#headerBody").animate({ opacity: 1 },500);
		$("#ymcThoughtCarrot").animate({ opacity: 1 },500);
		
		$("#comics-display").animate({ opacity: 0 },500,function(){ $(this).remove(); });
		
		$("#bubble-yelling-comics").animate({ opacity: 0 },500,function(){ $(this).remove(); });
		
		$("#title_animation").animate({ opacity: 0 },500,function(){ $(this).remove(); });
		
		$("#animationLinks").animate({ opacity: 0 },500,function(){ $(this).remove(); });
		
		$("#shatteredGlass").remove();
		
	}
	
	// Begin Comics Section
	
	$("#comicsLink").click(function(){
		
		if($.browser.msie || $("body").attr("id")=="wpTemplate") { document.location.href="/our-comics/"; return false; }
		
		resetEverything();
		
		$("#headerBody").animate({ opacity: 0 },1000);
						
		$("#ymcFace").animate({
			top: "0"		
		},1500, function(){
			
			$("body").append("<div id=\"comics-display\"><h3 id=\"title_comics\"></h3><div id=\"comics-display-glass1\"></div><div id=\"comics-display-glass2\"></div></div>");
			
			$("#comics-display").append("<div id=\"comicBooks\">"+$("#comicBooks").html()+"</div>");
			
			windowQuarter=($(window).width()/4);
			
			$("#comics-display").css({ top: $("body").height(), left: windowQuarter, opacity: 0, display: "block" }).animate({ top: 225, opacity: 1 },2500);
			
			$("#comicBooks a").each(function(){
				
				$(this).hover(function(){
				
					$(this).prepend("<span class='toolTip' style='display: none;'>"+$(this).find("img").attr("title")+"</span>")
					$(this).find(".toolTip").css({ opacity: 0, display: "block" }).animate({ opacity: 1 },500);
				
				},function(){
				
					$(this).find(".toolTip").animate({ opacity: 0 },500,function(){ $(this).remove(); });
				
				});
			
			});
			
			$("#ymcFace").prepend("<div id=\"bubble-yelling-comics\"></div>");
			
			$("#bubble-yelling-comics").css({ opacity: 0, display: "block" }).animate({
				marginTop: "+=128px",
				opacity: 1
			},1000);
						
		});
			
		$("#ymcThoughtCarrot").animate({
			opacity: 0
		},750);
		
		return false;
		
	});
		
	// End Comics Section 
	
	// Begin Animation Section
	
	$("#animationLink").click(function(){
		
		if($.browser.msie || $("body").attr("id")=="wpTemplate") { document.location.href="/animation/"; return false; }
			
		resetEverything();		
		
		$("#ymcFace").animate({ opacity: 0 },500);		
		$("#headerBody").animate({ opacity: 0 },1000,function(){	
		
			$("body").prepend("<div id=\"title_ani_box\"><div id=\"title_animation\"></div></div>");
		
			$("#title_animation").css({
			
				left: (center("x")-150)
			
			}).animate({
			
				top: 125
			
			},500,function(){
				
				$("#title_ani_box").prepend("<div id=\"shatteredGlass\" style=\"left: "+pos($("#title_animation")).left+"px; top: "+pos($("#title_animation")).top+"px\"></div>");
				
				shake($("#title_animation"));
				shake($("#logo"));
				shake($("#topNav"));				
				
				setTimeout(function(){
					$("#title_animation").after("<div id=\"animationLinks\">"+$("#animationThumbs").html()+"</div>");
					$("#animationLinks").css({ left: center("x")-296, opacity: 0, display: "block" }).animate({
						
						marginTop: 250,
						opacity: 1
						
					},1000).find("a").each(function(){
						
						var thisHREF=$(this).attr("href");
						
						$(this).prepend("<div class=\"ani-thumb-hover\"></div>").click(function(){
							
							pageShadow.css({ display: "block" }).animate({ opacity: .5 },750,function(){
								
								pageSpotlight.css({ display: "block" }).animate({ opacity: .75 },1250,function(){
								
									$("body").prepend('<div id="theaterBox"></div>');
								
									$("#theaterBox").css({
									
										top: center("y")-215,
										left: center("x")-390,
										opacity: 0,
										display: "block"
									
									}).animate({ opacity: 1 },500).html('<div id=\"closeTheaterBox\" class=\"closeButton\">Close this shit!</div><object type="application/x-shockwave-flash" wmode="transparent" data="'+thisHREF+'" width="780" height="439"><param name="movie" value="'+thisHREF+'" /><param name="wmode" value="transparent"></object>');
									
									$("#closeTheaterBox").click(function(){
									
										$("#theaterBox").animate({
										
											opacity: 0
										
										},500,function(){
										
											pageSpotlight.animate({ opacity: 0 },500,function(){ $(this).css({ display: "none" }); });
											pageShadow.animate({ opacity: 0 },750, function(){
										
												$(this).css({ display: "none" });
												$(this).remove();
											
												$("#theaterBox").remove();
											
											});										
																				
										}).find("object").animate({ opacity: 0 },500,function(){ $(this).remove(); });
									
									});
								
								});
								
							});
							
							return false;
							
						});
						$(".ani-thumb-hover").each(function(){
							
							$(this).css({ opacity: 0, display: "block" }).hover(function(){ $(this).animate({ opacity: 1 },500); },function(){ $(this).animate({ opacity: 0 }); },500);
							
						});
						
					});
				},1000);
				
			});
			
		});
		
		return false;
						
	});
	
	
	
	$("#linksLink").click(function(){
		
		
			
	});
	
	function shake(elementObj) {
		
		originalTop=elementObj.css("margin-top");
		
		elementObj.animate({ marginTop: "-=50px" },50,function(){
			elementObj.animate({ marginTop: "+=100px" },50,function(){
				elementObj.animate({ marginTop: "-=75px" },50,function(){
					elementObj.animate({ marginTop: "+=50px" },50,function(){
						elementObj.animate({ marginTop: "-=35px" },50,function(){
							elementObj.animate({ marginTop: "-=10px" },50,function(){
								elementObj.animate({ marginTop: originalTop });								
							});
						});
					});
				});
			});
		});
		
	}
	
	// End Animation Section
	
	$("#homeLink").click(function(){
		
		if($.browser.msie || $("body").attr("id")=="wpTemplate") { document.location.href="/"; return false; }
		
		resetEverything();
		
		return false;
		
	});
	
	$("#topNav ul li").each(function(){
				
	}).hover(function(){ $(this).animate({ top: "-24px"},750); },function(){ $(this).animate({ top: "-36px"},750); });
	
	function center(coord) {
		
		switch(coord){
			
			case "x":
				return $(window).width()/2
				break;
			case "y":
				return $(window).height()/2
				break;
			default:
				return ($(window).width()/2)+","+($(window).height()/2)
				break;
			
		}
		
	}
	
	if($(".type-post").length>0) { $("#comments").css("display","block"); }
	
	var post_img=$(".type-post img").eq(0);
	
	post_img.css("display","none").before("<div style=\"display: block;width: "+post_img.width()+"px;height: "+post_img.height()+"px;background: url('"+post_img.attr("src")+"') top left no-repeat;border-radius: 16px;-moz-border-radius: 16px;\"></div>");
	
	if($(".category-tweeted").length>0) {
		
		$(".category-tweeted .entry-content").html("<div id=\"twitterBlock\"><div class=\"tCont\">"+$(".category-tweeted .entry-content").html()+"</div></div>");
		$(".entry-meta").css("display","none");
		$(".entry-title").css("display","none");
		
	}
	
	if(ex($("#galleryBox"))) {
		
		$("#galleryBox a img").each(function(){
			
			$(this).wrap("<span class=\"roundedIMG galleryIMG\"></span>");
			
		});
		
		$("#galleryBox a").each(function(){ $(this).css({ opacity: 0, display: "block" }).animate({ "opacity": 1 },500).attr("rel","ymcGallery"); });
	
		$("#galleryBox a").colorbox();
		
	}
	
	$(".roundedIMG").each(function(){
		
		$(this).css({
			background: "url('"+$(this).find("img").attr("src")+"') center center no-repeat"
		});
		
	});
	
	$("#button_gallery").hover(function(){ $(this).find("img").animate({ width: 200, height: 82, left: 12, top: 4 },1000); },function(){ $(this).find("img").animate({ width: 223, height: 93, left: 0, top: 0 },1000); });
		
	$("#adminLogin").css({
		
		opacity: 0.25,
		display: "block"
		
	}).hover(function(){
		$(this).animate({ opacity: 1 },1000);
	},function(){		
		$(this).animate({ opacity: .25 },1000);
	}).click(function(){ document.location.href="/wp-admin"; });
	
	$("#prevPostsList li").each(function(){
		
		var thisLI=$(this);
		
		$(this).hover(function(){
			
			$(".prevPostsTooltip").each(function(){ $(this).remove(); });
			
			setTimeout(function(){
			
				$(".prevPostsTooltip").each(function(){ $(this).remove(); });
			
				thisLI.prepend("<div class=\"prevPostsTooltip\" rel=\""+$("#prevPostsList li").index($(this))+"\"><div class=\"newContent\">"+thisLI.attr("data-excerpt")+"</div><div class=\"tempContent "+thisLI.find("a").eq(0).attr("class")+"\">"+thisLI.find("a").eq(0).html()+"</div></div>");
				
				$(".prevPostsTooltip .tempContent").animate({
					width: 384,
					height: 32,
					fontSize: 14,
					marginLeft: "-24px",
					opacity: 0
				},500);
			
				$(".prevPostsTooltip .newContent").css({ opacity: 0, display: "block" }).animate({
					opacity: 1			
				},750);
				
			},750);
			
		},function(){
			
			$(".prevPostsTooltip .newContent").animate({
				opacity: 0			
			},750,function(){ thisLI.find(".prevPostsTooltip").remove(); });
			
			
			
		});
		
	});
	
	/*
	
	truth_guy_preload="";
	
	for(tg=1;tg<=48;tg++){
		if(tg<10) { tg_disp="0"+tg; } else { tg_disp=tg; }
		truth_guy_preload+="<img src='/images/hoodie-man/hoodie-man00"+tg_disp+".png' style='display: none;'>";
	}
	
	$("body").prepend(truth_guy_preload);
	
	*/
	
	//animateTruthGuy();
	
	// Believe it or not, this is the end, my friend.
	
});

var undefined;

function animateTruthGuy(){
	tg=$("#truth-guy");
	if(tg.attr("rel")==undefined){ tg.attr("rel","1"); }
	
	current_frame=parseInt(tg.attr("rel"));
	
	if(current_frame<10){ current_frame_display="0"+current_frame; } else { current_frame_display=current_frame; }
	tg.css("background-image","url('/images/hoodie-man/hoodie-man00"+current_frame_display+".png')");
	
	if(current_frame<48) { current_frame++; } else { current_frame=1; }
	
	tg.attr("rel",current_frame);
	setTimeout("animateTruthGuy()",160);
	
}
