// JavaScript Document

// Initial Value for Delay
function opacity_process() {
	this.count = -20;
	this.direct = 1;
	this.type = 1;
	this.length = 100;
	this.interval = 25;
	this.stoptime = 80;
	this.swaptime = 80;
	this.swapdelay = 1000;
	this.swapcount = 30;
	this.swapimg = null;
	this.items = [];
	this.itemc = 0;
	this.element = "";
	this.target = null;
	this.timer_timeout = null;
	this.timer_interval = null;
	this.handler = null;
	this.loaded = false;
	this.images = [];
	//
	this.start = function(element) {
		this.element = element;
		this.target = $(this.element);
		this.process();
	};
	this.swap = function(element, delay) {
		if( this.items.length <= 1 ) return;
		if( delay > 0 ) {
			this.count = this.count - this.length * 2;
		}
		this.element = element;
		this.target = $(this.element);
		this.preloadimage();
		this.process();
	};
	this.process = function(){
		var _obj = this;
		this.timer_interval = window.setInterval(function() {_obj.processFilter();}, this.interval);
	};
	this.processFilter = function() {
		this.count++;
		//$("debug").innerHTML = this.count + " ";
		if( this.count >= this.swaptime && this.swaptime > 0 ) {
			this.count = this.swapcount;
			this.direct = 0 - this.direct;
			if( this.direct > 0 ) {
				if( this.timer_timeout ) {
					window.clearTimeout(this.timer_timeout);	
				}
				if( this.timer_interval ) {
					window.clearInterval(this.timer_interval);	
				}
				
				var _obj = this;
				this.timer_timeout = window.setTimeout(function() {_obj.process();}, this.swapdelay);
			} else {
				//var src = this.target.src;
				//this.target.src = this.swapimg.src;
				//this.swapimg.src = src;
				if( this.target.src == this.swapimg.src ) {
					this.target.src = this.showimg.src;
				} else {
					this.target.src = this.swapimg.src;
				}				
				
				this.preloadimage();
				//this.processFilter();
			}
			if( this.handler != null && typeof(this.handler) == "function" ) {
				this.handler();
			}
		} else if( this.count >= this.stoptime && this.stoptime > 0 ) {
			if( this.timer_timeout ) {
				window.clearTimeout(this.timer_timeout);	
			}
			if( this.timer_interval ) {
				window.clearInterval(this.timer_interval);	
			}
			this.processJump();
			//For Back, Fail
			if( IS_IE ) {
				//this.target.style.filter = "alpha(opacity=" + 100 + ")";
			} else if( IS_MOZILLA ) {
				//this.target.style.MozOpacity = 1;
			}
		} else if( this.type == 1 ) {
			if( IS_IE && this.count > 0 ) {
				// From 0 to 100;
				if( this.direct > 0 ) {
					this.target.style.filter = "alpha(opacity=" + (this.length - this.count) + ")";
				} else {
					this.target.style.filter = "alpha(opacity=" + (this.count) + ")";
				}
			} else if( IS_MOZILLA && this.count > 0 ) {
				// Form 0 to 1
				if( this.direct > 0 ) {
					this.target.style.MozOpacity = (1 - this.count/this.length);
				} else {
					this.target.style.MozOpacity = this.count/this.length;
				}
			}
		} else if( this.type == 2 ) {
		}
	};
	this.processOver = function() {
		if( this.count <= 10 ) {
			return false;
		}
		if( IS_IE ) {
			this.target.style.filter = "alpha(opacity=" + 100 + ")";
		} else if( IS_MOZILLA ) {
			this.target.style.MozOpacity = 1;
		}
		var _obj = this;
		this.timer = window.setTimeout(function() {_obj.processJump();}, 3000);
	};
	this.processJump = function () {
		//document.location.href = "home.php";
	};
	this.preloadimage = function() {
		if( this.loaded == true ) {
			return false;	
		}
		if( this.items.length == 0 ) {
			this.showimg = new Image();
			this.showimg.src = this.target.src;
			var src = this.target.src;
			var srcp = src.lastIndexOf(".");
			var srcs = src.substring(0, srcp)+'_s'+src.substring(srcp);
			this.swapimg = new Image;
			this.swapimg.src = srcs;
			var _obj = this;
			this.swapimg.onload = function(){
				_obj.loaded = true;
			};
		} else {
			var _obj = this;
			if( this.images.length == 0 ) {
				for(var i=0; i<this.items.length; i++){
					var image = new Image();
					image.src = this.items[i];
					image.onload = function(){
						this.setAttribute("loaded", 1);
					};
					this.images.push(image);
				}
			}
			this.itemc++;
			if( this.itemc >= this.items.length ) {
				this.itemc = 0;	
			}
			if( this.swapimg == null ) {
				this.swapimg = new Image;
			}
			var image = this.images[this.itemc];
			if( image.getAttribute("loaded") == true ) {
				this.swapimg.src = image.src;
			} else {
				this.swapimg.src = this.items[this.itemc];
			}
		}
	};
}

