

function ImageBackground()
{
	var _self = this;
	var img = null;
	var stage = null;
	var imgOverlay = null;
	var bodyContainer = null;
	var imgPath;
	var imgFx;
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	this.__construct = function() {
		defImgPath = "/pix/main/";
		setImgFx();
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setImgFx() {
		imgFx = {
			animation: null,
			defaultWidth: 1920,
			defaultDistance: 30,
			duration: 4500,
			distance: 0,
			frozen:false,
			direction:1,
			direction:{
				x:false,
				y:false
			}
		}
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setImgFxParams() {
		imgFx.distance = Math.floor(window.getSize().x * imgFx.defaultDistance / imgFx.defaultWidth);
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setElements() {
		if ( bodyContainer === null ) {
			bodyContainer = document.getElementsByTagName("body");
			bodyContainer = bodyContainer[0];
		} if ( stage === null ) {
			stage = document.createElement("div");
			stage.id = 'bgStage';
			stage.size = new Array();
			bodyContainer.appendChild(stage);
		} if ( imgOverlay === null ) {
			imgOverlay = document.createElement("div");
			imgOverlay.id = 'bgImgOverlay'
			imgOverlay.style.backgroundImage = "url(" + defImgPath + "empty.gif)";
			stage.appendChild(imgOverlay);
		} if ( img === null ) {
			img = document.createElement("img");
			img.border = "0";
			img.alt = "";
			img.style.display = "block";
			img.style.visibility = "hidden";
			stage.appendChild(img);
		}
	}
	
	//--------------------------------------------------------------
	
	/**
	 * 
	 * @param string a_path
	 * @return void
	 */
	this.setDefImgPath = function( a_path ) {
		defImgPath = a_path;
	}
	
	//--------------------------------------------------------------
	
	/**
	 * 
	 * @return void
	 */
	function setStageSize() {
		if( typeof( window.innerWidth ) == 'number' ) {
			// Non-IE
			stage.size["w"] = window.innerWidth;
			stage.size["h"] = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			// IE 6+ in 'standards compliant mode'
			stage.size["w"] = document.documentElement.clientWidth;
			stage.size["h"] = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			// IE 4 compatible
			stage.size["w"] = document.body.clientWidth;
			stage.size["h"] = document.body.clientHeight;
		}
		stage.style.width	= stage.size['w'] + "px";
		stage.style.height	= stage.size['h'] + "px";
		imgOverlay.style.width	= stage.size['w'] + "px";
		imgOverlay.style.height	= stage.size['h'] + "px";
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	function setImageSize() {
		img.style.visibility = "hidden";
		img.style.width = "";
		img.style.height = "";
		
		var defRatio = img.offsetWidth / img.offsetHeight;
		var tmpRatio = stage.size['w'] / stage.size['h'];
		
		if(tmpRatio > defRatio) {
			img.style.width		= stage.size['w'] + "px";
			img.style.height	= stage.size['w'] / defRatio + "px";
		} else {
			img.style.height	= stage.size['h'] + "px";
			img.style.width		= stage.size['h'] * defRatio + "px";
		}
		
		img.style.visibility = "visible";
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @param string a_src
	 * @return void
	 */
	this.setImage = function( a_src ) {
		setElements();
		img.onload = function() {
			_self.adapt();
		}
		img.src = a_src;
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	this.adapt = function() {
		if( imgFx.animation !== null ) { this.stopAnimation(); }
		setTimeout(function() {
			setElements();
			setStageSize();
			setImageSize();
			if( imgFx.animation === null && imgFx.frozen === false ) {
				setImgFxParams();
				_self.startAnimation(true);
			}
		},10);
	}
	//--------------------------------------------------------------
	/**
	 * 
	 * @param boolean freeze
	 * @return void
	 */
	this.freezeAnimation = function( freeze ) {
		imgFx.frozen = freeze;
		if(freeze === true) {
			this.stopAnimation();
		}
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return min
	 */
	function getRandom( min, max ) {
		if( min > max ) {
			return( -1 );
		}
		if( min == max ) {
			return( min );
		}
		return( min + parseInt( Math.random() * ( max-min+1 ) ) );
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @param boolean zoom
	 * @return void
	 */
	this.startAnimation = function ( zoom ) {
		if( imgFx.frozen === true ) return;
		/*
		var distance = ( zoom === true ) ? imgFx.distance : imgFx.distance*-1;
		var next = ( zoom === true ) ? false : true;
		*/
		
		imgFx.animation = new Fx.Morph(img, {
			duration: imgFx.duration,
			onComplete:function() {
				_self.startAnimation(next)
			}
		});
		
		var next = getRandom(0,1);
		switch(next) {
			case 0: // x
				if( imgFx.direction.x === false ) {
					var distance = imgFx.distance;
					imgFx.direction.x = true;
				} else {
					var distance = imgFx.distance*-1;
					imgFx.direction.x = false;
				}
				imgFx.animation.start({ width: img.getSize().x + distance });
			break;
			case 1: // y
				if( imgFx.direction.y === false ) {
					var distance = imgFx.distance;
					imgFx.direction.y = true;
				} else {
					var distance = imgFx.distance*-1;
					imgFx.direction.y = false;
				}
				imgFx.animation.start({ height: img.getSize().y + distance });
			break;
		}
		
		//var distance = ( imgFx.direction <= 2 ) ? imgFx.distance : imgFx.distance*-1;
		
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return void
	 */
	this.stopAnimation = function() {
		imgFx.animation.cancel();
		imgFx.animation = null;
		this.adapt();
	}
	
	//--------------------------------------------------------------
	/**
	 * 
	 * @return object img
	 */
	this.getImage = function() {
		return img;
	}
	
	this.__construct();
}

