/*Scrolling Inline Pop-up
Copyright (C) 2007 Massimo Giagnoni - www.webmastergate.com

An online code generator is available to create the Javascript 
code needed to show the inline scrolling pop-up on your website.
visit:
http://www.webmastergate.com/popup-generator/

---
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

var topLeft = 0;
var topRight = 1;
var bottomLeft = 2;
var bottomRight = 3;
var hScroll = 0;
var vScroll = 1;

function wgPopUp(width, height, content, title, popUpId) {
	
	if(!popUpId)
		popUpId='wgpopup';
		
	this.width = width;
	this.height = height;
	this.content = content;
	this.title = title;
	
	this.backgroundColor = '#fff';
	this.borderTopLeftColor = '#d0d0d0';
	this.borderBottomRightColor = '#000';
	this.font = '';
	this.fontSize = 0;
	this.fontColor = '#fff';
	this.titleFont = '';
	this.titleFontSize = 0;
	this.titleFontColor = '#000';
	this.titleBackgroundColor = '#003399';
	
	this.startPos = topLeft;
	this.scrollDirection = hScroll;
	this.scrollBy = 5;
	this.scrollTimer = 50;
	this.scrollInit = true;
	this.autocloseSecs = 0;
	this.cookieSecs = 0;
	this.closeImg = '/close.png';
	this.start = 0;
	this.end =0;
	this.closing = false,
	this.popUpDiv = null;
	
	this.init = init;
	this.show = show;
	this.scroll = scroll;
	this.close = close;
	document.write('<div id="'+popUpId+'" style="position:absolute;padding:2px;z-index:9999;visibility:hidden;overflow:hidden">');
	document.write('<div id="'+popUpId+'-title" style="text-align:center; padding:2px">');
	document.write('</div>');
	document.write('<div id="'+popUpId+'-content" style="padding:2px">');
	
	document.write('</div>');
	document.write('<div id="'+popUpId+'-close" style="position:absolute; font-size:8pt; bottom:3px; right:2px">');
	document.write('<a id="'+popUpId+'-closelnk" href="javascript:void(0)" onclick="this.popUp.close()">close me</a>');
	document.write('</div>');
	document.write('</div>');
	
	this.popUpDiv = document.getElementById(popUpId);
	document.getElementById(popUpId+'-closelnk').popUp = this;
	
	function init() {
		this.popUpDiv.style.backgroundColor = this.backgroundColor;
		this.popUpDiv.style.borderRight = '1px solid ' + this.borderBottomRightColor;
		this.popUpDiv.style.borderBottom = '1px solid ' + this.borderBottomRightColor;
		this.popUpDiv.style.borderLeft = '1px solid ' + this.borderTopLeftColor;
		this.popUpDiv.style.borderTop = '1px solid ' + this.borderTopLeftColor;
		this.popUpDiv.style.height = this.height + 'px';
		this.popUpDiv.style.width = this.width + 'px';
		var div = document.getElementById(popUpId+'-title');
		if(this.title) {
			var style = '';
			div.style.textAlign = 'center';
			div.style.padding = '2px';
			if(this.titleFont)
				div.style.fontFamily = this.titleFont;
				
			if(this.titleFontSize)
				div.style.fontSize = this.titleFontSize +'pt';
				
			if(this.titleFontColor)
				div.style.color = this.titleFontColor;
				
			if(this.titleBackgroundColor)
				div.style.backgroundColor = this.titleBackgroundColor;
			
			div.innerHTML = '<div style="float:right;"><img id="'+popUpId+'-closeimg" src="'+this.closeImg+'" alt="" onclick="this.popUp.close()" /></div>' + this.title;
		}
		document.getElementById(popUpId+'-closeimg').popUp = this;
		var divc = document.getElementById(popUpId+'-content');
		divc.style.padding = '2px';
		if(this.font)
			divc.style.fontFamily = this.font;
			
		if(this.fontSize)
			divc.style.fontSize = this.fontSize + 'pt';
		divc.innerHTML = this.content; 
	}
	
	function show() {
		if(wgp_getCookie(popUpId)=='1')
			return;
		this.scrollInit=true;	
		this.init();
		if(this.cookieSecs) {
			wgp_setCookie(popUpId, '1', this.cookieSecs);
		}
		
		var self = this;
		this.f = function() 
		{
			if(self.closing) {
				self.closing=false;
				self.close();
			} else if(self.scroll()) {
				setTimeout(self.f, self.scrollTimer);
			} else {
				if(self.autocloseSecs) {
					self.closing = true;
					setTimeout(self.f, self.autocloseSecs*1000);
				}
			}
		}
		this.timer = setTimeout(this.f, this.scrollTimer);
	}
	function close() {
		this.popUpDiv.style.visibility="hidden";
	}
	function scroll() {
		
		if(this.scrollInit) {
			var myWindow = new Object();
			wgp_windowSize(myWindow);
			switch(this.startPos) {
				
				case topLeft:
					if(this.scrollDirection == vScroll) {
						this.start = myWindow.scrollY  - this.height;
						this.end = this.start + this.height +5;
						this.popUpDiv.style.top = this.start + 'px';
						this.popUpDiv.style.left = myWindow.scrollX + 5 + 'px';
					} else {
						this.start = myWindow.scrollX - this.width;
						this.end = this.start + this.width + 5;
						this.popUpDiv.style.left = this.start + 'px';
						this.popUpDiv.style.top = myWindow.scrollY + 5 + 'px';
					}
					break;
				case topRight:
					if(this.scrollDirection == vScroll) {
						this.start = -this.height + myWindow.scrollY;
						this.end = this.start + this.height + 5;
						this.popUpDiv.style.top = this.start + 'px';
						this.popUpDiv.style.right = 5 - myWindow.scrollX + 'px';
					} else {
						this.start = -this.width - myWindow.scrollX;
						this.end = this.start + this.width + 5;
						this.popUpDiv.style.right = this.start + 'px';
						this.popUpDiv.style.top =   5 + myWindow.scrollY + 'px';
					}
					break;
				case bottomLeft:
					if(this.scrollDirection == vScroll) {
						this.start = -this.height - myWindow.scrollY;
						this.end = this.start + this.height + 20;
						this.popUpDiv.style.bottom = this.start + 'px';
						this.popUpDiv.style.left = myWindow.scrollX + 5 + 'px';
					} else {
						this.start = myWindow.scrollX - this.width;
						this.end = this.start + this.width +5;
						this.popUpDiv.style.left = this.start + 'px';
						this.popUpDiv.style.bottom =   20 - myWindow.scrollY + 'px';
					}
					break;
				case bottomRight:
					if(this.scrollDirection == vScroll) {
						this.start = -this.height - myWindow.scrollY;
						this.end = this.start + this.height + 20;
						this.popUpDiv.style.bottom = this.start + 'px';
						this.popUpDiv.style.right = 5 - myWindow.scrollX + 'px';
					} else {
						this.start = -this.width - myWindow.scrollX;
						this.end = this.start + this.width + 5;
						this.popUpDiv.style.right = this.start + 'px';
						this.popUpDiv.style.bottom =   20 - myWindow.scrollY + 'px';
					}
					break;
			}
			this.popUpDiv.style.visibility="visible";
			this.scrollInit = false;
		}
		
		if(f=this.start<this.end) {
			this.start += this.scrollBy;
			switch(this.startPos) {
				case topLeft:
				if(this.scrollDirection == vScroll) {
					this.popUpDiv.style.top = this.start + 'px';
				} else {
					this.popUpDiv.style.left = this.start + 'px';
				}
				break;
				case topRight:
				if(this.scrollDirection == vScroll) {
					this.popUpDiv.style.top = this.start + 'px';
				} else {
					this.popUpDiv.style.right = this.start + 'px';
				}
				break;
				case bottomLeft:
				if(this.scrollDirection == vScroll) {
					this.popUpDiv.style.bottom = this.start + 'px';
				} else {
					this.popUpDiv.style.left = this.start + 'px';
				}
				break;
				case bottomRight:
				if(this.scrollDirection == vScroll) {
					this.popUpDiv.style.bottom = this.start + 'px';
				} else {
					this.popUpDiv.style.right = this.start + 'px';
				}
				break;
			}
		}
		return f;
	}
	
}

function wgp_windowSize(myWindow) {
  var myWidth = 0;
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  myWindow.width = myWidth;
  myWindow.height = myHeight;
  
  var scrOfX = 0;
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  
  myWindow.scrollX = scrOfX;
  myWindow.scrollY = scrOfY;
}

function wgp_setCookie(name, value, secs) {
	if (secs) {
		var date = new Date();
		date.setTime(date.getTime()+(secs*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";

}

function wgp_getCookie(name) {
	var sn = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length);
		if (c.indexOf(sn) == 0) return c.substring(sn.length, c.length);
	}
	return null;

}