/*
	'DOM Ready' code
*/
$(document).ready(function() {

	var eq = 1;
	$('.slide-container .image img').css('opacity', eq).hover(function() {
		if( !lookbook.goLeft() && !lookbook.goRight() && !$(this).parents('.slide-container').hasClass('open') ) {
			$(this).fadeTo('normal', 0.6);	
		}
	}, function() {
		if( $(this).css('opacity') != eq ) {
			$(this).fadeTo('normal', eq);	
		}
	});
	
	// setup the slide-container cycle
	$('.slide-container').each(function(n) {
		$(this).addClass('slide_' + n);
	});
	
	// .slides-container
	$('.slides-container').fadeOut(0).fadeIn();
	
	// 'lookbook' class needs some information
	lookbook.total_items = $('.slide-container').length;

	// trigger initial 'lookbook draw' – maybe not.
	// $(window).trigger('resize');
	
	// cufon
	apply_cufon();
	
	// this .image click
	$('.slide-container .image img').click(function() {
		$(this).trigger('mouseout');
		
		var $slide_container = $(this).parents('.slide-container').eq(0);
		
		// prevent others from being open
		$slide_container.addClass('this');
		$('.slide-container.open:not(.this) .image img:first').trigger('click');
		$slide_container.removeClass('this');
		// end prevention
		
		// and everthing is on a toggle
		$slide_container.toggleClass('open');
		
		// you gotta draw 'before' or 'after' depending on the situation.
		if( $slide_container.hasClass('open') ) {
			
			var className = '.' + $slide_container.get(0).className.split(' ')[1];
			
			$slide_container.find('.image:not(.cycled)').addClass('cycled').cycle({
				fx: 'scrollHorz',
				speed: 500,
				next: className + ' .change_view',
				// pager: className + ' .image-cycle',
				// pagerAnchorBuilder: function(idx, slide) { 
				//     return className + ' .image-cycle a:eq(' + idx + ')'; 
				// }
				before: function() {
					$slide_container.find('.change_view img').toggle();
				}
			});
			
			$slide_container.find('.image').cycle('pause');
			
			// lookbook.draw();
			$slide_container.css('width', lookbook.getWidth()).animate({
				'width': lookbook.getWidth() * 2
			}, function() {
				$slide_container.find('.slide').fadeIn('normal', function() {
					lookbook.draw();
				});
				
				// hide 2nd images.
				$slide_container.find('.change_view img:first-child').hide();
				$slide_container.find('.change_view img:not(:first-child)').show();
				
				apply_cufon();
			});
			
		} else {
			
			$slide_container.find('.slide').fadeOut('normal', function() {
				$slide_container.animate({
					'width': lookbook.getWidth()
				}, 'normal', function() {
					$(this).css('width', null); // unset the css('width') - super cool!
					// lookbook.draw();
				});				
			});
			
		}
	}); // end .click()
	
	// and the controls button
	$('.slide-container .controls a.close').click(function() {
		$(this).parents('.slide-container').eq(0).find('div.image img:first').trigger('click');
	})
	
	$('.slide-container .controls a.prev').click(function() {
		var prev = $(this).parents('.slide-container').eq(0).prev();
		
		if( prev.length > 0 ) {
			prev.find('div.image img:first').trigger('click');	
		} else {
			$('.slide-container:last div.image img:first').trigger('click');
		}
		
	});
	
	$('.slide-container .controls a.next').click(function() {
		
		var next = $(this).parents('.slide-container').eq(0).next();
		if( next.length > 0 ) {
			next.find('div.image img:first').trigger('click');
		} else {
			$('.slide-container:first div.image img:first').trigger('click');
		}
		
	});
	
	// testing. http://www.hulu.com/watch/102975/saturday-night-live-what-up-with-that
	// $('.slide-container:last .image a').trigger('click');
	
	lookbook.draw();

});

// keybind
$(document).bind('keydown', 'left', function() {
	$('.slide-container.open .prev').trigger('click');
	return false;
}).bind('keydown', 'right', function() {
	$('.slide-container.open .next').trigger('click');
	return false;
})


/*
	'resize'() code
*/
$(window).resize(function() {	
	
	// draw for the first time
	lookbook.draw();
	
	// just handle the max-width & max-height issues
	if( $(window).width() < 600 || $(window).height() < 300 ) {
		$('body').css('overflow', 'auto');
	} else {
		$('body').css('overflow', 'hidden');
	}
	
}).load(function() {
	// $(window).trigger('resize');
});



