/* toasty.js - Originally created by Ruddick "Vinny" Lawrence
 * ------------------------------------------------------------------
 * Every once in awhile shows a popout image, a la the "toasty" guy
 * from Mortal Kombat. Switches between a few pictures. Requires
 * Prototype and Scriptaculous.
 */

var toasty = {
    init: function() {
	toasty.images = new Array();
	toasty.images[0] = new Array();
	toasty.images[0]['img'] = new Image();
	toasty.images[0]['img'].src = site.MEDIA_URL + "website/images/toasty_bryan1.png";
	toasty.images[0]['dir'] = 1;  //1 is right side, -1 is left side.
	toasty.images[1] = new Array();
	toasty.images[1]['img'] = new Image();
	toasty.images[1]['img'].src = site.MEDIA_URL + "website/images/toasty_bryan2.png";
	toasty.images[1]['dir'] = -1;  //1 is right side, -1 is left side.
	toasty.images[2] = new Array();
	toasty.images[2]['img'] = new Image();
	toasty.images[2]['img'].src = site.MEDIA_URL + "website/images/toasty_bryan3.png";
	toasty.images[2]['dir'] = 1;  //1 is right side, -1 is left side.
	toasty.images[3] = new Array();
	toasty.images[3]['img'] = new Image();
	toasty.images[3]['img'].src = site.MEDIA_URL + "website/images/toasty_bryan4.png";
	toasty.images[3]['dir'] = -1;  //1 is right side, -1 is left side.
	toasty.images[4] = new Array();
	toasty.images[4]['img'] = new Image();
	toasty.images[4]['img'].src = site.MEDIA_URL + "website/images/toasty_bryan5.png";
	toasty.images[4]['dir'] = -1;  //1 is right side, -1 is left side.

	toasty.image = $('toasty');
	toasty.container = $('container');
	if(toasty.image == null || toasty.container == null) return;

	toasty.min_time = 30; //shortest amount of time before toasty is seen (seconds)
	toasty.max_time = 4*60; //longest amount of time
	toasty.set_timer();
    },

    show_toasty: function() {
	toasty.image.src = toasty.images[toasty.pic]['img'].src;

	var toasty_dims = toasty.image.getDimensions();
	var container_dims = toasty.container.getDimensions();
	var win_dims = document.viewport.getDimensions();

	if(toasty.images[toasty.pic]['dir'] == 1)
	    var x = toasty.container.cumulativeOffset()['left'] + container_dims.width - toasty_dims.width;
	else
	    x = toasty.container.cumulativeOffset()['left'];
	var y = document.viewport.getScrollOffsets()[1] + win_dims.height - toasty_dims.height;
	toasty.image.setStyle({left: x + 'px', top: y + 'px', display: 'block'});
	new Effect.Move(toasty.image, {x: toasty.images[toasty.pic]['dir']*toasty_dims.width, y: 0, mode: 'relative', afterFinish: toasty.hide_toasty});
    },

    hide_toasty: function() {
	new Effect.Move(toasty.image, {x: -toasty.images[toasty.pic]['dir']*toasty.image.getWidth(), y: 0, mode: 'relative', afterFinish: toasty.set_timer});
    },

    set_timer: function() {
	toasty.image.hide();
	var rand = (Math.floor(Math.random()*(toasty.max_time-toasty.min_time+1))+toasty.min_time)*1000;
	toasty.pic = Math.floor(Math.random()*5);
	setTimeout(toasty.show_toasty, rand);
    }
}

Event.observe(window, 'load', toasty.init);