$(document).ready(function() {
	$('#gallery a').css({opacity:0.0});
	numphotos = $('#gallery a').size();
	previous = -1;
	current = -1;
	next = -1;
	rndnum = getRandom();
	current = rndnum;
	$currImg = $('#gallery a').eq(current);
	$currImg.animate({'opacity':1.0})
		.addClass('show');
	$('div.content').html('<img src="/images/experienceamazing.png"/>' + $currImg.find('img').attr('rel'));
	$('div.content p').hide();
//	$('div.content h3').dropShadow({opacity:0.9, left:2, top:2});
	$('div.content h4').dropShadow({opacity:0.9, left:2, top:-10});
	$('div.transp').css({opacity:0.0});
	$('div#cover').css({opacity:0.0});
	if($currImg.attr('href')!="#") {
		$('div#cover').html('<a href=\"' + $currImg.attr('href') + '\">&nbsp;</a>');
	}
	mouse = 0;
	bindMouse();
	setInterval("slideshow();",11000);
});

function slideshow() {
	$('#cover').unbind(); // Prevent Mouseover action while changing slides
	if (mouse != 1) { // Wait to change slide if the mouse was already over it
		var $currImg = $('#gallery a.show');
		next = getRandom(),
		previous = current, 
		current = next;
		var $nextImg = $('#gallery a').eq(next);

		$nextImg.css({opacity: 0.0})
			.addClass('show')
			.animate({opacity: 1.0}, 2000);
		
		$currImg.animate({opacity: 0.0}, 2000)
			.removeClass('show');
		var caption = '<img src="/images/experienceamazing.png"/>' + $('#gallery a.show').find('img').attr('rel');
		$('div.content').html(caption), $('div.caption p').slideUp(0);
//		$('div.content h3').dropShadow({opacity:0.9, left:2, top:2});
		$('div.content h4').dropShadow({opacity:0.9, left:2, top:-10});

		if($nextImg.attr('href')!="#") {
			$('div#cover').html('<a href=\"' + $nextImg.attr('href') + '\">&nbsp;</a>');
		} else {
			$('div#cover').html('');
		}

	}
	bindMouse(); // Restore Mouseover action
}

function bindMouse() {
	$('#cover').mouseover(function(){
		$('div.content p').slideDown('fast',function(){$('div.transp').css({'height':$('div.content').height()+38}).fadeTo('fast',0.8); mouse = 1;});
	});
	$('#cover').mouseout(function(){
		$('div.content p').slideUp('fast',function(){$('div.transp').fadeTo('fast',0.0); mouse = 0;});
	});
}

function getRandom() {
/*	rndnum = (current + 1) % numphotos;*/
	rndnum = Math.floor(Math.random()*numphotos);
	while (rndnum == current || rndnum == previous) {
		rndnum = Math.floor(Math.random()*numphotos);
	}
	return rndnum;
}
