function MainGroupSlider(){
	this.prepare = function (){
		var groupCategory = this.getCurrentMainGroupCategory();
		sliderCatalog.addClassName('intransition');
		this.updateArrows();
		this.slideInGroupCategory(groupCategory, true);
		return false;
	};

	this.slideInGroupCategory = function(mainGroupCategory, fromLeft){
		var position, group, delay, effect;
		var groups = mainGroupCategory.childElements();
		
		groups.slice(6, groups.length).invoke('hide');
		groups.slice(0, 5). invoke('show');
		this.setSlideInStartPosition(groups, fromLeft);
		if(!fromLeft){
			groups.reverse();
		}
	
		mainGroupCategory.up().show();
		mainGroupCategory.up().addClassName("currentCategoryMainGroup");
		for(var index = 0; index < groups.length; index++){
			group = groups[index];
			delay = 0.0 + 0.05 * (groups.length - index - 1) + (groups.length - index - 1) * 0.01;
			if(fromLeft){
				position = this.getLeftMostMainGroupPosition() + index * mainGroupWidth;
			}else{
				position = this.getLeftMostMainGroupPosition() + (groups.length - index - 1) * mainGroupWidth;
			}
			
			effect = new Effect.Move(group, {	
				x: position, 
				y: 0, 
				mode: 'absolute',
				duration: 0.5,
				delay: delay,
				queue: { position: 'end', scope: group.id },
				transition: Effect.Transitions.bump
			});
			
			if(index == groups.length - 1){
				effect.options.afterFinish = (function(){
					if(sliderCatalog.hasClassName('intransition')){
						sliderCatalog.removeClassName('intransition');
					}
					return false;
				});
			}
		
		}
		this.updateArrows();
		return false;
	};

 	this.slideOutGroupCategory = function(mainGroupCategory, toLeft){
		var slideOutEndPosition, id;
		var groups = mainGroupCategory.childElements();
		// IE hack
		for(j=0; j < groups.length; j ++){
			if(! groups[j].isLastCategory){			
				Object.extend(groups[j], Element.prototype); 
			}
		}
		if(!toLeft){
			groups.reverse();
		}
	
		for(var index = 0; index < groups.length; index++){
			id = groups[index].id;
			if(toLeft){
				slideOutEndPosition = - (groups.length - index) * mainGroupWidth;
			}else{
				slideOutEndPosition = (groups.length - index) * mainGroupWidth + mainGroupSliderWidth;
			}
		
			new Effect.Move(id, {	
				x: slideOutEndPosition, 
				y: 0, 
				mode: 'absolute',
				duration: 0.5,
				delay: 0.0 + 0.05 * index,
				queue: { position: 'end', scope: id }
			});
		}
 		setTimeout(
			function(){
				mainGroupSlider.getCurrentMainGroupCategory().up().hide(); 
				mainGroupSlider.getCurrentMainGroupCategory().up().removeClassName("currentCategoryMainGroup");}, 
			betweenAnimationsTimeout);
 		return false;
	};

	this.setSlideInStartPosition = function(objects, fromLeft){	
		for(var i = 0; i < objects.length; i++){
			if(fromLeft){
				startPosition = - (objects.length - i) * mainGroupWidth;
			}else{
				startPosition = (i + 1) * mainGroupWidth + mainGroupSliderWidth;
			}
			objects[i].setStyle({
				left: startPosition  + 'px'
			});
		}
		return false;
	};

	this.getLeftMostMainGroupPosition = function (){
		var numberOfMainGroups = this.getCurrentMainGroupCategory().childElements().length;
		var visibleMainGroups = Math.min(numberOfMainGroups, maxMainGroupsVisible);
		var availableSpace = mainGroupSliderWidth - visibleMainGroups * mainGroupWidth;
		return Math.floor(availableSpace / 2);
	};
	
	this.getCurrentMainGroupCategory = function(){
		return mainGroupContainer.down('.currentCategoryMainGroup').down('ul');
	};

	this.showNextMainGroups = function(){
		this.slideMainGroups(this.getCurrentMainGroupCategory(), true);
		return false;
	};
	
	this.showPreviousMainGroups = function(){
		this.slideMainGroups(this.getCurrentMainGroupCategory(), false);
		return false;
	};

	this.slideMainGroups = function(groupCategory, toLeft){
		if(portal.isAnimationScheduled()){
			return false;
		}
		var delay, position, group, groupsToShow, groupsToHide, shift, effect;
		var groups = groupCategory.childElements();
		// IE hack
		for(j=0; j < groups.length; j ++){
			if(! groups[j].isLastCategory){			
				Object.extend(groups[j], Element.prototype); 
			}
		}
		var hiddenGroups = this.getHiddenGroups(groupCategory, !toLeft);
		var visibleGroups = this.getVisibleGroups(groupCategory);
		
		if(toLeft){
			groupsToShow = hiddenGroups.slice(0, Math.min(hiddenGroups.length, 5));
			groupsToHide = visibleGroups.slice(0, groupsToShow.length);
			groups.reverse();
		}else{
			groupsToShow = hiddenGroups.slice(hiddenGroups.length - Math.min(hiddenGroups.length, 5), hiddenGroups.length);
			groupsToHide = visibleGroups.slice(visibleGroups.length - groupsToShow.length, visibleGroups.length)
		}
		
		shift = groupsToShow.length * mainGroupWidth;
		groupsToShow.invoke('show');
		
		
		for(var index = 0; index < groups.length; index++){
			group = groups[index];
			delay = 0 + 0.05 * (groups.length - index - 1) + (groups.length - index - 1) * 0.01;
			if(toLeft){
				position = group.getOffsetLeft() - shift;
			}else{
				position = group.getOffsetLeft() + shift;
			}
			
			effect = new Effect.Move(group, {
				beforeSetup: 
					function(){
						sliderCatalog.addClassName('intransition');
						return false;
					},
				x: position,
				y: 0,
				mode: 'absolute',
				delay: delay,
				queue: {position: 'end', scope: group.id},
				transition: Effect.Transitions.bump
			});
			
			if(index == groups.length - 1){
				effect.options.afterFinish = function(){
					groupsToHide.invoke('hide');
					mainGroupSlider.updateArrows();
					sliderCatalog.removeClassName('intransition');
					return false;
				};
			}
		}
		
		
		return false;
	};
	
	this.updateArrows = function(){
		if(this.getHiddenGroups(this.getCurrentMainGroupCategory(), true).length > 0){
			mainGroupArrowLeft.style.visibility = 'visible';
		}else{
			mainGroupArrowLeft.style.visibility = 'hidden';
		}
		
		if(this.getHiddenGroups(this.getCurrentMainGroupCategory(), false).length > 0){
			mainGroupArrowRight.style.visibility = 'visible';
		}else{
			mainGroupArrowRight.style.visibility = 'hidden';
		}
		
	};
	
	this.getHiddenGroups = function(groupCategory, onLeft){
		var result = new Array();
		var skippedLeft = false;
		groupCategory.childElements().each(function(e){
			if(onLeft){
				if(!e.visible()){
					result.push(e);
				}else{
					throw $break;
				}
			}else{
				if(!e.visible() && skippedLeft){
					result.push(e);
				}else if(e.visible()){
					skippedLeft = true;
				}
			}
		});
		return result;
	};
	
	this.getVisibleGroups = function(groupCategory){
		return groupCategory.childElements().select(function(e){
			return e.visible();
		});
	};
}
