var loading = false;
var current_product = "";
/**
* List of Etiketten, where is no active state defined
*/
var no_active_product = [
	'powerade_sw'
]

/**
* Document ready function from jQuery.
*/
$(document).ready(function() {
	$('#etikett_outer').css('height', '260px');
	
	//default etikett
	changeEtikettByProduct('cc');
	
	//add hover and click to the product links
	$('table#markennavi a').each(function(e) {
		var id = $(this).children('img').attr('id');
		$(this).click(function() { changeEtikettByProduct(id); return false; });
		$(this).mouseover(function() {
			if(current_product != id) {
				highlight_marke(id);
			}
		});
		$(this).mouseout(function() {
			if(current_product != id) {
				normalise_marke(id);
			}
		});
	});
});


/**
* jQuery function for preloading images.
* @param _images An array with path to images
* @param _callback (optional) function that is calling, when all images loaded
*/
$.preloadImages = function(_images, _callback) {
	if(_callback == undefined) _callback = function(){};
	var counter = _images.length;
	
	$.each(_images,function(e) {
		$(new Image()).load(function() {
			if (--counter < 1) _callback(_images);
		}).attr('src',this);
	});
}

/**
* FadeOut the old etikett, shows loading state and initialize preloading of the new images
*/
function changeEtikettByProduct(product) {
	if(loading == false && current_product != product) {
		loading = true;
		
		if(current_product != "") {
			normalise_marke(current_product);
		}
		current_product = product;
		highlight_marke(product);
		
		//$('#etikett_holder').fadeOut('normal', function() {
		$('#etikett_holder').hide();
		$('#etikett_holder').html('');
		$('#etikett_outer').addClass('loading_etikett');
		$.preloadImages(getImagesByProduct(product), onPreloadingComplete);
		//}
	}
}

/**
* Returns a list with images and the correct path for the product
*/
function getImagesByProduct(product) {
	var basepath = "./img/etikett_"+product+"/";
	//first normal, than active
	return [
		basepath+"etikett_01.gif",
		basepath+"etikett_02.gif",
		basepath+"etikett_03.gif",
		basepath+"etikett_04.gif",
		basepath+"etikett_05.gif",
		basepath+"etikett_06.gif",
		basepath+"etikett_07.gif",
		basepath+"etikett_02_active.gif",
		basepath+"etikett_04_active.gif",
		basepath+"etikett_05_active.gif",
		basepath+"etikett_06_active.gif"
	]
}

/**
* Set the Tooltips for every normal image
*/
function getTooltips() {
	return [
		{label:"", width:"0"},
		{label:"Portionsgr&ouml;&szlig;e", width:"0"},
		{label:"", width:"0"},
		{label:"N&auml;hrwertbezeichnung", width:"0"},
		{label:"Absolute Menge pro Glas", width:"142px"},
		{label:"Prozentualer Anteil am Richtwert f&uuml;r die Tageszufuhr pro Glas", width:"130px"},
		{label:"", width:"0"}
	]
}

/**
* After preloading complete, add the normal images to the holder div. Add a tooltip, if nessecary. After all fade in the holder div.
*/
function onPreloadingComplete(images) {
	var tooltips = getTooltips();
	$.each(images, function(e) {
		var src = images[e];
		if(src.search(/_active/g) == -1) {
			if(tooltips[e].label == "" || $.inArray(current_product, no_active_product) > -1) {
				$('#etikett_holder').append('<img src="'+src+'" alt="" /><br />');
			} else {
				$('#etikett_holder').append('<a href="#" class="tooltiplink link_'+e+'"><img src="'+src+'" alt="" class="imgover" /><span>'+tooltips[e].label+'</span></a><br />');
				
				var src = $('a.link_'+e+' img').attr('src');
				var extension = src.substring(src.lastIndexOf('.'),src.length);
				
				$('a.link_'+e).click(function() { return false; });
				$('a.link_'+e).mouseover(function(e) {
					$(this).children('img').attr('src', src.replace(extension,'_active' + extension));
				});
				$('a.link_'+e).mouseout(function(e) {
					$(this).children('img').attr('src', src);
				});
				
				if(parseInt(tooltips[e].width) > 0) {
					$('a.link_'+e+' span').css('width', tooltips[e].width);
				}
			}
		}
	});
	
	$('#etikett_outer').removeClass('loading_etikett');
	//$('#etikett_holder').fadeIn();
	$('#etikett_holder').show();
	
	loading = false;
}