// dynPopup.js

// cookies
var exp = new Date(); 
exp.setTime(exp.getTime() + (60*60*1000));

function GetCookie(CookieName){  
	var arg = CookieName + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function SetCookie (CookieName, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = CookieName + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

// setup segment
var dhtmlPopupWidth = 250;
var dhtmlPopupHeight = 268;
var origWidth, origHeight;
if (document.layers) {
  origWidth = window.innerWidth;
  origHeight = window.innerHeight;
}

// do on resize
function to_do_onresize() {
	if (document.layers) {
	 if (window.innerWidth != origWidth || window.innerHeight != origHeight) 
	    window.location.reload();
	} else setTimeout("winWd=getWinWidth()",200);
}
window.onresize = to_do_onresize;

var floatLyr, winWd, winHt;
function initFloat() {
  // arguments: id, left, top, width, height
  winWd = getWinWidth();
  winHt = getWinHeight();
  intLeftSideInit = winWd - 10 - dhtmlPopupWidth;
  floatLyr = new floatObj('floatDiv',intLeftSideInit,-350,dhtmlPopupWidth,dhtmlPopupHeight);
  floatLyr.show();
  floatLyr.floatDown();
}

var count = GetCookie('dhtmlPopup');
if (count == null) {
	window.onload = initFloat;
}

function floatObj(id,x,y,w,h) {
  this.fobj = dynObj;
  this.fobj(id,x,y,w,h);
  this.timer = 0; this.yOff = y; 
}

function floatRight() {
  if (this.timer) this.timer = 0;
  var xScroll = getScrollX();
  // get current position in viewable window area
	this.curX = parseInt(this.css.left) - xScroll;
  if (this.curX < winWd) {
    this.shiftBy(null,2);
  } else {  // if down below window bottom, start over
    this.restartFloat();
    return;
  }
 this.timer = setTimeout(this.obj + ".floatRight()", 20);
}

function floatDown() {
  if (this.timer) this.timer = 0;
  this.curY = parseInt(this.css.top);
  if (this.curY < 100) {
    this.shiftBy(null,310);
  } else {  // if down below window bottom, start over
    return;
  }
 this.timer = setTimeout(this.obj + ".floatDown()", 20);
}

function floatUp() {
  if (this.timer) this.timer = 0;
  this.curY = parseInt(this.css.top);
  if (this.curY > -350) {
    this.shiftBy(null,-30);
  } else {  // if down below window bottom, start over
    return;
  }
 this.timer = setTimeout(this.obj + ".floatUp()", 20);
}

function restartFloat() {
  this.hide();
  this.shiftTo(-this.width,this.yOff);
  this.show();
  this.timer = setTimeout(this.obj + ".floatRight()", 2000);
}

floatObj.prototype = new dynObj;
floatObj.prototype.floatDown = floatDown;
floatObj.prototype.floatUp = floatUp;
floatObj.prototype.restartFloat = restartFloat;



// core segment
/////////////////////////////////////////////////////////////////////
//  dynObj constructor
//		arguments: id (required): id of positioned div.
//		left,top,width,height optional arguments.
/////////////////////////////////////////////////////////////////////
function dynObj(id,x,y,w,h) {
	this.el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getLyrRef(id,document): null;
	if (!this.el) return null;
	this.doc = (document.layers)? this.el.document: this.el;
	this.css = (this.el.style)? this.el.style: this.el;
	var px = (document.layers||window.opera)? "": "px";
	this.x = x || 0;	if (x) this.css.left = this.x+px;
	this.y = y || 0;	if (y) this.css.top = this.y+px;
	this.width = w? w: (this.el.offsetWidth)? this.el.offsetWidth: (this.css.clip.width)? this.css.clip.width: 0;
	this.height = h? h: (this.el.offsetHeight)? this.el.offsetHeight: (this.css.clip.height)? this.css.clip.height: 0;
	// if w/h passed, set style width/height
	if (w){ (document.layers)? this.css.clip.width=w+px: this.css.width=w+px;}
	if (h){ (document.layers)? this.css.clip.height=h+px: this.css.height=h+px;}
	this.obj = id + "dynObj"; 	eval(this.obj + "=this");
}

function dw_show() { this.css.visibility = "visible"; }
function dw_hide() { this.css.visibility = "hidden"; }

function dw_shiftTo(x,y) {
	if (x!=null) this.x=x; if (y!=null) this.y=y;	
	// rounded below (this.x/y can hold decimals)
	if (this.css.moveTo) { 
		this.css.moveTo(Math.round(this.x),Math.round(this.y)); 
	} else { 
		this.css.left=Math.round(this.x)+"px"; 
		this.css.top=Math.round(this.y)+"px"; 
	}
}

function dw_shiftBy(x,y) {
	this.shiftTo(this.x+x,this.y+y);
}

function dw_writeLyr(cntnt) {
	if (typeof this.doc.innerHTML!="undefined") {
      this.doc.innerHTML = cntnt;
  } else if (document.layers) {
			this.doc.write(cntnt);
			this.doc.close();
  }
}

function dw_setBgClr(bg) {
	if (document.layers) this.doc.bgColor=bg;
	else this.css.backgroundColor=bg;
}

// assign methods 
dynObj.prototype.show = dw_show;
dynObj.prototype.hide = dw_hide;
dynObj.prototype.shiftTo = dw_shiftTo;
dynObj.prototype.shiftBy = dw_shiftBy;
dynObj.prototype.writeLyr = dw_writeLyr;
dynObj.prototype.setBgClr=dw_setBgClr;


// get reference to nested layer for ns4
// from dhtmllib.js by Mike Hall of www.brainjar.com
function getLyrRef(lyr,doc) {
	if (document.layers) {
		var theLyr;
		for (var i=0; i<doc.layers.length; i++) {
	  	theLyr = doc.layers[i];
			if (theLyr.name == lyr) return theLyr;
			else if (theLyr.document.layers.length > 0) 
	    	if ((theLyr = getLyrRef(lyr,theLyr.document)) != null)
					return theLyr;
	  }
		return null;
  }
}


// util segment
// center in container (window or outer layer)
// NOTE: uses this.width/height properties of dynObj
// so make sure those have been obtained/set appropriately
// for type of content!!!!
dynObj.prototype.centerIn = function(outer) {
	var outWd, outHt, inWd, inHt, x, y;
	if (eval(outer)==window) {
		outWd=getWinWidth();
		outHt=getWinHeight();
	} else {
		outWd=outer.width;
		outHt=outer.height;
	}
	inWd=this.width;
	inHt=this.height;
	x=Math.round((outWd-inWd)/2);
	y=Math.round((outHt-inHt)/2);
	this.shiftTo(x,y);
}

// gets rendered height/width 
// for ns4, pass reference to layer. 
// for others, pass reference to layer or
// id of html element containing content (depends...)
function getWidth(obj,id) {
	var wd=0;
	if (document.getElementById||document.all) {
		var elem;
		if (id)	elem = (document.getElementById)? document.getElementById(id): document.all[id];
		else elem = obj;
		if (elem.offsetWidth) wd = elem.offsetWidth;
  } else if (obj.document) wd = obj.document.width;	// ns4
	return wd;
}

function getHeight(obj,id) {
	var ht=0;
	if (document.getElementById||document.all) {
    var elem;
		if (id)	elem = (document.getElementById)? document.getElementById(id): document.all[id];
		else elem = obj;
		if (elem.offsetHeight) ht = elem.offsetHeight;
  } else if (obj.document) ht = obj.document.height;	// ns4
	return ht;
}

// credit to http://www.13thparallel.com for the following 4 functions
// see http://13thparallel.com/?issue=2002.06&title=viewport
// returns width of window
function getWinWidth() {
	var winWd = 0;
	if (document.documentElement && document.documentElement.clientWidth) 
		winWd = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth) 
		winWd = document.body.clientWidth;
	else if (document.body && document.body.offsetWidth) 
		winWd = document.body.offsetWidth; // ns6
	else if (window.innerWidth) winWd = window.innerWidth-18;
	return winWd;
}

// returns height of window
function getWinHeight() {
	var winHt = 0;
	if (window.innerHeight) winHt = window.innerHeight-18;
	else if (document.documentElement && document.documentElement.clientHeight) 
		winHt = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight) 
		winHt = document.body.clientHeight;
	return winHt;
}	

// returns amount of vertical scroll
function getScrollY() {
	var scroll_y = 0;
	if (document.documentElement && document.documentElement.scrollTop)
		scroll_y = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop) 
		scroll_y = document.body.scrollTop; 
	else if (window.pageYOffset)
		scroll_y = window.pageYOffset;
	else if (window.scrollY)
		scroll_y = window.scrollY;
	return scroll_y;
}

// returns amount of horizontal scroll
function getScrollX() {
	var scroll_x = 0;
	if (document.documentElement && document.documentElement.scrollLeft)
		scroll_x = document.documentElement.scrollLeft;
	else if (document.body && document.body.scrollLeft) 
		scroll_x = document.body.scrollLeft; 
	else if (window.pageXOffset)
		scroll_x = window.pageXOffset;
	else if (window.scrollX)
		scroll_x = window.scrollX;
	return scroll_x;
}

function closeMyWindow(){
  document.getElementById('floatDiv').style.display="none";
   
	return false;
}
