/**
 * @copyright 2007 "Rambler Internet Holding"
 * @author Alexey Golubchik
 */
function showMore(id) {
	var b, t, s = document.getElementById(id).style,
	h = innerHeight || document.body.clientHeight;
	t = document.body.getElementsByTagName('TABLE')[0];
	b = t.offsetHeight >= h;
	s.display =  s.display ? '': 'none';
	b && (
	window.resizeBy(0, t.offsetHeight - h)
	);
	return s.display == ''
}
function _showPos() {
	var p = oPlayer, t, d;
	if (typeof p.o.controls == 'undefined') {
	return
	}
	t = p.o.controls.currentPosition;
	//p.oPosition.innerHTML = p.getTimeStr(t);
	d = p.o.currentMedia.duration;
	d && (
	p.oProgres.getElementsByTagName('DIV')[1].style.width = Math.round(((p.oProgres.offsetWidth-15) * t / d)+15) + 'px'
	);
	d = p.o.network.downloadProgress;
	d && (
	p.oProgres.getElementsByTagName('DIV')[0].style.width = Math.round(((p.oProgres.offsetWidth-16) * d / 100)+15) + 'px'
	)
}
function getMouseRelativePosition(t, e) {
	var x = 0, y = 0;
	while (t && t != document.body) {
	x += t.offsetLeft;
	y += t.offsetTop;
	t = t.offsetParent
	}
	if (e.pageX || e.pageY)	{
	x = e.pageX - x;
	y = e.pageY - y;
	} else if (e.clientX || e.clientY) {
	x = e.clientX + document.body.scrollLeft - x
		+ document.documentElement.scrollLeft;
	y = e.clientY + document.body.scrollTop - y
		+ document.documentElement.scrollTop;
	}
	return {'x': x-1, 'y': y}
}
function classPlayer(o) {
	var i;
	this.o = o;
	this.o.style.height = 0;
	if (document.all && document.all.item && !window.opera) {
	document.execCommand('BackgroundImageCache',false,true)
	}
	this.oBtns = {
	'play': {'o': document.getElementById('playBtnID')},
	'stop': {'o': document.getElementById('stopBtnID')},
	'mute': {'o': document.getElementById('muteBtnID')}
	};
	this.oDuration = document.getElementById('playDurationID');
	this.oPosition = document.getElementById('playPositionID');
	i = this.oProgres = document.getElementById('playProgresID');
	function _getTimeStr(n) {
	var s = '', t = {
		'h': Math.floor(n / 3600),
		'm': Math.floor(n % 3600 / 60),
		's': Math.floor(n % 60)
	};

	t.h < 10 && (s += '0');
	s += t.h + ':'
	t.m < 10 && (s += '0');
	s += t.m + ':';
	t.s < 10 && (s += '0');
	s += t.s;
	return s
	}
	this.getTimeStr = _getTimeStr;
	this.oProgres._this = _showPos._this = this;
	window.setInterval('_showPos()', 250);
	//this.oProgres.onclick = _setPosition;
	i.ondragstart = new Function('event.returnValue = false');
	i.onmousedown = i.onmousemove = _setPosition;
	i = this.oVolume = document.getElementById('volumeBtnID');
	i._this = this;
	i.ondragstart = new Function('event.returnValue = false');
	i.onmousedown = i.onmousemove = _setVolume;
	document.attachEvent('onmouseup', new Function('document.getElementById(\'volumeBtnID\')._mouseBtnPress = 0; document.getElementById(\'playProgresID\')._mouseBtnPress = 0;'));
	for (i in this.oBtns) {
	this.oBtns[i].o._this = this;
	this.oBtns[i].o.onmouseout = _out;
	}
	i = this.oBtns.play.o;
	i.onclick = _PlayPause;
	i.onmouseover = _overPlayPause;
	i = this.oBtns.stop.o;
	i.onclick = _Stop;
	i.onmouseover = _overStop;
	i = this.oBtns.mute.o;
	i.onclick = _Mute;
	i.onmouseover = _overMute;
	this.playStateChange = _playStateChange;
	_showVolume(this, o.settings.volume);
	function _setPosition(e) {
	var t = this, x, y, o = t._this, d = o.o.currentMedia.duration;
	if (!d) return;
	e || (e = window.event);
	e.type == 'mousedown' && (t._mouseBtnPress = 1);
	if (!t._mouseBtnPress) {
		return
	}
	x = getMouseRelativePosition(t, e);
	x = x.x;
	y = t.offsetWidth;
	if (x < -10 || y+10 < x) {
		return
	}
	x < 0 && (x = 0);
	//y -= 14;
	x > y && (x = y);
	o.o.controls.currentPosition = d * x / y
	}
	function _playStateChange(n, oPlr) {
	var t = oPlr.oBtns.play, b;
	if (n==1 || n == 8 || n==2) {
		b = t.o._isOver? 'playOver': 'play';
		t.o.className = oPlr.o.controls.isAvailable('play')? b: 'playDisabled';
		t.o.title = '';
	}
	if (n==3) {
	//3 Playing
		b = t.o._isOver? 'pauseOver': 'pause';
		t.o.className = oPlr.o.controls.isAvailable('pause')? b: 'pauseDisabled';
		t.o.title = '';
		t = oPlr.oBtns.stop;
		t.o.className = t.o._isOver? 'stopOver': 'stop';
		t.o.title = 'Стоп';
		t = oPlr.o.currentMedia.duration;
		oPlr.oDuration.innerHTML = _getTimeStr(t);
		t || (
			oPlr.oProgres.firstChild.style.width = ''
		);
	}
	if (n == 1 || n == 8) {
	//1 Stopped
		oPlr.oBtns.stop.o.className = 'stopDisabled';
	}
	if (n == 8) {
		oPlr.o.controls.currentPosition = 0;
		_showPos();
	}
	}
	function _overPlayPause() {
	var t = this._this, k = {1:1, 2:1};
	if (t.o.controls.isAvailable('pause')) {
		this.className = 'pauseOver'
		this.title='Пауза';
	} else if (t.o.controls.isAvailable('play') && typeof k[t.o.playState] != 'undefined' ) {
		this.className = 'playOver'
		this.title='Слушать';
	}
	this._isOver = 1;
	}
	function _overStop() {
	if (this._this.o.controls.isAvailable('stop')) {
		this.className = 'stopOver'
	} else {
		this.className = 'stopDisabled'
	}
	this._isOver = 1;
	}
	function _overMute() {
	this.className = this._this.o.settings.mute? 'offOver': 'onOver';
	this._isOver = 1;
	}
	function _out() {
	var t = this._this, n = t.o.playState;
	if (n == 1) {
	//1 Stopped
		t.oBtns.play.o.className =  t.o.controls.isAvailable('play')? 'play': 'playDisabled';
		t.oBtns.stop.o.className = 'stopDisabled'
	} else if (n==2) {
	//2 Paused
		t.o.controls.isAvailable('play') && (
			t.oBtns.play.o.className = 'play'
		);
	} else if (n==3) {
	//3 Playing
		t.oBtns.play.o.className = t.o.controls.isAvailable('pause')? 'pause': 'pauseDisabled'
	}
	if (n==2 || n==3) {
		t.oBtns.stop.o.className = 'stop'
	}
	t.oBtns.mute.o.className = t.o.settings.mute? 'off': 'on';
	t.oBtns.mute.o.title = t.o.settings.mute? 'Включить звук': 'Выключить звук';
	this._isOver = 0;
	}
	function _PlayPause() {
	if (this._this.o.controls.isAvailable('pause')) {
		this._this.o.controls.pause()
	} else if (this._this.o.controls.isAvailable('play')) {
		this._this.o.controls.play()
	}
	}
	function _Stop() {
	if (this._this.o.controls.isAvailable('stop')) {
		this._this.o.controls.stop()
	}
	}
	function _Mute() {
	var m = this._this.o.settings;
	this._this.oBtns.mute.o.className = (m.mute = !m.mute)? 'off': 'on'
	}
	function _setVolume(e) {
	var t = this, x, y;
	e || (e = window.event);
	e.type == 'mousedown' && (t._mouseBtnPress = 1);
	if (!t._mouseBtnPress) {
		return
	}
	x = getMouseRelativePosition(t, e);
	x = x.x;
	y = t.offsetWidth;
	if (x < -10 || y+10 < x) {
		return
	}
	x < 0 && (x = 0);
	y -= 12;
	x > y && (x = y);
	y = Math.floor(x * 100 / y);
	t._this.o.settings.volume = y;
	t._this.oVolume.firstChild.style.width = (x+12)+'px';
	x = t._this.o.settings;
	if (x.mute) {
		x.mute = 0;
		this._this.oBtns.mute.o.className = 'on'
	}
	}
	function _showVolume(o, v) {
	try {
	o.oVolume.firstChild.style.width = Math.floor(((o.oVolume.offsetWidth-13)* v / 100)+13) + 'px'
	} catch(e) {}
	}
	this.o._this = this;
	_playStateChange(this.o.playState, this)
}

