var iconwindow;
var iconscroller;
var applynowlink;
var applynowimg;

var maxdelta=18; // max scroll speed
var delta=0; // current scroll speed
var deltadelta=1; // acceleration rate

var framerate=20;

var lastquoteid;
var lastquotemenuid;

function scrollable() {
	return iconwindow.offsetWidth>iconscroller.offsetWidth;
}

function onleftarrowdown() {
	if (scrollable()) {
		delta=deltadelta;
		scroll();
	}

	return false;
}

function onleftarrowover() {
	document.getElementById('arrowleft').src='/images/arrowleftroll.gif';
	onleftarrowdown();
}

function onleftarrowout() {
	document.getElementById('arrowleft').src='/images/arrowleft.gif';
	onup();
}

function onrightarrowover() {
	document.getElementById('arrowright').src='/images/arrowrightroll.gif';
	onrightarrowdown();
}

function onrightarrowout() {
	document.getElementById('arrowright').src='/images/arrowright.gif';
	onup();
}

function onup() {
	delta=0;
}

function onrightarrowdown() {
	if (scrollable()) {
		delta=-deltadelta;
		scroll();
	}

	return false;
}

function scroll() {
	if (scrollable() && delta!=0) {
		if (delta<1) delta-=deltadelta; else delta+=deltadelta;
		if (delta<-maxdelta) delta=-maxdelta;
		if (delta>maxdelta) delta=maxdelta;

		var oldpos=parseInt(iconwindow.style.left);
		if (isNaN(oldpos)) oldpos=0;
		oldpos+=delta;
		if (oldpos>0) {
			oldpos=0;
			delta=0;
		}
		if (oldpos<-iconwindow.offsetWidth+iconscroller.offsetWidth) {
			oldpos=-iconwindow.offsetWidth+iconscroller.offsetWidth;
			delta=0;
		}

		iconwindow.style.left=oldpos+"px";

		setTimeout('scroll();',1000/framerate);
	}
}

function getid(node) {
	return node.id.match(/_([^_]+)$/)[1];
}

function oniconover() {
	var img=document.getElementById("quoteiconimg_"+getid(this));
	img.src=img.src.replace(/^(.*?)(roll)?\.gif$/i,"$1roll.gif");
}

function oniconout() {
	var id=getid(this);
	if (id!=lastquoteid) {
		var img=document.getElementById("quoteiconimg_"+id);
		img.src=img.src.replace(/roll\./i,".");
	}
}

function oniconclick() {

	var id=getid(this);
	//alert(id);
	if (id!=lastquoteid) {
		var iconimg=document.getElementById("quoteiconimg_"+id);
		if (!iconimg.src.match(/roll\./i)) iconimg.src=iconimg.src.replace(/\.gif$/i,"roll.gif");

		document.getElementById(id).style.visibility="visible";

		if (lastquoteid) {
			var lasticonimg=document.getElementById("quoteiconimg_"+lastquoteid);
			lasticonimg.src=lasticonimg.src.replace(/roll\./i,'.')
			document.getElementById(lastquoteid).style.visibility="hidden";
		}
		
		lastquoteid=id;
	}
}

function onquotemenuclick() {
	//var id=getid(this);
	/*if (id!=lastquotemenuid) { */
		document.getElementById("quoteicons_profile").style.display="block";
		this.className="quotemenucurrent";

		/*
		if (lastquotemenuid) {
			document.getElementById("quoteicons_"+lastquotemenuid).style.display="none";
			document.getElementById("quotemenu_"+lastquotemenuid).className="";
		}

		lastquotemenuid=id;
		*/
		fixscrollerwidth();
		
		var iconrandom = Math.floor(Math.random()*12+1);

		document.getElementById("quoteiconlink_profile"+iconrandom).onclick();
	//}
}

function fixscrollerwidth() {
	var oldwidth=iconscroller.style.width;
	iconscroller.style.visibility="hidden";
	iconscroller.style.width="1000px";
	iconwindow.style.width=iconwindow.offsetWidth+"px";
	iconscroller.style.width=oldwidth;
	iconscroller.style.visibility="visible";
}

function onapplynowover() {
	applynowimg.src='/images/applynowroll.gif';
}

function onapplynowout() {
	applynowimg.src='/images/applynow.gif';
}

function takeanumber(numstr) {
	var ary = numstr.split('|');
	var rnum = Math.floor(Math.random()*ary.length);
	
	return ary[rnum];
}

