/*

Developed by:

      ___           ___                 
     /__/|         /__/\        ___     
    |  |:|         \  \:\      /  /\    
    |  |:|          \  \:\    /  /:/    
  __|  |:|      _____\__\:\  /__/::\    
 /__/\_|:|____ /__/::::::::\ \__\/\:\__ 
 \  \:\/:::::/ \  \:\~~\~~\/    \  \:\/\
  \  \::/~~~~   \  \:\  ~~~      \__\::/
   \  \:\        \  \:\          /__/:/ 
    \  \:\        \  \:\         \__\/  
     \__\/         \__\/               

*/


/*******************************
          Ready Event
*******************************/
lbox.readyEvent = function() {
	
	// what are we dealing with
	lbox.iPad = lbox.isiPad();
	lbox.iPhone = lbox.isiPhone();
		
	// os detection
	$.os = {
		name: (lbox.iPad || lbox.iPhone) ? 'mobileSafari' : (/(win|mac|linux|sunos|solaris|iphone)/.exec(navigator.platform.toLowerCase()))[0].replace('sunos', 'solaris')
	};
	
	// iDevice specfic event ternarys
	lbox.handleClick = (lbox.iPad || lbox.iPhone) ? ('touchstart') : ('click');
	lbox.handleDown = (lbox.iPad || lbox.iPhone) ? ('touchstart') : ('mousedown');
	lbox.handleUp = (lbox.iPad || lbox.iPhone) ? ('touchend') : ('mouseup');
	
	// Attach custom stylesheets
	if(lbox.iPad || lbox.iPhone || $.os.name == 'mac' || $.browser.safari) {
		$('head').append('<link rel="stylesheet" type="text/css" charset="utf-8" href="style/browsers/mac.css" />');
	}	
	
	// Prompt for chrome frame install if configured
	if($.browser.msie && parseInt($.browser.version) < 7 && lbox.chromeFrameRequired) {
		lbox.chromeFrameInstall();
	}
	
	if(lbox.iPhone) {
		// Get rid of urlbar on iPhone
		window.scrollTo(0, 1);
	}

	/*******************************
			Event Handlers
	*******************************/
	// shorthand	
	var config = lbox.settings,
		handleClick = lbox.handleClick,
		handleDown = lbox.handleDown,
		handleUp = lbox.handleUp
	;
	
	var handler = lbox.handler = {
		logo: {
			mouseenter: function() {
				var $logo = $(this);
				// only animate on subsections
				if(lbox.section != 'home') {
					var	$icon = $logo.find('.icon'),
						endPosition = -27,
						currentPosition = $icon.css('top'),
						pixelDistance = Math.abs(parseInt(endPosition, 10) - parseInt(currentPosition,10)),
						pixelsPerSecond = 100,
						duration = parseInt(pixelDistance / pixelsPerSecond * 800, 10);
					;
					if(typeof(lbox.timer) != 'undefined') {
						clearTimeout(lbox.timer);	
					}
					lbox.timer = setTimeout(function() {
						if($icon.animated()) {
							$icon.stop();	
						}
						$icon.animate({ top: endPosition+'px' }, duration, 'easeOutQuad');
					}, 40);
				}
			},
			mouseleave: function() {
				var $logo = $(this);
				// only animate on subsections
				if(lbox.section != 'home') {
					var	$icon = $logo.find('.icon'),
						endPosition = 0,
						currentPosition = $icon.css('top'),
						pixelDistance = Math.abs(parseInt(endPosition, 10) - parseInt(currentPosition,10)),
						pixelsPerSecond = 100,
						duration = parseInt(pixelDistance / pixelsPerSecond * 800, 10);
					;
					if(typeof(lbox.timer) != 'undefined') {
						clearTimeout(lbox.timer);	
					}
					lbox.timer = setTimeout(function() {
						if($icon.animated()) {
							$icon.stop();	
						}
						$icon.animate({ top: endPosition+'px' }, duration, 'easeOutQuad');
					}, 40);
				}
			}
		},
		home: {
			// board of stats (top right section)
			stats: {
				arrows: {					
					// move stats left
					left: {
						click:  function() {
							var $boardItems = $('#content .board li');
							if(!$(this).hasClass('disabled') && !$boardItems.animated()) {
								var $activeItem = $boardItems.filter('.active'),
									activeSpot = $boardItems.index($activeItem),
									stats = lbox.handler.home.stats,
									$rightArrow = $(this).siblings('.right'),
									newSpot = activeSpot - 1
								;
								if(newSpot >= 0) {
									if(newSpot == 0) {
										$(this)
											.removeClass('hover down')
											.addClass('disabled')


										;	
									}
									$activeItem
										.animate({ opacity: 0}, 200, function() {
											$(this).removeAttr('style');	
										})
										.removeClass('active')
									;
									$boardItems.eq(newSpot)
										.animate({ opacity: 1}, 200, function() {
											$(this)
												.removeAttr('style')
												.addClass('active')
											;	
										})
									;
									$rightArrow.removeClass('disabled');
									stats.updateCount(newSpot+1);
								}
							}
							return false;
						}
					},
					// move stats right
					right: {
						click: function() {
							var $boardItems = $('#content .board li');
							if(!$(this).hasClass('disabled') && !$boardItems.animated()) {
								var $activeItem = $boardItems.filter('.active'),
									listSize = $boardItems.size(),
									activeSpot = $boardItems.index($activeItem),
									stats = lbox.handler.home.stats,
									$leftArrow = $(this).siblings('.left'),
									newSpot = activeSpot + 1
								;
								if(newSpot < listSize) {
									if(newSpot == (listSize - 1)) {
										$(this)
											.removeClass('hover down')
											.addClass('disabled')
										;	
									}
									$activeItem
										.animate({ opacity: 0}, 200, function() {
											$(this).removeAttr('style');	
										})
										.removeClass('active')
									;
									$boardItems.eq(newSpot)
										.animate({ opacity: 1}, 200, function() {
											$(this)
												.removeAttr('style')
												.addClass('active')
											;	
										})
									;
									$leftArrow.removeClass('disabled');
									stats.updateCount(newSpot+1);
								}
							}
							return false;
						}
					}
				},
				
				// update stats 'x of y' position
				updateCount: function(value) {
					var $count = $('#content .stats .count span');
					$count.html(value);
				}
			}
		},
		fun: {
	
			autoPlay: false,
			seek: 1,
			totalVideos: 0,
			archiveOn: false,
			$currentVideo: false,
			hasUsed: false,
			expanded: false,
			totalVideos: $('#content .box').size(),
			
			setVideo: function(id){
				var $video = $('#video-'+id),
					$videoTitle = $('.video-title'),
					$label = $video.find('.label'),
					$currentVideo = lbox.handler.fun.$currentVideo,
					position = $video.position(),
					title = 'LUNCHBOX WEEKLY TIMELAPSE VIDEO, '+ $label.html(),
					videoID = $video.attr('data-vimeo')
				;
				if($currentVideo){
					$currentVideo.removeClass('active');
					if(!lbox.handler.fun.hasUsed) {
						lbox.handler.fun.hasUsed = true;
						lbox.handler.fun.toggleBackButton('on');
					}
				}
				if($currentVideo != $video){
					$video
						.removeClass('hover')
						.addClass('active')
					;	
					lbox.handler.fun.$currentVideo = $video;
					
					//if its on row 2 open!
					if(position.top > 1){
						if(!lbox.handler.fun.archiveOn){
							lbox.handler.fun.showVideoArchive();
						}
					}
					//change title
					$videoTitle.html(title);
					
					//now load it
					lbox.handler.fun.loadVideo(videoID);
					lbox.handler.fun.setArrow();
				}
				lbox.handler.fun.seek = id;
			},
			nextVideo: function(){
				lbox.handler.fun.seek++;
				
				if(lbox.handler.fun.seek > lbox.handler.fun.totalVideos){
					lbox.handler.fun.seek= 1;
					if(lbox.handler.fun.archiveOn){
						lbox.handler.fun.hideVideoArchive();
					}
				}
				lbox.handler.fun.setVideo(lbox.handler.fun.seek);
			},
			prevVideo: function(){
				lbox.handler.fun.seek--;
				if(lbox.handler.fun.seek == 0){
					lbox.handler.fun.seek=lbox.handler.fun.totalVideos;
					//going back to last..should open
					if(!lbox.handler.fun.archiveOn){
						lbox.handler.fun.showVideoArchive();
					}
				}
				lbox.handler.fun.setVideo(lbox.handler.fun.seek);
			},
			toggleBackButton: function(toggleMode){
				var $backButton = $('#back-button');				
				if(toggleMode == 'off'){
					$backButton
						.css('opacity', 0.5)
						.find('a')
							.hide()
					;
				}else{
					$backButton
						.css('opacity', 1)
						.find('a')
							.show()
					;
				}
			},
			toggleExpandVideo: function(){
				var $feature = $('#feature');
				if(!lbox.handler.fun.expanded) {
					lbox.handler.fun.expanded = true;
					$feature
						.removeClass('normal')
						.addClass('expanded')
					;
					lbox.handler.fun.setArrow();
				}
				else {
					lbox.handler.fun.expanded = false;
					$feature
						.removeClass('expanded')
						.addClass('normal')
					;
					lbox.handler.fun.setArrow();
					
				}
			},
			setArrow: function(){
					var $currentVideo = lbox.handler.fun.$currentVideo,
						$arrow = $('#arrow'),
						offset = $currentVideo.offset(),
						half = ($currentVideo.width() / 2) - 6,
						scrollTop = $('html').scrollTop()
					;
					$arrow
						.css({
							top: (offset.top - 7) - scrollTop,
							left: offset.left+half
						})
					;
					lbox.handler.fun.checkArrow();
					
			},
			checkArrow: function(){
				var $currentVideo = lbox.handler.fun.$currentVideo,
					$arrow = $('#arrow')
				;
				if($currentVideo){
					var position = $currentVideo.position();
				
					if(position.top > 0){
						if(!lbox.handler.fun.archiveOn){
							$arrow.hide();
						}
						else{
							$arrow.show();
						}
					}
					else{
						$arrow.show();
					}
				}
			},
			showVideoArchive: function(){
				var $archiveOff = $('#archive-off'),
					$tinyHeader = $('.tiny-header'),
					$archiveOn = $('#archive-on')
				;
				lbox.handler.fun.archiveOn = true;
				
				$archiveOff.hide();
				$tinyHeader.removeClass('th-color');
				$archiveOn
					.find('a')
						.css('background-position','0px -300px')
						.end()
					.show()
				;
		
				lbox.handler.fun.placeVideoBoxes();
			},
			hideVideoArchive: function(){
				var $archiveOff = $('#archive-off'),
					$archiveOn = $('#archive-on')
				;
				lbox.handler.fun.archiveOn= false;
				$archiveOff.show();
				$archiveOn.hide();
				lbox.handler.fun.placeVideoBoxes();
			},
			loadVideo: function(vimeo){
				var 
					$video = $('#holder .video-container'),
					useAuto = lbox.handler.fun.autoPlay
						? 1 
						: 0
				;
				$video.html('<iframe src="http://player.vimeo.com/video/'+vimeo+'?autoplay='+useAuto+'&title=0&byline=0&portrait=0&color=55B37F"></iframe>');
			},
			placeVideoBoxes: function(){
				var itemWidth = 125,
					itemHeight = 125,
					$videos = $('#content .box'),
					$holder = $('#holder'),
					$funFeature = $('#fun #feature'),
					$content = $('#content'),
					$archiveButton = $('#archive-button'),
					itemsPerRow = 6,
					contentWidth = $('#content').width() - 6,
					windowWidth = $(window).width()
				;
								
				if(contentWidth > 1100){
					itemsPerRow = 9;
				}
				else if(contentWidth > 990){
					itemsPerRow = 8;
				}
				else if(contentWidth > 860){
					itemsPerRow = 7;
				}
				else {
					itemsPerRow = 6;
				}
				itemWidth = Math.ceil(contentWidth / itemsPerRow);
			
				// set starting position
				var row = 1;
				var rowPosition = 1;
				
				$videos
					.each(function(zeroIndex) {
						var $video = $(this),
							id = zeroIndex + 1,
							offsetTop = (row * itemHeight) - itemHeight,
							offsetLeft = (itemWidth * rowPosition) - itemWidth
						;
						$video
							.css({
								top: offsetTop,
								left: offsetLeft,
								width: itemWidth
							})
							.find('label')
								.html('Box #' + id)
						;
						// hack for seamless right side
						if(lbox.handler.fun.archiveOn){
							// last one
							if(rowPosition == itemsPerRow){
								$video.css('width', contentWidth - offsetLeft + 'px');
							}
						}
						
						if(rowPosition == itemsPerRow) {
							rowPosition = 1;
							// move it down to left
							if(row == 1){
								if(!lbox.handler.fun.archiveOn){
									rowPosition++;
									$video.css('left', '0px');
								}								
								$archiveButton.css('left', offsetLeft +'px');
							}
							row++;
							if(!lbox.handler.fun.archiveOn){
								$video.css('top', (row * itemWidth) - itemWidth);
							}
						} 
						else {
							rowPosition++;
						}
					})
				;
				lbox.handler.fun.checkArrow();
				if(lbox.handler.fun.archiveOn) {
					console.log(row);
					
					$content.css('height', row * itemHeight);	
					$archiveButton.css('top', (row * itemHeight) - itemHeight);
				}
				else {
					$content.css('height', itemHeight);
					$archiveButton.css('top', '0px');
				}
				lbox.handler.fun.setArrow();
			},
			// onwebsite resize
			resize: function() {
				if(lbox.section == 'fun') {
					lbox.handler.fun.placeVideoBoxes();
				}
			}
		},
		originalContent: {
			returnLink: {
				click: function() {
					var $link = $(this);
					var $video = $('#feature .video');
					$video.fadeOut(400, function(){ $("#video-player").html('<div id="video-player-seo"></div>')});
					return false;
				}
			},
			videoLink: {
				click: function() {
					var $link = $(this);
					var $video = $('#feature .video');
					// grab metadata
					var flvURL = $(this).parents("div.module").find('.flv').html();
					var flvTitle = $(this).parent("div.module").find('.title').html();
					var fadeDuration = 0;
					
					// embed swf
					if(lbox.iPad || lbox.iPhone) {
						// don't need entire url for youtube embed
						var flvURL = flvURL.replace('http://www.youtube.com/watch?v=','');
						var embedCode = '\n' +
							'<object width="640" height="365" border="0">' + '\n' +
							'	<param value="http://www.youtube.com/v/'+flvURL+'?rel=0&amp;fs=1&amp;disablekb=1&amp;autoplay=0&amp;hd=1&amp;showsearch=0&amp;iv_load_policy=3&amp;showinfo=0" name="movie">' + '\n' +
							'	<param value="true" name="allowFullScreen">' + '\n' +
							'	<param value="opaque" name="wmode">' + '\n' +
							'	<param value="#000000" name="bgcolor">' + '\n' +
							'	<param value="always" name="allowScriptAccess">' + '\n' +
							'	<embed width="640" height="363" border="0" bgcolor="#000000" wmode="opaque" allowscriptaccess="always" allowfullscreen="true" type="application/x-shockwave-flash"src="http://www.youtube.com/v/'+flvURL+'?rel=0&amp;fs=1&amp;disablekb=1&amp;autoplay=0&amp;hd=1&amp;showsearch=0&amp;iv_load_policy=3&amp;showinfo=0">' + '\n' +
							'</object>'
						;
						$('#video-player').html(embedCode);
					}
					else {
						var fadeDuration = 400;
						var config = {
							embed: 'video-player-seo',
							src: 'swf/videoplayer.swf',            
							width: '640',
							height: '395',
							flashVersion: '9.0.0',
							expressInstall: 'swf/express-install.swf',
							
							vars: { flv_url: flvURL, flv_title: flvTitle },
							params: { scale: 'scale', menu: 'false', wmode: 'opaque', allowFullscreen: true, allowscriptaccess: 'always' },
							attributes: { id: 'video-player-seo',    name: 'video-player-seo' }
						};
						swfobject.embedSWF(config.src, config.embed, config.width, config.height, config.flashVersion, config.expressInstall, config.vars, config.params, config.attributes);
					}
					$video.fadeIn(fadeDuration);					
					return false;
				}
			}
		},
		services: {
			anchorLinks: function() {
				var href = $(this).attr('href');
				// if anchor present
				if(href.search('#') != -1) {
					var anchorTag = href.split('#')[1];
					var scrollX = $('a[name='+anchorTag+']').offset().top;
					$('html, body').animate({scrollTop: scrollX}, 700, 'easeInOutQuad', function() {
						// add hash
						window.location.hash = '#'+anchorTag;	
					});
					return false;
				}
			}
		},
		results: {
			keyboard: {
				keydown: function(event) {
					var key = event.keyCode;
					// alt key is used for alt+left/right history shortcuts
					// ignore events when alt is fired
					if(!event.altKey) {
						var $leftArrow = $('#content .arrows .left');
						var $rightArrow = $('#content .arrows .right');
						// left arrow
						if(key == 37) {
							$leftArrow.trigger('mousedown');
						}
						// right arrow
						if(key == 39) {
							$rightArrow.trigger('mousedown');						
						}
					}
				},
				keyup: function(event) {
					var key = event.keyCode;
					if(!event.altKey) {
						var $leftArrow = $('#content .arrows .left');
						var $rightArrow = $('#content .arrows .right');
						// left arrow
						if(key == 37) {
							$leftArrow.trigger('mouseup');
						}
						// right arrow
						if(key == 39) {
							$rightArrow.trigger('mouseup');				
						}
					}
				}
			},
			arrows: {
				click: function() {
					var $arrow = $(this);
					var $arrows = $('#content .arrows .left, #content .arrows .right');
					var imageIndex = (typeof lbox.activeImage != 'undefined') 
						? lbox.activeImage
						: 0
					;
					var lastIndex = $(".thumbs ul").children().size() - 1;
					imageIndex = ($arrow.hasClass('left')) 
						? (imageIndex - 1)
						: (imageIndex + 1)
					;
					
					imageIndex = (imageIndex < 0) ? 0 : imageIndex;
					imageIndex = (imageIndex > lastIndex) ? lastIndex: imageIndex;
					lbox.imageSlide(imageIndex);
					// record new thumb position after move
					lbox.activeImage = imageIndex;		
					// update disabled css on mouseup
					$arrow.one(lbox.handleUp, function() {
						if(imageIndex == 0){
							$arrows.filter('.left').addClass("disabled");
							$arrows.filter('.right').removeClass("disabled");
						} 
						else if(imageIndex == lastIndex){
							$arrows.filter('.left').removeClass("disabled");
							$arrows.filter('.right').addClass("disabled");
						}
						else {
							$arrows.removeClass('disabled');
						}
					});
										
					return false;
										
				}
			},
			thumbs: {
				mousedown: function(){
					var $thumb = $(this);
					var $arrows = $('#content .arrows .left, #content .arrows .right');
					var imageIndex =  $thumb.parents("ul").find('li').index($thumb.parent());
					var lastIndex = $(".thumbs ul").children().size() - 1;
					if(imageIndex != lbox.activeImage) {
						lbox.imageSlide(imageIndex);
					}
					if(imageIndex == 0){
						$arrows.filter('.left').addClass("disabled");
						$arrows.filter('.right').removeClass("disabled");
					} 
					else if(imageIndex == lastIndex){
						$arrows.filter('.left').removeClass("disabled");
						$arrows.filter('.right').addClass("disabled");
					}
					else {
						$arrows.removeClass('disabled');
					}
					lbox.activeImage = imageIndex;					
					return false;
				}	
			}
			
		},
		careers: {
			
			accordionHeader: {
			
				click: function() {
					var $headers = $('#content .job > h3');
					var $header = $(this);
					var $listing = $header.next('.listing');
					var $activeListing = $('#content .job h3.active').next('.listing');
					var animationDuration = (lbox.iPad || lbox.iPhone) ? 0 : 400;
					
					
					if(!$header.hasClass('active')) {
						$headers.removeClass('active');
						$header.addClass('active');
						$listing
							.protectAnimation()
							.slideDown(animationDuration, 'easeOutQuad')
						;
						$activeListing.slideUp(animationDuration, 'easeOutQuad');
					}
					else {
						$header.removeClass('active');
						$listing
							.slideUp(animationDuration, 'easeOutQuad')
						;	
					}
					return false;	
				}			
				
			}
				
		}
	};

	lbox.section = lbox.findSection();
	
	// Global logo action
	var $logo = $('header .logo');
	$logo
		.bind('mouseenter', lbox.handler.logo.mouseenter)
		.bind('mouseleave', lbox.handler.logo.mouseleave)
		.hoverClass()
		.followLink()
	;

	if(lbox.section == 'home') {
		
		var handler = handler.home;
		
		var $statArrows = $('#content .stats .arrows .left, #content .stats .arrows .right');
		var $text = $('#content .who p');
		
		// prevent text widows
		$text.widont(true);
		// navigate with stat arrows
		$statArrows
			.hoverClass({ filter: '.disabled' })
			.downClass({ filter: '.disabled' })
				.filter('.left')
					.bind(handleClick, handler.stats.arrows.left.click)
					.end()
				.filter('.right')
					.bind(handleClick, handler.stats.arrows.right.click)
		;
		
		
		// ipad magic scroll (clean this up later)
		if(lbox.iPad || lbox.iPhone) {
			var $feature = $('#feature');
			var $marquee = $feature.find('.marquee ul');
			var $marqueeImages = $marquee.find('img');
			
			// add ipad class to feature
			$feature.addClass('ipad');
			
			// follow link to case studies
			$marqueeImages.click(function(){
				window.location = 'results/';
				return flase;
			});
			
			var $lastLi = $marquee.children(':last-child');
			// Add scroller after all images load
			var width = $marqueeImages.size() * 1200;
			$marquee.css('width', width);
			var marquee = $marquee[0];
			marquee.addEventListener('touchmove', function(e){ e.preventDefault(); });
			var myScroll = new iScroll(marquee, { hScrollbar: true, vScrollbar: false, snap: true });
		}
		
	}
	
	if(lbox.section == 'services') {
		
		var handler = handler.services;
		
		var $anchorLinks = $('#feature a');
		var $text = $('#feature li a, #content .text p');
		
		// smooth scroll anchor links
		$anchorLinks.click(handler.anchorLinks);
		// prevent text widows
		$text.widont(true);
	
	}
	if(lbox.section == 'original-content') {
		
		var handler = handler.originalContent;
		
		var $text = $('#feature .text p');
		var $videoLinks = $('#feature .text .module ul li:first-child a');
		var $videoThumb = $('#reels .soundtrack');
		var $returnLink = $('#feature .video h3');
		var $images = $('#feature .images');
		var $backgroundImages = $images.children();
		var $fullvideo = $("#lbox-video");
		
		$returnLink
			.click(handler.returnLink.click)
		;
		$videoLinks
			.hoverClass()
			.downClass()
			.click(handler.videoLink.click)
		;
		$videoThumb
			.smoothHover(200)
			.smoothDown(0, {
				click: function() {
					$videoLinks.eq(0).click();
				}
			})
		;
		
		// precache background images and video bg for smooth fades
		var precacheImages = [ 'media/images/video-bg.jpg' ];
		$backgroundImages.each(function() {
			var backgroundImage = $(this).css('background-image').replace('url("','').replace('")', '');
			precacheImages.push(backgroundImage);
		});
		lbox.precache(precacheImages);
		
		// cycle header images
		var crossfadeDuration = 
			(lbox.iPad || lbox.iPhone)
			? 0
			: 1000
		;
		lbox.cycler($images, crossfadeDuration, 5000);
		// prevent text widows
		$text.widont(true);
		
	}
	
	if(lbox.section == 'results') {
		
		var handler = handler.results;
		
		var $projects = $('#content .projects li');
		var $arrows = $('#content .arrows .left, #content .arrows .right');
		var $thumbs = $(".thumbs ul li img");
		
		
		$projects
			.hoverClass()
			.touchClass()
			.followLink()
		;

		
		$arrows
			.hoverClass()
			.downClass()
			.bind(handleDown, handler.arrows.click)
		;
		
		$(window).bind('keydown', handler.keyboard.keydown);
		$(window).bind('keyup', handler.keyboard.keyup);
		
		$thumbs.bind('mousedown', handler.thumbs.mousedown);
		
		// ipad magic scroll
		if(lbox.iPad || lbox.iPhone) {
			var $marqueeNavigation = $('#content .arrows, #content .thumbs');
			var $marquee = $('#content .marquee ul');
			var $marqueeImages = $marquee.find('img');
			var $lastLi = $marquee.children(':last-child');
			// hide navigation for iPad
			$marqueeNavigation.hide();
			// Add scroller after all images load
			lbox.imagesLoaded = 0;
			$marqueeImages.bind('load', function() {
				lbox.imagesLoaded++;
				if(lbox.imagesLoaded == $marqueeImages.size()) {
					var width = $lastLi[0].offsetLeft;
					$marquee.css('width', width);
					var marquee = $marquee[0];
					marquee.addEventListener('touchmove', function(e){ e.preventDefault(); });
					var myScroll = new iScroll(marquee, { hScrollbar: true, vScrollbar: false });
				}
			});
		}
	}

	
	if(lbox.section == 'careers') {
		
		var handler = handler.careers;
		
		var $jobHeaders = $('#content .job > h3');
		var $applyButton = $('#content .apply');
		
		$jobHeaders
			.hoverClass()
			.click(handler.accordionHeader.click)
		; 
		
		$applyButton
			.each(function() {
				var $button = $(this);
				$button
					.smoothHover(200)
					.smoothDown(0, { click: function() {
						var linkURL = $button.find('a').eq(0).attr('href');
						window.location = linkURL;
						return false;
					}})
				;
			})
		;
	}
	
	if(lbox.section == 'fun'){
		
		var handler = handler.fun;
		
		console.log(handler);
		
		var $contentBox = $('#content .box'),
			$archiveButton = $('#archive-button'),
			$expandText = $('#expand-text')
		;
				
		$contentBox
			.bind('click', function() {
				var video = $(this).attr('data-id');
				handler.setVideo(video);
				return false;
			})
			.bind('mouseenter', function() {
				if(!$(this).hasClass('active')){
					$(this).addClass('hover');
				}
			})
			.bind('mouseleave', function() {
				if(!$(this).hasClass('active')){
					$(this).removeClass('hover');
				}
			})
		;
		
		$expandText
			.bind('click', handler.toggleExpandVideo)
		;
		
		$archiveButton
			.bind('click', function(){
				if(!handler.archiveOn){
					handler.showVideoArchive();
				}
				else{
					handler.hideVideoArchive();
				}
				
			})
			.bind('mouseleave', function() {
				var $tinyHeader = $('.tiny-header'),
					$archiveOff = $('#archive-off a'),
					$archiveOn = $('#archive-on a')
				;
				$tinyHeader.removeClass('th-color');
				if(!handler.archiveOn){
					$archiveOff.css('background-position','0px 0px');
				}
				else {
					$archiveOn.css('background-position','0px -300px');
				}
			})
			.bind('mouseenter', function() {
				var $tinyHeader = $('.tiny-header'),
					$archiveOff = $('#archive-off a'),
					$archiveOn = $('#archive-on a')
				;
				$tinyHeader.addClass('th-color');
				if(!handler.archiveOn) {
					$archiveOff.css('background-position','0px -100px');
				}
				else {
					$archiveOn.css('background-position','0px -400px');
				}
			})
			.mousedown(function(){
				var $tinyHeader = $('.tiny-header'),
					$archiveOff = $('#archive-off a'),
					$archiveOn = $('#archive-on a')
				;
				$tinyHeader.addClass('th-color');
				if(!handler.archiveON){
					$archiveOff.css('background-position','0px -200px');
				}else{
					$archiveOn.css('background-position','0px -500px');
				}
			})
		;
		$(window)
			.bind('resize', handler.resize)			
		;
		$(window)
			.bind('scroll', handler.setArrow)
		;

		
		handler.setVideo(1);
		handler.resize();
		handler.toggleBackButton('off');
		
	}
	
}


$(document).ready(lbox.readyEvent); 


