/**
 * @author alex
 */
var LT = {};
LT.baseUrl = "http://" + location.host + "/";
// based on Rafael Lima's (http://rafael.adm.br) CSS Browser Selector   v0.2.5
LT.detector = {
	browser: "firefox",
	init: function() {
		var ua = navigator.userAgent.toLowerCase(),
			is = function(t){ 
				return ua.indexOf(t) != -1; 
			};
		this.browser = (!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie'):
				is('firefox/')? 	'firefox':
				is('opera')?		'opera':
				is('konqueror')?	'konqueror':
				is('applewebkit/')?	'safari':
				is('mozilla/')?		'mozilla':'';
				
		this.os = (is('x11')||is('linux'))?	'linux':
				is('mac')?					'mac':
				is('win')?					'win':'';
	}
}

LT.downloader = {	
	proceed: function() {
		LT.detector.init();
		var url = LT.baseUrl + "downloading/" + LT.detector.os + "/" + LT.detector.browser;
		//this.count();
		setInterval(function() {
			window.location.href = url;
		}, 6000);
	}
}
LT.makeId = function() {
	return 1*(Math.random().toString().replace('.', ''));
}
LT.player = {
	init: function(container) {
		this.playerName = 'flash';
		this.formats = ["mp3", "flash"];
		var d = document.createElement("div");
		container.appendChild(d);
		d.style.position = "absolute";
		this._id = LT.makeId();
		var flashvars = '"autostart=true&objectid=' + this._id + '"';
		if (/MSIE/.test(navigator.userAgent)) { // IE detection ~[-1]
			d.innerHTML = '<object id="' + this._id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1" height="1"><param name="allowScriptAccess" value="always" /><param name="movie" value="/js/mplayer.swf?' + this._id + '" /><param name="flashvars" value=' + flashvars + '/></object>';
		} else {
			d.innerHTML = '<embed id="' + this._id + '" type="application/x-shockwave-flash" width="1" height="1" src="/js/mplayer.swf?' + this._id + '" allowscriptaccess="always"  flashvars=' + flashvars + '/>';
		}
		container.innerHTML;
		this._player = d.firstChild;
	},
	play: function(url) {
		if (!this._player || !this._player.playDirectUrl) 
			return false;
		this._player.playDirectUrl(url);
		return true;
	},
	stop: function() {
		if (!this._player || !this._player.doStop) 
			return false;
		this._player.doStop();
		return true;
	},
	pause: function() {
		if (!this._player || !this._player.doPause) 
			return false;
		this._player.doPause();
		return true;
	},
	setState: function(state) {
		this.state = state;
		if (this.onstatechange) {
			this.onstatechange.call(this, state);
		}
	},
	isPlaying: function() {
		return this.state == 3 || this.state == 2;
	},
	states: {
		5:  "finished", 
		0:  "unknown", 
		1:  "stopped", 
		2:  "connecting", 
		3:  "playing",
		4:  "error"
	}	
}
LT.playOrStopLobby = function() {
	if (LT.player.isPlaying()) {
		return !LT.player.pause();
	} else {
		if (LT.player.state == 5) {
			LT.player.stop();
		}
		return !LT.player.play("/data/lobby_about_loudtalks.mp3");
	}
}
var RadioTime = {
	getUpdate: function(command, arg, objectid) {
		if (command == "status") {
			LT.player.setState(1*arg);
		}
	}	
}
