var DL_bNS4=(document.layers);
var DL_bNS6 = (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko"));
var DL_bDOM=(document.getElementById);
var DL_bIE=(document.all);
var DL_bIE4=(DL_bIE && !DL_bDOM);
var DL_bMac = (navigator.appVersion.indexOf("Mac") != -1);
var DL_bIEMac=(DL_bIE && DL_bMac);
var DL_bIE4Mac=(DL_bIE4 && DL_bMac);
var DL_bNS =(DL_bNS4 || DL_bNS6);

var DL_nCurrentX = 0;
var DL_nCurrentY = 0;
var isHomePage=false;

function DL_fGetRealLeft(oElement)
{
    var nXPos = oElement.offsetLeft;
    var oParentEl = (DL_bIE4Mac) ? oElement.parentElement : oElement.offsetParent;	

    while (oParentEl != null)
	{
		if(DL_bIE4Mac)
		{
			if(oParentEl.tagName=="SPAN")
			{
				oParentEl = oParentEl.parentElement;
			}
			if(oParentEl.tagName=="HTML")
			{
				break;
			}
		}

        nXPos += oParentEl.offsetLeft;
		oParentEl = (DL_bIE4Mac) ? oParentEl.parentElement : oParentEl.offsetParent;
    }
    return nXPos;

}


function DL_fGetRealTop(oElement)
{
    var nYPos = oElement.offsetTop;
    var oParentEl = (DL_bIE4Mac) ? oElement.parentElement : oElement.offsetParent;	
	while (oParentEl != null)
	{
		if(DL_bIE4Mac)
		{
			if(oParentEl.tagName=="SPAN")
			{
				oParentEl = oParentEl.parentElement;
			}
			if(oParentEl.tagName=="HTML")
			{
				break;
			}
		}	
	    nYPos += oParentEl.offsetTop;
		oParentEl = (DL_bIE4Mac) ? oParentEl.parentElement : oParentEl.offsetParent;
    }
    return nYPos;
}
//allNotes
allDivs =document.getElementsByTagName('div');
numOfDivs = allDivs.length;
var allNotes= new Array(); 
numOfNotes = 0;
// get all notes DIVS
for (var i = 0; i < numOfDivs; i++) {
	if (allDivs[i].className=="notesLayer") {
		allNotes[numOfNotes] = allDivs[i];
		numOfNotes=numOfNotes+1;
	}
}

//initialize all notes layers
for (var j = 0; j < allNotes.length; j++) {
	currentNote = allNotes[j];
	var DL_oLayer=currentNote;
	if(DL_bNS4)	{
		var DL_bOrigWidth=innerWidth;
		var DL_bOrigHeight=innerHeight;
		window.onresize=function(){
			if(innerWidth!=DL_bOrigWidth||innerHeight!=DL_bOrigHeight)location.reload();
		}
		DL_oLayer.captureEvents(Event.MOUSEDOWN);
		DL_oLayer.onmousedown=DL_fGrabEl;
		//DL_oLayer.moveTo(document.images["ph"].x,document.images["ph"].y);
		DL_oLayer.visibility="show"
	} else	{
		dragResizer=DL_oLayer.getElementsByTagName('img')[DL_oLayer.getElementsByTagName('img').length-1];
		dragResizer.onmousedown= expand;  
		DL_oLayer.getElementsByTagName('div')[0].onmousedown=DL_fGrabEl;   
		DL_fPositionLayer();
		DL_oLayer.style.visibility="visible";
		if(DL_bNS6)window.onload = DL_fPositionLayer;
	}
}

function DL_fPositionLayer()
{
	//DL_oLayer.style.left = DL_fGetRealLeft(DL_oPH) + "px";
	//DL_oLayer.style.top = DL_fGetRealTop(DL_oPH) + "px";
	//DL_oLayer.style.left="100px";
	//DL_oLayer.style.top="100px";
}
currentZIndex=5001;
function DL_fGrabEl(e) {
	DL_oLayer = this.parentNode;
	currentZIndex=currentZIndex+1;
	DL_oLayer.style.zIndex=currentZIndex;
	document.getElementById('noteSeparatorLayer').style.width='100%';
	var oDoc = document;
	DL_nCurrentX = (DL_bNS) ? e.pageX : (event.clientX + oDoc.body.scrollLeft);
    DL_nCurrentY = (DL_bNS) ? e.pageY : (event.clientY + oDoc.body.scrollTop);
	if(DL_bNS4)oDoc.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	oDoc.onmousemove = DL_fMoveEl;
	oDoc.onmouseup = DL_fDropEl;
	return false;
}

function DL_fMoveEl(e) {
	var oDoc = document;
	var nNewX = (DL_bNS) ? e.pageX : (event.clientX + oDoc.body.scrollLeft);
    var nNewY = (DL_bNS) ? e.pageY : (event.clientY + oDoc.body.scrollTop);

	var nDistanceX = (nNewX - DL_nCurrentX);
    var nDistanceY = (nNewY - DL_nCurrentY);
    DL_nCurrentX = nNewX;
    DL_nCurrentY = nNewY;

	if (DL_bNS4) {
		DL_oLayer.moveBy(nDistanceX,nDistanceY);
	}
	else
	{
		DL_oLayer.style.left = parseInt(DL_oLayer.style.left) + nDistanceX + 'px';
		DL_oLayer.style.top = parseInt(DL_oLayer.style.top) + nDistanceY + 'px';
    }
	return false;
}

function DL_fDropEl() {
	if(DL_bNS) {document.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP)}
	document.onmousemove = document.onmouseup = null;
	document.getElementById('noteSeparatorLayer').style.width='0px';
	if (isHomePage==false) {
		setNoteData();
	}
	return false;
}

