var spinnerImg = '/img/spinner.gif';

function slider(layerID){
	layer = "contentlayer_"+layerID;
	new Effect.toggle(layer, "SLIDE", {duration: .25});
	linkID = "clicker_"+layerID;
	divstyle= document.getElementById(linkID).style;
	if(divstyle.backgroundPosition == "" || divstyle.backgroundPosition == "0px 0px"){
		divstyle.backgroundPosition = "0px -33px";
	} else{
		divstyle.backgroundPosition = "0px 0px";
	}
}

function expandCollapseAll() {
	var task = document.getElementById('expandCollapseAll')
	content = document.getElementsByName('contentlayer');
	clickers = document.getElementsByName('clicker');
	if (task.innerHTML == 'Expand All') {
		for (i=0; i<content.length; i++) {
			content[i].style.display = 'inline';
			clickers[i].style.backgroundPosition = '0px 0px';
		}
		task.innerHTML = 'Collapse All';
	} else if (task.innerHTML == 'Collapse All') {
		for (i=0; i<content.length; i++) {
			content[i].style.display = 'none';
			clickers[i].style.backgroundPosition = '0px -33px';
		}
		task.innerHTML = 'Expand All';
	}
}


function embedLinks( whichLayer )
{
  var elem, vis;
  if( document.getElementById )
    elem = document.getElementById( whichLayer );
  else if( document.all ) // old msie version
      elem = document.all[whichLayer];
  else if( document.layers ) // firefox
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function PopIt(url) {
	w = 820;
	h = 620;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'width='+w+', height='+h+', top='+TopPosition+',left='+LeftPosition+',scrollbars=auto,resizable=yes';
	newwindow=window.open(url,'name',settings);
	if (window.focus) { newwindow.focus() }
}

function showArrow(key, direction){
	if (!key || key == 0)
		key = "User.lname";
	arrowdiv = document.getElementById(key);
	arrowdiv.className = direction;
}
function ajaxLoading() {
	tmp = document.getElementById('spinner');
	tmp.style.display = "inline";
}
function ajaxComplete() {
	tmp = document.getElementById('spinner');
	tmp.style.display = "none";
}

/**
 * The following two functions are used to display a spinner within an individual element for things like buttons
 */
var replaceObjs = [];
function ajaxLoadingReplace(objId) {
	//alert(objId);
	var tmp = document.getElementById(objId);
	replaceObjs[objId] = tmp.innerHTML;
	tmp.innerHTML = '<img src="'+spinnerImg+'">';
}
function ajaxCompleteReplace(objId) {
	var tmp = document.getElementById(objId);
	tmp.innerHTML = replaceObjs[objId];
}

/**
 * The following 3 functions are for the ajax dialog boxes
 */
function dialogAjaxComplete(replaceObj) {
	dlg = document.getElementById('dialog');
	dlg.style.visibility = "visible";
	var yOffset = (window.pageYOffset) ? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	dlg.style.top = (yOffset+125) + "px";
	if (replaceObj == undefined)
		ajaxComplete();
	else
		ajaxCompleteReplace(replaceObj);
}

/**
 * replaceObj is optional and refers to replacing the contents of an element with the spinner
 */
function showDialog(pageToLoad, replaceObj) {
	if (replaceObj == undefined) {
		new Ajax.Updater(
			'dialog', pageToLoad,
			{asynchronous:true, evalScripts:true, onComplete:function(request, json) {dialogAjaxComplete()},
			onLoading:function(request) {ajaxLoading()},
			requestHeaders:['X-Update', 'content']}
		)
		Event.observe(document, 'keydown', hideDialog);
	} else {
		new Ajax.Updater(
			'dialog', pageToLoad,
			{asynchronous:true, evalScripts:true, onComplete:function(request, json) {dialogAjaxComplete(replaceObj)},
			onLoading:function(request) {ajaxLoadingReplace(replaceObj)},
			requestHeaders:['X-Update', 'content']}
		)
		Event.observe(document, 'keydown', hideDialog);
	}
}

function hideDialog(e) {
	if (e) {
		e = (window.event) ? window.event: e;
		if (e.keyCode!=27) {
			return false;
		}
	}
	dlg = document.getElementById('dialog');
	dlg.style.visibility = 'hidden';
	Event.stopObserving(document, 'keydown', hideDialog);
}
