var DynamicSlideshow = 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:null,
			// delay in ms
			slideDelay: 3000,
			// transition length in ms
			slideTransitionLength: 2000,
			// whether to show next and prev buttons
			useNavButtons:false,
			// array of images to get
			imgArray:null
		}, 
	
	log: function(){
		if(window['console']) 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.setOptions(options);
		
		// Dynamically generate navigation links from promo links
		
		if(!this.options.viewPort || !this.options.imgArray) return false;
		
		
		
		
		
		//
		this.slides = new Array('');
		this.delayms = this.options.slideDelay + this.options.slideTransitionLength;
		this.currentImg = 0;
		
		
		
		//check if default img exists
		var defaultImg = this.options.viewPort.getElement('img.default');
		if(defaultImg){
			
			this.slides[0] ='';
			
			this.options.imgArray.unshift('');
		}
		
		this.totalImg =  this.options.imgArray.length;
		
			
		this.StartPeriodical(this.delayms);
		
		//$('article-viewport').getElement('div.articles').tween('left',0);
		
	},
	
	NextSlide:function(event,paramArray){
		// 'this' == ClassPromo
		// boolean isNext determines if moving forwards or back
		if(event) event.stop();

		var isNext = paramArray['isNext'];
		this.clickIsFromUser = paramArray['clickIsFromUser'];
		 
		//clear timer
		if(this.clickIsFromUser) {

			$clear(this.periodID);
		}
		
		this.nextImg='';
		
		if(isNext){
			//forward
			
			if(this.currentImg == this.totalImg-1){
				//reset to 0
				this.nextImg = 0;
			}else{
				this.nextImg = this.currentImg+1;
				
			}
		}else{
			//backwards
			if(this.currentImg == 0){
				//reset to 0
				this.nextImg = this.totalImg-1;
			}else{
				this.nextImg = this.currentImg-1;
				
			}
		}
		
		
		//check if 
		if(this.nextImg==0 || this.slides[this.nextImg]){
			//slide already exists
			this.TransitionSlide();
		}else if(this.nextImg > 0){
			// load img via ajax
			this.GetImgAjax(this.options.imgArray[this.nextImg]);
		}
		
		
	},
	
	GetImgAjax:function(filePath){
		// get next img via ajax
		
		var myHTMLRequest = new Request.JSON({url:'ajax-miniphoto.htm', 'onSuccess' : this.LoadImg.bind(this) , 'onFailure': function(){}  });
		myHTMLRequest.get({'file': filePath, 'width': '960','height':'647','fill':'1'});
		myHTMLRequest.send();

	},
	
	LoadImg:function(responseJSON, responseText){
		// loads image via HTTP
		
		if(responseJSON.success){
			
			this.tempImg = new Asset.image(responseJSON.img,{'onload':this.LoadImgSuccess.bind(this)});
		}else{
			
		}
	},
	
	
	
	LoadImgSuccess:function(){
		
			
			this.tempImg.fade('hide');
			// insert
			
			this.options.viewPort.grab(this.tempImg);
			
			this.slides[this.nextImg] = this.tempImg;
			
		
		
			//fadein
			this.TransitionSlide();
			
			//this.slides[nextImg].fade('in');
		
			if(this.clickIsFromUser) {
				this.periodID = this.StartPeriodical.delay(this.delayms*2, this, this.delayms);

			}

	},
	
	
	
	TransitionSlide:function(){
		// actual fade transition
		
		
		
		
		
		
		var currFx = false;
		var fadeOutCurrSlide = function(){this.currentImg = this.nextImg;}.bind(this);
		
		if(this.slides[this.currentImg]) {
			//this.slides[this.currentImg].fade('out');
			var currFx = new Fx.Tween(this.slides[this.currentImg],{property: 'opacity','duration':this.options.slideTransitionLength});
			// a bound function to fade out current slide
			fadeOutCurrSlide = function(){
				if(currFx) {
					if(this.nextImg == 0) {
						//fade out curr slide
						currFx.start(1,0).chain(function(){this.currentImg = this.nextImg;}.bind(this));
					}else{
						// hide curr slide
						currFx.set(0);
						this.currentImg = this.nextImg;
					}
					
					
				}
				
				
				
			}.bind(this);
		}
		
		
		
		if(this.slides[this.nextImg]) {
			var nextFx = new Fx.Tween(this.slides[this.nextImg],{property: 'opacity','duration':this.options.slideTransitionLength});
			
			
			
			nextFx.start(0,1).chain(fadeOutCurrSlide);
			
			
			
		}else{
			fadeOutCurrSlide();
		}
			
		
		
		
	},
	
	StartPeriodical:function(delayms){
		//console.log(delayms);
		this.periodID = this.NextSlide.bindWithEvent(this,{'isNext':true,'clickIsFromUser':false}).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':'22px'});
	//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));
	
}







