var WrappedImages = new Class({
	/* wraps images in an enclosing DIV */
	Implements: [Options,Events, Chain], 						  
	
	/* menu options. don't put a comma after the last option! */
	options: {
		/* (string) Slick selector of target elements  */
		targets:null,
		/* which element to wrap with? */
		wrapper: 'div'
	}, 
	
	log: function(){
		return false;
		console.log.apply(console, arguments);
		
	},
	/* this function runs whenever a new instance of this class is made. */
	initialize: function(options){ 
		
		this.setOptions(options);
		
		if(!this.options.targets) return false;
		
		
		document.getElements(this.options.targets).each(
			function(item, index, array){
				/* get all targets */
				this.log('item=',item);
				var css = item.getProperty('class');		
				this.log('css=',css);		
				
				/* img display block */
				if(item.get('tag') == 'img'){
					item.setStyle('display','block');
				}
				
				/* strip css */
				item.removeProperty('class');
				/* create wrapper */
				var wrapper = new Element('div',{'class':css} ).setStyle('overflow','hidden');
				this.log('wrapper=',wrapper);
				/* insert */
				item.grab(wrapper,'before').inject(wrapper);
				
				
			},this
		);
	}
});


var AlternatingList = new Class({
	/* UL with alternating colours */
	Implements: [Options,Events, Chain], 						  
	
	/* menu options. don't put a comma after the last option! */
	options: {
		/* which elements */
		targets:null,
		/* which elements to colour? */
		childElements: 3000,
		/* class name for alt */
		altCss: 'alt'
		
		
	}, 
	
	log: function(){
		return false;
		console.log.apply(console, arguments);
		
	},
	/* this function runs whenever a new instance of this class is made. */
	initialize: function(options){ 
		
		this.setOptions(options);
		
		if(!this.options.targets) return false;
		
		/* get all targets */
		document.getElements(this.options.targets).each(
			function(item, index, array){
				this.log('item=',item);
				/* get all child elements of each target */
				item.getElements(this.options.childElements).each(
								function(item, index, array){
									
									
									if((index % 2) == 0){
										/* 1st,3rd etc because arrays are zero-based*/
										item.addClass(this.options.altCss);
									} 
									
								},this
							);
				
			},this
		);
	}
});


