/* --------------------------------------------------------------------

NET4VISIONS.COM ISTREAM - istream.js 28-10-2009
Version: 1.0
Created by: net4visions.com
Last edited: 10-28-2009
Descriptions: Present images based on folder content - autoresize using phpThumb 
License: MIT License (http://www.opensource.org/licenses/mit-license.php)
Requirements: mootools core 1.2.4.js || mootools more 1.2.4.2.js

-------------------------------------------------------------------- */

var Stream = new Class({
	Implements: [Options, Events],
		
	options: {
		path:		'./assets/site/scripts/request_cover.php',
		host:		'rs3.radiostreamer.com',
		port:		9330,
		current:	'true',
		history:	true,
		cover:		'true',
		altCover:	'true',
		type:		'html', // html or array
		size:		'MediumImage',
		spinner:	true,
		onCallback:	$empty
	},	
	
	initialize: function(element, options) {
		this.setOptions(options);
			
		this.element = $(element);
		if (!this.element) return false;
		return this.getStream();
	},

	// REQUEST
	getStream: function() {
		if (this.options.spinner) var spinner = new Spinner(this.element.getParent(), { message: 'Please...' });
		new Request.JSON({
		    method:			'post',
		    url:			this.options.path,
		    initialDelay:	0,
		    delay:			15000,
		    limit:			60000,
		    noCache:		true,
		    onRequest:		function() {
		    	if (this.options.spinner) spinner.show();
		    }.bind(this),
		    onSuccess: function(responseJSON, responseText) {
		    	this.element.empty();
		    	if ($chk(responseJSON['current'])) Elements.from(responseJSON['current']).inject(this.element);
		    	if ($chk(responseJSON['history'])) Elements.from(responseJSON['history']).inject(this.element);
		    	
		    	var tbls = this.element.getElements('table');
		    	tbls.each(function(item){
		    		new HtmlTable(item, {zebra: true}).updateZebras();
		    	});
		    	
		    	this.fireEvent('onCallback', this);
		    }.bind(this),
		   	onComplete: function() {
		    	if (this.options.spinner) spinner.hide();
		    }.bind(this)
		}).startTimer({
		    'host': this.options.host,
		    'port': this.options.port,
		    'cimg': this.options.cover,
		    'aimg': this.options.altCover,
		    'size': this.options.size,
		    'curr': this.options.current,
		    'type': this.options.type,
		    'hist': this.options.history ? 'show' : 'hide'   
		});
		
		return this;
	}
});