/* ---------------------------------------------------------------------
 * This is based on the Stiky Notes script from Dynamic Drive DHTML code
 * library. The original copyright header is listed below.
 * I did some modification and name space changes.
 * $Id: notes.js,v 1.2 2007/01/31 20:21:49 wyan Exp $
 -------------------------------------------------------------------- */

/***********************************************
* Sticky Note script- ? Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use
* Go to http://www.dynamicdrive.com/ for full source code
***********************************************/

function notesObj(argsObj) {
	//Specify display mode. 3 possible values are:
	//1) "always"- This makes the fade-in box load each time the page is displayed
	//2) "oncepersession"- This uses cookies to display the fade-in box only once per browser session
	//3) integer (ie: 5)- Finally, you can specify an integer to display the box randomly via a frequency of 1/integer...
	// For example, 2 would display the box about (1/2) 50% of the time the page loads.

	this.displaymode="always"

	this.enablefade="yes" //("yes" to enable fade in effect, "no" to disable)
	this.autohidebox=["yes", 10] //Automatically hide box after x seconds? [yes/no, if_yes_hide_after_seconds]
	this.showonscroll="yes" //Should box remain visible even when user scrolls page? ("yes"/"no)
	this.showonscrollvar = null; // timeout function ref
	this.mozfadevar = null;      // timeout function ref
	this.objframeref = null;     // the iframe ref, hack for IE.
	this.objref = null;          // the content div id ref
	this.docheight = null;       
	this.objheight = null;

	for (arg in argsObj) { this[arg] = argsObj[arg]; }
}

notesObj.IEfadelength=1 //fade in duration for IE, in seconds
notesObj.Mozfadedegree=0.05 //fade in degree for NS6+ (number between 0 and 1. Recommended max: 0.2)

notesObj.prototype.displayfadeinbox = function() {
	var ie=document.all && !window.opera
	var dom=document.getElementById
	var iebody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body

    // This iframe is a hack for IE to cover the windowed controls, e.g. select box in IE.
    // This iframe is sitting between the sticky notes layer and the main window page layer.
	this.objframeref=(dom)? document.getElementById("fadeinboxframe") : document.all.fadeinboxframe	

	this.objref=(dom)? document.getElementById("fadeinbox") : document.all.fadeinbox;

	var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset;
	var docwidth=(ie)? iebody.clientWidth : window.innerWidth
	this.docheight=(ie)? iebody.clientHeight: window.innerHeight
	var objwidth=this.objref.offsetWidth
	this.objheight=this.objref.offsetHeight
	this.objref.style.left=docwidth/2-objwidth/2+"px"
	this.objref.style.top=scroll_top+this.docheight/2-this.objheight/2+"px"

	// Adjust the geometry for the iframe underneath.
	this.objframeref.style.left=this.objref.style.left;
	this.objframeref.style.top=this.objref.style.top;
	this.objframeref.style.width = objwidth +"px";
	this.objframeref.style.height= this.objheight+"px";

/*
myLogger.log("::displayfadeinbox left:top:width:height " + this.objframeref.style.left +":" +
                                                           this.objframeref.style.top  + ":"+
                                                           this.objframeref.style.width+":"+
                                                           this.objframeref.style.height );	
                                                           */
	var _this = this;

	if (this.showonscroll=="yes")
		this.showonscrollvar=setInterval(function(){_this.staticfadebox()}, 50)

	if (this.enablefade=="yes" && this.objref.filters){
//myLogger.log("::displayfadeinbox 0... " + this.objref.filters + "\n");
		this.objref.filters[0].duration = notesObj.IEfadelength
		this.objref.filters[0].Apply()
		this.objref.filters[0].Play()
	}

//myLogger.log("::displayfadeinbox objref visibility " + this.objref.style.visibility + "\n");
//myLogger.log("::displayfadeinbox objref MozOpacity " + this.objref.style.MozOpacity + "\n");
	this.objframeref.style.visibility="visible";
	this.objref.style.visibility="visible"
	if (this.objref.style.MozOpacity){
		if (this.enablefade=="yes") {
			this.mozfadevar=setInterval(function(){_this.mozfadefx()}, 90)
		}
		else{
			this.objref.style.MozOpacity=1
			this.controlledhidebox()
		}
	}
	else
		this.controlledhidebox()

}

notesObj.prototype.mozfadefx = function() {
	if (parseFloat(this.objref.style.MozOpacity)<1)
		this.objref.style.MozOpacity=parseFloat(this.objref.style.MozOpacity)+notesObj.Mozfadedegree;
	else{
		clearInterval(this.mozfadevar)
		this.controlledhidebox()
	}
}


notesObj.prototype.staticfadebox = function(){
	var iebody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
	var ie=document.all && !window.opera;
	var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset
	this.objref.style.top=scroll_top+this.docheight/2-this.objheight/2+"px";
	this.objframeref.style.top=this.objref.style.top;
}


notesObj.prototype.hidefadebox = function(){
	this.objref.style.visibility="hidden"
	this.objframeref.style.visibility="hidden"
	if (typeof this.showonscrollvar!="undefined")
		clearInterval(this.showonscrollvar)
}

notesObj.prototype.controlledhidebox = function(){
	var _this = this;
	if (this.autohidebox[0]=="yes"){
		var delayvar=(this.enablefade=="yes" && this.objref.filters)? (this.autohidebox[1]+this.objref.filters[0].duration)*1000 : this.autohidebox[1]*1000;
		setTimeout(function(){_this.hidefadebox()}, delayvar)
	}
}

notesObj.prototype.initfunction = function(){
  var _this = this;
  setTimeout(function(){_this.displayfadeinbox();}, 100);
}

/* Cookie is disabled */
/*
notesObj.get_cookie = function(Name) {
var search = Name + "="
var returnvalue = ""
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset)
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
*/


/* myNotes is the instance of notesObj */
function notesRegisterOnLoad(myNotes)
{
  var oldOnLoad;

  var random_num;
  if (parseInt(myNotes.displaymode)!=NaN)
	random_num=Math.floor(Math.random()*myNotes.displaymode);
 
  if (myNotes.displaymode=="oncepersession" || myNotes.displaymode=="always" || parseInt(myNotes.displaymode)!=NaN && random_num==0){
	oldOnLoad = window.onload;
	if (typeof window.onload != 'function') {
	    window.onload = function() {
      		myNotes.initfunction();
	    };
 	 } else {	
	    window.onload = function() {
	      oldOnLoad();
	      myNotes.initfunction();
    	    };
	 }
  }
}


