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

var _gaq = _gaq || [];
_gaq.push( [ '_setAccount', 'UA-10796814-5', '_trackPageview' ] );

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();
    
    // Note month and day are 0-based (hence April 10th, 2005 is Date( 2005, 3, 9 )).
    var climbdate = new Date( 2012, 3, 20 );

    // Magic number: 86400000 ms in a day
    var diff = Math.ceil( (climbdate - today) / 86400000 ) + 1;

    return( diff + ' days to get ready! (April 21, 2012)' );
}

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'
    );
}

function twitterBox() {
    var widget;

    widget = new TWTR.Widget(
        {
            version: 2,
            type: 'profile',
            rpp: 6,
            interval: 6000,
            width: 200,
            height: 300,
            theme: {
                shell: {
                    background: '#333333',
                    color: '#f5d99d'
                },
                tweets: {
                    background: '#f1ecde',
                    color: '#715066',
                    links: '#84573f'
                }
            },
            features: {
                scrollbar: true,
                loop: false,
                live: false,
                hashtags: true,
                timestamp: false,
                avatars: false,
                behavior: 'all'
            }
        }
    );

    return widget;
}

function gaPing() {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = 'http://www.google-analytics.com/ga.js';

    var s = document.getElementsByTagName( 'script' )[ 0 ];
    s.parentNode.insertBefore( ga, s );
}

document.onclick = closeMenu();
document.body.onload = gaPing();

