/**
* phpBB3 forum functions
*/

function selectCode(a)
{
	// Get ID of code block
	var e = a.parentNode.parentNode.getElementsByTagName('DIV')[2];
	// Not IE
	if (window.getSelection)
	{
		var s = window.getSelection();
		// Safari
		if (s.setBaseAndExtent)
		{
			s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
		}
		// Firefox and Opera
		else
		{
			var r = document.createRange();
			r.selectNodeContents(e);
			s.removeAllRanges();
			s.addRange(r);
		}
	}
	// Some older browsers
	else if (document.getSelection)
	{
		var s = document.getSelection();
		var r = document.createRange();
		r.selectNodeContents(e);
		s.removeAllRanges();
		s.addRange(r);
	}
	// IE
	else if (document.selection)
	{
		var r = document.body.createTextRange();
		r.moveToElementText(e);
		r.select();
	}
}


function linenumberOnOff(id){
	var parent = document.getElementById(id);
	var holder = parent.parentNode;

	if (parent.firstChild.nodeName == "OL"){
		var show = 'hide';
	} else if (parent.firstChild.nodeName == "DIV"){
		var show = 'show';
	}

	if (show == 'hide'){
		var child = parent.getElementsByTagName("ol");

		var children = child[0].childNodes;

		var replacement = document.createElement("div");
		replacement.setAttribute("id", id);
		replacement.setAttribute("class", parent.getAttribute("class"));
		replacement.setAttribute("className", parent.getAttribute("className"));
		replacement.setAttribute("style", parent.getAttribute("style"));
		
		for (var b = 0; b <= children.length - 1; b++){
			if (children[b].nodeType == 1){
				var row = document.createElement("div");

				row.setAttribute("class", children[b].getAttribute("class"));
				row.setAttribute("className", children[b].getAttribute("className"));
				row.setAttribute("style", children[b].getAttribute("style") + "; border-left: none;");
				row.style.cssText = 'border-left: none;';

				var rowdata = children[b].childNodes;

				for (var c = 0; c <= rowdata.length - 1; c++){
					row.appendChild(rowdata[c].cloneNode(true));
				}

				replacement.appendChild(row);
			}
		}
		
		holder.replaceChild(replacement, parent);
	} else {
		var replacement = document.createElement("div");
		replacement.setAttribute("id", id);
		replacement.setAttribute("class", parent.getAttribute("class"));
		replacement.setAttribute("className", parent.getAttribute("className"));
		replacement.setAttribute("style", parent.getAttribute("style"));

		var list = document.createElement("ol");

		var dds = parent.getElementsByTagName("div");

		for (var b = 0; b <= dds.length -1; b++){
			var row = document.createElement("li");
			row.setAttribute("class", dds[b].getAttribute("class"));
			row.setAttribute("className", dds[b].getAttribute("className"));
			row.setAttribute("style", dds[b].getAttribute('style') + "; border-left: 1px solid #999;");

			var rowdata = dds[b].childNodes;

			for (var c = 0; c <= rowdata.length - 1; c++){
				row.appendChild(rowdata[c].cloneNode(true));
			}

			list.appendChild(row);
		}

		replacement.appendChild(list);
		holder.replaceChild(replacement, parent);
	}
}

function expandCode(id){
	var parent = document.getElementById(id);

	if (parent.style.display === 'block' || parent.style.display === ''){
		parent.style.display = 'none';
	} else {
		parent.style.display = 'block';
	}
}

function highlightCodeLines(id){
	var parent = document.getElementById(id);
    var child = parent.getElementsByTagName('li');
    var li1 ="border-bottom: 1px solid #efefef; background-color: #fffffd;"
    var li2 ="border-bottom: 1px solid #efefef; background-color: #f9f9f9;"
    var noStyle ="border-bottom: none; background-color: #ffffff;"
    
    for(i=0;i<child.length;i++){
        if(child[i].className=="li1"){
            if (child[i].style.backgroundColor === '#fffffd' || child[i].style.backgroundColor === '' || child[i].style.backgroundColor === 'rgb(255, 255, 253)'){
                so_applyStyleString(child[i],noStyle);
            } 
            else {
                so_applyStyleString(child[i],li1);
            }
        }
        else{
            if (child[i].style.backgroundColor === '#f9f9f9' || child[i].style.backgroundColor === '' || child[i].style.backgroundColor === 'rgb(249, 249, 249)'){
                so_applyStyleString(child[i],noStyle);
            } 
            else {
                so_applyStyleString(child[i],li2);
            }

        }
	}
}

function so_applyStyleString(obj,str){
if(document.all && !window.opera)
    obj.style.setAttribute("cssText",str);
else
    obj.setAttribute("style",str);
}
