var ns = (document.layers)? true:false
var ie = (document.all)? true:false

// construtor
function Player() {
	// ** métodos **
	this.ra = null;
	this.raIsFake = false;
	this.callStack = new Array();
	
	this.setouVolume = false;
	
	// botoes
	this.play = PlayerPlay;
	this.pause = PlayerPause;
	this.stop = PlayerStop;
	this.next = PlayerNext;
	this.setVolume = PlayerSetVolume;
	this.getVolume = PlayerGetVolume;	
	this.volumeDown = PlayerVolumeDown;
	this.volumeUp = PlayerVolumeUp;
	
	// info
	this.getTitle = PlayerGetTitle;
	this.getPlayState = PlayerGetPlayState;
	this.getAuthor = PlayerGetAuthor;
	this.getCurrentEntry = PlayerGetCurrentEntry;
	this.getNumEntries = PlayerGetNumEntries;
	this.getEntryAuthor = PlayerGetEntryAuthor
	this.getLength = PlayerGetLength;
	this.printInfo = PlayerPrintInfo;
	this.getInfo = PlayerGetInfo;
	
	this.loadPlugin = LoadPlugin;	
	
	this.stopped = 0;
	this.contacting = 1;
	this.buffering = 2;
	this.playing = 3;
	this.paused = 4;
	this.seeking = 5;
}

// verifica se existe o Real instalado (plug-in)
function testaReal() {
	real = (ie)?document.frames.item(0):document.layers["player"];
	if (real.document.embeds.length>0) {
		old_window_error = window.onerror;
		window.onerror = erroDeteccao;
		var v, obj;
		obj =real.document.embeds["RealPlayer"];
		player.ra = obj;
		v = obj.GetVersionInfo();
		
		if (Number(v.charAt(0)) < 6) {
			player.raIsFake = true;
			player.ra = new FakeRealPlayer();
		} else {
			player.raIsFake = false;
		}
	
		window.onerror = old_window_error;
		
		callback();
	} 
	else {
		setTimeout("testaReal();",100);
	}
}

function erroDeteccao() {
	player.raIsFake = true;
	player.ra = new FakeRealPlayer();
	window.onerror = old_window_error;
	callback();
	return true;
}

// monta array das acoes a serem executadas
function callback() {
	for (i=0; i<player.callStack.length; ++i) {
		eval(player.callStack[i]);
	}
	player.callStack = new Array();
}

function FakeRealPlayer() {
	f1 = new Function("return false;");
	f2 = new Function("return 0;");
	this.source = "";
	this.DoPlay = new Function("alert('Você precisa ter o Real Player G2 ou superior (www.real.com) instalado.');");
	this.CanPlay = f1;
	this.CanPause = f1;
	this.DoPause = f1;
	this.DoStop = f1;
	this.SetVolume = f1;
	this.GetVolume = f2;
	this.SetLength = f1;
	this.GetLength = f2;
	this.SetPosition = f1;
	this.GetPosition = f2;
	this.SetSource = new Function("this.source = arguments[0];");
	this.GetSource = new Function("return this.source;");
}

// iframe ou  layer
function LoadPlugin(caller) {
	if (this.ra) {
		if (this.raIsFake) {
			if (ns) {
				document.layers["player"].src = "/clickstation/getreal.html";
				document.layers["player"].left = this.tela.left;
				document.layers["player"].top = this.tela.top;
				document.layers["player"].width = this.tela.width;
				document.layers["player"].height = this.tela.height;
			} 
			else {
				document.frames["player"].window.location = "/clickstation/getreal.html";
			}
		}
		return true;
	} 
	else {
		this.callStack[this.callStack.length] = caller;
		if (ns) {
			document.layers["player"].src = "embed.html";
			document.layers["player"].left = this.tela.left;
			document.layers["player"].top = this.tela.top;
			document.layers["player"].width = this.tela.width;
			document.layers["player"].height = this.tela.height;
		} 
		else {
			document.frames.item(0).window.location = "embed.html";
		}
		setTimeout("testaReal();",0);
		return false;
	}
}

