// JavaScript Document

var frame = 

{		

	loadbackgroundimage : function(){
		
		$('#background')
		.hide()
		;
		
		var theImages = new Array()
		
		theImages[0] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/ginza2.jpg'
		theImages[1] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/Akihabara-by-Kenta-Toyama.jpg'
		theImages[2] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/puppet1.jpg'
		theImages[3] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/tokyo-tower3.jpg'
		theImages[4] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/akihabara2.jpg'
		theImages[5] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/croped2.jpg'
		theImages[6] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/taito_station.jpg'
		theImages[7] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/habor1.jpg'
		theImages[8] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/louis-at-tokyo1.jpg'
		theImages[9] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/tv-station-tokyo.jpg'
		theImages[10] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/squares.jpg'
		theImages[11] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/disney-qr1.jpg'
		theImages[12] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/tokyo-habour.jpg'
		theImages[13] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/sapporo-tv-tower.jpg'
		theImages[14] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/osaka-subway-map.jpg'
		theImages[15] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/art-show-tokyo.jpg'
		theImages[16] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/cbh.jpg'
		theImages[17] = 'http://maniac-lizard.com/blog/wp-content/themes/newtrendy/backgroundimages/akihabara_building.jpg'
		;
		
		// var j = 0
		var p = theImages.length;
		// var preBuffer = new Array()
		// for (i = 0; i < p; i++){
   		// preBuffer[i] = new Image()
  		// preBuffer[i].src = theImages[i]
		// }
		
		var whichImage = Math.round(Math.random()*(p-1));

		//inserts the image
		$('#background')
		.attr({src:theImages[whichImage]})
		;
		
	},
	
	//shows the resized image
	showbackgroundimage : function (){
		$("#background")
		.fadeIn(1750);
		// $("#loader").css('z-index','0');
	},
		
	//this returns the window's width at this moment
	width : function(){
		return $(window).width() || window.innerWidth || window.clientWidth || document.documentElement.clientWidth;
	},
	//this returns the window's height at this moment
	height : function(){
		return window.innerHeight || window.clientHeight || document.documentElement.clientHeight;
	},
	// this function looks at the image & window sizes
	//  and resizes the image accordingly
	resize : function(){
		var w = this.width();
		var h = this.height();
		var winRatio = w / h;
		
		$('#background').removeAttr('width').removeAttr('height');
		var imgHeight = $('#background').height();
		var imgWidth = $('#background').width();
		var imgRatio = imgWidth / imgHeight;


		if( imgRatio > winRatio )
		{
			var wid = Math.floor(h*imgRatio);
			$('#background').attr('width',wid);
			$('#background').attr('height',h);
		}
		else
		{
			var hei = Math.floor(w / imgRatio);
			$('#background').attr('width',w);
			$('#background').attr('height',hei);
		}
		
	},
	// this will be called on resize
	//   this will invoke another call to itself in 20 ms
	doOnResize : function(){
			//create a little delay to "unstress" IE and Opera during resizing
			try {
				window.clearTimeout(this.resizeInt);
				this.resizeInt = null;
			}
			catch (err){
				//nice catch
			}
			this.resizeInt = window.setTimeout("frame.resize()", 70);
	},
	showbackground : function(){
			setTimeout("frame.showbackgroundimage()", 100);
	}
};

