Array.prototype.shuffle = function() {
    for ( var position = this.length; position > 0; position-- ) {
        // pickup a random element for each index
        var random_index = Math.floor( Math.random() * position );
        var temp = this[position-1];
        this[position-1] = this[random_index];
        this[random_index] = temp;
    }
}

$(document).ready(function(){
  
  // Background resizer
	resizeBackground();
	$(window).resize(resizeBackground);
	
	function resizeBackground() {
    var bgImg = $('#background img');
		bgImg.each(function() {
      var scale = $(this).width()/$(this).height();
			var imgWidth = $(window).width();
			var imgHeight = $(window).width()/scale;
			if (imgHeight < $(window).height()) {
				var imgWidth = $(window).height()*scale;
				var imgHeight = $(window).height();
			}
			var imgLeft = ($(window).width()-imgWidth)/2;
			var imgTop = ($(window).height()-imgHeight)/2;
			
			$(this).css({width:imgWidth, height:imgHeight, left:imgLeft, top:imgTop});
    });
	}
  
  // Background switcher
  var preloadEnded = false;
  var timeoutEnded = false;
  var backgroundArray = "";
  newArray();
  
  var backgroundTimeout = setTimeout(backgroundTimer, 6000);
  loadNextBackground();
  
  function backgroundTimer() {
    timeoutEnded = true;
    if (preloadEnded) {
      showNextBackground();
    }
  }
  
  function showNextBackground() {
    preloadEnded = false;
    timeoutEnded = false;
    
    var image = $('#background img').last();
    image.fadeIn('slow',
      function() {
        $('#background img').first().remove();
      }
    );
    
    $('#background-title img').first().fadeOut();
    $('#background-title img').last().fadeIn('slow',
      function() {
        $('#background-title img').first().remove();
      }
    );
    
    
    backgroundTimeout = setTimeout(backgroundTimer, 6000);
    loadNextBackground();
  }
  
  function loadNextBackground() {
    
    if (backgroundArray.length < 1) {
      newArray();
    }
    var id = backgroundArray[0];
    backgroundArray.shift();
    
    var image = $('<img src="./img/background/background-'+id+'.jpg" alt="" />').hide();
    var title  = $('<img src="./img/title/title-'+id+'.png" alt="" />').hide();
    
    image.load(function() {
      resizeBackground();
      preloadEnded = true;
      if (timeoutEnded) {
        showNextBackground();
      }
    });
    
    $('#background').append(image);
    $('#background-title').append(title);
  }
  
  function newArray() {
    backgroundArray = new Array();
    
    for(var i = 0; i < 18; i++) {
      backgroundArray.push(i+1);
    }
    
    backgroundArray.shuffle();
  }
  
  // Button hover
  $('.submit-button, .button').hover(
    function() {
      $(this).each(function() {
        var src = $(this).attr('src');
        var newSrc = src.replace(new RegExp("(\.png|\.jpg)", "i"), "_active$1");
        $(this).attr('src',newSrc);
      });
    },
    function() {
      $(this).each(function() {
        var src = $(this).attr('src');
        var newSrc = src.replace('_active','');
        $(this).attr('src',newSrc);
      });
    }
  );
  
  // Contact link
  $('.contact-link').click(function(e) {
    $('#contact').animate({bottom:0}, {duration:500, queue:false});
    $('#content').animate({bottom:160}, {duration:500, queue:false});
    e.preventDefault();
  });
  $('.content-link').click(function(e) {
    $('#content').animate({bottom:0}, {duration:500, queue:false});
    $('#contact').animate({bottom:160}, {duration:500, queue:false});
    e.preventDefault();
  });
  
});

