
preloadNum = 2;
scaleContain = 0;
isNested = 0;

function entixImgLoopInit() {
	// Basic checks
	if (!$(imgLoopParent)) { console.log("RandomImage: '"+imgLoopParent+"' not found in page, aborting."); return; }
	if (!$(imgLoopId)) { console.log("RandomImage: '"+imgLoopId+"' not found in page, aborting."); return; }
	
	if (!imgLoopTitleSuffix) // Not set, remove extension: '.jpg'
		imgLoopTitleSuffix = 4;
	
	// Is id nested in parent?
	oParent = $(imgLoopParent);
	for(var i=0; i<oParent.childNodes.length; i++) {
		if (oParent.childNodes[i].id == id)
			isNested = 1;
	}
	
	scaleContain = imgLoopGenerate && imgLoopContainScale;
	
	// Make sure top img is invisible
	$(imgLoopId).set('opacity', 0);
	// Set current image to background
	setImg($(imgLoopParent), getImgLoopSrc(0));
	randImageScaling($(imgLoopParent));
	updateRandImageTitle(0);
	
	// Preload & start loop if more than 1 image
	if (imgLoopList.length > 1) {
		setImg($(imgLoopId), getImgLoopSrc(1));
		var periodicalID = nextImg.periodical(imgLoopDelay);
	}
}

/**
 * Fades to the next image, run this periodical.
 */
function nextImg() {
	if (preloadNum == imgLoopList.length) // Wrap back to start
		preloadNum = 0;
	var num = (preloadNum == 0 ? imgLoopList.length-1 : preloadNum-1);
	// Fade
	if (num%2)
		fadeToChild($(imgLoopParent), $(imgLoopId), getImgLoopSrc(preloadNum))
	else
		fadeToParent($(imgLoopParent), $(imgLoopId), getImgLoopSrc(preloadNum))
	
	updateRandImageTitle(num);

	preloadNum++;
}

/**
 * Fades out the child (top element) so the parent (bottom element)
 * becomes visible, then preload next image.
 */
function fadeToParent(parent, child, preload) {
	randImageScaling(parent);
	if (!isNested)
		parent.set('opacity', 1);
	new Fx.Tween(child, {duration:1000, onComplete: function() {
		setImg(child, preload);
	}}).start('opacity', 0);
}

/**
 * Fades in the child (top element) so the parent (bottom element)
 * becomes obscured, then preload next image.
 */
function fadeToChild(parent, child, preload) {
	randImageScaling(child);
	new Fx.Tween(child, {onComplete: function() {
		if (!isNested)
			parent.set('opacity', 0);
		setImg(parent, preload);
	}}).start('opacity', 1);
}

/**
 * Handles the scaling of the image of its container
 */
function randImageScaling(img) {
	if (scaleContain) { // scale container to image
		new Fx.Morph(img.getParent()).start({'width':img.getSize().x,'height':img.getSize().y});
	} else if (imgLoopScaleImage) { // scale image to container
		img.set('width', img.getParent().getSize().x);
		img.set('height', img.getParent().getSize().y);
	}
}

/**
 * Updates the title and url (if available) of the image
 */
function updateRandImageTitle(n) {
	var new_url = '';
	if ($(imgLoopId).getParent('a')) { // Image has link?
		var parts = $(imgLoopId).getParent('a').href.split('?s=');
		if (parts.length == 2)
			new_url = parts[0] + '?s=' + imgLoopNums[n];
		$(imgLoopId).getParent('a').href = new_url;
	}
	
	if ($('RandImgTitle')) { // Update the title if it's present
		var cur_title = imgLoopTitles[n];
		if (!cur_title) {
			cur_title = imgLoopList[n];
			cur_title = cur_title.substr(0, cur_title.length - imgLoopTitleSuffix);
			cur_title = decodeURIComponent((cur_title + '').replace(/\+/g, '%20'));
		}
		if ($('RandImgTitle').getChildren('a').length) { // Title has link?
			$('RandImgTitle').getChildren('a')[0].set('text', cur_title);
			if (new_url != '')
				$('RandImgTitle').getChildren('a')[0].href = new_url;
		} else {
			$('RandImgTitle').set('text', cur_title);
		}
	}
}

/**
 * Set image src to element.
 */
function setImg(el, src) {
	if (!el) { return; }
	if (el.tagName.toLowerCase() == 'img')
		el.src = src;
	else
		el.style.backgroundImage = "url('"+src+"')";
}

/**
 * Get src (url) of image by number
 */
function getImgLoopSrc(num) {
	return imgLoopPath+'/'+imgLoopList[num].split("'").join("%27");
}