if (typeof Node != 'undefined') {
	Node.prototype.removeNode = function() { this.parentNode.removeChild(this) };
}

noteTemplateNode = document.getElementById('noteTemplate');
document.getElementById('noteTemplate').removeNode(true);
insertBeforeMarker = document.getElementById('bottomMarker');
var targetId;

function createElementWithName(typee, namee) {
  var element;
  // First try the IE way; if this fails then use the standard way
 if (document.all) {
	element = document.createElement('<'+typee+' name="'+namee+'" />');
  } else {
	element = document.createElement(typee);
	element.setAttribute('name', namee);
  }
  return element;
}

var targetId;
function insertNote(fadeIn,testing) {
	if (gup('groupnumber')!='' || testing==true){
		newNote = noteTemplateNode.cloneNode(true);
		targetId = newNote;
		if (fadeIn=="yes") {
			opacity1(targetId, 0, 90, 500); 
			newNote.style.left="100px";
			newNote.style.top="100px";
		} else {
			newNote.style.opacity = (90 / 100); 
			newNote.style.MozOpacity = (90 / 100); 
			newNote.style.KhtmlOpacity = (90 / 100); 
			newNote.style.filter = "alpha(opacity=90)"; 
		}
		infoContainer = document.getElementById('notesContainer');
		infoContainer.insertBefore(newNote,insertBeforeMarker);
		var DL_oLayer=newNote;
		if(DL_bNS4)	{
			var DL_bOrigWidth=innerWidth;
			var DL_bOrigHeight=innerHeight;
			window.onresize=function(){
				if(innerWidth!=DL_bOrigWidth||innerHeight!=DL_bOrigHeight)location.reload();
			}
			DL_oLayer.captureEvents(Event.MOUSEDOWN);
			DL_oLayer.onmousedown=DL_fGrabEl;
			//DL_oLayer.moveTo(document.images["ph"].x,document.images["ph"].y);
			DL_oLayer.visibility="show"
		} else	{
			DL_oLayer.getElementsByTagName('div')[0].onmousedown=DL_fGrabEl;  
			iframeBuffer=DL_oLayer.getElementsByTagName('iframe')[0];
			dragResizer=DL_oLayer.getElementsByTagName('img')[DL_oLayer.getElementsByTagName('img').length-1];
			dragResizer.onmousedown= expand;  
			DL_oLayer.getElementsByTagName('div')[0].onmousedown=DL_fGrabEl;  
			DL_oLayer.style.visibility="hidden";
		if (fadeIn=="yes") {
			DL_oLayer.style.visibility="visible";
			//alert();
		}
			//if(DL_bNS6)window.onload = DL_fPositionLayer;
		}
		//colorId=7;
		//DoFade(colorId, targetId);
		newNote.getElementsByTagName('iframe')[0].src=newNote.getElementsByTagName('iframe')[0].src;
	} else {
		alert('You must register to add notes. You can register in under 15 seconds at www.protonotes.com');
	}
	if (isHomePage==true) {
		DL_oLayer.getElementsByTagName('textarea')[0].focus();
	}
}

var currentTextArea
//var currentNote

function expand(e) {
	var oDoc = document;
	currentTextArea=this.parentNode.getElementsByTagName('textarea')[0];
	DL_oLayer=this.parentNode;
	DL_nCurrentX = (DL_bNS) ? e.pageX : (event.clientX + oDoc.body.scrollLeft);
    DL_nCurrentY = (DL_bNS) ? e.pageY : (event.clientY + oDoc.body.scrollTop);
	if(DL_bNS4)oDoc.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	oDoc.onmousemove = growIt;
	oDoc.onmouseup = DL_fDropEl;
	return false;
}
function contract(icon) {
	currentTextArea=icon.parentNode.parentNode.getElementsByTagName('textarea')[0];
	if(currentTextArea.style.height==""){currentTextArea.style.height="200px"}
	currentTextArea.style.height = parseInt(currentTextArea.style.height) - 50 + "px";
}
	
