/**************************************************
* Denken.Com.Au - JavaScript - Showcase Rotation
* Copyright (c) 2010
* Last update: 2010/01/29 11:42:00
**************************************************/
// Dynamic case-study array
var casestudies = [
	{id:1, ext:"jpg", scase:'consumer'},
	{id:2, ext:"jpg", scase:'aerospace'},
	{id:3, ext:"jpg", scase:'agri-products'}
];

// Rotation update code
var _rotate_timer = 15; // Delay (seconds) between automated rotation
var _rotate_delay = 15; // Delay (seconds) after an image is force-selected, before next rotation
var images = new Array(), _timer_rotation;
function showcase_init() {
	// Buffer all the Showcase images prior to rotation
	var im, n, cur = null, path = dge("iShowcase").src;
	for(n=0; n<casestudies.length; n++) {
		im = new Image();
		im.src = gPaths.showcase + casestudies[n].id.toString() + "." + casestudies[n].ext;
		im.casestudy = {id:casestudies[n].id, scase:casestudies[n].scase};
		if (im.src == path) cur = n;
		images.push(im);
	}
	// Update the MAP links to javascript
	for(n=1; n<=images.length; n++) {
		with(dge("iSCarea"+n.toString())) {
			onclick = new Function("ev", "return rotate("+n.toString()+");");
		}
	}
	// Update the general image area link
	if (cur != null)
		dge("iSCareaM").href = gPaths.casestudy + images[cur].casestudy.scase + '/';

	// Repeating timer
	_timer_rotation = window.setTimeout("rotate()", _rotate_timer*1000);
}
function rotate(idx) {
	if (idx != null) window.clearTimeout(_timer_rotation); // Cancel timer if necessary
	var img = dge("iShowcase"), cur, paws = _rotate_timer;
	if (idx == null) { // auto-rotate
		// Find index of current image
		for(im in images) { if(images[im].src == img.src) { cur = im+1; break; } }
		if (cur == null) return false; // Not found. O.o

		//var cur = 0 + img.src.match(/\/(\d+)\..*?$/i)[1]; // Get current image#
		idx = cur % images.length + 1; // Rotate by 1, cycling to start if necessary
	} else {
		idx = Math.min(Math.max(1,idx),images.length);
		paws = _rotate_delay; // delay caused by user-flip
	}
	idx--; // 0-index array

	// Update image# of showcase image and update general area link.
	var lnk = dge("iSCareaM");
	img.src = images[idx].src;
	lnk.href = gPaths.casestudy + images[idx].casestudy.scase + '/';

	// Set up new rotation timer
	_timer_rotation = window.setTimeout("rotate()", paws*1000);
	return false; // for the benefit of the map/area links (ie. don't follow href link)
}