/*
	'mousemove' code
*/
$(document).mousemove(function(e) {
	
	// handles movement
	lookbook.percentage_left = Math.ceil( (e.clientX / $(window).width()) * 100 );
	lookbook.percentage_top = Math.ceil( (e.clientY / $(window).height()) * 100 );
	
	if( lookbook.goRight() ) {
		lookbook.go('right');
	} else if( lookbook.goLeft() ) {
		lookbook.go('left');
	} else {
		lookbook.go('nowhere');
	}
	
});





			// some empty space
			// 
			// its for Scott




/*
	The lookbook class
*/
var lookbook = {
	/*
		Accessory functions / variable
	*/
	
	percentage_to_block_off: 10,
	
	goLeft: function() {
		return ( lookbook.percentage_left < lookbook.percentage_to_block_off );
	},
	
	goRight: function() {
		return ( lookbook.percentage_left > (100-lookbook.percentage_to_block_off) );
	},
	
	getHeight: function( width ) {
		return Math.ceil( width * ( 7/18 ) );
	},
	
	getWidth: function( height ) {
		if( !height ) { height = lookbook.getSlideHeight(); }
		return Math.ceil( height * ( 7/18 ) );
	},
	
	getSlideWidth: function() {
		return ( lookbook.getWidth( lookbook.getSlideHeight() ) * lookbook.total_items ) + ( $('.slide-container.open').length * lookbook.getWidth( lookbook.getSlideHeight() ) );
	},
	
	getSlideHeight: function() {
		return $(window).height() - 86;
	},
	
	/*
		'draw' code
	*/
	draw: function() {		
		$('.slides-container, .slide-container .image, .slide-container .image img, .slide-container .slide')
			.height( lookbook.getSlideHeight() )
			.width( lookbook.getWidth( lookbook.getSlideHeight() ) );

		$('.slides-container').width( lookbook.getSlideWidth() + lookbook.getWidth() );
		
		if( $('.slide-container.open').length > 0 ) {
			$('.slide-container.open').width( lookbook.getWidth() * 2 );
			
			var $open_container = $('.slide-container.open');
			var $slides_container = $('.slides-container');
			
			var offset_left = $open_container.position().left;
			var open_width = lookbook.getWidth() * 2;
			
			var move_to = -1 * ( ( offset_left + ( open_width / 2 ) ) - ( $(window).width() / 2 ) );
			
			if( move_to < 0 && move_to > ( -1 * ( lookbook.getSlideWidth() - $(window).width() ) ) ) {
				$slides_container.animate({
					'left': move_to
				});	
			}
			
			if( move_to >= 0 ) {
				$slides_container.animate({
					'left': 0
				});
			}
			
			if( move_to < ( -1 * ( lookbook.getSlideWidth() - $(window).width() ) ) ) {
				$slides_container.animate({
					'left': ( -1 * ( lookbook.getSlideWidth() - $(window).width() ) )
				});
			}
		}
	},
	
	/*
	 'go animate' code
	*/
	go_: {
		mode: 'nowhere',
		animate: false
	},
	
	go: function( mode ) {
		// this stops the animation to be triggered more than once.
		if( mode == lookbook.go_.mode ) {
			lookbook.go_.mode = mode; // just in case 
			return false;
		}
		
		// time management
		var time = 1000;
		
		var $slideshow = $('.slides');
		lookbook.go_.mode = mode;
		
		var fx = {
			type: 'left',
			delay: -10,
			step: 10
		}
		
		// console.log( 'going: ' + mode );
		
		switch( mode ) {
			case 'right':
				lookbook.go_.animate = $fx( $('.slides-container').get(0) );
				
				fx.to = ( -1 * ( $('.slides-container').width() - $(window).width() - lookbook.getWidth() ) );
				fx.step = fx.step * -1;
				
				lookbook.go_.animate.fxAdd(fx);
				lookbook.go_.animate.fxRun();
				
			break;
			
			case 'left':
				lookbook.go_.animate = $fx( $('.slides-container').get(0) );
				
				fx.to = 0;
				fx.step = fx.step * 1;
				
				lookbook.go_.animate.fxAdd(fx);
				lookbook.go_.animate.fxRun();
			break;
			
			default:
				lookbook.go_.animate.fxStop();
			break;
		}
	}
	
}

var i = 0;
