
var golfGallery = new Class({
	
	initialize: function(container, options) {

		/** Übergebenes Element gibt es nicht, initialisieren abbrechen   */		
		if(!container){
			return false;
		}
		
		/** Options mergen  */
		this.setOptions({ 
			transNav: 0.7,
			transInfo: 0.7,
			debug: false,
		}, options);
		

		/** Container  */
		this.container = container;
		this.containerItems  = container.getElement('.gg_elements'); 
		this.containerInfo   = container.getElement('.gg_info'); 
		this.containerNav    = container.getElement('.gg_nav'); 	

		/** Elemente  */
		this.items     = container.getElements('.gg_elements div.gg_element"');
		this.navItems  = container.getElements('.gg_nav div');
		if(this.items.length > 0){
			this.curElementIndex = 0;
			this.curElement = this.items[this.curElementIndex];
			if(this.items.length != this.navItems.length){
				return false;
			}
			this.initNavigation();
			this.initInfo();
			this.initItems();
			this.changeElement(this.curElementIndex);
			this.startTimer();
		}
	},
	
	initNavigation: function(){

		var gallery    = this;
		var transNav   = this.options.transNav;
		var navItems   = this.navItems;
		var items      = this.items;
		var curElement = this.curElement;
		
		this.navItems.addEvents({
			mouseenter: function(e) {
				if(this != curElement){
					this.morph({opacity: 1});
				}
				gallery.stopTimer();
			},
			mouseleave: function(e) {
				if(this != curElement){
					this.morph({opacity: transNav});					
				}
				gallery.startTimer();
			},
			click: function(e){
				itemIndex = navItems.indexOf(this);
				if(itemIndex !== false){
					gallery.changeElement(itemIndex);
				}
			}
		});
	},
	
	initInfo: function(){
		
		this.containerInfo.setStyle('display', 'block');
		this.containerInfo.morph({opacity: 0});		
		
	},
	
	initItems: function(){
		
		this.items.setStyle('position', 'absolute');	
		this.items.setStyle('top', '0px');
		this.items.setStyle('left', '0px');
		this.items.setStyle('opacity', 0);
		this.items[0].setStyle('opacity', 1);
	},
	
	changeElement: function(itemIndex){

     	if(itemIndex == null){
			itemIndex = this.curElementIndex + 1;
			if(itemIndex >= this.navItems.length){
				itemIndex = 0;
			}
		}
		
		/** Reset Items */
		this.curElement.morph({opacity: 0});
		this.navItems.morph({opacity: this.options.transNav});
		this.containerInfo.morph({opacity: 0});
		
		/** Set Link  */
		this.containerItems.removeEvents();
		this.curElementLink = null;
		var link = this.items[itemIndex].getElement('.gg_link');
		if(link){
			if(link.get('href')){
				this.items[itemIndex].setStyle('cursor', 'pointer');
				this.curElementLink = link.get('href');
				this.containerItems.addEvent('click', function(ev){
					window.location.href = link.get('href');
				});				
			}
		}		
		
		/** Hover Current  */
		this.items[itemIndex].morph({opacity: 1});
		this.navItems[itemIndex].morph({opacity: 1});	
		
		this.curElement = this.items[itemIndex];
		this.curElementIndex = itemIndex;
		
		/** Infobox */
		var infoContent = this.curElement.getElement('.gg_element_info');
		if(infoContent){
			if(infoContent.get('html') != ''){
				this.containerInfo.set('html', infoContent.get('html'));
				this.containerInfo.morph({opacity: this.options.transInfo});				
			}	
		}	

	},
	
	startTimer: function(){
		$clear(this.timer);
		this.timer = this.changeElement.periodical(5 * 1000, this);
	},
	
	stopTimer: function(){
		$clear(this.timer); 
	}
	
});

golfGallery.implement(new Options, new Events);

