//////////////////////////////////////////////////////////
//	http://www.naj-nevesta.si							//
//	Poročna Galerija									//
//	(c)2007-current kefo@email.si - ALL RIGHTS RESERVED	//
//														//
// Code below may not be copied, modified, distributed,	//
// republicated or used in any other way without the 	//
// prior written permission from its author.			//
// Using this code to build another website is strictly //
// prohibited. If you need a similar gallery, please 	//
// contact us.											//
// We do occasionally search the web for illegal copies //
// of our work!											//
//////////////////////////////////////////////////////////

var photoGallery_BigImagesFolder = '/big_photo/';

//////////////////////////////////////////////////////////
//	ActiveImages (PreloadImages & Functions)			//
//////////////////////////////////////////////////////////

//	PreloadImages: Gallery - Loading Image Icon (Animation)
//////////////////////////////////////////////////////////////
var fgLoadingIcon = new Image(171, 74);
fgLoadingIcon.src = '/img/fotogalerija/porocna-slika-loading.gif';

//	PreloadImages: Vertical Gallery - Scroll Up & Down Buttons
//////////////////////////////////////////////////////////////
var fgButScrollUpNormal = new Image(184, 11);
fgButScrollUpNormal.src = '/img/fotogalerija/scroll_up.gif';
var fgButScrollDownNormal = new Image(184, 11);
fgButScrollDownNormal.src = '/img/fotogalerija/scroll_down.gif';
var fgButScrollUpActive = new Image(184, 11);
fgButScrollUpActive.src = '/img/fotogalerija/scroll_up-active.gif';
var fgButScrollDownActive = new Image(184, 11);
fgButScrollDownActive.src = '/img/fotogalerija/scroll_down-active.gif';
/* Tega ne uporabljamo nikjer:
var fgButScrollUpDisabled = new Image(184, 11);
fgButScrollUpDisabled.src = '/img/fotogalerija/scroll_up-disabled.gif';
var fgButScrollDownDisabled = new Image(184, 11);
fgButScrollDownDisabled.src = '/img/fotogalerija/scroll_down-disabled.gif';
*/

//	PreloadImages: Gallery Window  - Previous & Next Buttons
////////////////////////////////////////////////////////////
var fgButNextNormal = new Image(67, 17);
fgButNextNormal.src = '/img/fotogalerija/but-next.png';
var fgButNextActive = new Image(67, 17);
fgButNextActive.src = '/img/fotogalerija/but-next-active.png';
var fgButPrevNormal = new Image(77, 17);
fgButPrevNormal.src = '/img/fotogalerija/but-prev.png';
var fgButPrevActive = new Image(77, 17);
fgButPrevActive.src = '/img/fotogalerija/but-prev-active.png';
var fgButCloseNormal = new Image(75, 35);
fgButCloseNormal.src = '/img/fotogalerija/but-zapri.png';
var fgButCloseActive = new Image(75, 35);
fgButCloseActive.src = '/img/fotogalerija/but-zapri-active.png';

//	PreloadImages: DetailMarker (in page text)
////////////////////////////////////////////////////////////
var fgButDetailDarkNormal = new Image(12, 9);
fgButDetailDarkNormal.src = '/img/detajli-slika-dark.gif';
var fgButDetailDarkActive = new Image(12, 9);
fgButDetailDarkActive.src = '/img/detajli-slika-dark-a.gif';


//	Functions
//////////////////////////////////////////////////////////
function fgHltBtn(buttonType, buttonState) {
	var objButton = document.getElementById('fgBut'+buttonType);
	if (objButton != null) {
		if (buttonType.substring(0, 7) == 'DetailD') buttonType = 'DetailDark';
		if (buttonState == 0) objButton.src = eval('fgBut'+buttonType+'Normal.src');
		 else if (buttonState == 1) objButton.src = eval('fgBut'+buttonType+'Active.src');
		  else if (buttonState == -1) objButton.src = eval('fgBut'+buttonType+'Disabled.src');
	}
}



//////////////////////////////////////////////////////////
//	Thumbnails											//
//////////////////////////////////////////////////////////

var activeGalleryThumbnail = null;

function drawThumbnailFrame(aThumbnail, aFrameColor) {
	aThumbnail.style.border = '1px solid '+aFrameColor;
}

