//Lightbox replacement
Event.observe(document, 'dom:loaded', function() {
	LightBoxExtend.init('div.LightboxGallery');
});

var LightBoxExtend = {
	mstrControlName: null,
	mintCurrent: 1,
	mintTotal: 0,
	mobjRec: null,
	moImg3: null,
	moImg2: null,
	moImg1: null,
	init: function(strControlName)
	{
		LightBoxExtend.mstrControlName = strControlName;
		LightBoxExtend.setupPaging();
		LightBoxExtend.initImage();
		LightBoxExtend.setActiveImage(true, true);
	},
	setupPaging: function()
	{
		var objItems = $$(LightBoxExtend.mstrControlName + ' ul.items li');
		mintTotal = objItems.length
		if (mintTotal > 3)
		{
			Event.observe($('imgNext'), 'click', LightBoxExtend.NextImage);
			Event.observe($('imgPrev'), 'click', LightBoxExtend.PrevImage);
		}			
	},
	initImage: function(){
		if(mintTotal > 2)
		{
			moImg3 = new Image();
			$('imgPreview03').appendChild(moImg3);	
			moImg2 = new Image();
			$('imgPreview02').appendChild(moImg2);
			moImg1 = new Image();
			$('imgPreview').appendChild(moImg1);		
		}
	},
	setActiveImage: function(blnForward, blnIsInit)
	{			
		if(mintTotal>2)
		{
			if (LightBoxExtend.mobjRec == null)
			{
				LightBoxExtend.mobjRec = $$(LightBoxExtend.mstrControlName + ' ul.items li img');
			}			
			LightBoxExtend.mintCurrent = LightBoxExtend.getImageIndex(LightBoxExtend.mintCurrent, LightBoxExtend.mobjRec.length);		
		
			moImg1.src = LightBoxExtend.mobjRec[LightBoxExtend.getImageIndex(LightBoxExtend.mintCurrent - 1, LightBoxExtend.mobjRec.length)].up().name;
			moImg2.src = LightBoxExtend.mobjRec[LightBoxExtend.mintCurrent].up().name;	
			moImg3.src = LightBoxExtend.mobjRec[LightBoxExtend.getImageIndex(LightBoxExtend.mintCurrent + 1, LightBoxExtend.mobjRec.length)].up().name;			
		}
		return false;
	},
	NextImage: function()
	{
		LightBoxExtend.mintCurrent++;
		LightBoxExtend.setActiveImage(true);		
	},
	PrevImage: function()
	{
		LightBoxExtend.mintCurrent--;
		LightBoxExtend.setActiveImage(false);		
	},
	getImageIndex: function(intCurrent, intLength) {
		if (intCurrent < 0)
			return intLength-1;
		if (intCurrent >= intLength)
			return 0;
		return intCurrent;
	}
};