// Copy label name into URL for tabs
function parseTabs(rootElem)
{
	var rootElement = document.getElementById(rootElem).childNodes[1]; // <ul> -- start here, recurse
	if(rootElement)
	{
		// Find a reference point, so we can use different breadcrumbs
		var ref = explode('/', window.location.pathname);
		ref = ref[ref.length - 1];
		ref = explode('-', ref);
		ref = ref[0];

		var i = 0;
		currentNode = rootElement.childNodes[i];
		while(currentNode)
		{
			if(currentNode.tagName == 'LI') // <li> -- each list item
			{
				var j = 0;
				var currentSubNode = currentNode.childNodes[j];
				var currentPOI = '';
				while(currentSubNode)
				{
					if(currentSubNode.tagName == 'P') // <p> -- Item name
					{
						currentLabel = currentSubNode.innerHTML;
						if(currentLabel.indexOf('<') != -1)
						{
							currentLabel = currentLabel.substr(0, currentLabel.indexOf('<') - 1);
						}
						currentLabel = trim(currentLabel);
					}
					else if(currentSubNode.tagName == 'DIV') // <div> -- Item Routes
					{
						var k = 0;
						var currentSubSubNode = currentSubNode.childNodes[k];
						while(currentSubSubNode)
						{
							if(currentSubSubNode.tagName == 'A') // <a> -- Route links
							{
								var prependURI = (currentSubSubNode.href.indexOf('?') == -1 ? '?' : '&');
								currentSubSubNode.href += prependURI + 'ref=' + urlencode(ref) + '&bc=' + urlencode(currentLabel);
							}
							k++;
							currentSubSubNode = currentSubNode.childNodes[k];
						}
					}
					j++;
					currentSubNode = currentNode.childNodes[j];
				}
			}
			i++;
			currentNode = rootElement.childNodes[i];
		}
	}
	else
	{
		console.log('Failed to locate tab UL');
	}
}

function explode( delimiter, string, limit ) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // discuss at: http://phpjs.org/functions/explode
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function trim (str, charlist) {
    // Strips whitespace from the beginning and end of a string  
    // http://phpjs.org/functions/trim
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}


function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    var histogram = {}, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}