var DynamicSlideshow = new Class({
	/* ajax slideshow */
	Implements: [Options,Events, Chain], 						  
	
	/* menu options. don't put a comma after the last option! */
	options: {
		/* which viewport to use */
		viewPort:null,
		/* delay in ms */
		slideDelay: 3000,
		/* transition length in ms */
		slideTransitionLength: 2000,
		/* delay at start? */
		startDelay: 0,
		/* whether to show next and prev buttons */
		useNavButtons:false,
		/* array of images to get */
		imgArray:null
		
		
	}, 
	
	log: function(){
		return false;
		console.log.apply(console, arguments);
		
	},
	/* this function runs whenever a new instance of this class is made. */
	initialize: function(options){ 
		
		this.setOptions(options);
		
		if(!this.options.viewPort || !this.options.imgArray) return false;
		
		this.options.viewPort = document.getElement(this.options.viewPort);
		
		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('div.slide.default');
		if(defaultImg){
			defaultImg.setStyle('z-index',1);
			this.slides[0] = defaultImg;
			this.options.imgArray.unshift('');
		
		}
		
		this.totalImg =  this.options.imgArray.length;
		
		/* convenience object */
		this.Nav = {};
		/* all nav links */
		this.Nav.StatusLinks = new Array();
		this.Nav.Container = new Element('div',{'class':'nav'});
		var navLinkWidth = 25;
		this.Nav.Container.setStyle('width', this.options.imgArray.length * navLinkWidth	);
		
		/* build nav */
		this.options.imgArray.each(
						function(item, index, array){

							/* status elements displayed inline, so store backwards */
							var css = '';

							if(index==0){
								/* first */
								css += 'active';
							}
							var status = new Element('a',{'class':css,'href':'javascript:'});
							status.fade('show');
							status.addEvent('click',this.NextSlide.bind(this,{'clickIsFromUser':true,'index':index}) );
							
							this.Nav.StatusLinks.include(status);
							this.Nav.Container.grab(status,'bottom');
							
						},this
					);
		
		/* insert nav into DOM */
		this.options.viewPort.grab(this.Nav.Container);
		
		/* add mouse handler */
		this.Nav.Container.addEvent('mouseenter',function(){clearInterval(this.periodID)}.bind(this));
		this.Nav.Container.addEvent('mouseleave',function(){this.StartPeriodical.delay(this.options.startDelay, this, this.delayms);}.bind(this));
			
		
		this.StartPeriodical.delay(this.options.startDelay, this, this.delayms);
	},
	
	NextSlide:function(params){
		/* boolean isNext determines if moving forwards or back */
		
		var isNext = params['isNext'];
		this.clickIsFromUser = params['clickIsFromUser'];
		 
		/* clear timer */
		if(this.clickIsFromUser) {
			//this.log('CLEAR TIMEOUT',this.periodID);
			clearTimeout(this.periodID);
			if(params.index != this.currentImg){
				this.nextImg = params.index;
			}else{
				return false;
			}
		}else{
			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){
					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 */
			this.LoadImg({'img':this.options.imgArray[this.nextImg]});
		}
		
	},
	
	
	
	LoadImg:function(params){
		/* loads image via HTTP */
		this.tempImg = '';
		this.tempImg = new Asset.image(params.img,{'onload':function(){this.LoadImgSuccess()}.bind(this)});
	},
	
	
	
	LoadImgSuccess:function(){
		/* create wrapper element */
		var wrapper = new Element('div',{'class':'slide'});
		wrapper.fade('hide');
		/* set bg */
		wrapper.setStyle('z-index',this.nextImg);
		wrapper.setStyle('background-image','url("'+this.tempImg.getProperty('src')+'")');
		/* insert */
		this.options.viewPort.grab(wrapper);
		
		this.slides[this.nextImg] = wrapper;
	
		/* fadein */
		this.TransitionSlide();

	},
	
	
	
	TransitionSlide:function(){
		/* actual fade transition */
		/* reset all z-index */
		this.slides.each(
						function(item, index, array){
							item.setStyle('z-index',index);
							if(this.clickIsFromUser) {
								item.fade('hide');
							}
						},this
					);
		
		var fadeOutCurrSlide;
		var thisSlide = this.slides[this.currentImg];
		var nextSlide = this.slides[this.nextImg];
		
		/* deactivate current nav link */
		this.Nav.StatusLinks[this.currentImg].removeClass('active');
		/* active next link */
		this.Nav.StatusLinks[this.nextImg].addClass('active');
		
		
		if(thisSlide) {
			
			if(!thisSlide.transFx){
				/* for user interaction */
				thisSlide.transFx = new Fx.Tween(thisSlide,{property: 'opacity','duration':this.options.slideTransitionLength,'link':'cancel'});
				
			}
			
			if(this.clickIsFromUser) {
				thisSlide.transFx.stop();
				thisSlide.transFx.set(0);
			}
			thisSlide.setStyle('z-index',99);
			
		}else{

		}
		
		if(nextSlide){
			/* move in front */
			nextSlide.setStyle('z-index',100);
		}
		/* move behind */
		thisSlide.setStyle('z-index',99);
		
		if(nextSlide) {
			if(!nextSlide.transFx){
				nextSlide.transFx = new Fx.Tween(nextSlide,{property: 'opacity','duration':this.options.slideTransitionLength,'link':'cancel'});
			}
			
			/* hide */
			nextSlide.transFx.set(0);
			/* fade in */
			nextSlide.transFx.start(1);
		}
			
		/* moved here since it needs to happen instantly */
		this.currentImg = this.nextImg;
	},
	
	StartPeriodical:function(delayms){
		clearTimeout(this.periodID);
		this.periodID = function(){this.NextSlide({'isNext':true,'clickIsFromUser':false})}.bind(this).periodical(delayms);
	}
	
	
});

function logoToolTip(){
	/* Hover effect for Logo link */
	var logoLink = document.id('atm-link');
	
	//create the tooltip DIV
	var toolTip = document.id('atm-info');
	toolTip.setStyles({'opacity':0,'right':'0px'});
	//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) clearInterval(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) clearInterval(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));
	
}

// Test for position:fixed support
Modernizr.addTest('positionfixed', function () {
    var test    = document.createElement('div'),
        control = test.cloneNode(false),
        fake = false,
        root = document.body || (function () {
            fake = true;
            return document.documentElement.appendChild(document.createElement('body'));
        }());

    var oldCssText = root.style.cssText;
    root.style.cssText = 'padding:0;margin:0';
    test.style.cssText = 'position:fixed;top:42px'; 
    root.appendChild(test);
    root.appendChild(control);
    
    var ret = test.offsetTop !== control.offsetTop;

    root.removeChild(test);
    root.removeChild(control);
    root.style.cssText = oldCssText;
    
    if (fake) {
        document.documentElement.removeChild(root);
    }

    return ret;
});



