var vickiAdsLayer = function(config) {	
	var bReserveShowAdsLayer = false;
	var layerName = config.layerName || 'vicki_ads_layer';

	var adsLayer = document.getElementById(layerName);

	function initialize() {
		adsLayer.style.position = 'absolute';
		adsLayer.style.display = 'none';
		adsLayer.style.zIndex = '99999';
		adsLayer.style.border = '1px solid black';

		if (config.contentAttr) {
			for (var p in config.contentAttr) {
				adsLayer.style[p] = config.contentAttr[p];
			}
		}
		
		if (window.attachEvent) {
			window.attachEvent('onscroll', hideAndReserveAdsLayer);
			window.attachEvent('onresize', hideAndReserveAdsLayer);
		} else if (window.addEventListener) {
			window.addEventListener('scroll', hideAndReserveAdsLayer, false);
			window.addEventListener('resize', hideAndReserveAdsLayer, false);
		}
	}

	function hideAndReserveAdsLayer() {
		adsLayer.style.display = 'none';
		if (getScrollPos().top > 300) {
			if (!bReserveShowAdsLayer) {
				bReserveShowAdsLayer = true;
				setTimeout(moveAdsLayer, 2000);
			}
		}
	}

	function moveAdsLayer() {
		adsLayer.style.display = 'block';
		adsLayer.style.top = (getWindowSize().height + getScrollPos().top - adsLayer.clientHeight - 20) + 'px';

		adnimateFadeIn(adsLayer, true);
		bReserveShowAdsLayer = false;
	}

	function setOpacity(obj, opacity) {
		obj.style['-ms-filter'] = 'alpha(opacity=' + (opacity * 100) + ')';
		obj.style['filter'] = 'alpha(opacity=' + (opacity * 100) + ')';
		obj.style['opacity'] = opacity;
	}

	function adnimateFadeIn(obj, bInit) {
		var opacity = obj.style['-vicki-opacity'];
		if (bInit || opacity == null) {
			opacity = 0;
		}
		
		if (opacity < 0.7) {
			opacity += 0.03;
			obj.style['-vicki-opacity'] = opacity;
			setOpacity(obj, opacity);
			setTimeout(function() {
				adnimateFadeIn(obj);
			}, 30);
		}
	}

	function getWindowSize() {
		var width, height;
		
		if (window.innerWidth) { width = window.innerWidth - 26; }
		else if (document.documentElement && document.documentElement.clientWidth) { width = document.documentElement.clientWidth - 20; }
		else if (document.body) { width = document.body.offsetWidth; }	
		
		if (window.innerHeight) { height = window.innerHeight - 26; }
		else if (document.documentElement && document.documentElement.clientHeight) { height = document.documentElement.clientHeight - 20; }
		else if (document.body) { height = document.body.clientHeight; }	
		
		return {
			width: width,
			height: height,
			toString: function() {
				return 'width: ' + width + ' height:' + height;
			}
		};
	}

	function getScrollPos() {
		var left, top;

		if (window.pageYOffset) { top = window.pageYOffset; }
		else if (document.documentElement && document.documentElement.scrollTop) { top = document.documentElement.scrollTop; }
		else if (document.body) { top = document.body.scrollTop; }
		
		if (window.pageXOffset) { left = window.pageXOffset; }
		else if (document.documentElement && document.documentElement.scrollLeft) { left = document.documentElement.scrollLeft; }
		else if (document.body) { left = document.body.scrollLeft; }
		
		return {
			left: left,
			top: top,
			toString: function() {
				return 'left: ' + left + ' top:' + top;
			}
		};
	}

	initialize();

};