function growIt(e) {
	document.getElementById('noteSeparatorLayer').style.width='100%';
	var oDoc = document;
	var nNewX = (DL_bNS) ? e.pageX : (event.clientX + oDoc.body.scrollLeft);
    var nNewY = (DL_bNS) ? e.pageY : (event.clientY + oDoc.body.scrollTop);

	var nDistanceX = (nNewX - DL_nCurrentX);
    var nDistanceY = (nNewY - DL_nCurrentY);
    DL_nCurrentX = nNewX;
    DL_nCurrentY = nNewY;
	if (DL_bNS4) {
		DL_oLayer.moveBy(nDistanceX,nDistanceY);
	}
	else
	{
		currentDragBar=DL_oLayer.getElementsByTagName('div')[0];
		currentBufferIframe=DL_oLayer.getElementsByTagName('iframe')[0];
		if(currentTextArea.style.height==""){
			currentTextArea.style.height="200px";
			currentBufferIframe.style.height="247px";
		}
		
		snapshotHeight=currentTextArea.style.height;
		currentTextArea.style.height = parseInt(snapshotHeight) + nDistanceY + 'px';
		currentBufferIframe.style.height = parseInt(snapshotHeight) + nDistanceY + 47 + 'px';
		
		if(DL_oLayer.style.width==""){
			DL_oLayer.style.width="200px";
			currentBufferIframe.style.width = '200px';
		}
		
		snapshotWidth=DL_oLayer.style.width;
		DL_oLayer.style.width = parseInt(snapshotWidth) + nDistanceX + 'px';
		currentBufferIframe.style.width = parseInt(snapshotWidth) + nDistanceX + 'px';
		if (parseInt(DL_oLayer.style.width)<200) {
			DL_oLayer.style.width = '200px';
			currentBufferIframe.style.width = '200px';
		}
		if (parseInt(currentTextArea.style.height)<30) {
			currentTextArea.style.height = '30px';
			currentBufferIframe.style.width = '82px';
		}
		if(currentDragBar.style.width==""){currentDragBar.style.width="200px"}
		currentDragBar.style.width = parseInt(currentDragBar.style.width) + nDistanceX + 'px';
		if (parseInt(currentDragBar.style.width)<200) {
			currentDragBar.style.width = '200px';
		}
    }
	currentNote.getElementsByTagName('input')[2].value=parseInt(currentDragBar.style.width) + nDistanceX + 'px';
	currentNote.getElementsByTagName('input')[3].value=parseInt(currentTextArea.style.height) + nDistanceY + 'px';
	
	//currentBufferIframe.style.width="100" + "%";
	//currentBufferIframe.style.height="100%" + "%"; //for some reason, height doesn't work, but width does
	return false;
}

//var hide=false;
function showHideAllNotes(hide) {
	allDivs =document.getElementsByTagName('div');
	numOfDivs = allDivs.length;
	notesExist=false;
	// get all notes DIVS
	for (var i = 0; i < numOfDivs; i++) {
		if (allDivs[i].className=="notesLayer") {
			notesExist=true;
			if (hide==false) {
				allDivs[i].style.display="none";
			} else {
				allDivs[i].style.display="block";
			}
		}
	}
	if (notesExist==false) alert('There are no notes on this page.');
	//if (hide==false) {
		//document.getElementById('showHide').src = "images/talk.gif";
		//document.getElementById('showHide').title="show all notes";
		//hide=true;
	//} else {
		//document.getElementById('showHide').src = "images/noTalk.gif";
		//document.getElementById('showHide').title="hide all notes";
		//hide=false;
	//}
}

var reallyUnload=true; //the var is needed because onunload fires when you add a new note
function hideAllNotes() {//used when going from page to page to hide all notes
	if (reallyUnload==true) {
		allDivs =document.getElementsByTagName('div');
		numOfDivs = allDivs.length;
		for (var i = 0; i < numOfDivs; i++) {
			if (allDivs[i].className=="notesLayer") {
				allDivs[i].style.display="none";
			}
		}
	}
	reallyUnload=true;
}

function scrollNotes() {
	allDivs =document.getElementsByTagName('div');
	numOfDivs = allDivs.length;
	tempCounter=0;
	var notesToBeDeleted = new Array();
	// get all notes DIVS
	for (var i = 0; i < numOfDivs; i++) {
		//try {
			if (allDivs[i].className=="notesLayer") {
				
				topper = parseFloat(allDivs[i].getElementsByTagName('input')[1].value) * 1 - (window.frames['webFrame'].document.documentElement.scrollTop*1);
				allDivs[i].style.top = topper + "px";
			}
	}
}

