/*-------------------------------------------------
MooSizer
-------------------------------------------------*/

var mooSizer = new Class({

	Implements: [Options, Events],
	options: {
		startwidth: 1440,  
		startheight: 900,
		minsize: .5,
		slideshow: 1,
		slideinterval: 5000,
		bgElement: ''
	},

	initialize: function(options){													
        this.setOptions(options);

		//Define image ratio & minimum dimensions
		var minwidth	= this.options.minsize*(this.options.startwidth);			
		var minheight	= this.options.minsize*(this.options.startheight);			
		var ratio		= this.options.startheight/this.options.startwidth;		

		this.resizenow(minwidth,minheight,ratio);

 		window.addEvent('resize', function(){										
			this.resizenow(minwidth,minheight,ratio);
		}.bind(this));

	},
	
	resizenow: function(minwidth,minheight,ratio) {								

		//Gather browser and current image size
		var imagesize		= $(this.options.bgElement).getSize();
		var imagewidth		= imagesize.x;											
		var imageheight		= imagesize.y;											
		var clientsize		= window.getSize();
		var browserwidth	= clientsize.x;											
		var browserheight	= clientsize.y;											
		
 		//Check for minimum dimensions
		if ((browserheight < minheight) && (browserwidth < minwidth)){				
				$(this.options.bgElement).setStyle('height',minheight);
				$(this.options.bgElement).setStyle('width',minwidth);
		}
		else{	
			//When browser is taller	
			if (browserheight > browserwidth){										
				imageheight = browserheight;
					$(this.options.bgElement).setStyle('height',browserheight);
				imagewidth = browserheight/ratio;									
					$(this.options.bgElement).setStyle('width',imagewidth);
				
				if (browserwidth > imagewidth){										
					imagewidth = browserwidth;										
						$(this.options.bgElement).setStyle('width',browserwidth);
					imageheight = browserwidth * ratio;								
						$(this.options.bgElement).setStyle('height',imageheight);
				}
			}			
			//When browser is wider
			if (browserwidth >= browserheight){										
				imagewidth = browserwidth;											
					$(this.options.bgElement).setStyle('width',browserwidth);
				imageheight = browserwidth * ratio;									
					$(this.options.bgElement).setStyle('height',imageheight);
				
				if (browserheight > imageheight){									
					imageheight = browserheight;									
						$(this.options.bgElement).setStyle('height',browserheight);
					imagewidth = browserheight/ratio;								
						$(this.options.bgElement).setStyle('width',imagewidth);
				}
			}
		}
	}

});


/*-------------------------------------------------
Index Animation
-------------------------------------------------*/
// http://www.broken-links.com/2007/05/27/using-mootools-for-animation-timings/

function indexSplashAnimation() {

	var animation = new Chain();


	var logoAnimationIn = function(){
		$$('#splashlogo').each(function(el){el.get('tween', {property: 'opacity', duration: 200, transition: Fx.Transitions.Quint.easeIn}).start(0,1)});  
	};
	
	var logoAnimationOut = function(){
		$$('#splashlogo').each(function(el){el.get('tween', {property: 'opacity', duration: 2000, transition: Fx.Transitions.Quint.easeIn}).start(1,0)});
		$$('#background').each(function(el){el.get('tween', {property: 'opacity', duration: 2000, transition: Fx.Transitions.Quint.easeIn}).start(1,0)});   
	
	};
	
    var logoluxuryReduceSize = function(){
		$$('#splashlogo').each(function(el){el.setStyle('margin','0px');el.setStyle('padding','0px');el.setStyle('height','0px');});
		$$('#index').each(function(el){el.setStyle('min-height','200px');});
	};
	
	var pageAnimation = function(){
		$$('#index').each(function(el){el.get('tween', {property: 'opacity', duration: 2000, transition: Fx.Transitions.Quint.easeIn}).start(0,1)}); 
	};

	animation.chain(logoAnimationIn);
  animation.chain(logoAnimationOut);
	animation.chain(logoluxuryReduceSize);
	animation.chain(pageAnimation);
	animation.callChain(); // fade in
	animation.callChain.delay(6500, animation);
	animation.callChain.delay(6000, animation);
	animation.callChain.delay(4000, animation);

}

