window.addEvent('domready', function() {
	var pageScroll = new Fx.Scroll( $('pagebody'), {
		duration: 500
	});


	$$('.backtotop').addEvent('click', function(e){
		e.stop();
		pageScroll.toTop();
	});
	
	
	if ( $('blog-dateselect') ) {
		$('blog-dateselect').addEvent('change', function(e) {
			pageScroll.toElement(this.get('value'));
		});
	}

	if ( $('location-output') ) {

		var req = new Request.HTML({
			url: '/includes/location.php',
			onSuccess: function(html) {
				$('location-output').set('text', '');
				$('location-output').adopt(html);
			}
		});

		$$('.location-trigger')[0].addClass('on');
		$$('.location-trigger')[0].addClass('disabled');
		var first_lid = $$('.location-trigger')[0].get('id').replace('lid-','');
		$('location-output').load('/includes/location.php?id='+first_lid);

		$$('.location-trigger').addEvent('click', function(e){
			e.stop();
			$$('.location-trigger').removeClass('on');
			$$('.location-trigger').removeClass('disabled');
			this.addClass('on');
			this.addClass('disabled');
			$('location-output').load('/includes/location.php?id=' + this.get('id').replace('lid-',''));
		});
	
	}

	if ( $('lookbook') ) {
		var nS4 = new noobSlide({
			box: $('lookbook'),
			size: 406,
			items: $$('#lookbook img'),
			onWalk: function(currentItem, currentHandle){
				$$('ul#lookbook-thumbs li').each(function(thumb,i){
					panel = i + 1;
					thumb.removeClass('active');
					$('lookbook-page-details-'+panel).setStyle('display', 'none');
				});

				currentHandle.addClass('active');

				$$('ul#lookbook-thumbs li').each(function(thumb,i){
					panel = i + 1;
					if ( thumb.hasClass('active') ) {
						$('lookbook-page-details-'+panel).setStyle('display', 'block');
					}
				});
			},
			handles: $$('ul#lookbook-thumbs li')
		});
	}


	function addImage( panel ) {
		var fx = new Fx.Tween(panel,{'duration':'600'});
		panel.setStyle('opacity', 0);
		fx.start('opacity', 1);
	}


	var myChainStore = new Chain();
	$$('body.store .wrapper-store.home .project-container').each(function(panel, i){ myChainStore.chain( function(){ addImage(panel); } ); });
	var runChainStore = function() { 
		myChainStore.callChain();
		//if (myChain.chains.length == 0) { runChain = $clear(timer); } 
	}
	var timer = runChainStore.periodical(400);


	var myChainHome = new Chain();
	$$('body.home .item-container').each(function(panel, i){ myChainHome.chain( function(){ addImage(panel); } ); });
	var runChainHome = function() { 
		myChainHome.callChain();
		//if (myChain.chains.length == 0) { runChain = $clear(timer); } 
	}
	var timer = runChainHome.periodical(400);



	function addThumb( panel ) {
		var fx = new Fx.Tween(panel,{'duration':'250'});
		panel.setStyle('opacity', 0);
		fx.start('opacity', 1);
	}


	var myChain2 = new Chain();
	$$('.thumbs li').each(function(panel, i){ myChain2.chain( function(){ addThumb(panel); } ); });
	var runChain2 = function() { 
		myChain2.callChain();
		//if (myChain.chains.length == 0) { runChain = $clear(timer); } 
	}
	var timer2 = runChain2.periodical(50);

	
	$$('.photos .gallery').each(function(panel, i){
		panel.set('opacity', 0);
		
		var fx = new Fx.Tween(panel,{'duration':'250'});
		fx.start('opacity', 0).chain(function() {
			this.set('display', 'none');
			this.callChain();
		}).chain(function() {
			panel.setStyle('display', 'block');
			this.callChain();
		}).chain(function() {
			panel.tween('opacity', 1);
			this.callChain();
		});
	});


	$each($$('.fade'), function(el) {
		var original = el.getStyle('color');
		var morph = new Fx.Morph(el,{ 'duration':'200', link:'cancel' });
		el.addEvents({
			'mouseenter' : function() { morph.start({ 'color': '#ffffff' }) },
			'mouseleave' : function() { morph.start({ 'color': original }) }
		});
	});



	$each($$('.fade-facebook'), function(el) {
		var original = el.getStyle('color');
		var morph = new Fx.Morph(el,{ 'duration':'200', link:'cancel' });
		el.addEvents({
			'mouseenter' : function() { morph.start({ 'color': '#ffffff' }) },
			'mouseleave' : function() { morph.start({ 'color': original }) }
		});
	});


	$each($$('.fade-button img'), function(el) {
		var morph = new Fx.Tween(el, { 'duration':'250', link:'cancel' });
		el.setStyle('opacity', 0.001);
		el.addEvents({
			'mouseenter' : function(e) { 
				morph.start('opacity', 0.999);
				e.stop();
			},
			'mouseleave' : function(e) {
				morph.start('opacity', 0.001);
				e.stop();
			}
		});
	});


	$$('a.disabled').addEvents({
		'click' : function(e){
			e.stop();
		}
	});
	
	
	if ( $('pagebody').hasClass('home') ) {
		
		homeSlideFx = new Fx.Slide( $('home-product-slide'), { duration: 350 } ).hide();
		$('product-slide-trigger').addEvent('click', function(e){
			e.stop();
			if ( homeSlideFx.open ) {
				this.removeClass('hover');
				this.removeClass('on');
				this.set('html', 'Learn more about this line');
			} else {
				this.addClass('hover');
				this.addClass('on');
				this.set('html', 'Close');
			}
			homeSlideFx.toggle();
		});

		$('product-slide-trigger').addEvent('mouseenter', function(e){
			if ( !homeSlideFx.open && !this.hasClass('on') ) {
				this.addClass('hover');
			}
		});
	
		$('product-slide-trigger').addEvent('mouseleave', function(e){
			if ( !homeSlideFx.open && !this.hasClass('on') ) {
				this.removeClass('hover');
			}
		});
	
	}


	/*
	http://davidwalsh.name/mootools-form-field-default-plugin
	*/
	var dwDefaults = new Class({
		//implements
		Implements: [Options],
	
		//options
		options: {
			collection: $$('input[type=text]')
		},
		
		//initialization
		initialize: function(options) {
			//set options
			this.setOptions(options);
			this.defaults();
		},
		
		//a method that does whatever you want
		defaults: function() {
			this.options.collection.each(function(el) {
				el.set('value',el.get('alt'));
				el.addEvent('focus', function() { if(el.get('value') == el.get('alt')) { el.set('value',''); } });
				el.addEvent('blur', function() { if(el.get('value') == '') { el.set('value',el.get('alt')); } });
			});
		}
		
	});

	var defs = new dwDefaults({
		collection: $$('#footer-container input[type=text]')
	});

});