var carouselImages = new Array();
var carouselImages_current = 0;
var carouselImages_interval = 0;
var carouselImages_current_fullScr = 0;

/* _________________________________________________________________________________________ Start Carousel */
function carousel_start(){
	if(carouselImages.length > 1){
		if(carouselImages_interval > 0){ clearInterval(carouselImages_interval); }
		carouselImages_interval = setInterval(carousel_nextImage, 5000);
	}
}

/* _________________________________________________________________________________________ Stop Carousel */
function carousel_stop(){
	if(carouselImages.length > 1 && carouselImages_interval > 0){
		clearInterval(carouselImages_interval);
	}
}

/* _________________________________________________________________________________________ Next Carousel Image */
function carousel_nextImage(){
	carouselImages_current++;
	if(carouselImages_current == carouselImages.length){ carouselImages_current = 0; }
	
	imageSrc = carouselImages[carouselImages_current];
	bgImg = $('#carouselCurrent').attr('src');
	
	$('#imageFadeBack').css('background-image', 'url(' + bgImg + ')');
	$('#carouselCurrent').hide();
	$('#carouselCurrent').attr('src', imageSrc);
	$('#carouselCurrent').fadeIn('250');
}

/* _________________________________________________________________________________________ Prepare Image Overlay */
function carousel_initOverlayImage(){
	
	$('#carouselCurrent').wrap('<a href="#" id="carouselCurrent_link"></a>');
}

function carousel_fullScr_prev(){
	carouselImages_current_fullScr--;
	if(carouselImages_current_fullScr < 0){ carouselImages_current_fullScr = carouselImages.length - 1; }
	
	carousel_fullScr_setImg();
}

function carousel_fullScr_next(){
	carouselImages_current_fullScr++;
	if(carouselImages_current_fullScr == carouselImages.length){ carouselImages_current_fullScr = 0; }
	
	carousel_fullScr_setImg();
}

function carousel_fullScr_setImg(){
	imageSrc = carouselImages[carouselImages_current_fullScr];
	imageSrc = imageSrc.replace('.jpg', '_PL.jpg');
	$('#imageOverlay_image img:first').attr('src', imageSrc);
}


/* _________________________________________________________________________________________ Full Size Image Overlay */
function carousel_overlayImage(){
	carousel_stop();
	imageUrl = $('#carouselCurrent').attr('src');
	imageUrl = imageUrl.substr(0,imageUrl.length - 4)+'_PL.jpg';
	
	pageWidth = $('body').width();
	pageHeight = $('body').height();
	
	$('body').append('<div id="imageOverlay"></div>');
	$('#imageOverlay').css('filter', 'alpha(opacity=50)');
	$('#imageOverlay').css('opacity', '0.5');
	$('#imageOverlay').css('-moz-opacity', '0.5');
	$('#imageOverlay').css('background-color', '#000000');
	$('#imageOverlay').css('position', 'absolute');
	$('#imageOverlay').css('top', '0px');
	$('#imageOverlay').css('left', '0px');
	$('#imageOverlay').css('width', '100%');
	$('#imageOverlay').css('height', '100%');
	
	$('body').append('<div id="imageOverlay_image"></div>');
	
	$('#imageOverlay_image').append('<a href="#" onClick="carousel_clearOverlayImage();"><img src="'+imageUrl+'" alt="" /></a><br />');
	$('#imageOverlay_image').append('<a href="#" class="carousel_fullScr_control" onClick="carousel_fullScr_prev();"><img src="/images/carousel_prev.gif" alt="Previous" title="Previous" /></a>');
	$('#imageOverlay_image').append('<a href="#" onClick="carousel_clearOverlayImage();">Close</a>');
	$('#imageOverlay_image').append('<a href="#" class="carousel_fullScr_control" onClick="carousel_fullScr_next();"><img src="/images/carousel_next.gif" alt="Next" title="Next" /></a>');
	
	$('#imageOverlay_image').css('position', 'absolute');
	
	$('#imageOverlay_image').css('top', '50px');
	$('#imageOverlay_image').css('left', '0');
	$('#imageOverlay_image').css('width', '100%');
	
	$('#imageOverlay_image').css('text-align', 'center');
	$('#imageOverlay_image a').css('font-size', '14px');
	$('#imageOverlay_image a').css('font-weight', 'bold');
	$('#imageOverlay_image a').css('text-decoration', 'none');
	$('#imageOverlay_image a:not(.carousel_fullScr_control) img').css('border', 'solid 15px #ffffff');
	$('#imageOverlay_image a:not(.carousel_fullScr_control) img').css('margin-bottom', '10px');
}

/* _________________________________________________________________________________________ Remove Image Overlay */
function carousel_clearOverlayImage(){
	$('#imageOverlay_image').remove();
	$('#imageOverlay').remove();
	carousel_start();
}

/* _________________________________________________________________________________________ onLoad */
$(document).ready(function(){
	
	$('img.carouselThumb').click(function(){			  
		thumbSrc = $(this).attr('src');
		thumbSrc = thumbSrc.replace('_S', '');
		bgImg = $('#carouselCurrent').attr('src');
		$('#imageFadeBack').css('background-image', 'url(' + bgImg + ')');
		$('#carouselCurrent').hide();
		$('#carouselCurrent').attr('src', thumbSrc);
		$('#carouselCurrent').fadeIn('250');
		carousel_start();
	});
	
	$('img.carouselThumb').each(function(){
		thumbSrc = $(this).attr('src');
		thumbSrc = thumbSrc.replace('_S', '');
		$("<img>").attr("src", thumbSrc);
		carouselImages[carouselImages.length] = thumbSrc;
	});
	
	carousel_initOverlayImage();
	$('#carouselCurrent_link').click(function(){
		carousel_overlayImage();
	});
	
	carousel_start();
	
});