var MyCatchSlideshow = new Class({
	// home page slideshow
	Implements: [Options,Events, Chain], 						  
	
	 // menu options. don't put a comma after the last option!
	options: {
	 	// whether to enable the first item. This is generally only true for the home page.
			
			// which viewport to use
			viewPort:'',
			// delay in ms
			slideDelay: 3000
			// whether to show next and prev buttons
			//useNavButtons:true
		}, 
	
	log: function(){
		console.log.apply(null, arguments);
	},
	// this function runs whenever a new instance of this class is made.
	initialize: function(options){ 
		// use the options specified
		//this.log($("hero-image-viewport"));
		this.setOptions(options);
		//this.log(this.options.viewPort); 
		// Dynamically generate navigation links from promo links
		
		//get all img
		this.slides = this.options.viewPort.getElements('div').reverse();
		
		//get all thumbs
		this.thumbs = $('home-mycatch-thumbs').getElements('div.wrap');
		
		
		
		
		this.delayms = this.options.slideDelay;
		this.currentImg = 0;
		
		this.totalImg =  this.slides.length;
		
		// thumbs navgiation control
		this.thumbs.each(function(item, index, array){
			var thumb = item.getElement('div.thumb');
			
			thumb.addEvent('click', this.NextSlide.bindWithEvent(this, {
				'isNext': true,
				'clickIsFromUser': true,
				'slideIndex':index
			}));

		}, this);
		
		
		//if (this.options.useNavButtons) {
			
			//this.nextButton = this.options.viewPort.getElement('span.next');
			//this.prevButton = this.options.viewPort.getElement('span.prev');
			
			
			
			//add clicky event. http://mootools.net/docs/core/Native/Function/#Function:bindWithEvent
			//this.nextButton.addEvent('click', this.NextSlide.bindWithEvent(this, {
			//	'isNext': true,
			//	'clickIsFromUser': true
			//}));
			//var params = new Array(false, true);
			//this.prevButton.addEvent('click', this.NextSlide.bindWithEvent(this, {
			//	'isNext': false,
			//	'clickIsFromUser': true
			//}));
		//}
		
		if(this.totalImg>0){
			for(var i=0;i<this.totalImg;i++){
				
				var img = this.slides[i];
				if(i>0)
					img.setStyle('opacity',0);//all except first img transparent
				
			}
			
			this.StartPeriodical(this.delayms);
		}
		
		
	},
	
	NextSlide:function(event,paramArray){
		// 'this' == ClassPromo
		// boolean isNext determines if moving forwards or back
		
		//this.log(paramArray['slideIndex']);
		
		if(event) event.stop();
		//console.log($type(paramArray));
		var isNext = paramArray['isNext'];
		var clickIsFromUser = paramArray['clickIsFromUser'];
		var slideIndex =   paramArray['slideIndex'];
		var nextImg;
		
		//clear timer
		if(clickIsFromUser) {
			//console.log(this.periodID);
			$clear(this.periodID);
			//console.log(this.periodID);
			//console.log('click');
			
			if(slideIndex >= 0){
				nextImg = slideIndex;
				
			}
			
		}else{
			//only used by the periodical
			if(isNext){
				//forward
				if(this.currentImg == this.totalImg-1){
				
					//reset to 0
					nextImg = 0;
				}else{
					nextImg = this.currentImg+1;
					
				}
			}else{
				//backwards
				if(this.currentImg == 0){
					//reset to 0
					nextImg = this.totalImg-1;
				}else{
					nextImg = this.currentImg-1;
					
				}
			}
		
		}
		//start timer
		
		//fadeout
		this.slides[this.currentImg].fade('out');
		// deactivate thumb
		this.thumbs[this.currentImg].removeClass('active');
		//this.slides[this.currentImg].tween('width',300);
		//fadein
		this.slides[nextImg].fade('in');
		// activate thumb
		this.thumbs[nextImg].addClass('active');
		
		this.currentImg = nextImg;

		if(clickIsFromUser) {
			this.periodID = this.StartPeriodical.delay(this.delayms*2, this, this.delayms);
			
		}
		

		//return false;
	},
	
	StartPeriodical:function(delayms){
		//console.log(delayms);
		this.periodID = this.NextSlide.bindWithEvent(this,{'isNext':true,'clickIsFromUser':false,'slideIndex':-1}).periodical(delayms);
		
	}
	
	
});

function logoToolTip(){
	// Hover effect for Logo link
	var logoLink = $('atm-link');
	
	//create the tooltip DIV
	var toolTip = $('atm-info');
	toolTip.setStyles({'opacity':0,'right':'30px'});
	//alert(Browser.Engine.name);
	
	if(Browser.Engine.name == 'webkit'){
		// safari, chrome
		//toolTip.setStyles({'top':'-95%'});
	}
	
	//create morph obj
	tooltipMorph = new Fx.Morph(toolTip,{'link':'cancel','duration': 'normal', 'transition': Fx.Transitions.Sine.easeInOut});
	
	var showTooltip = function(){
		var dest = 127;
		if(Browser.Platform.win && Browser.Engine.name == 'trident' && Browser.Engine.version<='5'){
			var dest = 120;
		}
		
		//clear timer
		if(this.timeoutID!==undefined) $clear(this.timeoutID);
		
		this.start({
			'opacity': 1   
		});
		
	}
	var hideTooltip = function(){
		// fade out
		this.start({
				'opacity': 0   
		});
	}
	
	var startHideTooltipTimer = function(){
		//begin timer to fade out
		this.timeoutID = hideTooltip.delay(1000,this);
	}
	
	var clearTimer = function(){
		//clear timer
		if(this.timeoutID!==undefined) $clear(this.timeoutID);
	}
	
	//console.log(hideTooltip);
	
	logoLink.addEvent('mouseenter', showTooltip.bind(tooltipMorph));
	logoLink.addEvent('mouseleave', startHideTooltipTimer.bind(tooltipMorph));
	
	toolTip.addEvent('mouseenter', clearTimer.bind(tooltipMorph));
	toolTip.addEvent('mouseleave', startHideTooltipTimer.bind(tooltipMorph));
	
}








