﻿// JavaScript Document By Ori Oron - Tourwise 1/5/2011
var class_galleryFadeStage = function(){
	var width, height, ME, images, curr_opacity, timerA, curr_Image, timerA
	ME = this
	ME.onfocus = function(){ME.blur()}
	width = ME.offsetWidth
	height = ME.offsetHeight
	merge(ME.style,{position:'relative',width:width+'px',height:height+'px'})
	ME.preload = function(srcArr){
		for(var i=0;i<srcArr.length;i++){
			ME.loadImage(srcArr[i],true)	
		}
	}
	ME.loadImage = function(src,noRun){
		noRun = noRun || false
		if(!images[src]){
			images[src] = new createImage(src)
		}
		if(curr_Image===images[src]) return
		curr_Image = images[src]
		curr_opacity = 0
		if(!noRun){
			clearInterval(timerA)
			timerA = setInterval(runLoader,30)
			runLoader()
		}
	}
	images ={}
	var runLoader= function(){
		
		if(curr_Image.loaded==0) return
		if(curr_Image.loaded<0){
			clearInterval(timerA)
			return
		}
		ME.appendChild(curr_Image.dom)
		curr_Image.setOpacity(curr_opacity)
		curr_opacity = curr_Image.getOpacity()
		
		if (curr_opacity<0.95){
			curr_opacity=curr_opacity+0.06
		}else{
			curr_Image.setOpacity(1)
			if(ME.onLoadFinished)ME.onLoadFinished()
			clearInterval(timerA)
			
		}
	}
	var createImage = function(src){
		var _ME = this
		_ME.loaded = 0
		_ME.dom = new Image()
		_ME.dom.onload=function(){
			_ME.loaded = 1
		}
		if(ME.filters)_ME.dom.style.filter="alpha(opacity=100)"
		_ME.dom.onerror=function(){
			_ME.loaded = -1	
		} 
		_ME.dom.src = src
		_ME.dom.width = width
		_ME.dom.height = height
		merge(_ME.dom.style,{position:'absolute',left:'0px',top:'0px',width:width,height:height})		
		_ME.setOpacity=function(o){
			if(ME.filters){
				_ME.dom.style.filter="alpha(opacity="+parseInt(o*100)+")"
			}else{
				_ME.dom.style.opacity = o
			}
		}
		_ME.getOpacity=function(){
			if(_ME.loaded<1)return 0
			if(ME.filters){
				try{
					return _ME.dom.filters.alpha.opacity/100
				}catch(e){
					return 0
				}
			}
			try{
				return eval(getStyle(_ME.dom,'opacity'))
			}catch(e){
				return 0
			}
			return 0
		}
		return _ME
	}	
}

