//this will tell the code not to do the opacity effect on page load
var notPageLoad;

Event.observe(window, 'load', function()
{
    carousel = new Carousel('small-carousel', {nextElementID:'next-element', prevElementID:'prev-element',size:imageCount,numVisible:3,scrollInc:1});
    initPlayer();
});

function initPlayer()
{
    if(pageload)
    {
        currentImage = 0;
        pageload = false;
    }
    activeImage(currentImage, true);
//     if(!player) player = new PeriodicalExecuter(startPlayer, 10);
    return;
}

function restartTimer()
{
    seconds = 10;
    timer = new PeriodicalExecuter(function(){
        if(seconds > 0) seconds--;
    }, 1);
}

function activeImage(imageID, fromPlayer)
{
    if(!fromPlayer)
    {
        //stopPlayer();
        currentImage = imageID;
    }

    $R(1,imageCount).each(function(n){
         $("image_"+(n-1)).setStyle({
        	 border: '1px solid #3F3F3F'
         });
	});

	$("image_"+imageID).setStyle({
		border: '1px solid #006198'
	});

	if (notPageLoad == 1) {
		$("listingImagesHolder").src = "/Homes/Images/Listings/"+lid+"/"+(imageID+1)+"/Photo.jpg";
		new Effect.Opacity('listingImagesHolder', {from: 0, to: 1, duration: 1} );

		carousel.scrollTo( (parseFloat(imageID)) );
	}
}

function goToBegining()
{
    activeImage(0, false);
}

function goToEnd()
{
    activeImage((imageCount-1), false);
}

function backOne()
{
	notPageLoad = 1;
    if(currentImage <= 0) {
        currentImage = 0;
    } else {
        currentImage--;
    }
    activeImage(currentImage, false);
}

function forwardOne()
{
	notPageLoad = 1;
    if(currentImage >= (imageCount - 1)) {
        currentImage = (imageCount - 1);
    } else {
        currentImage++;
    }
    activeImage(currentImage, false);
}