statusArray = new Array ( 0, 0, 0, 'checked in', 'checked out', 'waiting for approval', 'approval denied');

if (document.all) {
	CLASS = 'className'; // MSIE
} else {
	CLASS = 'class'; // good browser
}

function getElementId(clAr) {
	if ( elID = clAr[2] ) {
		if ( elID.indexOf('lrn-el-') > -1 ) {
			return elID.replace(/lrn\-el\-/,'');
		}
	}
	return false;
}

function getElementType(clAr) {
	if ( elTy = clAr[1] ) {
		if ( elTy.indexOf('lrn-') > -1 ) {
			return elTy.replace(/lrn\-/,'');
		}
	}
	return false;
}

function getElementStatus(clAr) {
	if ( elSta = clAr[4] ) {
		if ( elSta.indexOf('lrn-status-') > -1 ) {
			return elSta.replace(/lrn\-status\-/,'');
		}
	}
	return false;
}

function liveeditButton() {
	if( document.getElementById && document.getElementsByTagName ) {
		liveedit = getElementsByClassName('lrn-liveedit');
		if(liveedit.length > 0) {
	    	lrnElements = getElementsByClassName('lrn-element');
	    	for (e in lrnElements) {
	    		if (!isNaN(e)) {
		    		clAr = lrnElements[e].className.split(' ');
					editallowed = clAr.inArray('lrn-editallowed');
		    		editdisallowed = clAr.inArray('lrn-editdisallowed');
		    		elementID = getElementId(clAr);
		    		elementType = getElementType(clAr);
		    		elementStatus = getElementStatus(clAr);
					
					if ( elementID && elementType && elementStatus ) { // All required information available ?
						if ( editallowed ) {
							if ( elementStatus == 3 ) {
								imgsrc = 'le_checkout_and_editicon.gif';
							} else {
								imgsrc = 'le_editicon.gif';
							}
							newA = document.createElement('a');
			    			newA.setAttribute(CLASS,'lrn-editlink');	    			
			    			newA.setAttribute('href','javascript:editElement(' + elementID + ',\'' + elementType + '\')');
			    			newImg = document.createElement('img');
			    			newImg.setAttribute('src','/images/lrn/' + imgsrc);
			    			newImg.setAttribute('title', elementType + ' bearbeiten. Status: ' + statusArray[elementStatus]);
			    			newImg.setAttribute('width',13);
			    			newImg.setAttribute('height',13);
			    			newA.appendChild(newImg);
			    			lrnElements[e].insertBefore(newA,lrnElements[e].firstChild);

						} else if ( editdisallowed ) {
							if ( elementStatus > 3 ) {
								newImg = document.createElement('img');
				    			newImg.setAttribute('src','/images/lrn/le_editicon_grey.gif');
				    			newImg.setAttribute('title',elementType + '. ' + statusArray[elementStatus]);
				    			newImg.setAttribute(CLASS,'lrn-editlink');
				    			newImg.setAttribute('width',13);
				    			newImg.setAttribute('height',13);
				    			lrnElements[e].insertBefore(newImg,lrnElements[e].firstChild);
							}
						}
					}
	    		}
	    	}
		}
	}
}

addLoadEvent(liveeditButton);

