var popupboxBox={
displayfiles: ['popup.html'],

displayfrequency: ["chance", "1"],

defineheader: '<div class="headerbar"><table width="100%" border="0" ><tr><td><b></b></td><td align="right" style="text-align: right;">&nbsp;<a href="#" onClick="javascript:popupboxBox.closeit(); return false;">Schliessen</a></td></tr></table></div>',

cookiesetting: ["stitialcookie", "path=/"],

ajaxbustcache: false,

disablescrollbars: true,

autohidetimer: 0,


ie7: window.XMLHttpRequest && document.all && !window.opera,
ie7offline: this.ie7 && window.location.href.indexOf("http")==-1,  
launch:false,
scrollbarwidth: 16,

ajaxconnect:function(url, thediv){
var page_request = false
var bustcacheparameter=""
if (window.XMLHttpRequest && !this.ie7offline)  
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ 
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
popupboxBox.loadpage(page_request, thediv)
}
if (this.ajaxbustcache)  
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
},

loadpage:function(page_request, thediv){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
document.getElementById("interContent").innerHTML=page_request.responseText
}
},

createcontainer:function(){
document.write('<div id="interContainer">'+this.defineheader+'<div style="display:none;">test</div><div id="interContent"></div></div><div id="interVeil"></div>')
this.interContainer=document.getElementById("interContainer") ;
this.interVeil=document.getElementById("interVeil") 
this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body 
},


showcontainer:function(){
if (this.interContainer.style.display=="none") return ;
var ie=document.all && !window.opera
var dom=document.getElementById
var scroll_top=(ie)? this.standardbody.scrollTop : window.pageYOffset
var scroll_left=(ie)? this.standardbody.scrollLeft : window.pageXOffset
var docwidth=(ie)? this.standardbody.clientWidth : window.innerWidth-this.scrollbarwidth
var docheight=(ie)? this.standardbody.clientHeight: window.innerHeight
var docheightcomplete=(this.standardbody.offsetHeight>this.standardbody.scrollHeight)? this.standardbody.offsetHeight : this.standardbody.scrollHeight
var objwidth=this.interContainer.offsetWidth
var objheight=this.interContainer.offsetHeight
this.interVeil.style.width=docwidth+"px"  
this.interVeil.style.height=docheightcomplete+"px"  
this.interVeil.style.left=0  
this.interVeil.style.top=0  
this.interVeil.style.visibility="visible"  
this.interContainer.style.left=docwidth/2-objwidth/2+"px" 
var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+5+"px"  
this.interContainer.style.top=Math.floor(parseInt(topposition))+"px"
if (this.autohidetimer && parseInt(this.autohidetimer)>0 && typeof this.timervar=="undefined")
this.timervar=setTimeout("popupboxBox.closeit()", this.autohidetimer*1000)
 opacity('interContainer', 0, 100, 1*1000);
this.interContainer.style.visibility="visible" 
},
closeit:function(){
 opacity('interContainer', 100, 0, 1*1000);
 this.timervar=setTimeout("popupboxBox.realcloseit()", 1*1000);
},
realcloseit:function(){
this.interVeil.style.display="none";
this.interContainer.style.display="none";
if (this.disablescrollbars && window.XMLHttpRequest) this.standardbody.style.overflow="auto"
if (typeof this.timervar!="undefined") clearTimeout(this.timervar)
},
getscrollbarwidth:function(){
var scrollbarwidth=window.innerWidth-(this.interVeil.offsetLeft+this.interVeil.offsetWidth)  
this.scrollbarwidth=(typeof scrollbarwidth=="number")? scrollbarwidth : this.scrollbarwidth
},

hidescrollbar:function(){
if (this.disablescrollbars){  
if (window.XMLHttpRequest)  
this.standardbody.style.overflow="hidden"
else  
window.scrollTo(0,0)
}
},

dotask:function(target, functionref, tasktype){  
var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
if (target.addEventListener)
target.addEventListener(tasktype, functionref, false)
else if (target.attachEvent)
target.attachEvent(tasktype, functionref)
},

initialize:function(){
this.createcontainer() 
this.ajaxconnect(this.displayfiles[Math.floor(Math.random()*this.displayfiles.length)], this.interContainer) //load page into content via ajax
this.dotask(window, function(){popupboxBox.hidescrollbar(); popupboxBox.getscrollbarwidth(); setTimeout("popupboxBox.showcontainer()", 100)}, "load")
this.dotask(window, function(){popupboxBox.showcontainer()}, "resize")
}
}

function getCookie(Name){
var re=new RegExp(Name+"=[^;]+", "i");  
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return null
}

function setCookie(name, value, days){
var expireDate = new Date()
//set "expstring" to either an explicit date (past or future)
if (typeof days!="undefined"){ //if set persistent cookie
var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; "+popupboxBox.cookiesetting[1]
}
else //else if this is a session only cookie setting
document.cookie = name+"="+value+"; "+popupboxBox.cookiesetting[1]
}


var stitialvars=new Object() //temporary object to reference/ shorthand certain popupboxBox properties
stitialvars.freqtype=popupboxBox.displayfrequency[0] 
stitialvars.cookieduration=popupboxBox.displayfrequency[1] //"session" or int (integer specifying number of days)
stitialvars.cookiename=popupboxBox.cookiesetting[0]  


if (stitialvars.freqtype=="chance"){ //IF CHANCE MODE
if (Math.floor(Math.random()*popupboxBox.displayfrequency[1])==0)
popupboxBox.launch=true
}
else if (stitialvars.freqtype=="cookie" && stitialvars.cookieduration=="session"){ //IF "SESSION COOKIE" MODE
if (getCookie(stitialvars.cookiename+"_s")==null){ //if session cookie is empty
setCookie(stitialvars.cookiename+"_s", "loaded")
popupboxBox.launch=true
}
}
else if (stitialvars.freqtype=="cookie" && typeof parseInt(stitialvars.cookieduration)=="number"){ //IF "PERSISTENT COOKIE" MODE
if (getCookie(stitialvars.cookiename)==null || parseInt(getCookie(stitialvars.cookiename))!=parseInt(stitialvars.cookieduration)){ //if persistent cookie is empty or admin has changed number of days to persist from that of the stored value (meaning, reset it)
setCookie(stitialvars.cookiename, stitialvars.cookieduration, stitialvars.cookieduration)
popupboxBox.launch=true
} 
}

if (popupboxBox.launch){
popupboxBox.initialize();
}


 function opacity(id, opacStart, opacEnd, millisec) {
	 var speed = Math.round(millisec / 100);
	var timer = 0; changeOpac(opacStart, id);
 	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
function shiftOpacity(id, millisec) {
 	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}
function currentOpac(id, opacEnd, millisec) {
	 var currentOpac = 100;
	 if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}
 	 opacity(id, currentOpac, opacEnd, millisec)
}
