


/*	Author: Brandon Durham
----------------------------------------*/
var Blinds,
	sections,
	section_count,
	navs,
	nav_marker,
	resizing,
	scrolling,
	storage = false,
	blinker,
	your_name = null,
	marker;

/*	Window blinds
----------------------------------------*/
Blinds = {

	input_focused: false,
	running: false,
	here: 0,

	scroller: new Fx.Scroll(document.body, {
		duration: 600,
		transition: Fx.Transitions.Expo.easeInOut
	}),

	setup: function(){

		// navigation items
		navs = $$('#site-nav li');

		// sections
		sections = $$('section');
		section_count = sections.length;

		var ss = new Fx.SmoothScroll({
			duration: 600,
			transition: Fx.Transitions.Expo.easeInOut,
			wheelStops: false,
			onComplete: function(){
				if (Browser.ie) Blinds.highlightNav();
			}
		});

		// orange navigation marker
		nav_marker = new Element('div', {
			id : 'here'
		}).inject('site-header').set('morph', {
			duration: 400,
			transition: 'circ:in:out',
			onComplete: function(){
				_gaq.push(['_trackPageview', '../default.htm'+sections[Blinds.here].get('id')]);
				navs[Blinds.here].addClass('here');
				Blinds.running = false;
			}
		}).morph({
			'left': navs[Blinds.here].getLeft(),
			'width': navs[Blinds.here].getWidth()
		});

		// grab name if stored and update
		if (storage && your_name !== null) {
			$('your_name').set('value', your_name);
			Blinds.updateNames();
		}
		else {
			$('your_name').set('value', $('your_name').get('placeholder'));
		}

		// add form focus trump
		$('your_name').addEvents({
			'keydown': function(event){
				if (event.key === 'tab' || event.key === 'enter') {
					event.preventDefault();
					$('your_name').blur();
				}
			},
			'focus': function(event){
				Blinds.input_focused = true;
			},
			'blur': function(event){
				Blinds.input_focused = false;
				var name = $('your_name').get('value').trim();
				if (storage && name !== '') {
					localStorage.setItem('your_name', name);
					your_name = name;
					Blinds.updateNames();
				}
				else if (storage && (name === '' || name === 'Your name here')) {
					localStorage.removeItem('your_name');
					your_name = null;
					$('your_name').set('value', $('your_name').get('placeholder'));
					Blinds.updateNames();
				}
			}
		});

		// placeholder fallback
		if (!Modernizr.input.placeholder){
			$$('input[type=text]').each(function(input, i){
				var ph = input.get('placeholder');
				if (ph !== '') return false;
				input.set('value', ph);
				input.addEvent('focus', function(){
					if (this.get('value') == ph) this.set('value', '');
				});
				input.addEvent('blur', function(){
					if (this.get('value') == '') this.set('value', ph);
				});
			});
		}

	},

	highlightNav: function(){
		nav_marker.get('morph').cancel();
		nav_marker.morph({
			'left': navs[Blinds.here].getLeft(),
			'width': navs[Blinds.here].getWidth()
		});
	},

	updateNames: function(){
		if (your_name === '' || your_name === null || your_name === 'Your name here') {
			$('home').removeClass('star');
			$$('.your_name').each(function(el){
				el.set('text', 'You');
			});
		}
		else {
			$('home').addClass('star');
			_gaq.push(['_trackEvent', 'Input', 'Your name here', escape(your_name)]);
			$$('.your_name').each(function(el){
				el.set('text', your_name);
			});
		}
	},

	getClosest: function(){

		var body_scroll = $(document.body).getScrollTop(),
			body_height = $(document.body).getHeight(),
			theone = null;

		sections.each(function(section,id){
			var tops = section.getTop();
			var diff = tops - body_scroll;
			if (diff >= 0 && diff < body_height/2) {
				theone = id;
			}
			else if (diff >= 0 && diff > body_height/2 && diff < body_height) {
				theone = (id === 0) ? 0 : id - 1;
			}
		});

		if (theone !== null ) {
			if (theone !== Blinds.here) navs[Blinds.here].removeClass('here');
			Blinds.here = theone;
			Blinds.highlightNav();
		}
	}

}

var Sizing = {

	set: function(){

		if (DetectIphone() || DetectIpad()) return false;

		var doc_w = $(document.body).getWidth();
		var doc_h = $(document.body).getHeight();
		$(document.body).set('id', this.steps(doc_w, doc_h));
	},

	steps: function(w, h) {
		if (w <= 900) {
			return 'small';
		}
		else if (w > 900 && w <= 1250) {
			return 'medium';
		}
		else if (w > 1100) {
			return 'large';
		}
	}

}

window.addEvent('domready', function(){

	if (Modernizr.localstorage) {
		storage = true;
		if (localStorage.getItem('your_name')) {
			your_name = localStorage.getItem('your_name');
		}
	}

	// set size
	Sizing.set();

	// start blinds running
	Blinds.setup();

	// mobile stylesheets
	if (DetectIphone()) {
		var iphone = new Element('link', {
			'rel': 'stylesheet',
			'href': '../css/iphone.css'
		}).inject(document.body);
	}
	else if (DetectIpad()) {
		var ipad = new Element('link', {
			'rel': 'stylesheet',
			'href': '../css/ipad.css'
		}).inject(document.body);
	}

	var tips = new Tips('.tip',{
		className: 'tooltip',
		fixed: true,
		hideDelay: 50,
		showDelay: 0,
		offset: {
			x: 115,
			y: -20
		}
	});
});

window.addEvent('resize', function(){
    clearTimeout(resizing);
    resizing = setTimeout(function(){
		Sizing.set();
		Blinds.scroller.toElement(sections[Blinds.here]);
	}, 300);
});

window.addEvent('scroll', function(){
    clearTimeout(scrolling);
 	scrolling = setTimeout(function(){
		Blinds.getClosest();
	}, 400);
});

window.addEvent('load', function(){
	if ($('cover')) $('cover').destroy();
});

document.addEvent('touchmove', function(event){
	Blinds.getClosest();
});