/*-------------------------------------------------
Index Animation
-------------------------------------------------*/
// http://www.broken-links.com/2007/05/27/using-mootools-for-animation-timings/

function indexAnimation() {
	
	var animation = new Chain();
	
	var pageAnimation = function(){
		$$('#index').each(function(el){el.get('tween', {property: 'opacity', duration: 1000, transition: Fx.Transitions.Quint.easeIn}).start(0,1)});
	};
	
	animation.chain(pageAnimation);
	animation.callChain(); // fade in
	animation.callChain.delay(1000, animation);
}

/*-------------------------------------------------
Skip Splash Animation
-------------------------------------------------*/
// http://www.broken-links.com/2007/05/27/using-mootools-for-animation-timings/

function skipSplashAnimation() {
		
	var animation = new Chain();
	
	var pageAnimation = function(){
		$$('#index').each(function(el){el.get('tween', {property: 'opacity', duration: 1000, transition: Fx.Transitions.Quint.easeIn}).start(0,1)});
	};
	
	animation.chain(pageAnimation);
	animation.callChain(); // fade in
	animation.callChain.delay(1000, animation);
}

/*-------------------------------------------------
Subscribe Animation
-------------------------------------------------*/
// http://www.broken-links.com/2007/05/27/using-mootools-for-animation-timings/

function pageAnimation() {
	
	var animation = new Chain();
	
	var pageAnimation = function(){
		$$('#subscribe, #locations, #locationssydney, #locationsmelbourne, #brands, #thankyou, #privacy').each(function(el){el.get('tween', {property: 'opacity', duration: 1000, transition: Fx.Transitions.Quint.easeIn}).start(0,1)});
	};
	
	animation.chain(pageAnimation);
	animation.callChain(); // fade in
	animation.callChain.delay(1000, animation);
}

/*-------------------------------------------------
Slideshow
-------------------------------------------------*/
// http://davidwalsh.name/create-a-simple-slideshow-iii

var HSlideShow = new Class({
	options: {
		showDuration: 8000,
		showTOC: true,
		tocLeftIndent: 440, // 6 images 440, 4 images 420, 2 images 400
		tocWidth: 8,
		tocClass: 'toc',
		tocActiveClass: 'toc-active'
	},
	Implements: [Options,Events],
	initialize: function(container,elements,options) {
		//settings
		this.container = $(container);
		this.elements = $$(elements);
		this.currentIndex = 0;
		this.interval = '';
		if(this.options.showTOC) this.toc = [];
		
		//assign
		this.elements.each(function(el,i){
			if(this.options.showTOC) {
				this.toc.push(new Element('a',{
					text: i+1,
					href: '#',
					'class': this.options.tocClass + '' + (i == 0 ? ' ' + this.options.tocActiveClass : ''),
					events: {
						click: function(e) {
							if(e) e.stop();
							this.stop();
							this.show(i);
						}.bind(this)
					},
					styles: {
						 left: (this.options.tocLeftIndent + (i + 1) * (this.options.tocWidth + 10)) 
					}
				}).inject(this.container));
			}
			if(i > 0) el.set('opacity',0);
		},this);
		//events
		this.container.addEvents({
			mouseenter: function() { this.stop(); }.bind(this),
			mouseleave: function() { this.start(); }.bind(this)
		});

	},
	show: function(to) {
		this.elements[this.currentIndex].fade('out');
		if(this.options.showTOC) this.toc[this.currentIndex].removeClass(this.options.tocActiveClass);
		this.elements[this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0))].fade('in');
		if(this.options.showTOC) this.toc[this.currentIndex].addClass(this.options.tocActiveClass);
	},
	start: function() {
		this.interval = this.show.bind(this).periodical(this.options.showDuration);
	},
	stop: function() {
		$clear(this.interval);
	}
});

/*------------------------------------------------
Dom Ready
-------------------------------------------------*/

document.addEvent('domready', function(){

// Background
//moosizer = new mooSizer({ bgElement:'background' });

  																	
// Link hover	
$each($$('a'), function(el) {
	var original = el.getStyle('color');
	var morph = new Fx.Morph(el,{ 'duration':'500', link:'cancel' });
	el.addEvents({
		'mouseenter' : function() { morph.start({ 'color': '#999999' }) },
		'mouseleave' : function() { morph.start({ 'color': original }) }
	});
});


});