var oPlayer;
var embedPlayer;

function initPlayer(id) 
{

}


var swfArr = new Array();

function insertSwf(obj)
{
	if ( !obj.src || !obj.w || !obj.h || !obj.ver || !obj.div ) return;
	if ( !obj.id ) obj.id = "";
	if ( !obj.bg ) obj.bg = "#FFF";
	var so = new SWFObject(obj.src, obj.id, obj.w, obj.h, obj.ver, obj.bg);
	if (obj.param)
	{
		for (var key in obj.param) so.addParam(key,obj.param[key]);
	}
	if (obj.vars)
	{
		for (var key in obj.vars) so.addVariable(key,obj.vars[key]);
	}
	so.write(obj.div);
}
/*
function setOnAir ()
{
	var dt = new Date();
	dt.setUTCHours(dt.getUTCHours() + 3);
	var Ymd = dt.getUTCFullYear();
	var tmp = dt.getUTCMonth() + 1;
	Ymd += ((tmp < 10) ? '0' : '') + tmp;
	tmp = dt.getUTCDate();
	Ymd += ((tmp < 10) ? '0' : '') + tmp;
	
	$.get(
		'air/' + Ymd + '/livenow.xml',
		{player: "1"},
		function (xml)
		{
			var cur = $('Current', xml);
			
			var titleName = cur.find('titleName').text();
			var artistName = cur.find('artistName').text();
			var title = '';
			if (titleName)
			{
				title = '&laquo;' + titleName + '&raquo';
			}
			if (artistName)
			{
				if (titleName)
				{
					title += '&nbsp;&mdash;&nbsp;';
				}
				title += artistName;
			}
			
			var time = '';			
			var duration = parseInt(cur.find('itemDuration').text());
			if ( duration > 0 )
			{
				var sec = duration % 60;
				duration = (duration - sec) / 60;
				var min  = duration % 60;
				duration = (duration - min) / 60;
				var hours = duration % 24;
				if (sec < 10) sec = '0' + sec;
				if (min < 10) min = '0' + min;
				if (hours < 10) hours = '0' + hours;
				time = '' + hours + ':' + min + ':' + sec;
			}
			
			$('#marquee marquee:first').empty().append(title);
			$('#playPositionID').empty().append(time);
		}
	);
}
*/

