(function($){
	$.fn.wrapChildren = function(options) {

	var options = $.extend({
								childElem : undefined,
								sets : 1,
								wrapper : 'div'
							}, options || {});
							
	if (options.childElem === undefined) return this;

	return this.each(function() {
		var elems = $(this).children(options.childElem);
		var arr = [];

		elems.each(function(i,value) {
		arr.push(value);
		
			if (((i + 1) % options.sets === 0) || (i === elems.length -1)) {
				var set = $(arr);
				arr = [];
				set.wrapAll($("<" + options.wrapper + " class='mini-gallery'>"));
			}
		});
	});
}
})(jQuery);

var scrOfX, scrOfY, scrAmmt, scrNext, scrPrev;							//scrolling vars
var scrSpeed = 200;

var expand = true;

//var postPos = 1;

var galItems = 0;

var postNum = 1;

function scrollChanges() {												//when window scrolls
	var windowWidth = $(window).width();
	var windowScroll = $(window).scrollLeft();
	var rightEdgeWidth = ((windowWidth - 700)/2) - 15;					// ((windowWidth - 820)/2) - 15;

	$("#footer").stop().animate(
		{ marginLeft: rightEdgeWidth + windowScroll + 15 + "px"	},
		"slow" /*{ easing:'easeOutExpo' }*/
	);
	
	//$("#logo a").text(windowScroll);
	
	if (typeof(window.pageYOffset) == 'number') {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if (document.body && ( document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	
	//$("#logo a").text(scrOfX + ", " + scrOfY);
	
	//console.log("finished if");
	
	var boxWidth = 715;		//835px
	
	scrAmmt = Math.floor(scrOfX / boxWidth) * boxWidth;
	
	if (scrOfX % boxWidth == 0) {
		scrPrev = scrAmmt - boxWidth;
	} else {
		scrPrev = scrAmmt;
	}
	
	scrNext = scrAmmt + boxWidth;
	
	//$("#logo a").text(windowScroll);
	
	//alert(scrPrev + ", " + scrNext);
}

function resizeChanges() {												//when window resizes
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var rightEdgeWidth = ((windowWidth - 700)/2) - 15;					//((windowWidth - 820)/2) - 15;
	var paddingRight = rightEdgeWidth + "px";
	//var leftEdgeWidth = (windowWidth - 820)/2;
	//var paddingLeft = leftEdgeWidth + "px";
	
	
	var winHeightChange = 585;	//653
	
	if (windowHeight <= winHeightChange) {
		$("#logo").fadeTo("fast", 0.33);
		
	} else if (windowHeight > winHeightChange) {
		$("#logo").fadeTo("fast", 1);
		
	}
	
	//alert(paddingRight);
	
	$("#tables-ugh").css({"padding-right" : paddingRight});	
}

function scrollers() {													//scrollers animate
	$("#previous").click(function() {
		
		$.scrollTo(scrPrev, 200, {easing:'easeOutExpo'});
		scrollChanges();
		
		//postPos--;		
		//window.location.hash = postPos;
	});
	
	$("#next").click(function() {
		
		$.scrollTo(scrNext, 200, {easing:'easeOutExpo'});
		scrollChanges();
		
		//postPos++;		
		//window.location.hash = postPos;
	});

	//$("#previous").click(function() {$.scrollTo( '-=835', 500, {easing:'easeOutExpo'});});	
	//$("#next").click(function() {$.scrollTo( '+=835', 500, {easing:'easeOutExpo'});});
	
	var pageWidth = $("#tables-ugh").width()

	$(document)
		.bind("keydown", {combi:"left", disableInInput: true}, function() {
			$.scrollTo(scrPrev, scrSpeed, {easing:'easeOutExpo'});
		})
		.bind("keydown", {combi:"pageup", disableInInput: true}, function() {
			$.scrollTo(scrPrev, scrSpeed, {easing:'easeOutExpo'});
		})
		.bind("keydown", {combi:"right", disableInInput: true}, function() {
			$.scrollTo(scrNext, scrSpeed, {easing:'easeOutExpo'});
		})
		.bind("keydown", {combi:"pagedown", disableInInput: true}, function() {
			$.scrollTo(scrNext, scrSpeed, {easing:'easeOutExpo'});
		})
		.bind("keydown", {combi:"home", disableInInput: true}, function() {
			$.scrollTo(0, scrSpeed, {easing:'easeOutExpo'});
		})
		.bind("keydown", {combi:"end", disableInInput: true}, function() {
			$.scrollTo(pageWidth, scrSpeed, {easing:'easeOutExpo'});	
		});		
		
	$("#scrollers div").css("opacity", "0.50");
	
	$("#scrollers div").hover(function() {
		$(this).stop().fadeTo(250, .8);
		
		//.animate({ backgroundColor: "#0596D5", opacity: 0.5 }, 1000 );

	}, function() {
		$(this).stop().fadeTo(250, .5);
		
		//.animate({ backgroundColor: "#000", opacity: 0.5 }, 1000 );		
	});
}
	
function boldFirstWord() {												//bold first word
	$('.exerpt p:first-child').each(function(){
		var me = $(this);
		me.html( me.text().replace(/(^\w+)/,'<span>$1</span>') );
	});
}

function footerExpand(styleToDo) {										//close/toggle footer

	if (styleToDo == "toggle") { 

		if (expand) {
					$("#footer .expandable").stop().animate({
				height: "240px",
				top: "-240px",
				opacity: 1,
			},
				"slow" /*{ easing:'easeOutExpo' }*/
			);
			
			//$("#content").stop().fadeTo("slow", 0.1);			
			
			$("#footer a.expand").html("collapse");
			
			expand = false;
		} else {
			$("#footer .expandable").stop().animate({
				height: 0,
				top: 0,	//15px
				opacity: 0,
			},
				"slow" /*{ easing:'easeOutExpo' }*/
			);
			
			//$("#content").stop().fadeTo("slow", 1);
			
			$("#footer a.expand").html("expand");
			
			expand = true;	
		}
		
	} else if (styleToDo == "close") {
	
		$("#footer .expandable").stop().animate({
			height: 0,
			top: 0, //15px
			opacity: 0,
		},
			"slow" /*{ easing:'easeOutExpo' }*/
		);
		
		//$("#content").stop().fadeTo("slow", 1);
		
		$("#footer a.expand").html("expand");
		
		expand = true;
	}
}

jQuery(function($) {													//mouse wheel scroll
    $(document).bind('mousewheel', function(event, delta) {
		
		if (delta > 0) {
			$.scrollTo(scrPrev, scrSpeed, {easing:'easeOutExpo'});
		} else {
			$.scrollTo(scrNext, scrSpeed, {easing:'easeOutExpo'});
		}
		
		return false;
    });
});

// I GOT LAZY OKAY?

function NSFWToggling() {												//NSFW toggle
	var nsfwCookie = $.cookie("nsfw");
	
	if (nsfwCookie == "on") {
		$("a#nsfw").text("Hide NSFW Materials");
	
	} else {
		$("a#nsfw").text("Show NSFW Materials");
		$.cookie("nsfw", "off")
		
		$(".category-nsfw").each(function() {
			$(this).detach();		
		});
	}
	
	$("a#nsfw").click(function() {	
		if (nsfwCookie == "on") {
			$.cookie("nsfw", "off")
		} else {
			$.cookie("nsfw", "on")
		}
		window.location.reload();		
	});	
}

function videoPostConfig() {											//1. setup video posts
	var vidHeight = 680, vidWidth = 330;	//800 x 390
	var vidBigHeight = 420;					//480
	
	$('embed').each(function() {										//modify embed tags
		$(this).attr({
			width: vidHeight,
			height: vidWidth,
			wmode: "transparent"
		})
		
		.before("<param name='wmode' value='transparent'></param>")
		
		.removeAttr("style");
		
		$("#content .big-post embed").attr({height: vidBigHeight});
	});
	
	$("object").each(function() {										//modify object tags
		$(this).removeAttr("style").attr({width: vidHeight, height: vidWidth});
		
		$("#content .big-post object").attr({height: 480});
	});
	
	$("#content .video .disc").each(function() {						//modify description
		
		$(this).css("width", 665 - $(this).next().width()); 			//description width // 795px
		var discHeight = $(this).height();								//description height
		var discPHeight = $(this).find("p").height();					//p height
	
		if (discHeight > 39) {											//too big desc 
			$(this).css({"height" : 39}).append("<div class='fade'></div>");
			
			$(this).hover(function() {
				$(this).find(".fade").stop()							//fade out fader
				.animate({
					opacity : 0,
					bottom: -20
				}, 200);
				
				$(this).stop()											//change size of desc
				.animate({
					top: 5 + ((35 - 5) - discPHeight),
					height: discPHeight,
					paddingTop: 5
					
				}, 200);
				
			}, function() {												//fade in fader
				$(this).find(".fade").stop()
				.animate({
					opacity: 1,
					bottom: 4
				}, 200);
				
				$(this).stop()											//reset size of desc
				.animate({
					top: 5,
					height: 39,
					paddingTop: 0
				}, 200);
			});
	
		}
	});
}

function wrappingPosts() {												//2, wrap all posts
	$("#content .big-post object").each(function() {					//wrap video section
		$(this).wrap("<div class='section post'></div>");
	});
	
	$("#content .big-post .gallery").each(function() {					//wrap gallery section
		$(this).wrap("<div class='section post'></div>");
	});
	
	$("#content .post .section").each(function() {						//append to end
		$(this).detach().appendTo("#content");
	});	
}

function galleryPostConfig() {											//3. setup gallery posts
	$(".gallery").wrapChildren({ childElem : 'dl' , sets: 8})	
	
	$(".mini-gallery").each(function() {
		galItems++;
		
		if (galItems > 1) {
			$(this).detach().appendTo("#content")
				.wrap("<div class='section post'></div>")
				.wrap("<div id='gallery-1' class='gallery'></div>");
		}	
	});
	
	$(".gallery br").each(function() {
		$(this).remove();
	});
	
	$(".gallery-item").each(function() {
		$(this).appendTo().parent().parent();
	});
}

$(document).ready(function() {

	//-- RESIZE OR SCROLLING PAGE
	$(window).resize(function() { resizeChanges(); scrollChanges(); });				//on resize
	$(window).scroll(function() { scrollChanges(); footerExpand("close"); });		//on scroll
	
	NSFWToggling();
	
	videoPostConfig();
	wrappingPosts();
	galleryPostConfig();
	
	//-- APPEND COMMENTS TO END
	$("#comments").each(function() {
		$(this).detach().appendTo("#content");
	});	
	
	//-- ADDING TABLES	
	$("#content").wrapInner("<table cellspacing='30' id='tables-ugh'><tr>");	
	$(".post").wrap("<td>");
	
	//-- BACK TO BEGINING LINK
	$("#footer a.start").click(function() {					
		$.scrollTo('0', 500, {easing:'easeOutExpo'});
	});
	
	$("#footer .expandable").css("opacity", "0");
	
	//-- EXPANDING FOOTER	
	$("#footer a.expand").click(function() {	
		footerExpand("toggle");	
	});
	
	//-- POST NUMBERS
	$(".post-num").each(function() {
		$(this).text(postNum);
		postNum++;
	});
	
	//-- MISC FUNCTIONS
	$("a").each(function() {											//empty links
		var ahref = $(this).attr("href");
		if (ahref == "") {
			$(this).attr({
				href: "javascript:void(0)"
			});
		}
	});
	
	$("p").filter(function() {											//empty paragraphs
		return $.trim($(this).text()) === "";
    }).remove();
	
	//-- LEAVING THE PAGE
	
	$("a").click(function() {
		var aHref = $(this).attr("href").indexOf("anetmag.com/");
		var aRel = $(this).attr("rel").indexOf("shadowbox");
		
		//alert(aHref + " " + aRel);
		
		if (aHref >= 0 && aRel < 0) {
			//alert("internal link and not picture");
			$("#loading").fadeIn({ easing:"easeOutExpo" });
		}
	});
	
	//-- ONLOAD OF PAGE
	$(window).load(function() {
		$("#loading").fadeOut({ easing:"easeOutExpo" });
	});

	//-- INITILIZE FUNCTIONS
	boldFirstWord(); scrollers();
	
	//-- INITIALIZE FUNCTIONS AGAIN
	resizeChanges(); scrollChanges();
});