var videoplayer;

function gatherVids ( data ) {
	$(function(){
		videoplayer = '';
		videoplayer += '<div class="nydd-video-player clearfix">';
			videoplayer += '<h3 class="nydd-return">Return to videos</h3>';
			videoplayer += '<div id="nydd-vimeo-placeholder"></div>';
			videoplayer += '<div class="nydd-video-info clearfix">';
				videoplayer += '<h2 class="nydd-video-person nydd-title sub"></h2>';
				videoplayer += '<h3 class="nydd-video-position"></h3>';
				videoplayer += '<h3 class="nydd-video-place"></h3>';
				videoplayer += '<h3 class="nydd-video-site"><a href="#" target="_blank"></a></h3>';
			videoplayer += '</div>\n';
		videoplayer += '</div>\n';
		videoplayer = $(videoplayer);
		var vm = new VideoManager();
		vm.setContainer( $('.nydd-fiber-thumbs'), data );
	});
};

Array.prototype.randomize = function () {
	this.sort(
		function(a,b) {
			return Math.round( Math.random() );
		}
	);
};

var fulltrans = { opacity: 1 };
var halftrans = { opacity: .5 };

var fadein = function ( e ) {
	$(this).stop().animate( fulltrans );
};

var fadeout = function ( e ) {
	$(this).stop().animate( halftrans );
};

var Cheech = function () {
	var cheech = this;
	var _cheeched = false;
	var _currentID = 0;
	var indCheechTimer = null;
	
	var $indCheech = $('<div/>',{
		id: 'indCheech',
		css: {
			width: $(window).width(),
			height: $(window).height(),
			backgroundColor: '#000000',
			opacity: .85,
			zIndex: ( $.browser.msie == true && $.browser.version < 9 ) ? 0 : 10,
			position: 'fixed',
			top: 0,
			left: 0
		}
	});
	
	this.append = function () {
		if( !_cheeched ) {
			if( $.browser.msie == true && $.browser.version < 9) {
				return false
			} else {
				$indCheech.prependTo( 'body' );
				_cheeched = true;
			};
		};
	};
	
	this.startTimer = function () {
		indCheechTimer = setTimeout(cheech.remove, 150);	
	};
	
	this.killTimer = function () {
		clearTimeout( indCheechTimer );
	};
	
	this.remove = function () {
		if( _cheeched ) {
			$indCheech.detach();
			_cheeched = false;	
		};
		$('.info-window').stop().css('width', 0);
		clearTimeout( indCheechTimer );
	};
};

