/**
*
*	LAYER
**/
// constructor
function Layer(layerId, lyrWidth, lyrHeight, startX, startY, endX, endY, lyrSpeed, easeFactor, lyrDelay, lyrDisplay, foTime, foPath) {
	// mandatory
	this.layerId 	= layerId;
	this.lyrWidth	= lyrWidth;
	this.lyrHeight	= lyrHeight;
	
	// movement
	this.moveTimeOut	= 10;						// in milliseconds, smaller value for fluent animation
	this.timeOutCalls	= 1000/this.moveTimeOut;	// number of timeout-call (movement call) for each second
	
	// optional with default values
	var windowWidth = (typeof( window.innerWidth ) == 'number')? window.innerWidth: document.documentElement.clientWidth;
	var windowHeight= (typeof( window.innerHeight ) == 'number')? window.innerHeight: document.documentElement.clientHeight;
	var centerX = windowWidth/2 - this.lyrWidth/2;
	var centerY = windowHeight/2 - this.lyrHeight/2;
	
	this.startX = (!startX)? 	(0-this.lyrWidth): startX;		// default to left outside the visible area
	this.startY = (!startY)? 	centerY: startY;
	this.endX	= (!endX)?		centerX: endX;
	this.endY	= (!endY)?		centerY: endY;
	
	this.lyrSpeed 	= (!lyrSpeed)?		100: lyrSpeed;
	this.easeFactor	= (!easeFactor)? 	0: easeFactor;
	this.lyrDelay	= (!lyrDelay)?		0: lyrDelay;
	this.lyrDisplay	= (!lyrDisplay)? 'yes': lyrDisplay;
	this.foTime	= (!foTime)? -1: foTime*1000;
	this.foPath	= (!foPath)? 'no': foPath;
	
	this.XDistance = this.endX - this.startX;
	this.YDistance = this.endY - this.startY;
	
	// fade out
	this.initStartX = this.startX;
	this.initStartY = this.startY;
	this.initEndX = this.endX;
	this.initEndY = this.endY;
	this.initEaseFactor = this.easeFactor;
	this.timeoutFOid = '';
	
	// easing the animation
	this.distance = (startX != endX)? Math.abs(this.startX - this.endX): Math.abs(this.startY - this.endY);
	this.dynSpeed = this.lyrSpeed;	// dynamic layer speed
	
	// check for lightbox
	this.lightboxElem = $(this.layerId+'_lightbox');
	
	// check if layer is in global layer div, re-allocate if not
	//if (!$(this.layerId).up('div#layer'))  changed because IE doesn't support up() with multiple elements with the same ID
	if (!$(this.layerId).up('div') || $(this.layerId).up('div').getAttribute('id') != 'layer') {
		if (!$('layer')) {
			$$('div.container')[0].insert({before: new Element('div', { 'id': 'layer'})});
		}
		if (this.lightboxElem != null) {
			var lightboxInstance = this.lightboxElem.remove();
			$('layer').insert(lightboxInstance);
			this.lightboxElem = $(this.layerId+'_lightbox');
			this.lightboxElem.style.zIndex = '10010';
		}
		$('layer').insert($(this.layerId).remove());
		$(this.layerId).style.zIndex = '10012';
		$(this.layerId).style.position = 'absolute';
	}
	
	// put layer on global array list
	layerList.push(this);
	
}

Layer.prototype.RemoveLayer = function() {
	 for(var i=0; i<layerList.length;i++ ) { 
        if(layerList[i]==this)
        	layerList.splice(i,1); 
      } 
}

// define methods
Layer.prototype.ShowLayer = function() {
	if(closeOtherLayers) {
		for(i=0; i<layerList.length; i++) {
			var lObj = layerList[i];
			if(!lObj.IsLayerHidden()) {
				lObj.HideLayer();
				// execute the javascript functions defined on close link!
				if($(lObj.layerId).down('.closelyr_img a')!=null) {
					eval($(lObj.layerId).down('.closelyr_img a').href.replace('javascript:', '').replace(/%20/g, ' '));
				}
			}
		}
	}

	var layerElem = document.getElementById(this.layerId);
	var thisObjLyr = this; // help variable for setTimeout(), because "this" in window.setTimeout-function refer to Window Object
	
	if(layerElem!=null && this.IsLayerHidden()) {
		if (thisObjLyr.lyrDelay==0) {
			thisObjLyr.DisplayLayer(layerElem);
		} else {
			window.setTimeout(function(){thisObjLyr.DisplayLayer(layerElem)}, thisObjLyr.lyrDelay*1000);
		}
		this.ResetToInit();
	}
	
	// check for lightbox and open it if available
	if(this.lightboxElem!=null) {
		this.lightboxElem.style.display='block';
	}
	
	// hide SELECTs for IE6
	var is_ie6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if (is_ie6) {
		$$('select').each(function(oneSelect){
			oneSelect.addClassName('temporarilyRemovedForIE6');
			oneSelect.style.visibility = 'hidden';
		});
	}
}

Layer.prototype.DisplayLayer = function(layerElem) {
	var thisObjLyr = this;
	layerElem.style.left = this.startX + "px";
	layerElem.style.top = this.startY + "px";
	
	layerElem.style.display = 'block';
	
	if(this.lyrSpeed >= 0) this.MoveLayer();
	window.setTimeout(function(){thisObjLyr.IsReadyToFadeOut()}, 1000);
}

