	/* preload some images */
	
	var preloadImages = function () {

		var imgArray =[]
		
		for (i = 1; i <= 5; i++ ) {
			imgArray[i] = new Image();
			imgArray[i].src = "/wp-content/themes/njgibsontheme/images/part_"+i+".png"
		}
	} ();
	
	
	YUI().use('anim', 'node', 'async-queue', function(Y) {
		
		Y.namespace('njgibson');
		
		Y.njgibson.indexPage = function() {
			
			var _move_particle = function(elem){
				
					if(elem){ 
						var	elemID	= '#'+elem.get('id'),
							nodeX	= elem.getX(),
							nodeY	= elem.getY(),
							part_anim;
							
						if(elemID && nodeX && nodeY){
							part_anim = new Y.Anim({
								node: elemID,
								to: {
									curve: _randomCurve(elem,[nodeX, nodeY])
								},
								duration: _randomDuration(),
								easing: Y.Easing.easeBoth
							});
							part_anim.on('end', function(e){
								part_anim = null;
								_move_particle(e.currentTarget._node);
							},this);
							part_anim.run();
						}
					}
			};
			
			var _randomCurve = function(node, end) {
		        var points	= [],
					i,
					n = 3,
					rand_1,
					rand_2;
					
				rand_1 = Math.round((Math.random() * 35)*12);
				rand_2 = Math.round((Math.random() * 45)*12);
	
				for(i=0;i<3;i++ ){				
					if(!i%2){
						rand_1 = -350 + rand_1;
						rand_2 = -350 + rand_2;
					}
					points.push([end[0] + rand_1, end[1] + rand_2,end[0] + rand_2,end[1] + rand_1]);	
				} 
				points.push(end); // back to start
				return points;
			};
			
			var _randomDuration = function() { 
				var duration = Math.round((Math.random() * 15)+11);
				return duration;
			};
			
			var _fade = function(direction, duration, id, again) {
				var _fade_anim,
					cfg = {
						'node': id,
						'duration': duration
					};
				switch (direction) {
					case 'up':
						cfg.to = {
							opacity: 1
						};
						cfg.from = {
							opacity: 0
						};
						break;
					case 'down':
						cfg.to = {
							opacity: 0
						};
						cfg.from = {
							opacity: 1
						};
						break;
					default:
						break;
				}
				_fade_anim = new Y.Anim(cfg);
				_fade_anim.run();
			};
					
		return {
			
			createParticleContainers: function () {
				
				var container			= Y.Node.create('<div id="particles"></div>');
				var particles_container	= Y.Node.create('<div id="part_container"></div>');

				for(index = 1;index <=5 ;index++){
					particles_container.append("<img id='paricle_"+index+"_pic' class='part' src='/wp-content/themes/njgibsontheme/images/part_"+index+".png' title='paricle pic "+index+"' alt='paricle pic "+index+"' />")
				}
				container.append(particles_container);
				Y.one('#main_pics').append(container);
			},		
			particles_setup: function() {	
				var container						= Y.one('#particles');
				var particles_container				= Y.one('#part_container');
				var particles_container_partciles	= particles_container.all('img.part');
				
				// fade up all particles
				container.setStyle('display', 'block');
				_fade('up', 1.5, '#part_container');
				
				// move particles
				particles_container_partciles.each(function(particle){
					_move_particle(particle);
				}, this);
			},
				
			effects_setup: function(){
				
				if (this.DOM.bodyElem.get('id') === 'home') {
										
					Y.AsyncQueue.defaults.timeout = -1;
					var q = new Y.AsyncQueue();
					
					q.add(function(){
							
						var text_container_anim = null;
						var cfg_text_container = {
							node: '#text_container',
							to: {
								left: -10
							},
							from: {
								left: -100
							},
							duration: 3,
							easing: Y.Easing.easeBoth
						}; 
						
						
						

						cfg_text_container.to = {
							left: -10,
							opacity: 1
						};
						cfg_text_container.from = {
							left: -100,
							opacity: 0
						};
						
						
						text_container_anim = new Y.Anim(cfg_text_container);
						text_container_anim.on('end', function(e){
							text_container_anim = null;
							q.run();
						}, this);
						text_container_anim.run();
					});
					
					
					q.add(function(){
						q.pause();
						setTimeout(function(){
							var _fade_anim, cfg = {
							'node': '#header',
							'duration': 2,
							'to': {
								opacity: 1
							},
							'from': {
								opacity: 0
							}
						};
						_fade_anim = new Y.Anim(cfg);
						_fade_anim.on('end', function(e){
							q.run();
						}, this);
						_fade_anim.run();	
							
						},1000);
						
					}, function(){

						var _fade_anim, cfg = {
							'node': '#welcome_message',
							'duration': 1,
							'to': {
								opacity: 1
							},
							'from': {
								opacity: 0
							}
						};
						_fade_anim = new Y.Anim(cfg);
						_fade_anim.on('end', function(e){
							q.run();
						}, this);
						_fade_anim.run();
						
					},function(){
						q.pause();
						Y.one('#welcome_message_like').setStyle('display', 'block');
						var _fade_anim, cfg = {
							'node': '#welcome_message_like',
							'duration': 1,
							'to': {
								opacity: 1
							},
							'from': {
								opacity: 0
							}
						};
						_fade_anim = new Y.Anim(cfg);
						_fade_anim.on('end', function(e){
							q.run();
						}, this);
						_fade_anim.run();
					}, function(){
						setTimeout(function(){
						var _fade_anim, cfg = {
							'node': '#recent_projects',
							'duration': 1,
							'to': {
								opacity: 1
							},
							'from': {
								opacity: 0
							}
						};
						_fade_anim = new Y.Anim(cfg);
						_fade_anim.on('end', function(e){
							q.run();
						}, this);
						_fade_anim.run();
						},600);
					}, function(){
						q.pause();
						var _fade_anim, cfg = {
							'node': '#footer',
							'duration': 1,
							'to': {
								opacity: 1
							},
							'from': {
								opacity: 0
							}
						};
						_fade_anim = new Y.Anim(cfg);
						_fade_anim.on('end', function(e){
							q.run();
						}, this);
						_fade_anim.run();
					});
					

					q.add({
						fn: this.particles_setup,
						timeout: 150
					});
					
					q.run();
				}				
			},
			
			init: function () {
				Y.all('.invisible').removeClass('invisible');
				if(Y.UA.ie && Y.UA.ie <= 6) {
					Y.one('#welcome_message_like').setStyle('display', 'none');
					return false;
				}
				this.createParticleContainers();
				this.DOM = {};
				this.DOM.bodyElem = Y.one('body');
				this.DOM.digitalMediaPic = Y.one('#text_digitalmedia_pic');
				
				if(Y.one('body.firstTimeView')){
					this.DOM.digitalMediaPic.onLoad = this.effects_setup();
				} else {
					Y.one('#welcome_message_like').setStyle('display', 'block');
					this.particles_setup();
				}
			}
		};
	}; /* end namespace njgibson*/
	

	Y.on("domready", function(){
		window.indexPage = new Y.njgibson.indexPage();
		window.indexPage.init();
	});

});

/* jquery */
(function($){

	$(document).ready(function(){
		
		$('#work').masonry({
		    itemSelector : '.post',
		    columnWidth : 220
		  });
	});
  
  
})(jQuery);