var VideoItem = function ( entry, i, cheech, manager ) {
	var video = this;
	var title = entry.title;
			
	var thumburl = entry.thumbnail.hqDefault;
	var thumb = new Image();
			
	thumb.src = thumburl;
	
	$(thumb).addClass('fiber-thumbs-image');
	
	this.index = i;
	this.url = entry.content[5];
	this.person = entry.description.split(' | ')[0];
	this.position = entry.description.split(' | ')[1];
	this.place = entry.description.split(' | ')[2].replace(/_/,'');
	this.site = entry.description.split(' | ')[3];
	this.slideOut = function () {
		
		cheech.killTimer();

		var pos = $entry.data('sliderPos');
		$entry.css('z-index', 12).siblings().css('z-index', 11);
		$infoWindow.animate({
			width: 197,
			left: $entry.data('sliderPos') == 'right' ? 197 : -197
		}, 175);
		
		manager.highlight( $entry );
		$(thumb).addClass( 'highlight' ).addClass( $entry.data('sliderPos') );
		
		
		cheech.append();
	};
	
	this.slideIn = function () {
		$entry.css('z-index', 11);
		$infoWindow.css({
			width: 0,
			left: $entry.data('sliderPos') == 'right' ? 197 : 0
		});
		cheech.startTimer();
		
		$(thumb).removeClass( 'highlight' ).removeClass( $entry.data('sliderPos') );
		
		manager.unlight( $entry );
	};
	
	this.onHoverIn = function ( e ) {
		video.slideOut();
	};
	
	this.onHoverOut = function ( e ) {
		video.slideIn();
	};
	
	this.playVideo = function ( e ) {
		$infoWindow.css('width', 0);
		manager.play( video.index );
	};
	
	var $entry = $('<li/>',{
		id: 'vid_'+i,
		html: thumb,
		css: {
			opacity: 0
		},
		data: {
			sliderPos: ( i % 3 < 2 ) ? 'right' : 'left'
		},
		mouseover: video.onHoverIn,
		mouseout: video.onHoverOut,
		click: video.playVideo
	}).delay( 450 + ( i * Math.random() * 100 ) ).animate({
		opacity: 1
	}, 1000).addClass('fiber-thumbs-thumbnail');
	
	$entry.find('img').css('opacity', .15);
	
	var $infoWindow = $( '<div/> ' ).addClass('info-window').css('left', $entry.data('sliderPos') == 'right' ? 197 : 0 ).appendTo( $entry );
	var infoHTML = '';
	infoHTML += '<div class="info-window-content '+$entry.data('sliderPos')+'">\n';
	infoHTML += '<h3 class="person">'+video.person+'</h3>\n';
	infoHTML += '<h3 class="position">'+video.position+'</h3>\n';
	infoHTML += '<h3 class="place">'+video.place+'</h3>\n';
	infoHTML += '<h3 class="site">'+video.site+'</h3>\n';
	$infoHTML = $(infoHTML).appendTo( $infoWindow );
	
	this.spaceout = function () {
		var hh = $infoWindow.height();
		var h1 = $infoWindow.find('.person').outerHeight() + $infoWindow.find('.position').outerHeight();
		var h2 = $infoWindow.find('.place').outerHeight() + $infoWindow.find('.site').outerHeight();
		
		var rh = hh - h1 - h2 - 5;
		
		$infoWindow.find('.place').css('margin-top', rh);
	};
	
	this.thumbnail = $entry;
	
	this.dim = function () {
		$entry.find('img').stop().animate({
			opacity: .15
		});
		$infoWindow.stop().css({width: 0});
	};
	
	this.undim = function () {
		$entry.find('img').stop().animate({
			opacity: 1
		});
	};
	
	this.conceal = function () {
		$(thumb).stop().animate({
			opacity: 0
		});
		$infoWindow.stop().css({
			opacity: 0
		});
	};
	
	this.reveal = function () {
		$(thumb).stop().animate({
			opacity: 1
		});
		$infoWindow.stop().css({
			opacity: 1
		});
	};
	
	this.lock = function () {
		$entry.unbind({
			mouseover: video.onHoverIn,
			mouseout: video.onHoverOut,
			click: video.playVideo
		}).css('cursor', 'default');
	};
	
	this.unlock = function () {
		$entry.bind({
			mouseover: video.onHoverIn,
			mouseout: video.onHoverOut,
			click: video.playVideo
		}).css('cursor', 'pointer');
	};
	
	this.lock();
};

var VideoPlayer = function ( controller, manager ) {
	player = this;
	var $return = controller.find('.nydd-return');
	var $vimeo = controller.find('#nydd-vimeo-placeholder');
	var $person = controller.find('.nydd-video-person');
	var $position = controller.find('.nydd-video-position');
	var $place = controller.find('.nydd-video-place');
	var $site = controller.find('.nydd-video-site');
	
	var stopit = function ( e ) {
		manager.stop();
		controller.detach();
	};
	
	$return.bind({
		click: stopit
	});
	
	this.play = function ( video ) {		
		$('.nydd-content').prepend( controller );
	
		$person.text( video.person );
		$position.text( video.position );
		$place.text( video.place );
		$site.children('a').attr('href', 'http://'+video.site).text( video.site );		
		
		swfobject.embedSWF(
			video.url,
			'nydd-vimeo-placeholder',
			'430',
			'320',
			'9.0.0',
			false,
			false,
			{ allowfullscreen: 'true' }
		);
	
	};
	
};

