var loggedErrors = [];
if (typeof(window.console) == "undefined") { window.console = { log: function() {}}};

// Called from onerror event on img elements
function logImageError(image) {
	loggedErrors.include(image.src); // push() but only if not already present
}

function setupProductImages() {
	var removeUs = loggedErrors;
	var mainImages = $$('div.mainImage img');
	var rollImages = $$('div.rollImages img');
	
	if (mainImages.length == 0 || rollImages.length == 0) return;
		
	removeUs.each(function(source) {
		var fileName = source.substr(source.lastIndexOf('/'));
		$$('div.rollImages').getElements('img[src$=' + fileName + ']')[0].dispose();					
		$$('div.mainImage').getElements('img[src$=' + fileName + ']')[0].dispose();					
	});

	switch (removeUs.length) {
		case 4 :	// No productimages
			removeImageElements();
		break;
		case 3 :	// Single productimage only
			removeSlideShowElements();
		default :
			$('wrapContent').setStyle('height', 'auto');
			$$('div.mainImage img.img1')[0].show();
			syncDivHeights();
		break;
	}

	// If more than one image enable the Slideshow
	if (removeUs.length < 3) {
		setupSlideShow();
	}
	
	// If there is at least one image, allow for Slimboxing
	if (removeUs.length < 4) {
		 setupSlimboxing();
	}
}

function removeImageElements() {
	removeSlideShowElements();
	$('prodImg').dispose();
	$$('div.mainImage')[0].dispose();
}

function removeSlideShowElements() {
	['slider', 'count'].each(function(element) {
		$(element).dispose();
	});
	$$('div.rollImages')[0].dispose();
}

function setupSlideShow() {
	new SimpleImageSlideShow($('mainImage'), {
		startIndex: 0,
		slides: $$('div.mainImage img'),
		currentIndexContainer: 'imgCount',
		maxContainer: 'imgMax',
		nextLink: 'next',
		prevLink: 'prev'
	});
}

function setupSlimboxing() {
	// Misc. customization to the Slimbox component
	// $('lbCloseLinkBig').set('text', 'Luk');
	var boxOptions = {
		overlayOpacity: 0.8,			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
		overlayFadeDuration: 200,		// Duration of the overlay fade-in and fade-out animations (in milliseconds)
		resizeDuration: 200,			// Duration of each of the box resize animations (in milliseconds)
		resizeTransition: false,		// false uses the mootools default transition
		initialWidth: 25,			// Initial width of the box (in pixels)
		initialHeight: 25,			// Initial height of the box (in pixels)
		imageFadeDuration: 300,			// Duration of the image fade-in animation (in milliseconds)
		captionAnimationDuration: 200,		// Duration of the caption animation (in milliseconds)
		counterText: "Billede {x} af {y}"	// Translate or change as you wish, or set it to false to disable counter text for image groups
	};
	
	// Clicking the main image launches the Slimbox overlay
	$$('div.productItem').addEvent('click', function(e) {
		var clicked = $(e.target);
		var container = clicked.getParent('div');
		
		if (container.hasClass('mainImage') || container.hasClass('imgFix')) {
			var currentImage = clicked.src;
			if (clicked.src.test(/x\.gif$/)) {
				currentImage = $$('div.mainImage img.img1')[0].src;
			}
			Slimbox.open(currentImage.replace('produktbilleder/produktbilleder/', 'produktbilleder/overlay/'), '', boxOptions);
		}
	});	
}

window.addEvent('load', setupProductImages);		


