/*jslint browser: true, white: true, plusplus: true, newcap: true,
    eqeqeq: true, evil: true, nomen: false, regexp: true, undef: true,
    onevar: true, sub: true */

/*global jQuery,$,window,console,document,log,_gaq,YT,TBTO_VIDEO_ID,TBTO_VIDEO_ORIGIN */
window.log = function () {
	log.history = log.history || []; 
	log.history.push(arguments);
	if (this.console) {
		console.log(Array.prototype.slice.call(arguments));
	}
};

var TBTO = {

	//vids: ['zI4D1QOLGuM', 'EYKVvysl-Mo', 'YuW26BKVJ3U', '7J_2vaDgyBw'],

	initNav : function () {
		var ua = navigator.userAgent.toLowerCase();
		
		if (ua.indexOf("android") > -1 || ua.indexOf('like mac os x') > -1) {
			return false;
		}


		$('nav .menu').hover(
			function () {
				$('a:first-child').addClass('active');
				$('.subnav', this).css('display', 'block'); //.stop(true, true).slideDown();
			},
			function () {
				$('a:first-child').removeClass('active');
				$('.subnav', this).css('display', 'none'); //stop(true, true).slideUp();
			}
		);
	},

	initVideo : function () {
		$('#bg').prepend('<div id="ytapiplayer" />');

		var tag = document.createElement('script'),
		    firstScriptTag = null;

		tag.src = "http://www.youtube.com/player_api";
		firstScriptTag = document.getElementsByTagName('script')[0];
		firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
		
		
		if ( $('#mask').length < 1 ) {
			return;
		}
		$('#container').prepend('<a href="#" id="rolldown">Read the words</a> <a href="#" id="rollup">Watch the video</a>');
		
		$('#rollup').bind('click', function () {
			if ( $.browser.msie && parseInt($.browser.version, 10) <= 7 ) {
				$('#logo, #mask, article').css('visibility', 'hidden');
				$('#rolldown').slideDown(300);
			} else {
				$('#main').slideUp(550, function () {
					$('#mask').slideUp(550, function () {
					$('#rolldown').slideDown(300);
					});

				});	
			}
			$('#rollup').slideUp(200);
			return false;
		});
		
		$('#rolldown').bind('click', function () {
			if ( $.browser.msie && parseInt($.browser.version, 10) <= 7 ) {
				$('#logo, #mask, article').css('visibility', 'visible');
				$('#rollup').slideDown(300);
			} else {
				$('#mask').slideDown(500, function () {
					$('#main').slideDown(500, function () {
						$('#rollup').slideDown(300);
					});
				});
			}
			$('#rolldown').slideUp(300);
			return false;
		});
	},
	

	loadVideo : function (video_id) {
		// TODO: option to ramp up volume, esp. for front page first load
		var player = new YT.Player('ytapiplayer', {
			height: '100%',
			width: '100%',
			videoId: video_id,
			playerVars: {
				'autohide': 1,
				'autoplay': 1,
				'controls': 0,
				'loop': 1,
				'wmode': 'transparent',
				'origin': TBTO_VIDEO_ORIGIN,
				'playlist': video_id
			},
			events: {
				'onReady': function (evt) {
					evt.target.playVideo();
				}
			}
		});
	},
	
	initRollUp : function () {
		var total_h = $(window).height(),
		    masked = true,
		    main = $('#main'),
		    mask = null;
		log(document.location);
		if (document.location.pathname !== '/') {
		
			$('#bg').append('<div id="mask"/>')
			mask = $('#mask');
		}
			
		
		
		if ( TBTO.isLoFi() ) {
			return;
		}
		

		
	},

	initUI : function () {
		// logo img width is not available on document.ready so we use its aspect
		// ratio (5.7w x 1h) relative to the screen height to guess its height
		
		
		var total_h = $(window).height(),
		    logo_h = total_h / 5.7,
		    y = total_h - (total_h / 4) - logo_h, 
		    masked = false,
		    main = $('#main'),
		    mask = null,
		    homepage_interval = null;

			// TODO: logo - 1/3 up screen
			log('y', total_h, logo_h, y);

		if (TBTO.isLoFi()) {
			log('lo-fi yo');
			return;
		}

		// initial position
		
		
		if (TBTO.isMobileWebKit()) {
			// no position fixed, so no need for translucent BG stuff
			$('html').addClass('lo-fi');
			return;
		}
		
		//main.css({'margin-top': 0, 'top': y});
		
		
		$('#bg').append('<div id="mask"/>');

		mask = $('#mask');
		
		if ($.support.opacity === false) {
			mask.css({'opacity': 'auto', 'display': 'none'});
		}

		$(window).bind('scroll', function () {
			if (homepage_interval) {
				window.clearTimeout(homepage_interval);
			}
			var pos = $(window).scrollTop();
			if (pos < 100 && masked) {
				masked = false;
				if ($.support.opacity) {
					mask.animate({opacity: 0}, 500);
				} else {
					mask.hide();
				}

			} else if (pos > 100 && masked === false) {
				masked = true;
				if ($.support.opacity) {
					mask.animate({opacity: 1}, 500);
				} else {
					mask.show();
				}
			}
		});

		// auto scroll up the homepage
		if ($('body').hasClass('homepage') && $('body').scrollTop() < 250) {
			// TODO: what if people reload when they're already scrolled down

			homepage_interval = window.setTimeout(function () {
				$('html, body').animate({scrollTop: 250}, 1000);
			}, 3000);

		}

	},
	
	isMobileWebKit : function () {
	    return (
	        navigator.userAgent.match(/AppleWebKit/) &&
	        navigator.userAgent.match(/ Mobile\//)
	    );
	},
	
	isLoFi : function () {
		return ($('html').hasClass('lo-fi'));
	}
};

function onYouTubePlayerAPIReady() {
	if (typeof TBTO_VIDEO_ID !== 'undefined') {
		TBTO.loadVideo(TBTO_VIDEO_ID);	
	}
}

$(document).bind('ready', function () {
	TBTO.initNav();
	// TBTO.initUI();
	TBTO.initRollUp();
	
	if (typeof TBTO_VIDEO_ID !== 'undefined') {
		TBTO.initVideo();
	}
	
	//
});