Layer.prototype.HideLayer = function() {
	var layer = document.getElementById(this.layerId);
	if (layer != null) {
		var thisObjLyr = this;
		window.clearTimeout(thisObjLyr.timeoutFOid);
		if(this.foTime > 0 && this.foPath != 'no') {
			this.FOMove();	
		} else {
			layer.style.display = 'none';
		}
	}
	
	// check for lightbox and hide it if available
	if(this.lightboxElem!=null) {
		this.lightboxElem.style.display='none';
	}
	
	// show SELECTs again (for IE6)
	var is_ie6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if (is_ie6) {
		$$('select').each(function(oneSelect){
			if (oneSelect.hasClassName('temporarilyRemovedForIE6')) {
				oneSelect.style.visibility = '';
				oneSelect.removeClassName('temporarilyRemovedForIE6');
			}
		});
	}
}

Layer.prototype.MoveLayer = function() {
	var layer = document.getElementById(this.layerId);
	
	if(layer!=null && this.lyrSpeed>0 && layer.style.display!='none') {
		if(this.easeFactor>0 && this.easeFactor<=100) this.UpdateSpeed();
		this.UpdatePosition();
	}
}

Layer.prototype.UpdatePosition = function() {
	var layer = document.getElementById(this.layerId);
	var thisObjLyr = this;
	
	var generalStep = parseFloat(this.dynSpeed/this.timeOutCalls);
	var nrOfSteps 	= (this.XDistance >= this.YDistance)? parseFloat(this.XDistance/generalStep): parseFloat(this.YDistance/generalStep);
	
	if (this.startX!=this.endX) {
		var moveX = this.XDistance / nrOfSteps;
		var newX = (parseFloat(layer.style.left) < this.endX)? parseFloat(layer.style.left) + moveX: parseFloat(layer.style.left) - moveX;
		layer.style.left = newX + "px";
	}
	
	if (this.startY!=this.endY) {
		var moveY = this.YDistance / nrOfSteps;
		var newY = (parseFloat(layer.style.top) < this.endY)? parseFloat(layer.style.top) + moveY: parseFloat(layer.style.top) - moveY;
		layer.style.top = newY + "px";
	}
	
	if(!this.IsEndPos()) window.setTimeout(function(){thisObjLyr.MoveLayer()}, this.moveTimeOut);
}

Layer.prototype.FadeOut = function() {
	var thisObjLyr = this;
	if(this.foTime > 0) {
		if(this.foPath != 'no') {
			thisObjLyr.timeoutFOid = window.setTimeout(function(){thisObjLyr.FOMove()}, thisObjLyr.foTime);
		} else {
			thisObjLyr.timeoutFOid = window.setTimeout(function(){thisObjLyr.HideLayer()}, thisObjLyr.foTime);
		}
	}
}

Layer.prototype.FOMove = function() {
	this.ChangeStartEndPos();
	this.MoveLayer();
}

Layer.prototype.IsLayerHidden = function() {
	var layer = document.getElementById(this.layerId);
	var hiddenX = ((0 - parseFloat(layer.style.left)) >= this.lyrWidth) ? true: false;
	var hiddenY = ((0 - parseFloat(layer.style.top)) >= this.lyrHeight) ? true: false;
	
	if(layer.style.display == 'none' || hiddenX || hiddenY) {
		return true;
	} else return false;
}

Layer.prototype.GetPosX = function() {
	var layer = document.getElementById(this.layerId);
	return layer.style.left;
}

Layer.prototype.GetPosY = function() {
	var layer = document.getElementById(this.layerId);
	return layer.style.top;
}

Layer.prototype.IsReadyToFadeOut = function() {
	if (this.dynSpeed == 0 || this.IsEndPos()) {
		this.FadeOut();
	} else {
		var thisObjLyr = this;
		window.setTimeout(function(){thisObjLyr.IsReadyToFadeOut()}, 1000);
	}
}

Layer.prototype.IsEndPos = function() {
	var layer = document.getElementById(this.layerId);
	var isEndPos = false;
	
	// layer no longer between start and end position
	if(layer && ((parseFloat(layer.style.left) < this.startX && parseFloat(layer.style.left) <= this.endX) || (parseFloat(layer.style.left) > this.startX && parseFloat(layer.style.left) >= this.endX) || (parseFloat(layer.style.top) < this.startY && parseFloat(layer.style.top) <= this.endY) || (parseFloat(layer.style.top) > this.startY && parseFloat(layer.style.top) >= this.endY))) {
		isEndPos = true;
	}
	
	return isEndPos;
}

Layer.prototype.ChangeStartEndPos = function() {
	var helpVarX = this.startX;
	var helpVarY = this.startY;
	
	this.startX = this.endX;
	this.startY = this.endY;
	
	this.endX = helpVarX;
	this.endY = helpVarY;

	this.dynSpeed = this.lyrSpeed;
	this.easeFactor = 0;
}

Layer.prototype.UpdateSpeed = function() {
	var currDistToEnd = this.GetCurrentDist();
	var easePerMove = this.timeOutCalls/this.easeFactor;
	if(this.dynSpeed > 0 && currDistToEnd < 1/3*this.distance) {
		this.dynSpeed -= easePerMove*1.5;
	} else if(this.dynSpeed <= 0){
		this.dynSpeed = 0;
	}
}

Layer.prototype.GetCurrentDist = function() {
	var layer = document.getElementById(this.layerId);
	
	currDistToEnd = (this.startX != this.endX)? parseFloat(layer.style.left) - this.endX: parseFloat(layer.style.top) - this.endY;
	
	return Math.abs(currDistToEnd);
}

Layer.prototype.ResetToInit = function() {
	this.startX = this.initStartX;
	this.startY = this.initStartY;
	this.endX = this.initEndX;
	this.endY = this.initEndY;
	
	this.dynSpeed = this.lyrSpeed;
	this.easeFactor = this.initEaseFactor;
}