// Seriously Basic Slideshow

$(document).ready(function(){	
	
	//log("This document is ready.");
	
	// var expandContent = $(this).prevALL('.expandContent:first');
	// var expandExcerpt = $(this).prevALL('.expandExcerpt:first');
		
	$(".gallery").each(function () {
		$(this).attr("id", 0);
		$(this).children(".gallery-item").css("display", "none");
		$(this).children(".gallery-item").eq(0).css("display", "block");
	});
	
	$(".gallery").eq(0).children("h2").each(function () {
		$(this).css("display","none");
	});
			
	$(".gallery-right").click(function(event) {
		//Advance Slideshow
		//console.log($(this).parent().attr('id'));
		changeItem($(this),1)
	});
	
	function updateArrowPos(){
		$("img.gallery-nav").each(function () {
			var gallery = $(this).parent().parent();
			var n = Number( gallery.attr('id') );
			var fullH = gallery.children(".gallery-item").eq(n).children('div').height();
			var halfH = Math.round( (fullH - $(this).height()) /2) + "px";
			$(this).css("margin-top",halfH);
			gallery.children(".gallery-button").css('height',fullH+'px');
		});
	}
	
	$(".gallery-left").click(function(event) {
		//Reverse Slideshow
		//console.log($(this).parent().attr('id'));
		changeItem($(this),-1)
	});
	
	$(".gallery-button").hover(
  		function (event) {
  			$(this).addClass('hoverButton');
  		}, 
  		function (event) {
  			$(this).removeClass('hoverButton');
  		}
	);
    	
	function changeItem(clicked, direction){
		var gallery = $(clicked).parent();
		var tot = gallery.children(".gallery-item").length - 1;
		var curN = gallery.attr('id');
		var n = Number(curN);
		
		n+=direction;
		if (n < 0){
			n = tot;
		}
		if (n > tot){
			n = 0;
		}
		
		//Hide the other divs
		gallery.children(".gallery-item").css("display", "none");
		gallery.children(".gallery-item").eq(n).css("display", "block");


		log("id:" + gallery.attr('id') + "    n:" +  n);
		gallery.attr("id", n);
		updateArrowPos();
	}

	updateArrowPos();
	
	//setTimeout();
	//newsTimer = setTimeout("changeNews()", newsInterval);
	//linkEnlargeFunctions();
});
