// --[ Globals ]----------------------------------------------------------------
var dom=(document.getElementById)   // Modern dom browsers
var iebrowser=document.all
var i;							    // Fade out imageID
var x;							    // Fade in imageID
var y;							    // Image to preload
var degreeIn		= 0;		    // Degree for fade in opacity
var degreeOut		= 100;	        // Degree for fade out opacity
var direction;					    // Global direction (Back/Forth)
var fadeInI;					    // Fade In Interval Object.
var fadeOutI;					    // Fade Out Interval Object.
var fading = false;                 // Are we fading or not.
// -----------------------------------------------------------------------------

function switchImage(dir) {

	if (fading == true) {
		return;
	}

	// Let it start
	fading = true;

	// Assign the start image based on dir.
	if (dir == -1) {
		i = 14;
		x = 14;
		y = 14;
		direction = dir;
	}
	else if (dir == 1) {
		i = 1;
		x = 1;
		y = 1;
		direction = dir;
	}

	// Start fading out, start from i
	fadeOutI = setInterval("fadeOut(" + i + ")", 10);
}

function fadeOut(imgID) {

	// Get reference to the current image.
	var objX = document.getElementById('imgGallery' + imgID);

	// Apply the correct filter for the diffrent browsers
	if (objX.filters) {
		objX.style.filter="alpha(opacity="+ degreeOut +")";
	}
	else if (objX.style.Opacity || objX.style.MozOpacity || objX.style.MozOpacity == "") {
		objX.style.MozOpacity = degreeOut / 101;
	}

	// Decrease the opacity
	degreeOut = degreeOut - 10;

	if (degreeOut < 0) {
		clearInterval(fadeOutI);
		i = i + direction;

		// Preload an image
		if (direction == -1 && i <= 13 && i > 0) {
			var objY = document.getElementById('imgGallery' + y);
			objY.src = newImg[y][0];
			objY.alt = newImg[y][1];
			y--;
		}
		else if (direction == 1 && i >= 3) {
			var objY = document.getElementById('imgGallery' + y);
			objY.src = newImg[y][0];
			objY.alt = newImg[y][1];
			y++;
		}
		else{}

		// Check if we have faded 3 images. If we have, start fading in.
		if ((direction == -1 && i == 11) || (direction == 1 && i == 4)) {
			fadeInI = setInterval("fadeIn(" + x + ")", 10 )
		}
		else {}

		// Now check if we should continue with the next image
		if (i < 15 && i > 0) {
			// Reset the degreeOut and start the timer on the next img.
			degreeOut = 100;
			fadeOutI = setInterval("fadeOut(" + i + ")", 10);
		}
	}
	else {
	   // Just continue the fade out.
		clearInterval(fadeOutI);
		fadeOutI = setInterval("fadeOut(" + imgID + ")", 10);
	}
}

function fadeIn(imgIDb) {

	// Set the last item
	if ( direction == -1 && y == 1 && imgIDb == 2 ) {
		var objY = document.getElementById('imgGallery' + y);
		objY.src = newImg[y][0];
		objY.alt = newImg[y][1];
// 		if (objY.addEventListener) {
// 			objY.addEventListener ('click', function(){openPopUp('');},false);
// 		}
// 		else if (el.attachEvent) {
// 			objY.attachEvent ("onClick",function(){openPopUp('');});
// 		} else {
// 			objY.onClick = function(){openPopUp('');};
// 		}
		y--;
	}
	if ( direction == 1 && y == 14 && imgIDb == 13 ) {
		var objY = document.getElementById('imgGallery' + y);
		objY.src = newImg[y][0];
		objY.alt = newImg[y][1];
		y++;
	}

	// Get reference to the current image.
	var objZ = document.getElementById('imgGallery' + imgIDb);

	// Apply the correct filter for the diffrent browsers
	if (objZ.filters) {
		objZ.style.filter="alpha(opacity="+ degreeIn +")";
	}
	else if (objZ.style.Opacity || objZ.style.MozOpacity || objZ.style.MozOpacity == "") {
		objZ.style.MozOpacity = degreeIn / 101;
	}

	// Decrease the opacity
	degreeIn = degreeIn + 10;

	if (degreeIn > 100) {
		clearInterval(fadeInI);
		x = x + direction;

		// Now check if we should continue with the next image
		if (x <= 14 && x > 0) {
			// Reset the degreeOut and start the timer on the next img.
			degreeIn = 0;
			fadeInI = setInterval("fadeIn(" + x + ")", 10);
		}
		else {
			// DEBUG
			// switchArray();

			// And we're done with the fading.
			fading = false;
		}
	}
	else {
		 // Just continue the fade out.
		clearInterval(fadeInI);
		fadeInI = setInterval("fadeIn(" + imgIDb + ")", 10);
	}
}

function openPopUp(imgObj) {
	//alert(imgObj.alt);
	window.open(
		'zoom.asp?id=' + imgObj.alt,
		'ZOOM',
		'status=0,toolbar=no,scrollbars=no'
	);

}
