
/**
* @version		$Id: modscreensave.js 0.2.0 2008-11-30 21:18:41  ~0 $
* @package		ScreenSave
* @license		GPLv3
* @copyright	Marko Zabreznik
*/

var ScreenSave = new Class({
	options: {
		imgUrls: [],
  		path: '/images/screensave/',
		wait: 10000,
		delay: 5000,
		opacity: 0.7,
  		preload: 0
	},
	initialize: function(op){
		this.setOptions(op);
		
		this.imgNr = 0;
		this.imgMx = 0;
		this.options.imgUrls.each(function (e){
			this.imgList.include(this.options.path+e);
			this.imgMx++;
			if (this.options.preload) new Element('img',{'src':this.options.path+e});
		}.bind(this));
		
		this.overlay=new Element('div',{'class': 'screensave', 'id': 'screensave'});
		this.image = new Element('img',{'class': 'screensaveImg', 'id': 'screensaveImg'});
		this.overlay.adopt(this.image);
		$(document.body).adopt(this.overlay);
		this.overlay.style.display = 'none';
		
		this.createCSSRule('#screensave', {
			'height' : getScrollHeight()+'px',
			'-moz-opacity':this.options.opacity,
			'filter': 'alpha(opacity='+(this.options.opacity*100)+')',
			'opacity':this.options.opacity,
			'position': 'absolute',
			'top': '0',
			'left': '0',
			'z-index': '90',
  			 'width': getScrollWidth()+'px',
 			'background': '#000'
		});
		this.createCSSRule('#screensaveImg', {
			'position': 'absolute'
		});	
		
		this.isOn = false;
		this.delay = $clear(this.delay);
		
		this.wait = this.startSS.bind(this).delay(this.options.wait);
		$(document.body).addEvent('mousemove', function(e) {
			if (this.isOn == true) {
				this.stopSS();
			}
			else {
				this.wait = $clear(this.wait);
				this.wait = this.startSS.bind(this).delay(this.options.wait);
			}
		}.bindWithEvent(this));
	},
	imgList: [],
 	startSS: function () {
		this.isOn = true;
		this.ShowHide(true);
		this.changeImage();
		this.overlay.style.display = 'block';

		this.delay = this.changeImage.bind(this).periodical(this.options.delay);
	},
 	stopSS: function () {
		this.isOn = false;
		this.delay = $clear(this.delay);
		this.ShowHide(false);
		this.overlay.style.display = 'none';
	},
 	changeImage : function () {
		this.image.style.display = 'none';
		this.image.src = this.imgList[(this.imgNr < this.imgMx)? this.imgNr++ : (this.imgNr = 0)];
		this.image.style.top = $random(getScrollTop()+20,getScrollTop()+getHeight()-this.image.height-20)+'px';
		this.image.style.left= $random(getScrollLeft()+20,getScrollLeft()+getWidth()-this.image.width-20)+'px';
		this.image.style.display = 'block';
	},
	ShowHide: function (visible) {
		$$("applet", "iframe", "select").visibility = (visible) ? "visible": "hidden";
	},
	createCSSRule: function(rule,attributes)
	{
		var newRule = "\n"+rule+"{\n";
		for (var attribute in attributes){newRule += "\t" + attribute + ": " + attributes[attribute] + ";\n";}
		newRule += "}\n";
		var styleTag = $E('style[type="text/css"]') || new Element("style").setProperty('type','text/css').injectInside(document.head);
		if(window.ie) styleTag.styleSheet.cssText += newRule;
		else styleTag.appendText(newRule);
	}
});
ScreenSave.implement(new Options);
