$(function () {
	$('body').addClass('js');
	willenium.init();
});
var willenium = {
	init: function(){
		this.scroller();
		this.cufonize('h1, #main h2, #luxaflex a.close span + span, #luxaflex .column h2, legend, .item .story h2, #navigation li a');
		this.projectInfo();
		this.gohome();
	},

	scroller: function() {
		var scrollable = $('#scrollable');
		if(scrollable.length >0){
			$(scrollable).find('.item:first').addClass('active');
		    $(scrollable).after('<div class="navigator"></div><div id="item-nav"><a href="#" class="prev">Previous</a><a href="#" class="next">Next</a></div>').scrollable({
		        onBeforeSeek: function(e, index){
		        	var previous = this.getIndex();
		        	var scrollContainerHeight = scrollable.height();
		        	// Adjust height
	            	if(index >= previous){ scrollable.height(scrollContainerHeight-286); 
	            	}else{ scrollable.height(scrollContainerHeight+286); }
		        },
		        onSeek: function (e, index) {
		        	var items = this.getItems();
		            items.removeClass('active');
		            items.eq(index).addClass('active');
		        },
		        vertical: true,
		        mousewheel: true
		    }).navigator({
		        navi: '.navigator'
		    });
			var api = scrollable.data('scrollable');
			var names = api.getItems();
		    for (i = 0; i < names.length; i++) {
		        var client = $(names[i]).find('h2').text();
		        $('.navigator a').eq(i).attr('title', client);
		    }
		}
	},

	projectInfo: function() {
	    var timer,
	    story = $('.story');
	    $('.item').bind('mouseenter mouseleave', function (e) {
	        var screenshots = $(this).find('.screenshots');
	        if (e.type === 'mouseenter') {
	            timer = setTimeout(function () {
	                $(screenshots).animate({
	                    left: -498
	                }, 750);
	            }, 1500);
	        } else {
	            clearTimeout(timer);
	            $(screenshots).delay(1000).animate({
	                left: 0
	            });
	        }
	    });
	    $(story).append('<span class="hide"></span>').each(function () {
	        var height = $(this).height();
	        $(this).height(height).find('.hide').css({
	            top: -height + 10
	        }).toggle(function () {
	            var height = $(this).parent().height();
	            $(this).parent().animate({
	                bottom: -height - 10
	            }, {
	                queue: false,
	                duration: 800,
	                complete: function () {
	                    $(this).children('.hide').css('backgroundPosition', '3px 4px').animate({
	                        top: -height - 17
	                    }, 200).parent().nextAll('.date, .status').animate({
	                        top: '-20px'
	                    }, 200);
	                }
	            });
	        }, function () {
	            $(this).parent().animate({
	                bottom: 0
	            }, {
	                queue: false,
	                duration: 700,
	                complete: function () {
	                    $(this).children('.hide').css('backgroundPosition', '3px -20px').animate({
	                        top: -height + 17
	                    }, 200).parent().nextAll('.date, .status').animate({
	                        top: '15px'
	                    }, 200);
	                }
	            });
	        });
	    });
	},
	
	gohome: function() {
		var api = $('#scrollable').data('scrollable');
		$('#logo').click(function(){
			api.seekTo(0, 500);
			return false;
		});// Scroll to first item
	},
	
	cufonize: function(elements){
		Cufon.replace(elements, {
	        hover: true
	    });

	}	
}
