/*
 * 'noHoverAd' = true?
 */


var HoverAd = {
	aId: 'hoverAd',
	top: 0,
	bounceLimit: 16,
    noHoverAd: false,
	ready: false,

	init: function(cookieName)
	{
        this.cookieName = cookieName;
		this.scrollTop = ('undefined' != typeof(window.pageYOffset))
			? window.pageYOffset : document.body.scrollTop;

		var ad = this.getAdObj()
		if (ad && !this.checkCookie()) {
			ad.style.display = 'block';
			// center align
			ad.style.top = (this.scrollTop - ad.offsetHeight) + 'px';
			ad.style.left = ((this.getWindowWidth() - ad.offsetWidth) / 2) + 'px';
			this.dropStartInt = setInterval('HoverAd.dropIn()', 50);
		}

		this.ready = true;
	},

    checkCookie: function()
    {
        this.noHoverAd = getCookie(this.cookieName) || getCookie('noHoverAd');
        return this.noHoverAd;
    },

	show: function()
	{
		var ad = this.getAdObj();
		if (ad && !this.checkCookie()) {
			ad.style.display = 'block';
		}
	},

	hide: function()
	{
		var ad = this.getAdObj();
		if (ad) {
			ad.style.display = 'none';
            setCookie('noHoverAd', 'true');
		}
	},

	dropIn: function()
	{
		var ad = this.getAdObj()
		if (ad) {
			if (parseInt(ad.style.top) < this.top + this.scrollTop) {
				ad.style.top = (parseInt(ad.style.top) + 40) + 'px';
			} else {
				clearInterval(this.dropStartInt);
				this.bounceStartInt = setInterval('HoverAd.bounceIn()', 50);
			}
		}
	},

	bounceIn: function()
	{
		var ad = this.getAdObj()
		if (ad) {
			ad.style.top = (parseInt(ad.style.top) - this.bounceLimit) + 'px';
			if (this.bounceLimit < 0) this.bounceLimit += 4;
			this.bounceLimit = -this.bounceLimit;
			if (this.bounceLimit == 0) {
				clearInterval(this.bounceStartInt);
			}
		}
	},

	getAdObj: function()
	{
		if(document.getElementById && document.getElementById(this.aId)) {
			return document.getElementById(this.aId);
		} else if (document.all && document.all[this.aId]) {
			return document.all[this.aId];
		} else if (document.layers && document.layers[this.aId]) {
			return document.layers[this.aId];
		} else {
			return false;
		}
	},

	getWindowWidth: function()
	{
		if (window.innerWidth) {
			return window.innerWidth - 18;
		} else if (document.documentElement && document.documentElement.clientWidth) {
			return document.documentElement.clientWidth;
		} else if (document.body && document.body.clientWidth) {
			return document.body.clientWidth;
		}
		return 0;
	}
};

// setup onload function
function HoverAdInit() {
	if (!HoverAd.ready) HoverAd.init('resRegd');
}

addLoadEvent(HoverAdInit);