//var leftLocation = -585;);
var windowWidth = (navigator.userAgent.indexOf("MSIE")!=-1) ? document.body.clientWidth : window.innerWidth;
var leftLocation = -windowWidth + 11;
var r=0
var t=0;
var rightLocation = 0;
var topLocation = 0;
var velocity = 1.01;
var accelFactor = 1.1;

function slideIt() {
	windowWidth = (navigator.userAgent.indexOf("MSIE")!=-1) ? document.body.clientWidth : window.innerWidth;
	leftLocation = -windowWidth + 11;
	if (parseInt(document.getElementById('menu').style.left) != leftLocation) {
		slideLeft();
		document.getElementById('webFrame').height = winHeight;
	} else {
		slideRight();
		document.getElementById('webFrame').height = winHeight-42;
	}
}

function slideRight() {
	velocity = velocity * accelFactor;
	r += velocity;
	if (r<rightLocation) {
		document.getElementById('menu').style.left = r + "px";
		setTimeout('slideRight()',1);
	} else {
		document.getElementById('menu').style.left = rightLocation + "px";
		velocity = 1.01;
		r = rightLocation;
		document.getElementById('menuPusher').src = "images/collapse.gif";
		document.getElementById('menu').style.position="relative";
	}  
	document.getElementById('menuPusher').title="slide in";
}

function slideLeft() {
	velocity = velocity * accelFactor;
	r -= velocity;
	if (r>leftLocation) {
		document.getElementById('menu').style.left = r + "px";
		setTimeout('slideLeft()',1);
	} else {
		document.getElementById('menu').style.left = leftLocation + "px";
		velocity = 1.01;
		r = leftLocation;
		document.getElementById('menuPusher').src = "images/expand.gif";
	}
	document.getElementById('menuPusher').title="slide out";
	document.getElementById('menu').style.position="absolute";
}

function goUrl() {
	urlEntered=document.getElementById('urlAddress').value;
	if (urlEntered.indexOf("http://")==-1){
		urlEntered = 'http://' + urlEntered;		
	} 
	//if (urlEntered.indexOf("www.")==-1){
			//urlEntered = urlEntered.substr(0,7) + "www." + urlEntered.substr(7,urlEntered.length)
	//}
	document.getElementById('urlAddress').value = urlEntered;
	//document.location="http://www.verilogue.com/annotateV/index1.php?page=" + document.getElementById('urlAddress').value;
	frames['webFrame'].location.href=document.getElementById('urlAddress').value;
	//updateNotes();
	//calcHeight();
}
function useFullProtonotes() {
	if (confirm('If you would like to add/edit/delete notes, you need to run the full version of Protonotes (requires IE). Click OK to do so.')) {
		document.location.href="http://www.protonotes.com/protonotes.hta?groupnumber=u1160035743&startpage=http://www.useit.com/";	
	}
}
var objectToDisappear;
function opacity(e,objID, opacStart, opacEnd, millisec) { 
    //speed for each frame 
	//window.event.cancelBubble = true;
	e = (e) ? e : window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	useFullProtonotes();
} 
function opacity1(objID, opacStart, opacEnd, millisec) { 
    //speed for each frame 
	//window.event.cancelBubble = true;
		//
		var speed = Math.round(millisec / 100); 
		var timer = 0; 
		objectToDisappear = objID;
	
		//determine the direction for the blending, if start and end are the same nothing happens 
		if(opacStart > opacEnd) { 
			for(i = opacStart; i >= opacEnd; i--) { 
				setTimeout("changeOpac(" + i + ",'" + objectToDisappear + "')",(timer * speed)); 
				timer++; 
			} 
		} else if(opacStart < opacEnd) { 
			for(i = opacStart; i <= opacEnd; i++) 
				{ 
				setTimeout("changeOpac(" + i + ",'" + objectToDisappear + "')",(timer * speed)); 
				timer++; 
			} 
		} 
} 

//change the opacity for different browsers 
function changeOpac(opacity, blahblah) { 
    var object = objectToDisappear.style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function fixFlash() {
	objElems = frames['webFrame'].document.body.getElementsByTagName("OBJECT");
	for (i=0;i<objElems.length;i++){
		//nod = frames['webFrame'].document.createElement("<param name='wmode' value='transparent'>")
		//objElems[i].insertBefore(nod);		
		objElems[i].style.zIndex="0";
		objElems[i].style.position="absolute";
	}
}
//end
function viewNotesIndex() {
	notesIndexPage="http://www.protonotes.com/getAllGroupNotes2.php?groupnumber=" + gup('groupnumber');
	document.location.href=notesIndexPage;
}
