$(document).ready(function() {

	var navExp = false;
	
	var menu_images_length = $("#navigation .menu_images img").length;
	var menu_image = $("#navigation .menu_images img").get().sort(function(){
		return Math.floor(Math.random()*menu_images_length);
	}).slice(0,1);
	
	$(menu_image).show();
	
	$(".post .gallery").mouseenter(function(){
		$(this).find(".thumb_overlay").show();
	}).mouseleave(function(){
		$(this).find(".thumb_overlay").hide();
	});

	$.fn.fadeToggle = function(speed, easing, callback){
        return this.animate({opacity:'toggle'}, speed, easing, callback);
    }
	
	var top_items_amt = $("#top_menu .category").size();
	$("#top_menu td").attr('width',top_items_amt + "%");
	
	$(".post .floater").mouseenter(function(){
		$(this).find(".text").fadeIn(200);
	}).mouseleave(function(){
		$(this).find(".text").fadeOut(200);
	});
	
	$("#navigation").mouseenter(function(){
		$("#top_menu").fadeIn(200);
		$(".arrow").fadeOut(200);
/*
		$("#page").click(function(){
			$("#top_menu").fadeOut(200);
			$(this).unbind('click');
		});
*/
	}).mouseleave(function(){
		$("#top_menu").fadeOut(200);
	});
	
	$("#top_menu img").mouseenter(function(){
		$(this).fadeTo(100,.5);
	}).mouseleave(function(){
		$(this).fadeTo(100,1);
	});
	
	$("#top_menu a").click(function(){
		$("#top_menu").fadeOut(200);
	});
	
	var currImg = 0;
	var imgArray = $(".post:first .images .image");
	$(imgArray[currImg]).show();
	
	if(imgArray.length > 0) {
		$(".body").find(".control.left").hide();
		$(".body").find(".control.right").show();
		currImg = 0;
		$(imgArray[currImg]).show();
	}
	
	$("#top_menu .name a").click(function(){
		var id = $(this).attr('id');
		id = id.replace("ca_","");
		var $category = $("#category_" + id);
		$("#post_grid").scrollTo($category,500, function(){
			window.location.hash = "#"+$category.find("a:first").attr('name');
		});
//		$(".side_menu").fadeOut("fast");
//		$(this).closest(".category").find(".side_menu").fadeIn("fast");
		$("#top_menu a").removeClass('selected');
		$(this).addClass('selected');
		return false;
	});
	
	$(".side_menu li a").click(function(){
		var id = $(this).attr('id');
		id = id.replace("pa_","");
		var $post = $("#z_" + id);
		$("#post_grid").scrollTo($post,500, function(){
			window.location.hash = "#"+$post.find("a:first").attr('name');
		});
		$(".side_menu a").removeClass('selected');
		$(this).addClass('selected');
		return false;
	});
	
	$(".addComment").click(function(){
		$(this).hide();
		$("#comment_form").fadeIn("fast");
	});
	
	$("#categories").find(".name").mouseenter(function(){
		$(this).addClass("highlighted");
	}).mouseleave(function(){
		$(this).removeClass("highlighted");
	});
	
	$(".menu_post .selected").closest(".category").prev().addClass('selected');
	
	
	$(".control:not(.inactive)").mouseenter(function(){
/*
		$(this).children(".arrow").css('filter','alpha(opacity=90)');
		$(this).children(".arrow").css('-moz-opacity','0.90');
		$(this).children(".arrow").css('-khtml-opacity','0.90');
		$(this).children(".arrow").css('opacity','0.90');
*/
	}).mouseleave(function(){
/*
		$(this).children(".arrow").css('filter','alpha(opacity=0)');
		$(this).children(".arrow").css('-moz-opacity','0');
		$(this).children(".arrow").css('-khtml-opacity','0');
		$(this).children(".arrow").css('opacity','0');
*/
	}).click(function(){
		if($(this).hasClass("left")) {
			$(".control.right:hidden").show();
			if(currImg == 1) {
				$(this).hide();
			} else {
				
			}
			currImg --;
		} else {
			$(".control.left:hidden").show();
			if(currImg == ($(imgArray).length)-2) {
				$(this).hide();
			} else {
				
			}
			currImg ++;
		}
		$(this).siblings(".image").hide();
		$(imgArray[currImg]).show();
	});
	
	$("#newsletterForm #leave").click(function(){
		$(this).siblings("#leaveBox").val('true');
	});
	
	$("#newsletterForm").bind('submit',function(e){
		$this = $(this);
		$("#newsletterFormResponse").html("processing...");
		$this.hide();
		e.preventDefault();
		$.ajax({
			url: 'announceListHandler.php',
			type: 'GET',
			data: $this.serialize(),
			complete: function(feedback) {
				$this.find("#leaveBox").val('false');
				$this.find("#email").attr('value','');
				$this.show();
				$("#newsletterFormResponse").html(feedback.responseText);
			}
		});
	});
	
/*	$("#post_grid").scroll(function(){
		var scrollY = (this.scrollY) ? this.scrollY : this.scrollTop;
		for(var i=0; i<anchors.length; i++) {
			var loc = anchors[i].offsetTop - scrollY;
			var base = loc + $(anchors[i]).next().height();
//			$("#header #address").html(currentWork);
			if(base > pplOffset.top && loc < pplOffset.top && currentWork != anchors[i].id) {
				currentWork = anchors[i].id;
				var classes = $("#" + currentWork).attr("class").split(" ");
				classes.shift();
				classes.shift();
//				alert(classes);
				$("#posts ul li").removeClass("active");
				$("#posts ul li .sidenote").hide();
				$("#posts ul li#" + currentWork).next().addClass("active").find(".sidenote").fadeIn("fast");
				$("#people ul li").removeClass("active");
				for(var i=0; i<classes.length; i++) {
					var li = $("#people ul #" + classes[i]);
					$(li).addClass("active");
					if($(li).prev().attr("id")) {
					}
				}
			}
		}
		$("#currentWork").html("current work: " + currentWork + ", location: " + loc);
	});
*/	
/* 	$(".category .year + li").show(); */

// LIGHTBOX AUTOLOAD MODULE
	if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
	jQuery(function($) {
		$("div[rel^='lightbox'], a[rel^='lightbox']").slimbox({	overlayFadeDuration: 200,
											resizeDuration: 200,
											imageFadeDuration: 200,
											captionAnimationDuration: 200,
											counterText: "{x} of {y}"
										}, null, function(el) {
			return (this == el) || ((this.getAttribute('rel').length > 8) && (this.getAttribute('rel') == el.getAttribute('rel')));
		});
	});
}
	
});
