(function( $ ){
	
	// Typewriter text effect
	// https://github.com/jaz303/jquery-grab-bag/blob/master/javascripts/jquery.text-effects.js
	// slightly modified
	$.fn.typewriter = function() {
        this.each(function() {
            var $ele = $(this), str = $ele.text(), progress = 0;
            $ele.text('');
            var timer = setInterval(function() {
                $ele.text(str.substring(0, progress++) + (progress & 1 ? '_' : '')).append('<span class="cursor">_</span>');
                if (progress >= str.length) clearInterval(timer);
            }, 200);
        });
        return this;
    };
	    
	//Thanks to https://github.com/ivanvanderbyl/ui-progress-bar
  $.fn.animateProgress = function(progress, callback) {
    return this.each(function() {
      $(this).animate({
        width: progress+'%'
      }, {
        duration: 2000,
        easing: 'swing',

        step: function( progress ){
          var labelEl = $('.ui-label', this),
              valueEl = $('.value', labelEl);

          if (Math.ceil(progress) < 20 && $('.ui-label', this).is(":visible")) {
            labelEl.hide();
          }else{
            if (labelEl.is(":hidden")) {
              labelEl.fadeIn();
            };
          }

          if (Math.ceil(progress) == 100) {
            labelEl.text('Done');
            setTimeout(function() {
              labelEl.fadeOut();
            }, 1000);
          }else{
            valueEl.text(Math.ceil(progress) + '%');
          }
        },
        complete: function(scope, i, elem) {
          if (callback) {
            callback.call(this, i, elem );
          };
        }
      });
    });
  };
})( jQuery );

$(function() {

	
});

//Finally load all this after the content has loaded
$(window).load(function() {
	
	// The content fadeIn
	if (Modernizr.csstransitions){
		$('body').css({
			opacity: 1,
		});
	} else {
		$('body').animate({
			opacity: 1,
		});
	}
		
	// Animated progress Bar
	// Thanks to https://github.com/ivanvanderbyl/ui-progress-bar
	$('#progressbar #progress').css('width', '5%').animateProgress(33);
	
	// Typewriting
	$('h1').typewriter();
	
	// Blinking cursor fallback
	if (!Modernizr.cssanimations){
		
		$cursor = $('span.cursor')
		$cursor.show();
		
		animate_loop = function() {           
            $cursor.animate({opacity: 1}, 700, function(){
                $cursor.animate({opacity: 0}, 700, function(){
                    animate_loop();
                });
            });
        }
        animate_loop();
	}
		
});