var VideoManager = function () {
	var videos = this;
	var items = [];
	var container;
	var feed = new Object();
	var entries = feed.items || [];
	var _playing = false;
	var CURRENT_VIDEO = -1;
	var lockTimer = null;
	this.cheech = new Cheech(); 
	this.player = new VideoPlayer( videoplayer, this );
	this.data = new Object();
	
	$('.nydd-watch-testimonials').bind({
		click: function ( e ) {
			$('.nydd-fiber-welcome').animate({
				opacity: 0
			}, 250, function () {
				$('.nydd-fiber-welcome').remove();
				revealAll();
			});
		}
	});
	
	this.setContainer = function ( c, d ) {
		container = c;
		feed = d;
		parseVideos();
	};
	
	var parseVideos = function ( ) {
		entries = feed.data.items;
		entries.randomize();
		
		createThumbnails();
	};
	
	var createThumbnails = function () {  
		var i;
		for( i = 0; i < entries.length; i++ ) {
			var item = new VideoItem( entries[i], i, videos.cheech, videos );
			items.push( item );
			item.thumbnail.appendTo( container );
			item.spaceout();
		};
	};
	
	this.play = function ( id ) {
		CURRENT_VIDEO = id;
		if( _playing == false ) {
			_playing = true;
			concealAll();
			var video = items[CURRENT_VIDEO];
			videos.player.play( video );
		};
	};
	
	this.stop = function ( e ) {
		CURRENT_VIDEO = -1;
		if( _playing == true ) {
			_playing = false;
			revealAll();			
		};
	};
	
	var concealAll = function () {
		var i;
		for( i = 0; i < items.length; i++ ) {
			items[i].conceal();
			items[i].lock();
		};
	};
	
	var revealAll = function () {
		var i;
		for( i = 0; i < items.length; i++ ) {
			items[i].reveal();
		};
		lockTimer = setTimeout( unlockAll, 1000 );
		container.bind({
			mousemove: killLockTimer
		});
	};
	
	var unlockAll = function () {
		container.unbind({
			mousemove: killLockTimer
		});
		var i;
		for( i = 0; i < items.length; i++ ){
			items[i].unlock();
		};
	};
	
	var killLockTimer = function ( e ) {
		clearTimeout( lockTimer );
		unlockAll();
	};
	
	this.highlight = function ( target ) {
		var i;
		for( i = 0; i < items.length; i++) {
			if( items[i].thumbnail.attr('id') != target.attr('id') )
				items[i].dim();
		};
	};
	
	this.unlight = function ( target ) {
		var i;
		for( i = 0; i < items.length; i++) {
			items[i].undim();
		};
	};
};

NYDDForm = function () {
	var form, required, submit, target, self;

	return {
		init: function ( obj ) {
			self = this;
			form = obj;
			target = form.attr('action');
			required = form.find('.required');
			required.bind('keyup', this.checknull);
			submit = $('.submit-information');
			submit.bind('click', this.ajax);
		},
		checknull: function ( e ) {
			var i = 0,
				l = required.length,
				invalid = true;

			for( i; i < l; i++ ) {
				if( required.eq( i ).val() == '' ) {
					invalid = false;
				}
			}

			if( invalid ) {
				submit.removeClass('inactive').attr('disabled', false);
			}
			else {;
				submit.addClass('inactive').attr('disabled', true);
			}
		},
		ajax: function ( e ) {
			e.preventDefault();
			e.stopImmediatePropagation();
			form.animate({
				opacity: .35
			}, 150);
			var data = form.serialize();
			$.post( target, data, self.dataProcessed );	
		},
		dataProcessed: function ( data ) {
			form.replaceWith( data );
		}
	}
};

$(function () {
	$('.nydd-nav-founders, .nydd-nav-supporters').find('img').css( halftrans ).bind({
		mouseenter: fadein,
		mouseleave: fadeout
	});
	
	$subs = $('.nydd-nav-sub');
	$subs.each( function( i, item ) {
		var _isactive = false;
		$subkids = $(this).find('.nydd-nav-sub-item');
		var c;
		for( c = 0; c < $subkids.length; c++ ) {
			if( $subkids.eq(c).children('a').hasClass('active') )
				_isactive = true;
		};
		if( !_isactive )
			$subs.eq(i).hide();
	});
	
	$tabs = $('.nydd-nav-tab').bind({
		click: function ( e ) {
			$(this).next('.nydd-nav-sub').toggle();
		}
	});
	
	var isvid = $('#initiatives').length > 0;
	
	if( $('.nydd-form').length > 0 ) {
		var form = new NYDDForm();
		form.init( $('.nydd-form') );
	}
});
