var HAMCAM_ONLINE = 1;
var HAMCAM_UPDATE_SMALL = 300; // 120;
var HAMCAM_UPDATE_LARGE = 120; // 10;
var hamcam_timer;

var hamcam_shrink_timer = undefined;

function hamcam_open_full ()
{
    window.open("/hamcam/", "hamcam", "width=710,height=675,scrolling=no,status=0,resize=0");
}

function hamcam_init ()
{
    /* check if hamcam is offline */
    if ( HAMCAM_ONLINE == 0 )
    {
	var sm_img = document.getElementById('hamcam_img_small');
	var lg_img = document.getElementById('hamcam_img_big');
	if ( sm_img ) sm_img.src ="/images/testpattern.jpg";
	if ( lg_img ) lg_img.src ="/images/testpattern.jpg";
    }
    else
    {
	hamcam_update(); // force an initial reload to be sure we have the latest
	hamcam_timer = setInterval (hamcam_update, HAMCAM_UPDATE_SMALL * 1000);
    }
}

function hamcam_setinterval (which)
{
    var interval = HAMCAM_UPDATE_SMALL;

    if ( which == "big" ) interval = HAMCAM_UPDATE_LARGE; 

    // clear the small timer and start the new timer
    clearInterval (hamcam_timer);
    hamcam_timer = setInterval (hamcam_update, interval * 1000);
}

function hamcam_expand ()
{
    var sm = document.getElementById('hamcam_teaser');
    var lg = document.getElementById('hamcam_big');
    var lg_img = document.getElementById('hamcam_img_big');
    var sm_img = document.getElementById('hamcam_img_small');

    if ( ! sm || ! lg ) return;

    if ( hamcam_shrink_timer )
    {
	debug ("shrink cancelled");
	clearTimeout(hamcam_shrink_timer);
	hamcam_shrink_timer = undefined;
    }

    // if we're already expanded then return
    if ( lg.style.display == "block" )
	return;

    lg.style.display = "block";
    sm.style.display = "none";

    if ( HAMCAM_ONLINE )
    {
	// clear the small timer and start the big timer
	clearInterval (hamcam_timer);
	hamcam_timer = setInterval (hamcam_update, HAMCAM_UPDATE_LARGE * 1000);

	// when we expand, if the large version points to the thumbnail,
	// replace it with the real version
	if ( lg_img )
	{
	    if ( lg_img.src.indexOf("Thumb") > 0 )
		hamcam_update();
	}
    }
}

function hamcam_shrink ()
{
    var sm = document.getElementById('hamcam_teaser');
    var lg = document.getElementById('hamcam_big');

    if ( ! sm || ! lg ) return;

    debug("hamcam shrink");
    lg.style.display = "none";
    sm.style.display = "block";
    hamcam_shrink_timer = undefined;

    if ( HAMCAM_ONLINE )
    {
	// clear the big timer and go back to the small timer
	clearInterval (hamcam_timer);
	hamcam_timer = setInterval (hamcam_update, HAMCAM_UPDATE_SMALL * 1000);
    }
}

function hamcam_shrink_trigger ()
{
    debug("shrink trigger");
    if ( hamcam_shrink_timer )
    {
	debug ("shrink cancelled");
	clearTimeout (hamcam_shrink_timer);
    }
    hamcam_shrink_timer = setTimeout (hamcam_shrink, 100);
}

function hamcam_update ()
{
    var sm = document.getElementById('hamcam_teaser');
    var lg = document.getElementById('hamcam_big');
    var sm_img = document.getElementById('hamcam_img_small');
    var lg_img = document.getElementById('hamcam_img_big');
    var url = "http://chimehosting.com/kpig/hamcam/";

    // if the large is displayed, update that for both sm and lg
    // otherwise use the thumbnail
    if ( lg.style.display == "block" )
	url += "hamcam.jpg";
    else
	url += "hamcamThumb.jpg";

    url += "?t=" + (new Date()).getTime();	// make it bypass cache

    var img = new Image();
    img.onload = function () {
	if ( sm_img ) sm_img.src = url;
	if ( lg_img ) lg_img.src = url;
    }

    // this is distracting since they load so fast it only flickers
    // if loading were to take a while, it would make sense to do
    // if ( sm_img ) sm_img.src = "/images/loader-107x80.gif";
    // if ( lg_img ) lg_img.src = "/images/loader-335x251.gif";
    img.src = url;
}