function setOnAir ()
{
	$.getJSON(
		'/online/air/song.js',
		function (json)
		{
			$('#marquee marquee:first').empty().append('&laquo;'+json.song+'&raquo;&nbsp;&nbsp;'+json.artist);
			//$('#playPositionID').empty().append(time);
		}
	);
}

$(document).ready (
	function ()
	{
		for(i=0; i<swfArr.length; i++) insertSwf(swfArr[i]);

		if ( !opener )
		{
			$('div.player-bg').after('<table id="tbl"><tr><td></td><td id="cntr"></td><td></td></tr></table>').appendTo('#cntr');
		}
		
		var o = $('#MediaPlayer1');
		if( !o.get(0).versionInfo ) 
		{
			embedPlayer = o.find('embed:first');
			
			$('#muteBtnID').add('#volumeBtnID').hide();
			
			$('#playBtnID').
				hover(
					function() 
					{
						var t = $(this); 
						if ( !t.hasClass('playDisabled') && !t.hasClass('pauseDisabled') )
						{
							t.removeClass().addClass('playOver'); 
						}
					},
					function() 
					{
						var t = $(this); 
						if ( !t.hasClass('playDisabled') && !t.hasClass('pauseDisabled') )
						{
							t.removeClass().addClass('play'); 
						}
					}
				).
				removeClass().
				addClass('pauseDisabled').
				click( function () {
					$(this).
						removeClass().
						addClass('pauseDisabled').
						attr('title', '');
						
					$('#stopBtnID').
						removeClass().
						addClass('stop').
						attr('title', 'Стоп');
						
					embedPlayer.css('display','');
				});
				
			$('#stopBtnID').hover(
					function() 
					{
						var t = $(this); 
						if ( !t.hasClass('stopDisabled') )
						{
							t.removeClass().addClass('stopOver'); 
						}
					},
					function() 
					{
						var t = $(this); 
						if ( !t.hasClass('stopDisabled') )
						{
							t.removeClass().addClass('stop'); 
						}
					}
				).
				removeClass().
				addClass('stop').
				click( function () {
					$(this).
						removeClass().
						addClass('stopDisabled').
						attr('title', '');
						
					$('#playBtnID').
						removeClass().
						addClass('play').
						attr('title', 'Слушать');
						
					embedPlayer.css('display','none');
				});
		} 
		else 
		{
			oPlayer = new classPlayer(o.get(0));
			$(window).unload( function () { oPlayer.o.close(); } );
		}
		
		setOnAir();
		setInterval( setOnAir, 30000 );

	}
);