var absPath = document.location.href;
var basePath = absPath.replace(/[^\/]+$/, '');

function getAbsPath (file) {
    return basePath + file;
}

var nav_links = new Array(
    getAbsPath('for-insurance.html'),
    getAbsPath('for-telecommunications.html'),
    getAbsPath('for-online-services.html'),
    getAbsPath('for-banking.html'),
    getAbsPath('for-credit.html'),
    getAbsPath('for-capital-markets.html'),
    getAbsPath('for-healthcare.html'),
    getAbsPath('for-partners.html'),
    getAbsPath('for-investors.html')
);

connect(window, 'onDOMload', 
    function () {

        // Reorder nav links using the prediction server service
        var container = byId('left');

        if (!container) {
            // On a site page other than the home page.
            container = byId('left-sub');
        }                    

        // Just return if there's nothing to reorder.
        if (!container) return;

        // Send all the available nav links to the prediction service and get back
        // an ordered list.
        var links = '';
        var nav_link;
        var available_links = new Array();

        for (var i=0; i<nav_links.length; i++) {
            nav_link = byId('left_nav_' + (i+1));
            if (nav_link) {
                if (links.length > 0) links += '\t';
                links += nav_links[i];
                available_links.push(i);
            }
        }

        // Do the request    
	    var req = doJSONRequest('/vortexpredictions_get_predictions',
            {op: 'list',
		    keys: links});

	    req.addCallback(function (resp) {
		    // If we successfully get the list, reorder the links.
		    if (resp) {
                if (resp.matches) {
                    // Remove each of the links from container and re-add
                    // them to the container in the correct order.
                    var els = {};
                    for (var i=0; i<available_links.length; i++) {
                        link_id = 'left_nav_' + (available_links[i] + 1);
                        link_url = nav_links[available_links[i]];
                        els[link_url] = container.removeChild(byId(link_id));
                    }

                    for (var i=0; i<resp.matches.length; i++) {
                        container.appendChild(els[resp.matches[i][0]]);
                    }
                }
                else {
                    console.log('order_nav_links: ' + resp.msg);
                }
		    }
	    });

	    req.addErrback(function (resp) {
            log('order_nav_links: ' + resp);
	    });
    }
);

function update_link (target, url) {
    // Triggered on click of an HTML link. Sends an update request to the
    // prediction server and then redirects to the requested page.
    if (!url) url = target;

    var abs_url = getAbsPath(url);
    var req = doJSONRequest('/vortexpredictions_update', {key: abs_url});
    req.addBoth(function(resp) {
        window.location.href = getAbsPath(target);
    });
}

function log(msg) {
    try {
        console.log('order_nav_links: ' + msg);
    }
    catch (e) {}
}

function getAbsUrl (url) {
    if (url.substr(0, 1) != '/') url = '/' + url;
	return 'http://' + document.location.hostname + url;
}

function doJSONRequest (relative_url, postdata) {
	if (!postdata) {
		postdata = {};
	}
	
	return loadJSONDoc(getAbsUrl(relative_url), postdata);
}

function byId (id) {
    return document.getElementById(id);
}

