function getBaseURL() {
    var url = location.href;  	// entire url including querystring - also: window.location.href;
    var baseURL = url.substring (0, url.indexOf('/', 15));

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url 		= location.href;	// window.location.href;
        var pathname 		= location.pathname;  	// window.location.pathname;
        var index1 		= url.indexOf(pathname);
        var index2 		= url.indexOf("/", index1 + 1);
        var baseLocalUrl 	= url.substr(0, index2);

        return baseLocalUrl + "/DanAndAssana/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }
}

// Update a <div id="sidebar"> with data.

function InsertSectionInSidebar(sectionImageURL, section, baseURL) {
    var sidebar = document.getElementById("sidebar");
    
    // Add the image, <br>, and <ul>
    var image = document.createElement("img");
    image.src = baseURL + sectionImageURL;
    image.alt = section.alt;
    image.width = section.width;
    image.height = "30";
    sidebar.appendChild(image);
    var list = document.createElement("ul");
    sidebar.appendChild(list);
    
    for (var key in section.list) {
	var url = section.list[key].url;
	var title = document.createTextNode(key);
	var date = document.createTextNode(" (" 
	    + countDown(section.list[key].month, section.list[key].day, section.list[key].year) + ")");
	var item = document.createElement("li");
	list.appendChild(item);
	
	if (url.length) {
	    var a = document.createElement("a");
	    if (url.indexOf('http://') != -1) {
		a.href = url;
		a.target = "_blank";
	    } else {
		a.href = baseURL + url;
	    }
	    a.appendChild(title);
	    a.appendChild(date);
	    item.appendChild(a);
	} else {
	    item.appendChild(title);
	    item.appendChild(date);
	}
    }
    // Need to add the <br />
    var b = document.createElement("br");
    sidebar.appendChild(b);
}

function UpdateSidebar() {
    // Get the URL for the current document and calculate the base. This will allow
    // local editing of the page.
    var baseURL = getBaseURL();
    
    // Display name is the "key", the URL is the "value" (empty string if no URL).
    var recentTrips = {
	"Rome, Italy": 		{'url': "trips/rome", 		'month':10, 'day':18,	'year':2011},
	"Dalmatia":		{'url': "trips/dalmatia", 	'month':10, 'day':4,	'year':2011},
    };
    var recentUpdates = {
	"San Martin, CA": 	{'url': "updates/cordevalle", 	'month':12, 'day':27,	'year':2011},
	"Muir Woods with NPF": 	{'url': "updates/muirwoods", 	'month':11, 'day':12,	'year':2011},
	"Goodbye Rascal": 	{'url': "updates/rascal", 	'month':11, 'day':3,	'year':2011},
    };
    var upcomingTrips = {
	"Sedona, AZ": 		{'url': "",	'month':2,	'day':17,	'year':2012},
	"Palm Springs, CA": 	{'url': "",	'month':3,	'day':30,	'year':2012},
	"Willamette Valey, OR": {'url': "",	'month':5,	'day':19,	'year':2012},
	"The Arctic!": 
	    {'url':"http://www.geoex.com/adventure-travel/arctic/complete-circumnavigation.asp",
	    	'month': 7,	'day': 20,	'year':2012},
	"Glacier NP, MT": 	{'url': "",	'month':7,      'day':14,	'year':2012},
    };
    
    // Create a container for each section.
    var sections = {
	"images/recentTrips.png": 	{ 'list': recentTrips, 	'alt': 'Recent trips', 'width': 100},
	"images/recentUpdates.png": 	{ 'list': recentUpdates,'alt': 'Recent Updates', 'width': 120}, 
	"images/upcoming.png": 		{ 'list': upcomingTrips,'alt': 'Upcomig Trips', 'width': 100}
    };

    for (var sectionImageURL in sections) {
	InsertSectionInSidebar(sectionImageURL, sections[sectionImageURL], baseURL);
    }
}


