$(document).ready(function() {
	$('#photos-hdr .slide').css({float: 'none', position: 'absolute'}).append('<span class="control play" />');
	$('#photos-hdr .slide').not(':eq(1)').find('.control').hide();
	$('#photos-hdr .slide:eq(1)')
       	.prev().css({marginTop: '219px', borderWidth: '3px', left: 0, top: 0}).end()
		.css({borderWidth: '3px', left: '77px', top: '46px'})
		.nextAll('.slide').each(function(i) {
			$(this).css({top: 0 - 71*i, borderWidth: '3px', left: 270 + 95*i});
		});
	$('#photos-hdr .slide:eq(1)').append('<span class="caption">' + $('#photos-hdr .slide:eq(1)').find('img').attr('alt') + '</span>');	
	$('#photos-hdr .slide:eq(1)').find('img').attr('src', function() { return $('#photos-hdr .slide:eq(1)').find('#view').attr('value');});
	$('#photos-hdr .slide').click(function() {
		if	($(this).prev().css('left') == '77px') {
            $(this).find('img').attr('src', function() { return $(this).parent().find('#view').attr('value');});
			$(this).prev().find('img').attr('src', function() {  return $(this).prev().parent().find('#prev').attr('value'); });
			$(this).prev().animate({marginTop: '173px', borderWidth: '3px', left: '-=77px'}, {duration: 600, easing: 'jswing'}).find('img').animate({width: '89px', height: '76px'}, {duration: 500, easing: 'jswing'});
			$(this).prev().prevAll().animate({left: '-=95px', opacity: '0', marginTop: '+=71px' }, {duration: 600, easing: 'jswing'});
			$(this).animate({top: '46px', borderWidth: '3px', left: '-=193px'}, {duration: 600, easing: 'jswing'}).find('img').animate({width: '210px', height: '180px'}, {duration: 500, easing: 'jswing'});
			$(this).next().animate({left: '-=95px', opacity: '1', top: '0px'}, {duration: 600, easing: 'jswing'}).show();
			$(this).next().nextAll().animate({left: '-=95px', top:'-71px'}, {duration: 600, easing: 'jswing'}).hide();
			$(this).append('<span class="caption">' + $(this).find('img').attr('alt') + '</span>').prev().find('.caption').remove();
			$(this).find('.caption').css({display: 'none'}).show();

		};
		if	($(this).next().css('left') == '77px') {
		    
			$(this).find('img').attr('src', function() {  return $(this).parent().find('#view').attr('value'); });
			$(this).next().find('img').attr('src', function() { return $(this).next().parent().find('#prev').attr('value'); });
			$(this).next().animate({marginTop: '0px', top: '0px', borderWidth: '3px', left: '+=193px'}, {duration: 600, easing: 'jswing'}).find('img').animate({width: '89px', height: '76px'}, {duration: 500, easing: 'jswing'});
			$(this).next().nextAll().animate({left: '+=95px', opacity: '0', top: '-71px' }, {duration: 600, easing: 'jswing'});
			$(this).animate({top: '46px', borderWidth: '3px', left: '+=77px', marginTop: '0px'}, {duration: 600, easing: 'jswing'}).find('img').animate({width: '210px', height: '180px'}, {duration: 500, easing: 'jswing'});
			$(this).prev().animate({left: '+=95px', opacity: '1', marginTop: '-=71px'}, {duration: 600, easing: 'jswing'}).end();
			$(this).prev().prevAll().animate({left: '+=95px', top:'-=71px'}, {duration: 600, easing: 'jswing'});
			$(this).append('<span class="caption">' + $(this).find('img').attr('alt') + '</span>').next().find('.caption').remove();
			$(this).find('.caption').css({display: 'none'}).show();

		};
		return
	});
	$('#photos-hdr .control').click(function() {
		if ($(this).hasClass('play'))	{
			interval = setInterval(rightToLeft, 5000);
			$('#photos-hdr .control').removeClass('play').addClass('pause');
		}
		else if ($(this).hasClass('pause'))	{
			clearInterval(interval);
			$('#photos-hdr .control').removeClass('pause').addClass('play');
		}
	});
	$('#photos-hdr img').not(':eq(1)').each(function(i) {
		$(this).css({width: '89px', height: '65px'});
	});
});

var clickableElement = 2;

function rightToLeft()	{
	if	($('#photos-hdr .slide:last').css('left') == '200px')
		clickableElement = 0;
	else if ($('#photos-hdr .slide:first').css('left') == '0px')
		clickableElement = 2;

	$('#photos-hdr .slide:visible:eq(' + clickableElement + ')').trigger('click');
}