function onbodyload() {
	applynowlink=document.getElementById('applynowlink');
	applynowimg=document.getElementById('applynowimg');
	if (applynowlink && applynowimg) {
		applynowimg.onmouseover=onapplynowover;
		applynowimg.onmouseout=onapplynowout;
	}

	iconwindow=document.getElementById('iconwindow');
	iconscroller=document.getElementById('iconscroller');

	if (iconscroller && iconwindow) {
		// register events for icons
		for (var i=0; i<iconwindow.childNodes.length; i++) {
			var div=iconwindow.childNodes[i];
			if (div.nodeName=="DIV") {
				for (var j=0; j<div.childNodes.length; j++) {
					var a=div.childNodes[j];
					if (a.nodeName=="A") {
						a.onmouseover=oniconover;
						a.onmouseout=oniconout;
						a.onclick=oniconclick;
					}
				}
			}
		}

		// register events for links
		/* removed, combined to one profiles icon scroll 
		var quotemenu=document.getElementById('quotemenu');
		var firstquotemenu;
		for (var i=0; i<quotemenu.childNodes.length; i++) {
			var a=quotemenu.childNodes[i];
			if (a.nodeName=="A") {
				if (!firstquotemenu) firstquotemenu=getid(a);
				a.onclick=onquotemenuclick;
			}
		}
		*/
		
		onquotemenuclick();

		document.body.onmouseup=onup;

		var arrowleft=document.getElementById("arrowleftlink");
		arrowleft.onmousedown=onleftarrowdown;
		arrowleft.onmouseover=onleftarrowover;
		arrowleft.onmouseout=onleftarrowout;
		var arrowright=document.getElementById("arrowrightlink");
		arrowright.onmousedown=onrightarrowdown;
		arrowright.onmouseover=onrightarrowover;
		arrowright.onmouseout=onrightarrowout;

		//document.getElementById("quotemenu_"+firstquotemenu).onclick();
	}

	// highlights
	var lasthighlight=0;

	while (document.getElementById("highlight"+(lasthighlight+1))) {
		lasthighlight++;
	}

	if (lasthighlight>0) {
		var selectedhighlight=Math.floor(Math.random()*lasthighlight)+1;
		document.getElementById("highlight"+selectedhighlight).style.display="inline";
	}

	var middlelinks=document.getElementById("middlemenu");
	if (middlelinks) {
		for (var i=0; i<middlelinks.childNodes.length; i++) {
			var a=middlelinks.childNodes[i];
			if (a.nodeName=="A" && window.location.href.match(a.href)) {
				a.className="selected";
			}
		}
	}

	var rightbarlinks=document.getElementById("rightbarlinks");
	if (rightbarlinks) {
		for (var i=0; i<rightbarlinks.childNodes.length; i++) {
			var a=rightbarlinks.childNodes[i];
			if (a.nodeName=="A" && window.location.href.match(a.href)) {
				a.className="selected";
			}
		}
	}

	var leftbarlinks=document.getElementById("leftbarlinks");
	if (leftbarlinks) {
		for (var i=0; i<leftbarlinks.childNodes.length; i++) {
			var a=leftbarlinks.childNodes[i];
			if (a.nodeName=="A" && window.location.href.match(a.href)) {
				a.className="selected";
			}
		}
	}

	var banner=document.getElementById("banner");
	if (banner) {
		bnum = takeanumber("1|2|3|4|5|6|7|8|9|10");
		banner.className="hd"+bnum;
	}
}

function randomimg( eId, prefix, numbers, ext ) {
	var obj = document.getElementById( eId );
	if (obj) {
		obj.src = prefix + takeanumber( numbers ) + ext;
	}
}

function dropmenu(targ,selObj,restore){
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

function openpopup( vURL, w, h ){
	popupWindow=window.open( vURL,'','width=' + w + ', height=' + h + ', location=no, menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, directories=no')
	popupWindow.focus()
}

function openlibchat(){
	chatWindow=window.open('','','width=420, height=320, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, directories=no')
	chatWindow.document.write("<html><head><title>Chat Window</title></head><body bgcolor='#eeeecc' marinwidth='0' marginheight='0' topmargin='0' leftmargin='0' style='text-align:center'><div style='width:400;text-align:left;margin-left:auto;margin-right:auto;'><object width='400' height='300'><param name='movie' value='http://widget.meebo.com/mm.swf?WgITcpGjar'/><embed src='http://widget.meebo.com/mm.swf?WgITcpGjar' type='application/x-shockwave-flash' width='400' height='300'></embed></object></div></body></html>")
	chatWindow.focus()
}

function openwebtour(){
	webtourWindow=window.open('/lawlibrary/webtour/webtour.html','','width=580, height=436, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, directories=no')
	webtourWindow.focus()
}

function openvideo( videoURL, w, h ){
	openpopup( videoURL, w, h )
}
