if (typeof MINMUSLI == 'undefined') {
	var MINMUSLI = {};
}
window.addEvent('domready', function() {
	
	// function for toggeling of the more info pane
	var toggleInfo = function(e) {
		e.stop();
		var thisId = this.retrieve('id'); // id of the clicked cereal
		var openId = 0;
		for (var key in MINMUSLI.moreInfoSlide) {
			if (MINMUSLI.moreInfoSlide[key].open) {
				openId = key; // id of the open pane
			}
		}
		if (openId > 0 && !MINMUSLI.moreInfoSlide[thisId].open) { 
			/* fade to another info view (box is slid out) */
			var onFadeOutComplete = function() { 
				$('moreInfoLink' + thisId).set('text', MINMUSLI.moreInfoClose);
				$('moreInfoLink' + openId).set('text', MINMUSLI.moreInfo);
				MINMUSLI.moreInfoSlide[openId].hide();  /* hide the previous pane (slide) */ 
				MINMUSLI.moreInfoTween[openId].set('opacity', 0);  /* hide the previous pane (tween) */ 
				MINMUSLI.moreInfoSlide[thisId].show();  /* ... and show the new one (slide) */ 
				MINMUSLI.moreInfoTween[thisId].start('opacity', 0, 1);  /* fade in new info */ 
				MINMUSLI.moreInfoTween[openId].removeEvent('complete', onFadeOutComplete);
			};
			MINMUSLI.moreInfoTween[openId].addEvent('complete', onFadeOutComplete).start('opacity', 1, 0);  /* fade out previous info */ 			
		} else {  /* close or open box */ 
			if (!MINMUSLI.moreInfoSlide[thisId].open) {  /* box is closed, slide in */ 
				MINMUSLI.moreInfoTween[thisId].set('opacity', 1);
				$('selectBaseCerealExtendedContainer').setStyle('margin-bottom', '9px'); // add margin-bottom
				var onSlideInComplete = function() {
					$('moreInfoLink' + thisId).set('text', MINMUSLI.moreInfoClose);
					MINMUSLI.moreInfoSlide[thisId].removeEvent('complete', onSlideInComplete);
				};
				MINMUSLI.moreInfoSlide[thisId].addEvent('complete', onSlideInComplete).slideIn();
			} else { // box is open, slide out
				var onSlideOutComplete = function() {
					//$('selectBaseCerealExtendedRow' + thisId).setStyle('display', 'none');
					MINMUSLI.moreInfoTween[thisId].set('opacity', 0);
					$('moreInfoLink' + thisId).set('text', MINMUSLI.moreInfo);
					MINMUSLI.moreInfoSlide[thisId].removeEvent('complete', onSlideOutComplete);
					$('selectBaseCerealExtendedContainer').setStyle('margin-bottom', 0); // remove margin-bottom
				};
				MINMUSLI.moreInfoSlide[thisId].addEvent('complete', onSlideOutComplete).slideOut();
			}
		}
	};
	
	$$('.moreInfoLink').addEvent('click', toggleInfo);
	
	// close the info pane from the link within the pane
	$$('a.closeMoreInfoLink').addEvent('click', function(e) {
		e.stop();
		var thisId = this.retrieve('id')
		var onSlideOutComplete = function() {
			MINMUSLI.moreInfoTween[thisId].set('opacity', 0);
			$('selectBaseCerealExtendedContainer').setStyle('margin-bottom', 0); // remove margin-bottom
			MINMUSLI.moreInfoSlide[thisId].removeEvent('complete', onSlideOutComplete);
			$('moreInfoLink' + thisId).set('text', MINMUSLI.moreInfo);
		}
		MINMUSLI.moreInfoSlide[thisId].addEvent('complete', onSlideOutComplete).slideOut(); // closes the pane from within itself
	});
	
	$$('.selectBaseCerealRow').addEvent('mouseenter', function() {
		this.addClass('hover'); // rollover class
	});
	
	$$('.selectBaseCerealRow').addEvent('mouseleave', function() {
		this.removeClass('hover'); // remove rollover class
	});
	
	// selects base cereal
	$$('a.selectBaseCerealButton').addEvent('click', function(e) { 
		e.stop();
		
		var articleUid = this.retrieve('articleUid');
		var basketRequest = new Request({ 
			url: 'index.php',  
			onSuccess: function(responseText) { window.location.href = MINMUSLI.nextStepUrl }
		}).get({
			'eID': 'tx_mycereal_basket',
			'action': 'selectBaseCereal',
			'articleUid': articleUid,
			'timestamp': new Date().getTime()
		}); /*.addEvent('success', function() {
			alert('success');
				//;
		}).addEvent('failure', function() {
			alert('failure');
		}).addEvent('request', function() {
			alert('request');
		}).addEvent('complete', function() {
			alert('complete');
		}).addEvent('exception', function() {
			alert('exception');
		});*/
	});
	
	// dismiss entry message
	if ($chk($('selectBaseCerealEntryMessage'))) {
		$('selectBaseCerealEntryMessage').set('slide');
		$$('#selectBaseCerealEntryMessage .button a').addEvent('click', function(e) {
			e.stop();
			$('selectBaseCerealEntryMessage').slide('out');
		});
	}
});