function activateThumbnail(picId) {
	activeGalleryThumbnail = document.getElementById('imgFg'+picId);
	if (activeGalleryThumbnail != null) {
		galleryImageLoadingStart();
		objBigImage.src = activeGalleryThumbnail.src.replace(/\/slike\//, photoGallery_BigImagesFolder);
		drawThumbnailFrame(activeGalleryThumbnail, '#F89430');
	}
}

function deactivateThumbnail() {
	drawThumbnailFrame(activeGalleryThumbnail, '#000000');
	activeGalleryThumbnail = null;
}

function getThumbAbsoluteLeft(objThumbnail) {
	var aLeft = 0;
	if (objThumbnail != null) {
//		aLeft += objThumbnail.offsetLeft;		// ker je thumbnail itak zapakiran v <A> tag
		var objParent = objThumbnail.parentNode;
		while (objParent != null && objParent.tagName != 'BODY') {
			aLeft += parseInt(objParent.offsetLeft) || 0;
			objParent = objParent.parentNode;
		}
	}
	return aLeft;
}

function getThumbAbsoluteTop(objThumbnail) {
	var aTop = 0;
	if (objThumbnail != null) {
//		aTop += objThumbnail.offsetTop;		// ker je thumbnail itak zapakiran v <A> tag
		var objParent = objThumbnail.parentNode;
		while (objParent != null && objParent.tagName != 'BODY') {
			aTop += (parseInt(objParent.offsetTop) || 0) - (parseInt(objParent.scrollTop) || 0);
			objParent = objParent.parentNode;
		}
	}
	return aTop;
}



//////////////////////////////////////////////////////////
//	GalleryWindow Functions								//
//////////////////////////////////////////////////////////

var galleryAnimationSteps = 10;
var galleryWindowMargin = 40;

function hideImageGalleryWindow() {
	if (objImageGalleryWindow != null) {
		objImageGalleryWindow.style.visibility = 'hidden';
		objImageGalleryWindow.style.top = '0px';
		objImageGalleryWindow.style.left = '0px';
		objImageGalleryWindow.style.width = '200px';
		objImageGalleryWindow.style.height = '200px';
	}
}

function galleryImageLoadingStart() {
	if (galFrmLoadingIcon != null) galFrmLoadingIcon.style.visibility = 'visible';
	if (objBigImageContainer != null) objBigImageContainer.style.visibility = 'hidden';
}

function galleryImageLoadingComplete() {
	if (objBigImageContainer != null) objBigImageContainer.style.visibility = 'visible';
	if (galFrmLoadingIcon != null) galFrmLoadingIcon.style.visibility = 'hidden';
}

//	Realign-s
//////////////////////////////////////////////////////////

function adjustImageGalleryWindow(aWidth, aHeight) {
	if (objImageGalleryWindow != null) {
		var currentWidth = parseInt(objImageGalleryWindow.style.width) || 0;
		var currentHeight = parseInt(objImageGalleryWindow.style.height) || 0;
		var requiredWidth = aWidth + (galleryWindowMargin*2);
		var requiredHeight = aHeight + (galleryWindowMargin*2);
		if (requiredWidth <= currentWidth) requiredWidth = currentWidth;
		aWidth = requiredWidth - 149;
		if (requiredWidth > currentWidth) {
			objImageGalleryWindow.style.width = requiredWidth + 'px';
			if (galFrmTopBorder != null) galFrmTopBorder.width = aWidth;
			if (galFrmBottomBorder != null) galFrmBottomBorder.width = aWidth;
		}
		if (requiredHeight <= currentHeight) requiredHeight = currentHeight;
		aHeight = requiredHeight - 49;
		if (requiredHeight > currentHeight) {
			objImageGalleryWindow.style.height = requiredHeight + 'px';
			if (galFrmLeftBorder != null) galFrmLeftBorder.height = aHeight;
			if (galFrmRightBorder != null) galFrmRightBorder.height = aHeight;
		}
		var newLeft = Math.round((browserWidth - requiredWidth)/2)-galleryWindowMargin;
		var newTop = Math.round((browserHeight - requiredHeight)/2)-galleryWindowMargin;
		if (newTop < 10) newTop = 10;
		if (requiredWidth > currentWidth || requiredHeight > currentHeight) {
			objImageGalleryWindow.style.left = newLeft + 'px';
			objImageGalleryWindow.style.top = newTop + 'px';
			// realign ImageLoading Icon:
			if (galFrmLoadingIcon != null) {
				galFrmLoadingIcon.style.left = parseInt(newLeft + Math.round((requiredWidth-171)/2))+'px';
				galFrmLoadingIcon.style.top = parseInt(newTop + Math.round((requiredHeight-74)/2))+'px';
			}
			realignPrevNextButtons(requiredWidth, requiredHeight);
			if (galFrmMiddle != null) {
				galFrmMiddle.width = aWidth;
				galFrmMiddle.height = aHeight;
			}
		}
		// realignBigImage:
		if (objBigImageContainer != null && objBigImage != null) {
			objBigImageContainer.style.left = parseInt(newLeft + Math.round((requiredWidth-objBigImage.width)/2)) + 'px';
			objBigImageContainer.style.top = parseInt(newTop + Math.round((requiredHeight-objBigImage.height)/2)) + 'px';
		}
	}
}

function realignPrevNextButtons(newWidth, newHeight) {
	if (galFrmPrevNextBlock != null) {
		galFrmPrevNextBlock.style.left = parseInt(newWidth - 200) + 'px';
		galFrmPrevNextBlock.style.top = parseInt(newHeight - 34) + 'px';
	}
}

//	Show Previous and Next Image						
//////////////////////////////////////////////////////////

function showPrevNextImage(direction) {
	var imageIndex = 0;
	if (activeGalleryThumbnail != null) {
		var imageIndex = parseInt(activeGalleryThumbnail.id.substr(5, 3)) || 0;
		deactivateThumbnail();
	}
	imageIndex += direction;
	if (imageIndex >= imageGalleryCount) imageIndex = 0;
	 else if (imageIndex < 0) imageIndex = imageGalleryCount-1;
	activateThumbnail(imageIndex);
	if (activeGalleryThumbnail != null) {
		var onClickCommand = String(activeGalleryThumbnail.parentNode.onclick);
		var idx = onClickCommand.indexOf('view(');
		if (idx > 0) {
			onClickCommand = onClickCommand.substr(idx+5, 255);
			idx = onClickCommand.indexOf(');');
			if (idx > 0) {
				onClickCommand = onClickCommand.substring(0, idx);
				var imgDimensions = onClickCommand.split(',');
				var imgWidth = parseInt(imgDimensions[1]) || 0;
				if (imgWidth > 0) objBigImage.width = imgWidth;
				var imgHeight = parseInt(imgDimensions[2]) || 0;
				if (imgHeight > 0) objBigImage.height = imgHeight;
				adjustImageGalleryWindow(imgWidth, imgHeight);
			}
		}
	}
}

function showNextImage() {
	showPrevNextImage(+1);
}

function showPrevImage() {
	showPrevNextImage(-1);
}





//////////////////////////////////////////////////////////
//	Animation											//
//////////////////////////////////////////////////////////

function TDirectionalAnim() {
	this.target = 0;
	this.original = 0;
	this.step = 0;
	this.current = 0;
	this.finished = true;
	this.setAnimation = function(aCurrent, aTarget) {
		this.target = aTarget;
		this.original = aCurrent;
		this.current = aCurrent;
		this.step = (aTarget - aCurrent)/galleryAnimationSteps;
		this.finished = (aCurrent == aTarget);
	}
	this.animate = function() {
		var delta = this.target-this.current;
		var absDelta = Math.abs(delta);
		if (absDelta > Math.abs(this.step)) delta = this.step;
		this.current += delta;
		this.finished = (delta == 0);
		return Math.round(this.current);
	}
}
var continiousLoopProtector = 0;

function TPhotoAnimation() {
	this.moveX = new TDirectionalAnim();
	this.moveY = new TDirectionalAnim();
	this.sizeX = new TDirectionalAnim();
	this.sizeY = new TDirectionalAnim();
	this.startAnimation = function(aCurrentLeft, aCurrentTop, aCurrentWidth, aCurrentHeight, aTargetLeft, aTargetTop, aFinalWidth, aFinalHeight) {
		if (aTargetLeft == 'SCR_CENTER') aTargetLeft = Math.round((browserWidth-aFinalWidth-(galleryWindowMargin*2))/2);
		this.moveX.setAnimation(aCurrentLeft, aTargetLeft);
		if (aTargetTop == 'SCR_CENTER') aTargetTop = Math.round((browserHeight-aFinalHeight-(galleryWindowMargin*2))/2);
		this.moveY.setAnimation(aCurrentTop, aTargetTop);

		this.sizeX.setAnimation(aCurrentWidth, aFinalWidth);
		this.sizeY.setAnimation(aCurrentHeight, aFinalHeight);

		continiousLoopProtector = 0;
		timGalleryAnim = setInterval('galleryAnim.animate();', 25);
		if (this.sizeX.step > 0) objBigImageContainer.style.visibility = 'visible';
	}
	this.animate = function() {
		if (!this.moveX.finished) objBigImageContainer.style.left = this.moveX.animate()+'px';
		if (!this.moveY.finished) objBigImageContainer.style.top = this.moveY.animate()+'px';
		if (!this.sizeX.finished) {
			var newWidth = this.sizeX.animate();
			objBigImage.width = newWidth;
			objBigImageContainer.width = newWidth;
		}
		if (!this.sizeY.finished) {
			var newHeight = this.sizeY.animate();
			objBigImage.height = newHeight;
			objBigImageContainer.height = newHeight;
		}
		continiousLoopProtector += 1;
		if (continiousLoopProtector > 15) clearInterval(timGalleryAnim);

		if (this.moveX.finished && this.moveY.finished && this.sizeX.finished && this.sizeY.finished) {
			clearInterval(timGalleryAnim);
			if (this.sizeX.step < 0) objBigImageContainer.style.visibility = 'hidden';
			 else {
			 	adjustImageGalleryWindow(this.sizeX.target, this.sizeY.target);
			 	objImageGalleryWindow.style.visibility = 'visible';
			 }
		}
	}
}

var galleryAnim = new TPhotoAnimation();
var timGalleryAnim;



//////////////////////////////////////////////////////////
//		Show/Hide BigPic								//
//////////////////////////////////////////////////////////

function view(picId, fullSizeX, fullSizeY) {
	var objThumbnail = document.getElementById('imgFg'+picId);
	if (objThumbnail != null && objImageGalleryWindow != null && objBigImage != null) {
		hideImageGalleryWindow();
		objBigImage.src = objThumbnail.src;
		galleryAnim.startAnimation(getThumbAbsoluteLeft(objThumbnail), getThumbAbsoluteTop(objThumbnail), parseInt(objThumbnail.offsetWidth), parseInt(objThumbnail.offsetHeight), 'SCR_CENTER', 'SCR_CENTER', fullSizeX, fullSizeY);
		activateThumbnail(picId);
	} 
}

function flyAway() {
	hideImageGalleryWindow();
	galleryAnim.startAnimation(parseInt(objBigImageContainer.style.left), parseInt(objBigImageContainer.style.top), parseInt(objBigImage.width), parseInt(objBigImage.height),  getThumbAbsoluteLeft(activeGalleryThumbnail), getThumbAbsoluteTop(activeGalleryThumbnail), parseInt(activeGalleryThumbnail.offsetWidth), parseInt(activeGalleryThumbnail.offsetHeight));
	deactivateThumbnail();
}





////////// NOVO, UREDI... ////////////


// VerticalGallery vars  (predefiniramo jih kasneje v porocna_galerija-1.js)
//////////////////////////////////////////////////////////////////////////////////////
var objVerticalGalleryThumbs = null;
var verticalGalleryHeight = -1;
var verticalGalleryThumbsHeight = -1;

function moveThumbsLayer(aStep) {
	if (objVerticalGalleryThumbs != null && verticalGalleryHeight != -1) {
		var newTop = (parseInt(objVerticalGalleryThumbs.scrollTop) || 0) + aStep;
		if (newTop + verticalGalleryHeight > verticalGalleryThumbsHeight) newTop = newTop - verticalGalleryScrollOverflowStart + 2;
		if (newTop < 0) newTop = verticalGalleryScrollOverflowStart + newTop + 2;
		objVerticalGalleryThumbs.scrollTop = newTop;
//		var objInfoLayer = document.getElementById('infoTag');
//		if (objInfoLayer != null) objInfoLayer.innerHTML = 'scrollTop: '+objVerticalGalleryThumbs.scrollTop +'px<BR>clientHeight: '+objVerticalGalleryThumbs.clientHeight +'px<BR>scrollHeight: '+parseInt(objVerticalGalleryThumbs.scrollHeight)+'px<BR>Overflow: '+parseInt(verticalGalleryScrollOverflowStart)+'px';
	}
}

var timVerticalGalleryThumbs;

function startVertGalleryScroll(aStep) {
	timVerticalGalleryThumbs = setInterval('moveThumbsLayer('+aStep+')', 100);
}

function stopVertGalleryScroll() {
	clearInterval(timVerticalGalleryThumbs);
}

