var menutimeout = 500;
var menutimer = 0;
var menuitem = 0;

function clearBox( box ) {
    if (box.value == "Search ClimbForWilderness.ca") {
        box.value = "";
        box.style.color = "#000000";
    }
}

function resetBox( box ) {
    if (box.value == "") {
        box.value = "Search ClimbForWilderness.ca";
        box.style.color = "#808080";
    }
}

function openMenu( menuid ) {
    cancelCloseMenuT();
    closeMenu();

    menuitem = document.getElementById( menuid );
    menuitem.style.visibility = 'visible';
}

function closeMenu() {
    if (menuitem) {
        menuitem.style.visibility = 'hidden';
    }
}

function closeMenuT() {
    menutimer = window.setTimeout( closeMenu, menutimeout );
}

function cancelCloseMenuT() {
    if (menutimer) {
        window.clearTimeout( menutimer );
        menutimer = null;
    }
}

function daysRemaining() {
    var today = new Date();
    var climbdate = new Date( 2011, 3, 22 );
    
    // Magic number: 86400000 ms in a day
    var diff = Math.ceil( (climbdate - today) / 86400000 ) + 1;

    return( diff + ' days to get ready! (April 23, 2011)' );
}

function calorieCalc( weight, units, mins ) {
    if (units == 'lbs') {
        weight = weight / 2.205;
    }

    return ((weight * mins * 8.995) / 60);
}

function showCalories() {
    var weight;
    var units;
    var mins;
    var calories;
    var err;

    weight = document.getElementById( 'weight' ).value;
    units = (document.getElementById( 'units_lbs' ).checked) ? 'lbs' : 'kg';
    mins = document.getElementById( 'mins' ).value;

    err = '';
    if (weight == '') {
        err = 'Please enter your weight in either kg or lbs.';
    }
    if (mins == '') {
        if (err != '') {
            err = err + '\n';
        }
        err = err + 'Please enter how many minutes it took you to climb.';
    }
    if (err == '') {
        calories = Math.round( calorieCalc( weight, units, mins ) );
        alert( 'Congratulations! You burned ' + calories + ' kCal climbing the Calgary Tower!' );
    } else {
        alert( err );
    }
}

function openGallery( gallery ) {
	var url;

	url = '/photos/2010/the-bit-photography/' + gallery + '/default.htm';
	
    popupWindow = window.open(
        url, 'popUpWindow',
		'height=600, width=800, left=10, top=10,' +
        'resizable=yes, scrollbars=yes, toolbar=no, menubar=no, ' +
        'location=no, directories=no, status=yes'
    );
}

document.onclick = closeMenu();
