/*
 * 	Sprokets JavaScript Library
 *  (c) 2007 Sprokets LLC
 *  
 *  Author: Minch Design Inc. <info@minchdesign.com>
 *  PreloadSection.js, version .8.1
 *
 *  [ copyright statement  ]
 *
/*-------------------------------------------------------------------*/

var PreloadSection = new Class({
						 
	initialize: function(el, options) {
		
		this.options = Object.extend({
            preloadingClass:               'preloading',
			preloadingText:					'Loading Page Content',
			fadeSpeed:               		25,
			preloadingDelay:               500
        }, options || {});
		
		this.setOptions(this.options);
		
		this.preloadID;
		this.periodical;
		this.elOp = .1;
		this.preOp = .9;
		
		this.el = $(el);
		
		if(this.el) {
			this._setUp();
		}
		
	},
	
	_setUp: function() {
		var el = this.el;
		this.preloadID = this._createLoadingHTML();
		$(this.preloadID).injectBefore(el);
		el.setStyle('display','none');
		
		window.addEvent('load', function() { 
			this.periodical = this._runLoad.periodical(this.options.preloadingDelay, this);
		}.bind(this));
		
	},
	
	_pageLoaded: function() {
		
	},
	
	_runLoad: function() {
		var el = this.el;
		$clear(this.periodical);
		this.periodical = this._fadeBack.periodical(this.options.fadeSpeed, this);
		var tmp = $(this.preloadID);
		tmp.setStyle('display','none');
		
		el.setStyle('display','block');
		el.setOpacity(.1);
	},
	
	_fadeBack: function() {
		var el = this.el;
		
		if(this.elOp >= 1) {
			//$(this.preloadID).setStyle('display','none');
			$clear(this.periodical);
		} else {
			this.elOp = this.elOp + .1;
			//this.preOp = this.preOp - .1;
			el.setOpacity(this.elOp);
			//$(this.preloadID).setOpacity(this.preOp);
		}
	},
	
	_createLoadingHTML: function() {
		//$('myElement').setHTML(newHTML)
		var div = new Element('div').setHTML(this.options.preloadingText).injectInside($E('body'));
		var randId = $random(0,100) + $time();
		var divId = "pre"+randId;
		div.setProperty('id', divId);
		div.setProperty('class', this.options.preloadingClass);
		
		return divId;
	},
	
	blank: function() {} /* just so as I add more i dont have to do commas */
	
});

PreloadSection.implement(new Options);
PreloadSection.implement(new Events);
