// JavaScript Document
var BackgroundImages = function() {
	this.$container;
	this.objects;
	this.constructor.apply(this);
}

BackgroundImages.prototype = {
	
	constructor : function() {
		//console.log("BackgroundImages.constructor");		
		this.$container = $('<div>').css({
		});
	},
	
	initialize : function( $container, imageFiles, postID ) {
		//console.log("BackgroundImages.initialize");
		this.$container.css({ "width":$container.width(), "height":$container.height() });
		this.objects = new Array();
		
		for(var i = 0; i < imageFiles.length; i++){
			var obj = new Obj();
			this.$container.append( obj.$image );
			obj.initialize( imageFiles[i], postID );
			this.objects[i] = obj;
		}		
		$container.append( this.$container );
		
		var self = this;
		$container.scroll(function() {
			self.scroll.apply( self );
		});		
	},
	
	scroll : function() {
		//console.log("BackgroundImages.scroll");
		for(var i = 0; i < this.objects.length; i++){
			this.objects[i].setProperty();
		}
	},
	
	remove : function() {
		//console.log("BackgroundImages.remove");
		this.$container.find("img").remove();
	}
};




var Obj = function() {
	this.image_dir = "http://doppoinc.com/wp/wp-content/themes/doppo/images/";
	this.$image;
	this.$container;
	this.postID;
	this.isMoving;
	this.originalSize = 272;
	this.degree = 180;
	this.x;
	this.y;
	this.size;
	this.tx;
	this.ty;
	this.ts;	
	this.constructor.apply(this);
}

Obj.prototype = {
	constructor : function() {
		//console.log("Obj.constructor");		
		this.$image = $('<img>').css({
			"position":"absolute",
			"left":this.x,
			"top":this.y
		});
	},
	
	initialize : function( imageFile, postID ) {
		//console.log("Obj.initialize");
		this.postID = postID;
		this.$image.attr({"src":this.image_dir + imageFile });
		
		this.x = parseInt( this.$image.parent().width() * Math.random());
		this.y = parseInt( this.$image.parent().height() * Math.random());
		this.size = this.originalSize * Math.random() + .1;
		this.setProperty();

		//エンターフレームイベントの登録
		var self = this;
		var timeline = new Timeline({ fps:30 });
		timeline.bind( timeline.EVENT_ENTER_FRAME, function(){
			self.update.apply( self );
		} );
		setTimeout( function(){ timeline.start(); } );		
		
		this.isMoving = true;
	},
	
	update : function() {
		//console.log("Obj.update");
		this.x += ( this.tx - this.x ) * .08;
		this.y += ( this.ty - this.y ) * .08;
		this.size += ( this.ts - this.size ) * .08;
		this.$image.css({ "left":this.x, "top":this.y, "width":this.size, "height":this.size });
				
		if( parseInt(this.$image.css("left")) == this.tx || parseInt(this.$image.css("top")) == this.ty ){ 
			this.isMoving = false;
		}else{
			this.isMoving = true;
		};
	},
	
	setProperty : function() {
		//console.log("Obj.setPoint");
		if( !this.isMoving ){
			
			if( this.postID == "about" ) {
				//console.log(this.postID);
				this.tx = parseInt( parseInt( this.$image.parent().width() * .15 ) * Math.random());
				if( Math.random() > .5 ){ this.tx += parseInt( this.$image.parent().width() * .7 ) };
			} else {
				//console.log(this.postID);
				this.tx = parseInt( parseInt( this.$image.parent().width() ) * Math.random());
			};
			
			this.ty = parseInt( parseInt( this.$image.parent().height() ) * Math.random());	
			
			this.ts = this.originalSize * Math.random() + .1;
			
			degree = 180;			
			degree = parseInt( Math.random() * degree * 2 ) - degree;
			d = 2000 * Math.random() + 1000;
			
			this.$image.rotate({
				animateTo:degree,
				duration:d,
				easing:$.easing.easeOutBack,
				callback:function(){}
			});		

		}
		
	}
}
