
$(function() {
		
	var url = 'http://www.madeinlapland.net/wp-content/themes/madeinlapland/';
	var img = new Image();
	
	$(img).load(function () {
		var div = $('<div></div>');
		div.css('position', 'absolute');
		div.css('left', '0px');
		div.css('top', '0px');
		div.css('overflow', 'hidden');
		div.hide();
		
		
		div.css('z-index', '-100002');
		div.append(this);
		
		if($('html').width()/$('html').height() > 700/1058) {
			div.css('width', $('html').width());
			$(this).css('width', $('html').width());
			
		} else {
			div.css('height', $('html').height());
			$(this).css('height',  $('html').height());
			
		}
		div.css('width', '100%');
		$(this).css('width', '100%');
		$('body').append(div);
		loadFooter();
		div.fadeIn(2000);
	}).error(function(e) {
	
	}).attr('src', url + 'bg2.png');
	
	
	
	var footer = [
        'nassu.png',
        'puhe.png'
	];
	var footerPos = [[500,0, 100, '#footer-content'],[750,-75, 100, '#footer-content']];//,  [-50,20,-10000, 'body'],[-40,500,-10000, 'body'],[750,550,-10000, 'body'],[800,-50,-10000, 'body']];
	
	var raiskeet = [
		'rvihrea-1.png',				
		'rturkoosi-1.png',
		'roranssi-1.png',
		'rvihrea-2.png'
		];
	var raiskeetPos =  [[-50,20,-10000, 'body'],[650,550,-10000, 'body'],[-40,500,-10000, 'body'],[600,-50,-10000, 'body']];
	var curri = -1;
	var furri = -1;
	var yPos = 0;
	
	function loadFooter() {				
		
		if(furri < footer.length-1) {

			furri++;
			var img = new Image();			
			$(img).load(function () {					
				var div = $("<div></div>");
				div.css('position', 'absolute');
				div.css('left', footerPos[furri][0]+'px');
				div.css('top', footerPos[furri][1]+'px');			
				div.css('z-index', footerPos[furri][2]);
				div.css('overflow', 'visible');
				div.append(this)
				$(footerPos[furri][3]).append(div);
				loadFooter();					
			}).attr('src', url + footer[furri]);
		} else {
		
			loadNext();
		}
	}
	
	function loadNext() {

		if(yPos < $('html').height() - 800) {
			
			curri++;
			if(curri >= raiskeet.length) {
				curri = 0;
			}
			var img = new Image();			
			$(img).load(function () {
				var div = $("<div></div>");
				div.css('position', 'absolute');
				div.css('left', raiskeetPos[curri][0]+'px');
				div.css('top', yPos+'px');
				yPos += 300;
				div.css('z-index', raiskeetPos[curri][2]);
				div.css('overflow', 'visible');
				div.append(this)
				$(raiskeetPos[curri][3]).append(div);				
				
				loadNext();
				
			}).error(function(e) {
				
			}).attr('src', url + raiskeet[curri]);
		}
	}
});