// play
function PlayerPlay() {
	if (this.loadPlugin("player.play()")) {
		if (!this.setouVolume) {
			this.setouVolume = true;
			this.setVolume(50);
		}
		if (this.ra.GetSource()!="") {
			 if (this.ra.CanPlay()) {
    			this.ra.DoPlayPause();
			 }
		}		
	}
}

// pause
function PlayerPause() {
	if(this.loadPlugin("player.pause()")) {
			this.ra.DoPlayPause();
	}
}

// stop 
function PlayerStop() {
	if (this.loadPlugin("player.stop()")) {
		if (this.ra.GetSource()!="") {
			this.ra.DoStop();
		}
	}
}

// next
function PlayerNext() {
	if (this.loadPlugin("player.next()")) {
		if (this.ra.GetSource()!="") {
			this.ra.DoNextItem();
		}
	}
}

// volume UP
function PlayerVolumeUp() {
	if (this.loadPlugin("player.volumeUp()")) {
		if (this.ra.GetSource()!="") {
			volume = this.ra.getVolume();
			if (volume < 90) volume = volume + 10;
			this.ra.setVolume(volume);
		}
	}
}

// volume DOWN
function PlayerVolumeDown() {
	if (this.loadPlugin("player.volumeDown()")) {
		if (this.ra.GetSource()!="") {
			volume = this.ra.getVolume();
			if (volume > 10) volume = volume - 10;
			this.ra.setVolume(volume);
		}
	}
}

// set volume
function PlayerSetVolume(volume) {
	if (this.loadPlugin("player.setVolume("+volume+")")) {
		this.ra.SetVolume(volume);
	}
}

// get volume
function PlayerGetVolume() {
	if (this.loadPlugin("player.getVolume()")) {
		this.ra.GetVolume();
	}
}

// get tempo do clip
function PlayerGetLength() {
	if(this.loadPlugin("player.getLength()")) {
		this.ra.GetLength();
	}

}

// get Titulo 
function PlayerGetTitle() {
	if (this.loadPlugin("player.getTitle()")) {
		document.frames.item(1).window.document.write("Música : " + this.ra.GetTitle() + "<br>");
	}
}

// get Author
function PlayerGetAuthor() {
	if (this.loadPlugin("player.getAuthor()")) {
		document.frames.item(1).window.document.write("Artista : " + this.ra.GetAuthor() + "<br>");
	}
}

// get musica atual
function PlayerGetCurrentEntry() {
	if(this.loadPlugin("player.getCurrentEntry()")) {
		this.ra.GetCurrentEntry();
	}
}

// get o autor corrente
function PlayerGetEntryAuthor() {
	if(this.loadPlugin("player.getEntryAuthor()")) {
		this.ra.GetEntryAuthor(player.getCurrentEntry());
	}
}

// get total de musicas
function PlayerGetNumEntries() {
	if(this.loadPlugin("player.getNumEntries()")) {
		this.ra.GetNumEntries();
	}
}

// Get info
function PlayerGetInfo() {
	var entries;
	var i
	var time;
	if(this.loadPlugin("player.getInfo()")) {
		entries = this.ra.GetNumEntries();		
		time = this.ra.GetLength();
		i = this.ra.GetCurrentEntry();
		//if (i < entries) {
		if (this.ra.GetPlayState() != player.stopped) {
			player.printInfo();
			setTimeout("player.getInfo();",1000);
		}
	}
}

// print info
function PlayerPrintInfo() {
	if(this.loadPlugin("player.printInfo()")) {
		document.all["contents"].innerHTML = "Música : " + this.ra.GetTitle() + "<br>"+"Artista : " + this.ra.GetAuthor() + "<br>";
	}
}

/* get play state
0 — Stopped
1 — Contacting
2 — Buffering
3 — Playing
4 — Paused
5 — Seeking
*/
function PlayerGetPlayState() {
	if (this.loadPlugin("player.getPlayState()")) {
		this.ra.GetPlayState();
	}
}



